SCOREC core
Parallel unstructured mesh tools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
apfMatrix.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011 Scientific Computation Research Center
3  *
4  * This work is open source software, licensed under the terms of the
5  * BSD license as described in the LICENSE file in the top-level directory.
6  */
7 
8 #ifndef APFMATRIX_H
9 #define APFMATRIX_H
10 
14 #include "apfVector.h"
15 
16 namespace apf {
17 
30 template <std::size_t M, std::size_t N>
31 class Matrix : public Array<Vector<N>,M>
32 {
33  public:
35  Matrix() {}
37  Matrix(double const (*array)[N])
38  {
39  for (std::size_t i=0; i < M; ++i)
40  for (std::size_t j=0; j < N; ++j)
41  (*this)[i][j] = array[i][j];
42  }
44  Matrix(Vector<N> const* vectors)
45  {
46  for (std::size_t i=0; i < M; ++i)
47  for (std::size_t j=0; j < N; ++j)
48  (*this)[i][j] = vectors[i][j];
49  }
52  {
53  Matrix<M,N> r;
54  for (std::size_t i=0; i < M; ++i)
55  r[i] = (*this)[i] + b[i];
56  return r;
57  }
60  {
61  Matrix<M,N> r;
62  for (std::size_t i=0; i < M; ++i)
63  r[i] = (*this)[i] - b[i];
64  return r;
65  }
67  Matrix<M,N> operator*(double s) const
68  {
69  Matrix<M,N> r;
70  for (std::size_t i=0; i < M; ++i)
71  r[i] = (*this)[i] * s;
72  return r;
73  }
75  Matrix<M,N> operator/(double s) const
76  {
77  Matrix<M,N> r;
78  for (std::size_t i=0; i < M; ++i)
79  r[i] = (*this)[i] / s;
80  return r;
81  }
83  Vector<M> operator*(Vector<N> const& b) const
84  {
85  Vector<M> r;
86  for (std::size_t i=0; i < M; ++i)
87  r[i] = (*this)[i] * b;
88  return r;
89  }
94  template <std::size_t O>
96  {
97  Matrix<M,O> r;
98  for (std::size_t i=0; i < M; ++i)
99  for (std::size_t j=0; j < O; ++j)
100  {
101  r[i][j] = (*this)[i][0]*b[0][j];
102  for (std::size_t k=1; k < N; ++k)
103  r[i][j] += (*this)[i][k]*b[k][j];
104  }
105  return r;
106  }
107 };
108 
110 template <std::size_t M, std::size_t N>
112 {
113  Matrix<N,M> r;
114  for (std::size_t i=0; i < M; ++i)
115  for (std::size_t j=0; j < N; ++j)
116  r[j][i] = m[i][j];
117  return r;
118 }
119 
121 template <std::size_t M, std::size_t N>
123 {
124  Matrix<M,N> r;
125  for (std::size_t i=0; i < M; ++i)
126  r[i] = b * a[i];
127  return r;
128 }
129 
132 template <std::size_t M, std::size_t N>
133 Matrix<M-1,N-1> getMinor(Matrix<M,N> const& A, std::size_t i, std::size_t j);
134 
137 template <std::size_t M, std::size_t N>
138 double getCofactor(Matrix<M,N> const& A, std::size_t i, std::size_t j);
139 
142 template <std::size_t M, std::size_t N>
143 double getDeterminant(Matrix<M,N> const& A);
144 
147 {
148  Matrix<3,3> r;
149  for (std::size_t i = 0; i < 3; ++i)
150  for (std::size_t j = 0; j < 3; ++j)
151  r[i][j] = getCofactor(m, i, j);
152  return r;
153 }
154 
157 {
158  Matrix<2,2> a;
159  a[0][0] = m[1][1]; a[0][1] = -m[0][1];
160  a[1][0] = -m[1][0]; a[1][1] = m[0][0];
161  return a / getDeterminant(m);
162 }
163 
166 {
167  Matrix<3,3> x = transpose(m);
168  Matrix<3,3> r;
169  r[0] = cross(x[1],x[2]);
170  r[1] = cross(x[2],x[0]);
171  r[2] = cross(x[0],x[1]);
172  return r / getDeterminant(m);
173 }
174 
178 class Matrix3x3 : public Matrix<3,3>
179 {
180  public:
185  Matrix3x3(double a11, double a12, double a13,
186  double a21, double a22, double a23,
187  double a31, double a32, double a33)
188  {
189  (*this)[0] = Vector3(a11,a12,a13);
190  (*this)[1] = Vector3(a21,a22,a23);
191  (*this)[2] = Vector3(a31,a32,a33);
192  }
194  Matrix3x3(Matrix<3,3> const& other):
195  Matrix<3,3>(other)
196  {}
199  void toArray(double (*array)[3]) const
200  {
201  for (std::size_t i=0; i < 3; ++i)
202  for (std::size_t j=0; j < 3; ++j)
203  array[i][j] = (*this)[i][j];
204  }
205 };
206 
208 template <std::size_t M, std::size_t N>
209 double getInnerProduct(Matrix<M,N> const& a,
210  Matrix<M,N> const& b)
211 {
212  double r = a[0]*b[0];
213  for (std::size_t i=1; i < M; ++i)
214  r += a[i]*b[i];
215  return r;
216 }
217 
219 Matrix3x3 cross(Vector3 const& u);
223 Matrix3x3 rotate(Vector3 const& u, double a);
233 Matrix3x3 getFrame(Vector3 const& v);
234 
236 int eigen(Matrix3x3 const& A,
237  Vector<3>* eigenVectors,
238  double* eigenValues);
239 
249 template <std::size_t M>
250 void applyMatrixFunc(Matrix<M,M> const& A, double (*callback)(double), Matrix<M,M> & newMat);
251 
252 }//namespace apf
253 
254 template <std::size_t M, std::size_t N>
255 std::ostream& operator<<(std::ostream& s, apf::Matrix<M,N> const& A)
256 {
257  for (std::size_t i = 0; i < M; ++i) {
258  for (std::size_t j = 0; j < N; ++j)
259  s << A[i][j] << ' ';
260  s << '\n';
261  }
262  return s;
263 }
264 
265 #endif
Matrix< M, N > operator*(double s) const
multiply a matrix by a scalar
Definition: apfMatrix.h:67
Matrix< M, N > operator/(double s) const
divide a matrix by a scalar
Definition: apfMatrix.h:75
int eigen(Matrix3x3 const &A, Vector< 3 > *eigenVectors, double *eigenValues)
get the eigenvectors and eigenvalues of a 3 by 3 matrix
Vector< M > operator*(Vector< N > const &b) const
multiply a matrix by a vector
Definition: apfMatrix.h:83
Matrix< N, M > transpose(Matrix< M, N > const &m)
transpose a matrix
Definition: apfMatrix.h:111
Matrix< 3, 3 > cofactor(Matrix< 3, 3 > const &m)
get the matrix of cofactors for a given matrix
Definition: apfMatrix.h:146
apf::Matrix3x3 Matrix
convenient matrix name
Definition: maMesh.h:25
double getInnerProduct(Matrix< M, N > const &a, Matrix< M, N > const &b)
get the component-wise inner product of two matrices
Definition: apfMatrix.h:209
double getDeterminant(Matrix< M, N > const &A)
get the determinant of a matrix A
Matrix3x3 getFrame(Vector3 const &v)
derive an orthogonal frame whose x axis is the given vector
Matrix3x3(Matrix< 3, 3 > const &other)
constructor from base type
Definition: apfMatrix.h:194
convenience wrapper over apf::Matrix&lt;3,3&gt;
Definition: apfMatrix.h:178
template-generic vector of N doubles
Definition: apfVector.h:26
Matrix< M, N > operator+(Matrix< M, N > const &b) const
add two matrices
Definition: apfMatrix.h:51
Matrix(Vector< N > const *vectors)
construct from an array
Definition: apfMatrix.h:44
Matrix()
mandatory
Definition: apfMatrix.h:35
void applyMatrixFunc(Matrix< M, M > const &A, double(*callback)(double), Matrix< M, M > &newMat)
void toArray(double(*array)[3]) const
write matrix to an array
Definition: apfMatrix.h:199
Matrix< 2, 2 > invert(Matrix< 2, 2 > const &m)
invert a 2 by 2 matrix
Definition: apfMatrix.h:156
convenience wrapper over apf::Vector&lt;3&gt;
Definition: apfVector.h:150
Matrix< M, O > operator*(Matrix< N, O > const &b) const
multiply two matrices
Definition: apfMatrix.h:95
Matrix3x3()
required default constructor
Definition: apfMatrix.h:182
template-generic matrix of M by N doubles
Definition: apfMatrix.h:31
Matrix< M, N > tensorProduct(Vector< M > const &a, Vector< N > const &b)
tensor product of two vectors
Definition: apfMatrix.h:122
Matrix3x3(double a11, double a12, double a13, double a21, double a22, double a23, double a31, double a32, double a33)
component-wise constructor
Definition: apfMatrix.h:185
Matrix3x3 rotate(Vector3 const &u, double a)
get the rotation matrix around an axis
Matrix< M-1, N-1 > getMinor(Matrix< M, N > const &A, std::size_t i, std::size_t j)
get the minor matrix associated with entry (i,j) of matrix A
Matrix< M, N > operator-(Matrix< M, N > const &b) const
subtract two matrices
Definition: apfMatrix.h:59
double getCofactor(Matrix< M, N > const &A, std::size_t i, std::size_t j)
get the cofactor associated with entry (i,j) of matrix A
Vector< 3 > cross(Vector< 3 > const &a, Vector< 3 > const &b)
3D vector cross product
Definition: apfVector.h:123
The APF linear algebra vector interface.