Jet  v1.3.3
matrix.h
Go to the documentation of this file.
1 // Copyright (c) 2018 Doyub Kim
2 //
3 // I am making my contributions/submissions to this project solely in my
4 // personal capacity and am not conveying any rights to any intellectual
5 // property of any third parties.
6 
7 #ifndef INCLUDE_JET_MATRIX_H_
8 #define INCLUDE_JET_MATRIX_H_
9 
10 #include <jet/macros.h>
11 #include <jet/matrix_expression.h>
12 
13 #include <array>
14 #include <type_traits>
15 
16 namespace jet {
17 
28 template <typename T, size_t M, size_t N>
29 class Matrix final : public MatrixExpression<T, Matrix<T, M, N>> {
30  public:
31  static_assert(
32  M > 0,
33  "Number of rows for static-sized matrix should be greater than zero.");
34  static_assert(
35  N > 0,
36  "Number of columns for static-sized matrix should be greater than "
37  "zero.");
38  static_assert(!(M == 2 && N == 2) && !(M == 3 && N == 3) &&
39  !(M == 4 && N == 4),
40  "Use specialized matrix for 2z2, 3x3, and 4x4 matricies.");
41  static_assert(std::is_floating_point<T>::value,
42  "Matrix only can be instantiated with floating point types");
43 
44  typedef std::array<T, M * N> ContainerType;
45  typedef typename ContainerType::iterator Iterator;
46  typedef typename ContainerType::const_iterator ConstIterator;
47 
50  Matrix();
51 
53  template <typename... Params>
54  explicit Matrix(Params... params);
55 
72  Matrix(const std::initializer_list<std::initializer_list<T>>& lst);
73 
75  template <typename E>
77 
79  Matrix(const Matrix& other);
80 
81  // MARK: Basic setters
82 
84  void resize(size_t m, size_t n, const T& s = T(0));
85 
87  void set(const T& s);
88 
106  void set(const std::initializer_list<std::initializer_list<T>>& lst);
107 
109  template <typename E>
110  void set(const MatrixExpression<T, E>& other);
111 
113  void setDiagonal(const T& s);
114 
116  void setOffDiagonal(const T& s);
117 
119  template <typename E>
120  void setRow(size_t i, const VectorExpression<T, E>& row);
121 
123  template <typename E>
124  void setColumn(size_t j, const VectorExpression<T, E>& col);
125 
126  // MARK: Basic getters
127  template <typename E>
128  bool isEqual(const MatrixExpression<T, E>& other) const;
129 
132  template <typename E>
134  double tol = std::numeric_limits<double>::epsilon()) const;
135 
137  constexpr bool isSquare() const;
138 
140  constexpr Size2 size() const;
141 
143  constexpr size_t rows() const;
144 
146  constexpr size_t cols() const;
147 
149  T* data();
150 
152  const T* const data() const;
153 
156 
159 
162 
165 
166  // MARK: Binary operator methods - new instance = this instance (+) input
167 
169  MatrixScalarAdd<T, Matrix> add(const T& s) const;
170 
172  template <typename E>
173  MatrixAdd<T, Matrix, E> add(const E& m) const;
174 
176  MatrixScalarSub<T, Matrix> sub(const T& s) const;
177 
179  template <typename E>
180  MatrixSub<T, Matrix, E> sub(const E& m) const;
181 
183  MatrixScalarMul<T, Matrix> mul(const T& s) const;
184 
186  template <typename VE>
188 
190  template <size_t L>
192 
194  MatrixScalarDiv<T, Matrix> div(const T& s) const;
195 
196  // MARK: Binary operator methods - new instance = input (+) this instance
198  MatrixScalarAdd<T, Matrix> radd(const T& s) const;
199 
201  template <typename E>
202  MatrixAdd<T, Matrix, E> radd(const E& m) const;
203 
206 
208  template <typename E>
209  MatrixSub<T, Matrix, E> rsub(const E& m) const;
210 
212  MatrixScalarMul<T, Matrix> rmul(const T& s) const;
213 
215  template <size_t L>
217 
220 
221  // MARK: Augmented operator methods - this instance (+)= input
222 
224  void iadd(const T& s);
225 
227  template <typename E>
228  void iadd(const E& m);
229 
231  void isub(const T& s);
232 
234  template <typename E>
235  void isub(const E& m);
236 
238  void imul(const T& s);
239 
241  template <typename E>
242  void imul(const E& m);
243 
245  void idiv(const T& s);
246 
247  // MARK: Modifiers
248 
250  void transpose();
251 
257  void invert();
258 
259  // MARK: Complex getters
261  T sum() const;
262 
264  T avg() const;
265 
267  T min() const;
268 
270  T max() const;
271 
273  T absmin() const;
274 
276  T absmax() const;
277 
280  T trace() const;
281 
283  T determinant() const;
284 
287 
290 
293 
296 
299 
302 
305 
307  Matrix inverse() const;
308 
309  template <typename U>
311 
312  // MARK: Setter operators
313 
315  template <typename E>
316  Matrix& operator=(const E& m);
317 
319  Matrix& operator=(const Matrix& other);
320 
322  Matrix& operator+=(const T& s);
323 
325  template <typename E>
326  Matrix& operator+=(const E& m);
327 
329  Matrix& operator-=(const T& s);
330 
332  template <typename E>
333  Matrix& operator-=(const E& m);
334 
336  Matrix& operator*=(const T& s);
337 
339  template <typename E>
340  Matrix& operator*=(const E& m);
341 
343  Matrix& operator/=(const T& s);
344 
345  // MARK: Getter operators
346 
348  T& operator[](size_t i);
349 
351  const T& operator[](size_t i) const;
352 
354  T& operator()(size_t i, size_t j);
355 
357  const T& operator()(size_t i, size_t j) const;
358 
360  template <typename E>
361  bool operator==(const MatrixExpression<T, E>& m) const;
362 
364  template <typename E>
365  bool operator!=(const MatrixExpression<T, E>& m) const;
366 
367  // MARK: Helpers
368 
395  template <typename Callback>
396  void forEach(Callback func) const;
397 
424  template <typename Callback>
425  void forEachIndex(Callback func) const;
426 
427  // MARK: Builders
428 
431 
435 
436  private:
437  ContainerType _elements;
438 
439  template <typename... Params>
440  void setRowAt(size_t i, T v, Params... params);
441  void setRowAt(size_t i, T v);
442 };
443 
444 } // namespace jet
445 
446 #include "detail/matrix-inl.h"
447 
448 #endif // INCLUDE_JET_MATRIX_H_
jet::Matrix::Matrix
Matrix(const std::initializer_list< std::initializer_list< T >> &lst)
Constructs a matrix with given initializer list lst.
jet::Matrix::begin
ConstIterator begin() const
Returns the begin const iterator of the matrix.
jet::Matrix::end
Iterator end()
Returns the end iterator of the matrix.
jet::Matrix::determinant
T determinant() const
Returns determinant of this matrix.
jet::Matrix::setOffDiagonal
void setOffDiagonal(const T &s)
Sets off-diagonal elements with input scalar.
jet::Matrix::size
constexpr Size2 size() const
Returns the size of this matrix.
jet::VectorExpression
Base class for vector expression.
Definition: vector_expression.h:25
jet::Matrix::absmin
T absmin() const
Returns absolute minimum among all elements.
jet::Matrix::set
void set(const T &s)
Sets whole matrix with input scalar.
jet::Matrix::operator*=
Matrix & operator*=(const T &s)
Multiplication assignment with input scalar.
jet::Matrix::sum
T sum() const
Returns sum of all elements.
jet::Matrix::setRow
void setRow(size_t i, const VectorExpression< T, E > &row)
Sets i-th row with input vector.
jet::Matrix
Static-sized M x N matrix class.
Definition: matrix.h:29
jet::Matrix::sub
MatrixSub< T, Matrix, E > sub(const E &m) const
Returns this matrix - input matrix (element-wise).
jet::Matrix::Iterator
ContainerType::iterator Iterator
Definition: matrix.h:45
jet::Matrix::Matrix
Matrix(const Matrix &other)
Copy constructor.
jet::Matrix::operator*=
Matrix & operator*=(const E &m)
Multiplication assignment with input matrix.
jet::MatrixConstant
Constant matrix expression.
Definition: matrix_expression.h:49
jet::Matrix::castTo
MatrixTypeCast< U, Matrix, T > castTo() const
jet::Matrix::cols
constexpr size_t cols() const
Returns number of columns of this matrix.
jet::Matrix::trace
T trace() const
jet::Matrix::data
T * data()
Returns data pointer of this matrix.
jet::Matrix::strictLowerTri
MatrixTriangular< T, Matrix > strictLowerTri() const
Returns strictly lower triangle part of this matrix.
jet::MatrixVectorMul
Vector expression for matrix-vector multiplication.
Definition: matrix_expression.h:295
jet::MatrixBinaryOp
Matrix expression for binary operation.
Definition: matrix_expression.h:225
jet::Matrix::resize
void resize(size_t m, size_t n, const T &s=T(0))
Resizes to m x n matrix with initial value s.
macros.h
jet::Matrix::rmul
MatrixMul< T, Matrix< T, N, L >, Matrix > rmul(const Matrix< T, N, L > &m) const
Returns input matrix * this matrix.
jet::Matrix::operator[]
T & operator[](size_t i)
Returns reference of i-th element.
jet::Matrix::Matrix
Matrix(const MatrixExpression< T, E > &other)
Constructs a matrix with expression template.
jet::Matrix::begin
Iterator begin()
Returns the begin iterator of the matrix.
jet::Matrix::transpose
void transpose()
Transposes this matrix.
jet::Matrix::radd
MatrixAdd< T, Matrix, E > radd(const E &m) const
Returns input matrix + this matrix (element-wise).
jet::Matrix::operator-=
Matrix & operator-=(const E &m)
Subtraction assignment with input matrix (element-wise).
jet::Matrix::set
void set(const std::initializer_list< std::initializer_list< T >> &lst)
Sets a matrix with given initializer list lst.
jet::Matrix::isEqual
bool isEqual(const MatrixExpression< T, E > &other) const
jet::Matrix::rows
constexpr size_t rows() const
Returns number of rows of this matrix.
jet::Matrix::transposed
Matrix< T, N, M > transposed() const
Returns transposed matrix.
jet::Matrix::end
ConstIterator end() const
Returns the end const iterator of the matrix.
jet::Matrix::ConstIterator
ContainerType::const_iterator ConstIterator
Definition: matrix.h:46
jet::Matrix::data
const T *const data() const
Returns constant pointer of this matrix.
jet::Matrix::makeZero
static MatrixConstant< T > makeZero()
Makes a M x N matrix with zeros.
jet::Matrix::operator()
T & operator()(size_t i, size_t j)
Returns reference of (i,j) element.
jet::Matrix::isub
void isub(const E &m)
Subtracts input matrix from this matrix (element-wise).
jet::Matrix::iadd
void iadd(const E &m)
Adds input matrix to this matrix (element-wise).
jet::Matrix::set
void set(const MatrixExpression< T, E > &other)
Copies from input matrix expression.
jet::Matrix::diagonal
MatrixDiagonal< T, Matrix > diagonal() const
Returns diagonal part of this matrix.
jet
Definition: advection_solver2.h:18
jet::Matrix::imul
void imul(const T &s)
Multiplies input scalar to this matrix.
jet::Matrix::idiv
void idiv(const T &s)
Divides this matrix with input scalar.
jet::Matrix::isub
void isub(const T &s)
Subtracts input scalar from this matrix.
jet::MatrixExpression
Base class for matrix expression.
Definition: matrix_expression.h:26
jet::Matrix::Matrix
Matrix(Params... params)
Constructs matrix instance with parameters.
jet::Matrix::operator+=
Matrix & operator+=(const E &m)
Addition assignment with input matrix (element-wise).
jet::Matrix::inverse
Matrix inverse() const
Returns inverse matrix.
jet::Matrix::iadd
void iadd(const T &s)
Adds input scalar to this matrix.
jet::Matrix::operator()
const T & operator()(size_t i, size_t j) const
Returns constant reference of (i,j) element.
jet::Matrix::add
MatrixScalarAdd< T, Matrix > add(const T &s) const
Returns this matrix + input scalar.
jet::Matrix::mul
MatrixMul< T, Matrix, Matrix< T, N, L > > mul(const Matrix< T, N, L > &m) const
Returns this matrix * input matrix.
jet::Matrix::operator[]
const T & operator[](size_t i) const
Returns constant reference of i-th element.
matrix_expression.h
jet::MatrixScalarBinaryOp
Matrix expression for matrix-scalar binary operation.
Definition: matrix_expression.h:261
jet::Matrix::isSimilar
bool isSimilar(const MatrixExpression< T, E > &other, double tol=std::numeric_limits< double >::epsilon()) const
jet::Matrix::rdiv
MatrixScalarRDiv< T, Matrix > rdiv(const T &s) const
Returns input matrix / this scalar.
jet::Matrix::mul
MatrixVectorMul< T, Matrix, VE > mul(const VectorExpression< T, VE > &v) const
Returns this matrix * input vector.
jet::Matrix::setDiagonal
void setDiagonal(const T &s)
Sets diagonal elements with input scalar.
jet::Matrix::operator/=
Matrix & operator/=(const T &s)
Division assignment with input scalar.
jet::Matrix::mul
MatrixScalarMul< T, Matrix > mul(const T &s) const
Returns this matrix * input scalar.
jet::Matrix::add
MatrixAdd< T, Matrix, E > add(const E &m) const
Returns this matrix + input matrix (element-wise).
jet::Matrix::min
T min() const
Returns minimum among all elements.
jet::Matrix::invert
void invert()
Inverts this matrix.
jet::Matrix::isSquare
constexpr bool isSquare() const
Returns true if this matrix is a square matrix.
jet::Matrix::forEachIndex
void forEachIndex(Callback func) const
Iterates the matrix and invoke given func for each index.
jet::Matrix::operator-=
Matrix & operator-=(const T &s)
Subtraction assignment with input scalar.
jet::Matrix::forEach
void forEach(Callback func) const
Iterates the matrix and invoke given func for each index.
jet::MatrixIdentity
Identity matrix expression.
Definition: matrix_expression.h:80
jet::Matrix::operator==
bool operator==(const MatrixExpression< T, E > &m) const
Returns true if is equal to m.
jet::Matrix::offDiagonal
MatrixDiagonal< T, Matrix > offDiagonal() const
Returns off-diagonal part of this matrix.
jet::Matrix::sub
MatrixScalarSub< T, Matrix > sub(const T &s) const
Returns this matrix - input scalar.
jet::Matrix::operator=
Matrix & operator=(const Matrix &other)
Copies to this matrix.
jet::Matrix::strictUpperTri
MatrixTriangular< T, Matrix > strictUpperTri() const
Returns strictly upper triangle part of this matrix.
jet::Size2
2-D size class.
Definition: size2.h:19
jet::Matrix::upperTri
MatrixTriangular< T, Matrix > upperTri() const
Returns upper triangle part of this matrix (including the diagonal).
jet::Matrix::operator!=
bool operator!=(const MatrixExpression< T, E > &m) const
Returns true if is not equal to m.
jet::MatrixTriangular
Triangular matrix expression.
Definition: matrix_expression.h:179
jet::Matrix::operator+=
Matrix & operator+=(const T &s)
Addition assignment with input scalar.
jet::Matrix::imul
void imul(const E &m)
Multiplies input matrix to this matrix.
jet::Matrix::setColumn
void setColumn(size_t j, const VectorExpression< T, E > &col)
Sets j-th column with input vector.
jet::MatrixMul
Matrix expression for matrix-matrix multiplication.
Definition: matrix_expression.h:321
jet::Matrix::radd
MatrixScalarAdd< T, Matrix > radd(const T &s) const
Returns input scalar + this matrix.
jet::Matrix::lowerTri
MatrixTriangular< T, Matrix > lowerTri() const
Returns lower triangle part of this matrix (including the diagonal).
jet::MatrixDiagonal
Diagonal matrix expression.
Definition: matrix_expression.h:146
jet::Matrix::ContainerType
std::array< T, M *N > ContainerType
Definition: matrix.h:33
jet::Matrix::operator=
Matrix & operator=(const E &m)
Assigns input matrix.
jet::MatrixUnaryOp
Matrix expression for unary operation.
Definition: matrix_expression.h:114
jet::Matrix::makeIdentity
static MatrixIdentity< T > makeIdentity()
jet::Matrix::rmul
MatrixScalarMul< T, Matrix > rmul(const T &s) const
Returns input scalar * this matrix.
jet::Matrix::absmax
T absmax() const
Returns absolute maximum among all elements.
jet::Matrix::div
MatrixScalarDiv< T, Matrix > div(const T &s) const
Returns this matrix / input scalar.
jet::Matrix::Matrix
Matrix()
jet::Matrix::avg
T avg() const
Returns average of all elements.
jet::Matrix::rsub
MatrixSub< T, Matrix, E > rsub(const E &m) const
Returns input matrix - this matrix (element-wise).
jet::Matrix::max
T max() const
Returns maximum among all elements.
jet::Matrix::rsub
MatrixScalarRSub< T, Matrix > rsub(const T &s) const
Returns input scalar - this matrix.