KSquare Utilities
Matrix.h
Go to the documentation of this file.
1 /* Matrix.h -- A simple two dimensional floating point matrix.
2  * Copyright (C) 1994-2014 Kurt Kramer
3  * For conditions of distribution and use, see copyright notice in KKB.h
4  */
5 #ifndef _MATRIX_
6 #define _MATRIX_
7 //****************************************************************************************
8 //* Matrix Class *
9 //* *
10 //* *
11 //* Supports two dimensional matrices. *
12 //* *
13 //* Developed for Machine Learning Project for support of Import Vector Machine. *
14 //* Handles two dimensional matrixes. Functions supported are matrix addition, sub- *
15 //* traction, multiplication, transpose, determinant, and inversion. Where appropriate *
16 //* arithmetic operators +, -, * were overloaded. Addition, Subtraction and Multipli- *
17 //* cation can be done against either another matrix or scaler. *
18 //* *
19 //*======================================================================================*
20 //* *
21 //* Date Descriptions *
22 //* =========== ====================================================================== *
23 //* Nov-11-2002 Initial Development. *
24 //* *
25 //****************************************************************************************
26 
27 #include <fstream>
28 
29 #include "KKBaseTypes.h"
30 
31 namespace KKB
32 {
33  class Row;
34  typedef Row* RowPtr;
35 
36  /**
37  *@class Matrix
38  *@brief Supports two dimensional matrices.
39  *@details Developed for Machine Learning Project for support of Import Vector Machine.
40  * Handles two dimensional matrices. Functions supported are matrix addition, subtraction
41  * multiplication, transpose, determinant, and inversion. Where appropriate arithmetic
42  * arithmetic operators +, -, * were overloaded. Addition, Subtraction and Multiplication
43  * can be done against either another matrix or scaler. Also Transpose and Determinant
44  * operations are supported.
45  */
46  class Matrix
47  {
48  public:
49  typedef Matrix* MatrixPtr;
50 
51  Matrix ();
52 
53  Matrix (kkint32 _numOfRows,
54  kkint32 _numOfCols
55  );
56 
57  Matrix (const Matrix& _matrix);
58 
59  Matrix (const VectorDouble& _v);
60 
61  ~Matrix ();
62 
63  template<typename T>
64  static MatrixPtr BuildFromArray (kkint32 numOfRows,
65  kkint32 numOfCols,
66  T** data
67  );
68 
69  Matrix& operator= (const Matrix& right);
70 
71  Matrix& operator= (const VectorDouble& right);
72 
73  Matrix& operator*= (double right);
74 
75  Matrix& operator+= (double right);
76 
77  Matrix& operator+= (const Matrix& right);
78 
79  Matrix operator+ (const Matrix& right);
80 
81  Matrix operator+ (double right);
82 
83  Matrix operator- (const Matrix& right);
84 
85  Matrix operator- (double right);
86 
87  Matrix operator* (const Matrix& right);
88 
89  Matrix operator* (double right);
90 
91  Row& operator[] (kkint32 rowIDX) const;
92 
93  friend Matrix operator- (double left, const Matrix& right);
94 
95 
97 
98  /**
99  *@brief Returns a Covariance matrix.
100  *@details Each column represents a variable and each row represents an instance of each variable.
101  *@return Returns a symmetric matrix that will be (numOfRows x numOfRows) where each element will represent the covariance
102  * between their respective variables.
103  */
104  MatrixPtr Covariance () const;
105 
106  double** const Data () const {return data;}
107 
108  double Determinant ();
109 
110  double DeterminantSlow (); /**< @brief Recursive Implementation. */
111 
112  void EigenVectors (MatrixPtr& eigenVectors,
113  VectorDouble*& eigenValues
114  ) const;
115 
116  /** @brief Locates the maximum value in a matrix along with the row and column that is located. */
117  void FindMaxValue (double& maxVal,
118  kkint32& row,
119  kkint32& col
120  );
121 
122  VectorDouble GetCol (kkint32 col) const;
123 
124  Matrix Inverse ();
125 
126  kkint32 NumOfCols () const {return numOfCols;}
127 
128  kkint32 NumOfRows () const {return numOfRows;}
129 
130  void ReSize (kkint32 _numOfRows,
131  kkint32 _numOfCols
132  );
133 
134  bool Symmetric () const; /**< Returns true is the matrix is Symmetric */
135 
136  Matrix Transpose ();
137 
138  friend std::ostream& operator<< ( std::ostream& os,
139  const Matrix& matrix
140  );
141 
142  private:
143  void Destroy ();
144 
145  void AllocateStorage ();
146 
147  double DeterminantSwap (double** mat,
148  unsigned short offset
149  );
150 
151  double CalcDeterminent (kkint32* rowMap,
152  kkint32* colMap,
153  kkint32 size
154  );
155 
156  double Pythag (const double a,
157  const double b
158  ) const;
159 
160  kkint32 Tqli (double* d,
161  double* e,
162  kkint32 n,
163  double** z
164  ) const;
165 
166  void Tred2 (double** a,
167  kkint32 n,
168  double* d,
169  double* e
170  ) const;
171 
172  double** data; /**< A two dimensional array that will index into 'dataArea'. */
173  double* dataArea; /**< one dimensional array that will contain all the matrices data. */
174  kkint32 numOfCols;
175  kkint32 numOfRows;
176  RowPtr rows;
177  kkint32 totNumCells; /**< Total number of cells allocated = (numOfRows x nmumOfCols). */
178  }; /* matrix */
179 
180 
181  typedef Matrix::MatrixPtr MatrixPtr;
182 
183  class Row
184  {
185  public:
186  Row ();
187 
188  Row (kkint32 _numOfCols,
189  double* _cells
190  );
191 
192  Row (const Row& _row);
193 
194  ~Row ();
195 
196 
197  double* Cols () {return cells;}
198 
199  double& operator[] (kkint32 idx);
200 
201  void Define (kkint32 _numOfCols,
202  double* _cells
203  );
204 
205  private:
206  double* cells;
207  kkint32 numOfCols;
208  }; /* Row */
209 
210  Matrix operator- (double left, const Matrix& right);
211 } /* KKB */
212 
213 
214 
215 
216 #endif
Row & operator[](kkint32 rowIDX) const
Definition: Matrix.cpp:270
Matrix Transpose()
Definition: Matrix.cpp:707
kkint32 NumOfRows() const
Definition: Matrix.h:128
Matrix & operator=(const Matrix &right)
Definition: Matrix.cpp:329
__int32 kkint32
Definition: KKBaseTypes.h:88
VectorDouble GetCol(kkint32 col) const
Definition: Matrix.cpp:891
Matrix operator-(double right)
Definition: Matrix.cpp:443
Matrix operator+(double right)
Definition: Matrix.cpp:519
Matrix(kkint32 _numOfRows, kkint32 _numOfCols)
Definition: Matrix.cpp:108
Supports two dimensional matrices.
Definition: Matrix.h:46
static MatrixPtr BuildFromArray(kkint32 numOfRows, kkint32 numOfCols, T **data)
Will create a new matrix using the 2dArray "data" for source.
Definition: Matrix.cpp:190
Matrix(const Matrix &_matrix)
Definition: Matrix.cpp:142
~Row()
Definition: Matrix.cpp:59
Matrix * MatrixPtr
Definition: Matrix.h:49
Matrix & operator=(const VectorDouble &right)
Definition: Matrix.cpp:337
MatrixPtr Covariance() const
Returns a Covariance matrix.
Definition: Matrix.cpp:1004
double * Cols()
Definition: Matrix.h:197
kkint32 NumOfCols() const
Definition: Matrix.h:126
double & operator[](kkint32 idx)
Definition: Matrix.cpp:76
Matrix operator*(const Matrix &right)
Definition: Matrix.cpp:478
double DeterminantSlow()
Recursive Implementation.
Definition: Matrix.cpp:286
void EigenVectors(MatrixPtr &eigenVectors, VectorDouble *&eigenValues) const
Will derive the Eigen vectors and values of the matrix.
Definition: Matrix.cpp:768
KKTHread * KKTHreadPtr
bool Symmetric() const
Definition: Matrix.cpp:688
Matrix(const VectorDouble &_v)
Definition: Matrix.cpp:157
void FindMaxValue(double &maxVal, kkint32 &row, kkint32 &col)
Locates the maximum value in a matrix along with the row and column that is located.
Definition: Matrix.cpp:818
Matrix operator+(const Matrix &right)
Definition: Matrix.cpp:369
Matrix & operator+=(const Matrix &right)
Definition: Matrix.cpp:396
Row(kkint32 _numOfCols, double *_cells)
Definition: Matrix.cpp:34
Matrix Inverse()
Definition: Matrix.cpp:731
Matrix & operator*=(double right)
Definition: Matrix.cpp:349
Row(const Row &_row)
Definition: Matrix.cpp:51
Matrix operator-(const Matrix &right)
Definition: Matrix.cpp:419
double Determinant()
Definition: Matrix.cpp:945
friend std::ostream & operator<<(std::ostream &os, const Matrix &matrix)
Row * RowPtr
Definition: Matrix.h:33
void ReSize(kkint32 _numOfRows, kkint32 _numOfCols)
Definition: Matrix.cpp:224
Matrix operator-(double left, const Matrix &right)
Definition: Matrix.cpp:456
MatrixPtr CalcCoFactorMatrix()
Definition: Matrix.cpp:606
void Define(kkint32 _numOfCols, double *_cells)
Definition: Matrix.cpp:66
Matrix & operator+=(double right)
Definition: Matrix.cpp:359
double **const Data() const
Definition: Matrix.h:106
Matrix operator*(double right)
Definition: Matrix.cpp:530
std::vector< double > VectorDouble
Vector of doubles.
Definition: KKBaseTypes.h:148