SCOREC core
Parallel unstructured mesh tools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
mthMatrix.h
Go to the documentation of this file.
1 /******************************************************************************
2 
3  Copyright 2015 Scientific Computation Research Center,
4  Rensselaer Polytechnic Institute. All rights reserved.
5 
6  This work is open source software, licensed under the terms of the
7  BSD license as described in the LICENSE file in the top-level directory.
8 
9 *******************************************************************************/
10 
11 #ifndef MTH_MATRIX_H
12 #define MTH_MATRIX_H
13 
14 #include "mthVector.h"
15 
19 namespace mth {
20 
22 template <class T, unsigned M=0, unsigned N=0>
23 class Matrix : public can::Array<Vector<T,N>,M>
24 {
25  public:
27  Matrix() {}
31  Matrix(unsigned m, unsigned n) { (void) m; (void) n; }
33  unsigned rows() const {return M;}
35  unsigned cols() const {return N;}
40  T& operator()(unsigned i, unsigned j) {return (*this)[i][j];}
43  T const& operator()(unsigned i, unsigned j) const {return (*this)[i][j];}
46  {
47  Matrix<T,M,N> r;
48  for (unsigned i=0; i < M; ++i)
49  r.elems[i] = this->elems[i] + b.elems[i];
50  return r;
51  }
54  {
55  Matrix<T,M,N> r;
56  for (unsigned i=0; i < M; ++i)
57  r.elems[i] = this->elems[i] - b.elems[i];
58  return r;
59  }
61  Matrix<T,M,N> operator*(T const& s) const
62  {
63  Matrix<T,M,N> r;
64  for (unsigned i=0; i < M; ++i)
65  r.elems[i] = this->elems[i] * s;
66  return r;
67  }
69  Matrix<T,M,N> operator/(T const& s) const
70  {
71  Matrix<T,M,N> r;
72  for (unsigned i=0; i < M; ++i)
73  r.elems[i] = this->elems[i] / s;
74  return r;
75  }
78  {
79  Vector<T,M> r;
80  for (unsigned i=0; i < M; ++i)
81  r[i] = this->elems[i] * b;
82  return r;
83  }
87  template <unsigned O>
89  {
90  Matrix<T,M,O> r;
91  for (unsigned i=0; i < M; ++i)
92  for (unsigned j=0; j < O; ++j)
93  {
94  r[i][j] = this->elems[i][0]*b[0][j];
95  for (unsigned k=1; k < N; ++k)
96  r[i][j] += this->elems[i][k]*b[k][j];
97  }
98  return r;
99  }
101  void zero()
102  {
103  for (unsigned i=0; i < M; ++i)
104  this->elems[i].zero();
105  }
106  void resize(unsigned m, unsigned n)
107  {
108  (void) m;
109  (void) n;
110  }
111 };
112 
123 template <class T>
124 class Matrix<T,0,0>
125 {
126  public:
128  Matrix() : columns(0) {}
130  Matrix(unsigned m, unsigned n) : columns(n), elems(m*n) {}
132  unsigned rows() const {return elems.size()/columns;}
134  unsigned cols() const {return columns;}
136  void resize(unsigned m, unsigned n)
137  {
138  columns = n;
139  elems.resize(m*n);
140  }
142  T& operator()(unsigned i, unsigned j)
143  {
144  return elems[i*columns + j];
145  }
147  T const& operator()(unsigned i, unsigned j) const
148  {
149  return elems[i*columns + j];
150  }
153  {
154  for (unsigned i=0; i < this->elems.size(); ++i)
155  this->elems[i] += b.elems[i];
156  return *this;
157  }
160  {
161  for (unsigned i=0; i < this->elems.size(); ++i)
162  this->elems[i] -= b.elems[i];
163  return *this;
164  }
166  Matrix<T>& operator*=(T const& s)
167  {
168  for (unsigned i=0; i < this->elems.size(); ++i)
169  this->elems[i] *= s;
170  return *this;
171  }
173  Matrix<T>& operator/=(T const& s)
174  {
175  for (unsigned i=0; i < this->elems.size(); ++i)
176  this->elems[i] /= s;
177  return *this;
178  }
180  void zero()
181  {
182  for (unsigned i=0; i < cols() * rows(); ++i)
183  this->elems[i] = (T)0.0;
184  }
185  protected:
186  unsigned columns;
187  can::Array<T> elems;
188 };
189 
193 template <class T>
194 class Matrix3x3 : public Matrix<T,3,3>
195 {
196  public:
201  T const& a11,T const& a12,T const& a13,
202  T const& a21,T const& a22, T const& a23,
203  T const& a31,T const& a32, T const& a33)
204  {
205  (*this)[0] = Vector3<T>(a11,a12,a13);
206  (*this)[1] = Vector3<T>(a21,a22,a23);
207  (*this)[2] = Vector3<T>(a31,a32,a33);
208  }
210  Matrix3x3(Matrix<T,3,3> const& other) : Matrix<T,3,3>(other) {}
212  void toArray(T (*array)[3]) const
213  {
214  for (unsigned i=0; i < 3; ++i)
215  for (unsigned j=0; j < 3; ++j)
216  array[i][j] = (*this)[i][j];
217  }
218 };
219 
220 }
221 
222 template <class T, unsigned M, unsigned N>
223 std::ostream& operator<<(std::ostream& s, mth::Matrix<T,M,N> const& a)
224 {
225  for (unsigned i=0; i < a.rows(); ++i) {
226  for (unsigned j=0; j < a.cols(); ++j)
227  s << a(i,j) << ' ';
228  s << '\n';
229  }
230  return s;
231 }
232 
233 #endif
Matrix< T > & operator-=(Matrix< T > const &b)
subtract a matrix from this matrix
Definition: mthMatrix.h:159
unsigned rows() const
get the number of rows
Definition: mthMatrix.h:132
Matrix< T, M, N > operator+(Matrix< T, M, N > const &b) const
add two matrices
Definition: mthMatrix.h:45
Matrix< T > & operator/=(T const &s)
divide this matrix by a scalar
Definition: mthMatrix.h:173
Matrix< T, M, N > operator*(T const &s) const
multiply by a scalar
Definition: mthMatrix.h:61
Matrix< T > & operator+=(Matrix< T > const &b)
add a matrix to this matrix
Definition: mthMatrix.h:152
compile-time (static) matrix
Definition: mthMatrix.h:23
void toArray(T(*array)[3]) const
write matrix to an array
Definition: mthMatrix.h:212
compile-time (static) vector of size N
Definition: mthVector.h:29
Matrix< T > & operator*=(T const &s)
multiply this matrix by a scalar
Definition: mthMatrix.h:166
Matrix()
default constructor
Definition: mthMatrix.h:27
convenience wrapper over apf::Vector&lt;3&gt;
Definition: mthVector.h:195
Matrix3x3(T const &a11, T const &a12, T const &a13, T const &a21, T const &a22, T const &a23, T const &a31, T const &a32, T const &a33)
component-wise constructor
Definition: mthMatrix.h:200
unsigned rows() const
get the number of rows
Definition: mthMatrix.h:33
Matrix< T, M, N > operator-(Matrix< T, M, N > const &b) const
subtract two matrices
Definition: mthMatrix.h:53
convenience wrapper over Matrix&lt;T,3,3&gt;
Definition: mthMatrix.h:194
T const & operator()(unsigned i, unsigned j) const
immutable index operator
Definition: mthMatrix.h:43
Matrix(unsigned m, unsigned n)
construct with m by n elements
Definition: mthMatrix.h:31
Small compile-time and run-time linear algebra vectors.
Matrix3x3()
default constructor
Definition: mthMatrix.h:198
compile-time (static) array of size N
Definition: canArray.h:23
Matrix< T, M, O > operator*(Matrix< T, N, O > const &b) const
multiply two matrices
Definition: mthMatrix.h:88
T & operator()(unsigned i, unsigned j)
mutable index operator
Definition: mthMatrix.h:142
Matrix< T, M, N > operator/(T const &s) const
divide by a scalar
Definition: mthMatrix.h:69
unsigned cols() const
get the number of columns
Definition: mthMatrix.h:134
void zero()
zero a matrix
Definition: mthMatrix.h:101
Matrix3x3(Matrix< T, 3, 3 > const &other)
copy constructor
Definition: mthMatrix.h:210
unsigned cols() const
get the number of columns
Definition: mthMatrix.h:35
Matrix(unsigned m, unsigned n)
construct m by n elements
Definition: mthMatrix.h:130
Matrix()
default constructor - no allocation
Definition: mthMatrix.h:128
T const & operator()(unsigned i, unsigned j) const
immutable index operator
Definition: mthMatrix.h:147
T & operator()(unsigned i, unsigned j)
mutable index operator
Definition: mthMatrix.h:40
void zero()
zero a matrix
Definition: mthMatrix.h:180
void resize(unsigned m, unsigned n)
resize to m by n elements
Definition: mthMatrix.h:136
Vector< T, M > operator*(Vector< T, N > const &b) const
multiply a matrix by a vector
Definition: mthMatrix.h:77