22 template <
class T,
unsigned M=0,
unsigned N=0>
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];}
48 for (
unsigned i=0; i < M; ++i)
49 r.elems[i] = this->elems[i] + b.elems[i];
56 for (
unsigned i=0; i < M; ++i)
57 r.elems[i] = this->elems[i] - b.elems[i];
64 for (
unsigned i=0; i < M; ++i)
65 r.elems[i] = this->elems[i] * s;
72 for (
unsigned i=0; i < M; ++i)
73 r.elems[i] = this->elems[i] / s;
80 for (
unsigned i=0; i < M; ++i)
81 r[i] = this->elems[i] * b;
91 for (
unsigned i=0; i < M; ++i)
92 for (
unsigned j=0; j < O; ++j)
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];
103 for (
unsigned i=0; i < M; ++i)
104 this->elems[i].
zero();
106 void resize(
unsigned m,
unsigned n)
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;}
144 return elems[i*columns + j];
149 return elems[i*columns + j];
154 for (
unsigned i=0; i < this->elems.size(); ++i)
155 this->elems[i] += b.elems[i];
161 for (
unsigned i=0; i < this->elems.size(); ++i)
162 this->elems[i] -= b.elems[i];
168 for (
unsigned i=0; i < this->elems.size(); ++i)
175 for (
unsigned i=0; i < this->elems.size(); ++i)
182 for (
unsigned i=0; i <
cols() *
rows(); ++i)
183 this->elems[i] = (T)0.0;
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)
214 for (
unsigned i=0; i < 3; ++i)
215 for (
unsigned j=0; j < 3; ++j)
216 array[i][j] = (*
this)[i][j];
222 template <
class T,
unsigned M,
unsigned N>
223 std::ostream& operator<<(std::ostream& s, mth::Matrix<T,M,N>
const& a)
225 for (
unsigned i=0; i < a.rows(); ++i) {
226 for (
unsigned j=0; j < a.cols(); ++j)
Matrix< T > & operator-=(Matrix< T > const &b)
subtract a matrix from this matrix
unsigned rows() const
get the number of rows
Matrix< T, M, N > operator+(Matrix< T, M, N > const &b) const
add two matrices
Matrix< T > & operator/=(T const &s)
divide this matrix by a scalar
Matrix< T, M, N > operator*(T const &s) const
multiply by a scalar
Matrix< T > & operator+=(Matrix< T > const &b)
add a matrix to this matrix
compile-time (static) matrix
void toArray(T(*array)[3]) const
write matrix to an array
compile-time (static) vector of size N
Matrix< T > & operator*=(T const &s)
multiply this matrix by a scalar
Matrix()
default constructor
convenience wrapper over apf::Vector<3>
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
unsigned rows() const
get the number of rows
Matrix< T, M, N > operator-(Matrix< T, M, N > const &b) const
subtract two matrices
convenience wrapper over Matrix<T,3,3>
T const & operator()(unsigned i, unsigned j) const
immutable index operator
Matrix(unsigned m, unsigned n)
construct with m by n elements
Small compile-time and run-time linear algebra vectors.
Matrix3x3()
default constructor
compile-time (static) array of size N
Matrix< T, M, O > operator*(Matrix< T, N, O > const &b) const
multiply two matrices
T & operator()(unsigned i, unsigned j)
mutable index operator
Matrix< T, M, N > operator/(T const &s) const
divide by a scalar
unsigned cols() const
get the number of columns
Matrix3x3(Matrix< T, 3, 3 > const &other)
copy constructor
unsigned cols() const
get the number of columns
Matrix(unsigned m, unsigned n)
construct m by n elements
Matrix()
default constructor - no allocation
T const & operator()(unsigned i, unsigned j) const
immutable index operator
T & operator()(unsigned i, unsigned j)
mutable index operator
void resize(unsigned m, unsigned n)
resize to m by n elements
Vector< T, M > operator*(Vector< T, N > const &b) const
multiply a matrix by a vector