Machine Learning Library
CMatrix.h
Go to the documentation of this file.
1 /*
2  COPYRIGHT (C) 2003 APPLIED NEUROINFORMATIC GROUP - UNIVERSITY OF BIELEFELD.
3 
4  ALL RIGHTS RESERVED.
5 
6  REDISTRIBUTION AND USE IN SOURCE AND BINARY FORM, WITH OR WITHOUT
7  MODIFICATION, REQUIRE THE PERMISSION OF THE COPYRIGHT HOLDERS.
8 
9  COMMERCIAL USE WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT HOLDERS
10  IS FORBIDDEN
11 
12  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
13  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15  ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
16  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
20  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
21  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 */
23 
24 
25 
26 #ifndef CMATRIX_H
27 #define CMATRIX_H
28 
29 #include <iostream>
30 #include <fstream>
31 #include "CObject.h"
32 using std::ostream;
33 
34 
36 
37 
38 template<class Type> class CMatrix;
39 template<class Type> class CVector;
40 template<class Type> class CDenseVector;
41 
42 
43 
44 
45 
46 /*- Friends --- Matrix-Matrix-Functions --------------------------------------------------*/
47 
51 template<class Type> CMatrix<Type> operator+(const CMatrix<Type>& rtMat1, const CMatrix<Type>& rtMat2);
52 
56 template<class Type> CMatrix<Type> operator-(const CMatrix<Type>& rtMat1, const CMatrix<Type>& rtMat2);
57 
61 template<class Type> CMatrix<Type> elementMul(const CMatrix<Type>& rtMat1, const CMatrix<Type>& rtMat2);
62 
66 template<class Type> CMatrix<Type> elementDiv(const CMatrix<Type>& rtMat1, const CMatrix<Type>& rtMat2);
67 
71 template<class Type> CMatrix<Type> operator*(const CMatrix<Type>& tMat1, const CMatrix<Type>& tMat2);
72 
73 
74 /*- Friends --- Matrix-Scalar-Functions --------------------------------------------------*/
75 
79 template<class Type> CMatrix<Type> operator+(const CMatrix<Type>& rtMat, const Type& rtScalar);
80 
84 template<class Type> CMatrix<Type> operator*(const CMatrix<Type>& tMat, const Type& rtScalar);
85 
89 template<class Type> CMatrix<Type> operator-(const CMatrix<Type>& rtMat, const Type& rtScalar);
90 
94 template<class Type> CMatrix<Type> operator/(const CMatrix<Type>& rtMat, const Type& rtScalar);
95 
96 
97 /*- Friends --- Matrix-Vector-Functions --------------------------------------------------*/
98 
99 
103 template<class Type> CDenseVector<Type> operator*(const CMatrix<Type>& rtMat, const CVector<Type>& rtVec);
104 
105 
109 template<class Type> void save(const CMatrix<Type>& rtMat, char* acString);
110 
111 
115 template<class Type> void load(CMatrix<Type>& rtMat,char* acString);
116 
117 /*-Class definition------------------------------------------------------------*/
118 
126 template<class Type>
127 class CMatrix : public CObject<Type>
128 {
129  friend CMatrix<Type> operator+<>(const CMatrix<Type>& rtMat1, const CMatrix<Type>& rtMat2);
130  friend CMatrix<Type> operator-<>(const CMatrix<Type>& rtMat1, const CMatrix<Type>& rtMat2);
131  friend CMatrix<Type> elementMul<>(const CMatrix<Type>& rtMat1, const CMatrix<Type>& rtMat2);
132  friend CMatrix<Type> elementDiv<>(const CMatrix<Type>& rtMat1, const CMatrix<Type>& rtMat2);
133  friend CMatrix<Type> operator*<>(const CMatrix<Type>& tMat1, const CMatrix<Type>& tMat2);
134 
135 
136 
137  friend CMatrix<Type> operator+<>(const CMatrix<Type>& rtMat, const Type& rtScalar);
138  friend CMatrix<Type> operator-<>(const CMatrix<Type>& rtMat, const Type& rtScalar);
139  friend CMatrix<Type> operator*<>(const CMatrix<Type>& rtMat, const Type& rtScalar);
140  friend CMatrix<Type> operator/<>(const CMatrix<Type>& rtMat, const Type& rtScalar);
141 
142  friend CDenseVector<Type> operator*<>(const CMatrix<Type>& rtMat, const CVector<Type>& rtVec);
143 
144  friend void load<>(CMatrix<Type>&, char* acString);
145  friend void save<>(const CMatrix<Type>&, char* acString);
146 protected:
147  int iRows;
148  int iCols;
149  Type* ptData;
151 public:
160  CMatrix(int iRows = 0, int iColumns = 0, bool bInitialize = false);
161 
168  CMatrix(const CMatrix<Type>& rtMatrix);
169 
173  ~CMatrix();
174 
180  CMatrix<Type>& operator=(const CMatrix<Type>& rtSrc);
181 
188  void resize(int iRows, int iCols, bool bPreserveData=false);
189 
195  inline int rows() const { return iRows; };
196 
197  virtual string className() const { return string("CMatrix");};
203  inline int columns() const { return iCols; };
204 
212  const Type& getElement(int i, int j) const;
213 
221  Type& operator()(int i, int j);
222 
223 
224 
234  CMatrix<Type> operator()(int i, int j, int rn, int cn) const;
235 
243  void insert(int i, int j, const CMatrix<Type> &rtSubmatrix);
244 
252  void setElement(int i, int j, const Type& tValue);
253 
259  Type determinant() const;
260 
266  Type trace() const;
267 
268 
274  Type sumElements() const;
275 
282  CDenseVector<Type> getRow(int iRow) const;
283 
290  CDenseVector<Type> getColumn(int iColumn) const;
291 
298 
299 
306  Type minElement(int& rtX, int& rtY) const;
307 
308 
315  Type maxElement(int& rtX, int& rtY) const;
321  void operator*=(Type rScalar);
322 
328  void operator+=(Type rScalar);
329 
335  void operator-=(Type rScalar);
336 
337 
338 
339 
345  void operator/=(Type rScalar);
346 
352  void operator+=(const CMatrix<Type>& tM);
353 
359  void operator-=(const CMatrix<Type>& tM);
360 
361 
368  void setRow(int iRow, const CDenseVector<Type>& tVec);
369 
376  void setColumn(int iIndex, CDenseVector<Type>& tVec);
377 
383  void setDiagonal(Type tValue);
384 
390  void setDiagonal(CDenseVector<Type>& tDiag);
391 
400  Type frobeniusNorm();
401 
408 
413  bool isNaN() const;
414 
415 #ifdef UE_ARPACK
416 
419  void MultMv(Type* v1, Type* v2);
420 #endif
421 
427  CMatrix<Type> invert() const;
428 
432  void dump(ostream& stream=cout);
433 
434  bool serialize(fstream& stream, IO_MODE=READ);
435  bool serialize2(CArchiv& tArchiv);
439  void zeros() { for(int i=0; i < iRows*iCols;i++) ptData[i]=0; };
440 
446  void setData(Type* ptData, bool bColumnMajorOrder=false);
447 
448 
456  bool exportToMatFile(const char* acPath, const char* acVarame, bool bAppend = true);
457 
458  int importFromMatFile(const char* acPath, const char* acVarname);
465  void replicateMatrix(int iX, int iY, const CMatrix<Type> &tMat);
466 
467  Type* _data() { return ptData;};
468 
469 
470 
481  int eig( CDenseVector<Type>& rtEigenvalues, CMatrix<Type>& rtEigenvectors, int iNum=0, TMatrixType tType = SYMETRIC);
482 
483 
484 
485 
492 
493 
494 
495 
496 }; // end of class
497 
498 
499 #endif
CMatrix< Type > invert() const
int rows() const
Definition: CMatrix.h:195
void setDiagonal(Type tValue)
void operator*=(Type rScalar)
CMatrix< Type > operator*(const CMatrix< Type > &tMat1, const CMatrix< Type > &tMat2)
Type trace() const
int iRows
Definition: CMatrix.h:147
CDenseVector< Type > getRow(int iRow) const
CMatrix< Type > operator+(const CMatrix< Type > &rtMat1, const CMatrix< Type > &rtMat2)
void setData(Type *ptData, bool bColumnMajorOrder=false)
Type * _data()
Definition: CMatrix.h:467
IO_MODE
Definition: CObject.h:38
void save(const CMatrix< Type > &rtMat, char *acString)
bool serialize(fstream &stream, IO_MODE=READ)
Read/write from binary stream.
Template object implementing a matrix of single and double precision elements.
Definition: CDenseVector.h:38
bool serialize2(CArchiv &tArchiv)
bool isNaN() const
bool exportToMatFile(const char *acPath, const char *acVarame, bool bAppend=true)
void resize(int iRows, int iCols, bool bPreserveData=false)
CMatrix< Type > operator/(const CMatrix< Type > &rtMat, const Type &rtScalar)
void operator+=(Type rScalar)
virtual string className() const
Returns the class name.
Definition: CMatrix.h:197
Type maxElement(int &rtX, int &rtY) const
const Type & getElement(int i, int j) const
CMatrix< Type > transpose()
void setElement(int i, int j, const Type &tValue)
CMatrix(int iRows=0, int iColumns=0, bool bInitialize=false)
Type & operator()(int i, int j)
Definition: CObject.h:38
void operator/=(Type rScalar)
void setRow(int iRow, const CDenseVector< Type > &tVec)
void replicateMatrix(int iX, int iY, const CMatrix< Type > &tMat)
Type minElement(int &rtX, int &rtY) const
CMatrix< Type > elementDiv(const CMatrix< Type > &rtMat1, const CMatrix< Type > &rtMat2)
CDenseVector< Type > getDiagonal() const
Type frobeniusNorm()
void setColumn(int iIndex, CDenseVector< Type > &tVec)
Type determinant() const
Type * ptData
Definition: CMatrix.h:149
CDenseVector< Type > getColumn(int iColumn) const
CMatrix< Type > elementMul(const CMatrix< Type > &rtMat1, const CMatrix< Type > &rtMat2)
void insert(int i, int j, const CMatrix< Type > &rtSubmatrix)
Base class of object serialization.
Definition: CArchiv.h:19
int columns() const
Definition: CMatrix.h:203
int eig(CDenseVector< Type > &rtEigenvalues, CMatrix< Type > &rtEigenvectors, int iNum=0, TMatrixType tType=SYMETRIC)
Template object for vectors of single and double precision and integer.
Definition: CDenseVector.h:37
Type sumElements() const
templatized vector for numerical applications
Definition: CMatrix.h:39
CMatrix< Type > & operator=(const CMatrix< Type > &rtSrc)
Base class for all object.
Definition: CObject.h:51
int iCols
Definition: CMatrix.h:148
int importFromMatFile(const char *acPath, const char *acVarname)
void dump(ostream &stream=cout)
void operator-=(Type rScalar)
Definition: CMatrix.h:35
TMatrixType
Definition: CMatrix.h:35
void zeros()
Definition: CMatrix.h:439
int calcLUFactorization(CMatrix< Type > &rtL, CMatrix< Type > &rtU, CDenseVector< int > &rtIdx) const
void load(CMatrix< Type > &rtMat, char *acString)
CMatrix< Type > operator-(const CMatrix< Type > &rtMat1, const CMatrix< Type > &rtMat2)