30 template <std::
size_t M, std::
size_t N>
31 class Matrix :
public Array<Vector<N>,M>
37 Matrix(
double const (*array)[N])
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];
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];
54 for (std::size_t i=0; i < M; ++i)
55 r[i] = (*
this)[i] + b[i];
62 for (std::size_t i=0; i < M; ++i)
63 r[i] = (*
this)[i] - b[i];
70 for (std::size_t i=0; i < M; ++i)
71 r[i] = (*
this)[i] * s;
78 for (std::size_t i=0; i < M; ++i)
79 r[i] = (*
this)[i] / s;
86 for (std::size_t i=0; i < M; ++i)
87 r[i] = (*
this)[i] * b;
94 template <std::
size_t O>
98 for (std::size_t i=0; i < M; ++i)
99 for (std::size_t j=0; j < O; ++j)
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];
110 template <std::
size_t M, std::
size_t N>
114 for (std::size_t i=0; i < M; ++i)
115 for (std::size_t j=0; j < N; ++j)
121 template <std::
size_t M, std::
size_t N>
125 for (std::size_t i=0; i < M; ++i)
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);
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);
142 template <std::
size_t M, std::
size_t N>
149 for (std::size_t i = 0; i < 3; ++i)
150 for (std::size_t j = 0; j < 3; ++j)
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];
169 r[0] =
cross(x[1],x[2]);
170 r[1] =
cross(x[2],x[0]);
171 r[2] =
cross(x[0],x[1]);
186 double a21,
double a22,
double a23,
187 double a31,
double a32,
double a33)
189 (*this)[0] =
Vector3(a11,a12,a13);
190 (*this)[1] =
Vector3(a21,a22,a23);
191 (*this)[2] =
Vector3(a31,a32,a33);
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];
208 template <std::
size_t M, std::
size_t N>
212 double r = a[0]*b[0];
213 for (std::size_t i=1; i < M; ++i)
219 Matrix3x3
cross(Vector3
const& u);
223 Matrix3x3
rotate(Vector3
const& u,
double a);
233 Matrix3x3
getFrame(Vector3
const& v);
236 int eigen(Matrix3x3
const& A,
237 Vector<3>* eigenVectors,
238 double* eigenValues);
249 template <std::
size_t M>
250 void applyMatrixFunc(Matrix<M,M>
const& A,
double (*callback)(
double), Matrix<M,M> & newMat);
254 template <std::
size_t M, std::
size_t N>
255 std::ostream& operator<<(std::ostream& s, apf::Matrix<M,N>
const& A)
257 for (std::size_t i = 0; i < M; ++i) {
258 for (std::size_t j = 0; j < N; ++j)
Matrix< M, N > operator*(double s) const
multiply a matrix by a scalar
Matrix< M, N > operator/(double s) const
divide a matrix by a scalar
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
Matrix< N, M > transpose(Matrix< M, N > const &m)
transpose a matrix
Matrix< 3, 3 > cofactor(Matrix< 3, 3 > const &m)
get the matrix of cofactors for a given matrix
apf::Matrix3x3 Matrix
convenient matrix name
double getInnerProduct(Matrix< M, N > const &a, Matrix< M, N > const &b)
get the component-wise inner product of two matrices
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
convenience wrapper over apf::Matrix<3,3>
template-generic vector of N doubles
Matrix< M, N > operator+(Matrix< M, N > const &b) const
add two matrices
Matrix(Vector< N > const *vectors)
construct from an array
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
Matrix< 2, 2 > invert(Matrix< 2, 2 > const &m)
invert a 2 by 2 matrix
convenience wrapper over apf::Vector<3>
Matrix< M, O > operator*(Matrix< N, O > const &b) const
multiply two matrices
Matrix3x3()
required default constructor
template-generic matrix of M by N doubles
Matrix< M, N > tensorProduct(Vector< M > const &a, Vector< N > const &b)
tensor product of two vectors
Matrix3x3(double a11, double a12, double a13, double a21, double a22, double a23, double a31, double a32, double a33)
component-wise constructor
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
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
The APF linear algebra vector interface.