Protium
Math and Design Features
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
Matrix.h
Go to the documentation of this file.
1 #ifndef Protium_Matrix_h_
2 #define Protium_Matrix_h_
3 
5 
6 #include <vector>
7 #include <cmath>
8 
9 
10 namespace Protium{
11  namespace LinearAlgebra{
12 
14  template<typename T, int n, int m>
15  class Matrix;
16 
18  template<typename T, int n, int m>
19  struct SubMatrix{
20  Matrix<T, n-1,m-1> Of(const Matrix<T,n,m>& other, const int& i=0, const int& j=0){
21  Matrix<T,n-1,m-1> temp;
22  int index1=0;
23  for(int k=0;k<m;k++){
24  if(k!=j){
25  temp[index1++] = other.At(k).GetSubVector(i);
26  }
27  }
28  return temp;
29  }
30  };
31 
33  template<typename T, int n>
34  struct SubMatrix<T,n,0>{
35  Matrix<T, n-1,0> Of(const Matrix<T,n,2>& other, const int& i=0, const int& j=0){
36  Matrix<T,n-1,1> temp;
37  int index1 = j==0? 1 : 0;
38  int index2=0;
39  for(int k=0;k<n;k++)
40  if(k!=i)
41  temp[index2++][0] = T(other.At(k,index1) );
42  return temp;
43  }
44  };
45 
47  template<typename T, int m>
48  struct SubMatrix<T,0,m>{
49  Matrix<T,0,m-1> Of(const Matrix<T,2,m>& other, const int& i=0, const int& j=0){
50  Matrix<T,1,m-1> temp;
51  int index1 = i==0? 1 : 0;
52  int index2=0;
53  for(int k=0;k<m;k++)
54  if(k!=j)
55  temp[0][index2++] = T(other.At(index1,k) ) ;
56  return temp;
57  }
58  };
59 
61  template<typename T>
62  struct SubMatrix<T,2,2>{
63  Matrix<T,1,1> Of(const Matrix<T,2,2>& other, const int& i=0, const int& j=0){
64  Matrix<T,1,1> temp;
65  if(i==0){
66  if(j==0)
67  temp[0][0] = T(other.At(1,1) );
68  else
69  temp[0][0] = T(other.At(0,1) );
70  }
71  else{
72  if (j==0)
73  temp[0][0] = T(other.At(1,0) );
74  else
75  temp[0][0]= T(other.At(0,0) );
76  }
77  return temp;
78  }
79  };
80 
82  template<typename T, int n, int m>
83  struct Determinant{
84  T Of(const Matrix<T,n,m>& other){
85  return T( nanf('0') );
86  }
87  };
88 
90  template<typename T, int n>
91  struct Determinant<T,n,n>{
92  T Of(const Matrix<T,n,n>& other){
93  T det = T(0);
94  T s = T(1);
95  for(int i=0;i<n;i++){
96  det+= (s)*(other.At(0,i) )*(other.GetSubMatrix(i,0).GetDeterminant() );
97  s = -s;
98  }
99  return det;
100  }
101  };
102 
104  template<typename T>
105  struct Determinant<T,1,1>{
106  T Of(const Matrix<T,1,1>& other){
107  return T(other.At(0,0) );
108  }
109  };
110 
111  template<typename T, int n, int m>
112  class Matrix : public Protium::Allocation::DefaultSmallObject {
115  public:
116 
118  void Init(){
119  for(int i=0; i< m;i++)fComponents.push_back(Vector<T,n>() );
120  }
121 
123  Matrix() : Protium::Allocation::DefaultSmallObject() {
124  Init();
125  }
126 
128  Matrix(const Matrix<T,n,m>& other) {
129  Init();
130  for(int i=0; i<n;i++)
131  for(int j=0; j<m;j++)
132  fComponents[i][j] = T(other.At(i,j) );
133  }
134 
136  virtual ~Matrix(){
137  fComponents.clear();
138  }
139 
141  inline static int GetNRows(){return n;}
143  inline static int GetNColumns(){return m;}
144 
147  Vector<T, n>& operator[](const int& row){
148  return fComponents[row];
149  }
150 
154  Vector<T,n> At(const int& row) const{
155  return fComponents.at(row);
156  }
157 
160  const T& At(const int& row, const int& column) const{
161  return fComponents.at(row).At(column);
162  }
163 
166  Matrix<T,m,n> trans;
167  for(int i =0;i<n;i++)for(int j=0;j<m;j++)
168  trans[i][j]= this->At(j,i);
169  return trans;
170  }
171 
173  Matrix<T,m-1,n-1> GetSubMatrix(const int& i, const int& j) const{
174  SubMatrix<T, m,n> helper;
175  return helper.Of( (*this) , i,j);
176  }
177 
179  T GetDeterminant() const{
180  Determinant<T,m,n> helper;
181  return helper.Of(*this );
182  }
183 
188  for(int i=0; i<n;i++)
189  (*this)[i] += rhs.At(i);
190  return *this;
191  }
192 
195  Matrix<T,n,m>& operator*=(const T& rhs) {
196  //PROTIUM_STATIC_ASSERT(n == m,"Vector Dimensions Must Match");
197  for(int i=0; i<n;i++)
198  (*this)[i] *=rhs;
199  return *this;
200  }
201 
205  Vector<T,m> other;
206 
207  for(int i=0; i<m;i++)
208  other[i] = fComponents[i]*rhs;
209  return other;
210  }
211 
214  template<int k>
216  Matrix<T,k,m> transpose = rhs.Transpose();
217  Matrix<T,n,k> ret;
218  for(int i=0; i<n;i++)
219  for(int j=0;j<k;j++)
220  ret[i][j] = (this->At(i) ) * (transpose[j]);
221  return ret;
222  }
223 
227  //PROTIUM_STATIC_ASSERT(n == m,"Vector Dimensions Must Match");
228  for(int i=0; i<n;i++)
229  (*this)[i] -=rhs.At(i);
230  return *this;
231  }
232 
235  const Matrix<T,n,m> operator+(const Matrix<T,n,m>& rhs) const {
236  return Matrix<T,n,m>(*this) += rhs;
237  }
238 
241  const Matrix<T,n,m> operator-(const Matrix<T,n,m>& rhs) const {
242  return Matrix<T,n,m>(*this) -= rhs;
243  }
244 
253  template<int k>
254  const Matrix<T,n,k> operator*(const Matrix<T,m,k>& rhs) const {
255  Matrix<T,k,m> transpose = rhs.Transpose();
256  Matrix<T,n,k> ret;
257  for(int i=0; i<n;i++)
258  for(int j=0;j<k;j++)
259  ret[i][j] = ((*this)[i]) * (transpose[j]);
260  return ret;
261  }
262 
267  const Matrix<T,n,m> operator*(const T& rhs) const {
268  return Matrix<T,n,m>(*this) *=rhs;
269  }
270 
273  const Vector<T,m> operator*(const Vector<T,n>& rhs) {
274  Vector<T,m> other;
275 
276  for(int i=0; i<m;i++)
277  other[i] = fComponents[i]*rhs;
278  return other;
279  }
280 
281 
285  for(int i=0; i<n;i++)fComponents[i] = rhs.At(i);
286  return *this;
287  }
288 
291  bool operator==( const Matrix<T,n,m> &rhs) const {
292  bool ret=true;
293  //PROTIUM_STATIC_ASSERT(n == m,"Vector Dimensions Must Match");
294  for(int i=0; i<n;i++)
295  ret &= ( this->At(i) == rhs.At(i) );
296  return ret;
297  }
298 
301  bool operator!=( const Matrix<T,n,m>&rhs) const {
302  return !(*this == rhs);
303  }
304 
307  static Matrix<T,n,m> Unit(){
308  Matrix<T,n,m> temp;
309  for(int i=0; i<m;i++)
310  temp[i] = Vector<T,n>::UnitVector(i);
311  return temp;
312  }
313  };
314 
321 
322  }
323 }
324 
325 
326 #endif //File Guardian
Matrix< T, n, k > operator*=(const Matrix< T, m, k > &rhs) const
Definition: Matrix.h:215
Matrix< T, n, m > & operator*=(const T &rhs)
Definition: Matrix.h:195
static Matrix< T, n, m > Unit()
Definition: Matrix.h:307
const Vector< T, m > operator*(const Vector< T, n > &rhs)
Definition: Matrix.h:273
virtual ~Matrix()
Default Destructor.
Definition: Matrix.h:136
const T & At(const int &row, const int &column) const
Definition: Matrix.h:160
Vector< T, n > At(const int &row) const
Definition: Matrix.h:154
Matrix< T, n, m > & operator=(const Matrix< T, n, m > &rhs)
Definition: Matrix.h:284
void Init()
Standard initialization.
Definition: Matrix.h:118
std::vector< Vector< T, n >, Protium::Allocation::STLAdapter< Vector< T, n > > > fComponents
Data implementation.
Definition: Matrix.h:114
Matrix< double, 6, 6 > SixMatrix
Definition: Matrix.h:319
static int GetNColumns()
Retrieves abcissa dimension.
Definition: Matrix.h:143
Helper template to get submatrices for a given matrix.
Definition: Matrix.h:19
const Matrix< T, n, m > operator*(const T &rhs) const
Definition: Matrix.h:267
Matrix< T, 1, 1 > Of(const Matrix< T, 2, 2 > &other, const int &i=0, const int &j=0)
Definition: Matrix.h:63
SmallObject DefaultSmallObject
A default small obect type.
Definition: SmallObject.h:173
Matrix< T, n-1, 0 > Of(const Matrix< T, n, 2 > &other, const int &i=0, const int &j=0)
Definition: Matrix.h:35
static int GetNRows()
Retrieves ordinant dimension.
Definition: Matrix.h:141
Matrix< T, m-1, n-1 > GetSubMatrix(const int &i, const int &j) const
Get submatrix.
Definition: Matrix.h:173
Matrix< T, m, n > Transpose() const
Computes matrix transpose.
Definition: Matrix.h:165
Matrix< T, n-1, m-1 > Of(const Matrix< T, n, m > &other, const int &i=0, const int &j=0)
Definition: Matrix.h:20
Matrix()
Default constructor.
Definition: Matrix.h:123
bool operator!=(const Matrix< T, n, m > &rhs) const
Definition: Matrix.h:301
Matrix< double, 7, 7 > SevenMatrix
Definition: Matrix.h:320
const Matrix< T, n, m > operator-(const Matrix< T, n, m > &rhs) const
Definition: Matrix.h:241
Matrix< double, 5, 5 > FiveMatrix
Definition: Matrix.h:318
Matrix(const Matrix< T, n, m > &other)
Copy Constructor.
Definition: Matrix.h:128
T GetDeterminant() const
Gets determinant.
Definition: Matrix.h:179
T Of(const Matrix< T, 1, 1 > &other)
Definition: Matrix.h:106
Vector< T, n > & operator[](const int &row)
Definition: Matrix.h:147
const Matrix< T, n, m > operator+(const Matrix< T, n, m > &rhs) const
Definition: Matrix.h:235
Matrix< double, 2, 2 > TwoMatrix
Definition: Matrix.h:315
Inherit from this in order to benefit from small object allocation.
Definition: SmallObject.h:131
T Of(const Matrix< T, n, m > &other)
Definition: Matrix.h:84
T Of(const Matrix< T, n, n > &other)
Definition: Matrix.h:92
Implements matrix addition, subtraction, multiplication.
Definition: Matrix.h:15
Matrix< T, n, m > & operator-=(const Matrix< T, n, m > &rhs)
Definition: Matrix.h:226
Allocator to be used by STL containers.
Definition: STLAdapter.h:17
Implementation of vector to be used in Linear Algebra.
Definition: Vector.h:16
Helper template produces NaN Determinant in general.
Definition: Matrix.h:83
Matrix< double, 4, 4 > FourMatrix
Definition: Matrix.h:317
Vector< T, m > operator*=(const Vector< T, n > &rhs)
Definition: Matrix.h:204
Matrix< double, 3, 3 > ThreeMatrix
Definition: Matrix.h:316
bool operator==(const Matrix< T, n, m > &rhs) const
Definition: Matrix.h:291
const Matrix< T, n, k > operator*(const Matrix< T, m, k > &rhs) const
Definition: Matrix.h:254
Matrix< T, 0, m-1 > Of(const Matrix< T, 2, m > &other, const int &i=0, const int &j=0)
Definition: Matrix.h:49
Matrix< T, n, m > & operator+=(const Matrix< T, n, m > &rhs)
Definition: Matrix.h:187