NCBI C++ ToolKit
cuMatrix.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: cuMatrix.hpp 33815 2007-05-04 17:18:18Z kazimird $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: David Hurwitz
27  *
28  * File Description:
29  * part of CDTree app
30  *
31  *
32  * ===========================================================================
33  */
34 
35 #ifndef CU_MATRIX_HPP
36 #define CU_MATRIX_HPP
37 #include <corelib/ncbistl.hpp>
38 //#include <ncbierr.h>
39 #include <assert.h>
40 
42 BEGIN_SCOPE(cd_utils)
43 
45 
46  protected:
47  double** m_Array;
49  int m_NumRows, m_NumCols;
51 
52  public:
54  m_NumRows = 0;
55  m_NumCols = 0;
56  m_Array = 0;
57  m_ColumnFlags = 0;
58  }
59 
60  AMatrix_base(const int NumRows, const int NumCols) {
61  assert(NumRows >= 0);
62  assert(NumCols >= 0);
63  Allocate(NumRows, NumCols);
64  }
65 
67  Copy(Matrix);
68  }
69 
70  AMatrix_base& operator= (const AMatrix_base& Matrix) {
71  Copy(Matrix);
72  return(*this);
73  }
74 
76  DeAllocate();
77  }
78 
79  void GetSize(int& NumRows, int& NumCols) {
80  NumRows = m_NumRows;
81  NumCols = m_NumCols;
82  }
83 
84  int GetNumRows() const {return(m_NumRows);}
85  int GetNumCols() const {return(m_NumCols);}
86 
87  // for speed. no error checking!
88  double FastGet(const int RowIndex, const int ColIndex) const{
89  return(m_Array[RowIndex][ColIndex]);
90  }
91 
92  bool IsColSet(int ColIndex) const {
93  if (ColIndex < m_NumCols) {
94  if (m_ColumnFlags) {
95  return(m_ColumnFlags[ColIndex]);
96  }
97  }
98  return(false);
99  }
100 
101  bool Shrink(const int NumRows, const int NumCols);
102 
103  void ReSize(const int NumRows, const int NumCols) {
104  MakeSureArrayIsBigEnough(NumRows-1, NumCols-1);
105  }
106 
107  // in combination with AMatrix::operator[], returns m_Array[i][j].
108  // return ref to double so this operator can be used for setting.
109  double& operator[] (int ColIndex) {
110  assert(ColIndex >= 0);
111  MakeSureArrayIsBigEnough(m_RowIndex, ColIndex);
112  m_ColumnFlags[ColIndex] = true;
113  return(m_Array[m_RowIndex][ColIndex]);
114  }
115 
116  double Get(int RowIndex, int ColIndex) {
117  assert((RowIndex >= 0) && (RowIndex < m_NumRows));
118  assert((ColIndex >= 0) && (ColIndex < m_NumCols));
119  return(m_Array[RowIndex][ColIndex]);
120  }
121 
122  void Set(int RowIndex, int ColIndex, double Val) {
123  assert(RowIndex >= 0);
124  assert(ColIndex >= 0);
125  MakeSureArrayIsBigEnough(RowIndex, ColIndex);
126  m_ColumnFlags[ColIndex] = true;
127  m_Array[RowIndex][ColIndex] = Val;
128  }
129 
130  // Apply linear transformation to all values y(new) = mx(old) + b
131  void LinearTransform(double b, double m, bool ignoreDiagonal=false);
132  void GetExtremalEntries(double& max, double& min, bool ignoreDiagonal=false);
133 
134  // null out the matrix
135  void DeAllocate();
136 
137  private:
138  void Allocate(const int NumRows, const int NumCols);
139  void Copy(const AMatrix_base& Matrix);
140  void SlowCopy(const AMatrix_base& Matrix);
141  void MakeArrayBigger(const int RowIndex, const int ColIndex);
142  bool MakeSureArrayIsBigEnough(const int RowIndex, const int ColIndex) {
143  if ((m_NumRows > RowIndex) && (m_NumCols > ColIndex)) return(true);
144  MakeArrayBigger(RowIndex, ColIndex);
145  return(false);
146  }
147 
148  protected:
149  int Max(int Val1, int Val2) {
150  return(Val1 > Val2 ? Val1 : Val2);
151  }
152 };
153 
154 
156 //---------------------------------------------------------------
157 // I've introduced this class so that I can peform the
158 // [][] operator. (e.g. Matrix[3][5])
159 //---------------------------------------------------------------
160 
161  public:
162 
164 
165  AMatrix(const int NumRows, const int NumCols) : AMatrix_base(NumRows, NumCols) {}
166 
168 
169  // see AMatrix_base::operator[]
170  AMatrix_base& operator[] (int RowIndex) {
171  assert(RowIndex >= 0);
172  m_RowIndex = RowIndex;
173  return(*this);
174  }
175 };
176 
177 END_SCOPE(cd_utils)
179 
180 #endif // ALGMATRIX_HPP
AMatrix_base(const AMatrix_base &Matrix)
Definition: cuMatrix.hpp:66
bool * m_ColumnFlags
Definition: cuMatrix.hpp:48
bool MakeSureArrayIsBigEnough(const int RowIndex, const int ColIndex)
Definition: cuMatrix.hpp:142
double ** m_Array
Definition: cuMatrix.hpp:47
AMatrix_base(const int NumRows, const int NumCols)
Definition: cuMatrix.hpp:60
void GetSize(int &NumRows, int &NumCols)
Definition: cuMatrix.hpp:79
int Max(int Val1, int Val2)
Definition: cuMatrix.hpp:149
double FastGet(const int RowIndex, const int ColIndex) const
Definition: cuMatrix.hpp:88
int GetNumCols() const
Definition: cuMatrix.hpp:85
double & operator[](int ColIndex)
Definition: cuMatrix.hpp:109
void Set(int RowIndex, int ColIndex, double Val)
Definition: cuMatrix.hpp:122
int GetNumRows() const
Definition: cuMatrix.hpp:84
void ReSize(const int NumRows, const int NumCols)
Definition: cuMatrix.hpp:103
double Get(int RowIndex, int ColIndex)
Definition: cuMatrix.hpp:116
bool IsColSet(int ColIndex) const
Definition: cuMatrix.hpp:92
int m_RowIndex
Definition: cuMatrix.hpp:50
AMatrix(const AMatrix &Matrix)
Definition: cuMatrix.hpp:167
AMatrix(const int NumRows, const int NumCols)
Definition: cuMatrix.hpp:165
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define END_SCOPE(ns)
End the previously defined scope.
Definition: ncbistl.hpp:75
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define BEGIN_SCOPE(ns)
Define a new scope.
Definition: ncbistl.hpp:72
#define NCBI_CDUTILS_EXPORT
Definition: ncbi_export.h:376
The NCBI C++/STL use hints.
T max(T x_, T y_)
T min(T x_, T y_)
#define assert(x)
Definition: srv_diag.hpp:58
Modified on Fri Apr 12 17:16:55 2024 by modify_doxy.py rev. 669887