Jet  v1.3.3
matrix_mxn.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_MXN_H_
8 #define INCLUDE_JET_MATRIX_MXN_H_
9 
10 #include <jet/array2.h>
11 #include <jet/matrix_expression.h>
12 #include <jet/vector_n.h>
13 
14 namespace jet {
15 
16 // MARK: MatrixMxN
17 
25 template <typename T>
26 class MatrixMxN final : public MatrixExpression<T, MatrixMxN<T>> {
27  public:
28  static_assert(
29  std::is_floating_point<T>::value,
30  "MatrixMxN only can be instantiated with floating point types");
31 
35 
36  // MARK: Constructors
37 
40 
42  MatrixMxN(size_t m, size_t n, const T& s = T(0));
43 
62  MatrixMxN(const std::initializer_list<std::initializer_list<T>>& lst);
63 
65  template <typename E>
67 
70  MatrixMxN(size_t m, size_t n, const T* arr);
71 
73  MatrixMxN(const MatrixMxN& other);
74 
76  MatrixMxN(MatrixMxN&& other);
77 
78  // MARK: Basic setters
79 
81  void resize(size_t m, size_t n, const T& s = T(0));
82 
84  void set(const T& s);
85 
105  void set(const std::initializer_list<std::initializer_list<T>>& lst);
106 
108  template <typename E>
109  void set(const MatrixExpression<T, E>& other);
110 
113  void set(size_t m, size_t n, const T* arr);
114 
116  void setDiagonal(const T& s);
117 
119  void setOffDiagonal(const T& s);
120 
122  template <typename E>
123  void setRow(size_t i, const VectorExpression<T, E>& row);
124 
126  template <typename E>
127  void setColumn(size_t j, const VectorExpression<T, E>& col);
128 
129  // MARK: Basic getters
130  template <typename E>
131  bool isEqual(const MatrixExpression<T, E>& other) const;
132 
135  template <typename E>
137  double tol = std::numeric_limits<double>::epsilon()) const;
138 
140  bool isSquare() const;
141 
143  Size2 size() const;
144 
146  size_t rows() const;
147 
149  size_t cols() const;
150 
152  T* data();
153 
155  const T* const data() const;
156 
159 
162 
165 
168 
169  // MARK: Binary operator methods - new instance = this instance (+) input
170 
173 
175  template <typename E>
176  MatrixAdd<T, MatrixMxN, E> add(const E& m) const;
177 
180 
182  template <typename E>
183  MatrixSub<T, MatrixMxN, E> sub(const E& m) const;
184 
187 
189  template <typename VE>
191  const VectorExpression<T, VE>& v) const;
192 
194  template <typename E>
195  MatrixMul<T, MatrixMxN, E> mul(const E& m) const;
196 
199 
200  // MARK: Binary operator methods - new instance = input (+) this instance
203 
205  template <typename E>
206  MatrixAdd<T, MatrixMxN, E> radd(const E& m) const;
207 
210 
212  template <typename E>
213  MatrixSub<T, MatrixMxN, E> rsub(const E& m) const;
214 
217 
219  template <typename E>
220  MatrixMul<T, E, MatrixMxN> rmul(const E& m) const;
221 
224 
225  // MARK: Augmented operator methods - this instance (+)= input
226 
228  void iadd(const T& s);
229 
231  template <typename E>
232  void iadd(const E& m);
233 
235  void isub(const T& s);
236 
238  template <typename E>
239  void isub(const E& m);
240 
242  void imul(const T& s);
243 
245  template <typename E>
246  void imul(const E& m);
247 
249  void idiv(const T& s);
250 
251  // MARK: Modifiers
252 
254  void transpose();
255 
261  void invert();
262 
263  // MARK: Complex getters
265  T sum() const;
266 
268  T avg() const;
269 
271  T min() const;
272 
274  T max() const;
275 
277  T absmin() const;
278 
280  T absmax() const;
281 
284  T trace() const;
285 
287  T determinant() const;
288 
291 
294 
297 
300 
303 
306 
309 
312 
314  template <typename U>
316 
317  // MARK: Setter operators
318 
320  template <typename E>
321  MatrixMxN& operator=(const E& m);
322 
324  MatrixMxN& operator=(const MatrixMxN& other);
325 
328 
330  MatrixMxN& operator+=(const T& s);
331 
333  template <typename E>
334  MatrixMxN& operator+=(const E& m);
335 
337  MatrixMxN& operator-=(const T& s);
338 
340  template <typename E>
341  MatrixMxN& operator-=(const E& m);
342 
344  MatrixMxN& operator*=(const T& s);
345 
347  template <typename E>
348  MatrixMxN& operator*=(const E& m);
349 
351  MatrixMxN& operator/=(const T& s);
352 
353  // MARK: Getter operators
354 
356  T& operator[](size_t i);
357 
359  const T& operator[](size_t i) const;
360 
362  T& operator()(size_t i, size_t j);
363 
365  const T& operator()(size_t i, size_t j) const;
366 
368  template <typename E>
369  bool operator==(const MatrixExpression<T, E>& m) const;
370 
372  template <typename E>
373  bool operator!=(const MatrixExpression<T, E>& m) const;
374 
375  // MARK: Helpers
376 
403  template <typename Callback>
404  void forEach(Callback func) const;
405 
432  template <typename Callback>
433  void forEachIndex(Callback func) const;
434 
454  template <typename Callback>
455  void parallelForEach(Callback func);
456 
474  template <typename Callback>
475  void parallelForEachIndex(Callback func) const;
476 
477  // MARK: Builders
478 
480  static MatrixConstant<T> makeZero(size_t m, size_t n);
481 
484  static MatrixIdentity<T> makeIdentity(size_t m);
485 
486  private:
487  ContainerType _elements;
488 };
489 
492 
495 
496 } // namespace jet
497 
498 #include "detail/matrix_mxn-inl.h"
499 
500 #endif // INCLUDE_JET_MATRIX_MXN_H_
jet::MatrixMxN
M x N matrix class.
Definition: matrix_mxn.h:26
jet::MatrixMxN::determinant
T determinant() const
Returns determinant of this matrix.
jet::MatrixMxN::data
const T *const data() const
Returns constant pointer of this matrix.
jet::MatrixMxN::iadd
void iadd(const E &m)
Adds input matrix to this matrix (element-wise).
array2.h
jet::VectorExpression
Base class for vector expression.
Definition: vector_expression.h:25
jet::MatrixMxN::data
T * data()
Returns data pointer of this matrix.
jet::MatrixMxN::operator==
bool operator==(const MatrixExpression< T, E > &m) const
Returns true if is equal to m.
jet::MatrixMxN::rmul
MatrixMul< T, E, MatrixMxN > rmul(const E &m) const
Returns input matrix * this matrix.
jet::MatrixMxN::cols
size_t cols() const
Returns number of columns of this matrix.
jet::MatrixMxN::ConstIterator
ContainerType::ConstIterator ConstIterator
Definition: matrix_mxn.h:34
jet::MatrixMxN::forEachIndex
void forEachIndex(Callback func) const
Iterates the matrix and invoke given func for each index.
jet::MatrixMxN::isEqual
bool isEqual(const MatrixExpression< T, E > &other) const
jet::MatrixMxN::rmul
MatrixScalarMul< T, MatrixMxN > rmul(const T &s) const
Returns input scalar * this matrix.
jet::MatrixMxN::idiv
void idiv(const T &s)
Divides this matrix with input scalar.
jet::MatrixMxN::mul
MatrixMul< T, MatrixMxN, E > mul(const E &m) const
Returns this matrix * input matrix.
jet::MatrixMxN::size
Size2 size() const
Returns the size of this matrix.
jet::Array< T, 2 >::ConstIterator
ContainerType::const_iterator ConstIterator
Definition: array2.h:46
jet::MatrixMxN::sum
T sum() const
Returns sum of all elements.
jet::MatrixMxN::imul
void imul(const E &m)
Multiplies input matrix to this matrix.
jet::MatrixMxN::rsub
MatrixSub< T, MatrixMxN, E > rsub(const E &m) const
Returns input matrix - this matrix (element-wise).
jet::MatrixMxN::offDiagonal
MatrixDiagonal< T, MatrixMxN > offDiagonal() const
Returns off-diagonal part of this matrix.
jet::MatrixMxN::resize
void resize(size_t m, size_t n, const T &s=T(0))
Resizes to m x n matrix with initial value s.
jet::MatrixConstant
Constant matrix expression.
Definition: matrix_expression.h:49
jet::MatrixMxN::MatrixMxN
MatrixMxN(const MatrixExpression< T, E > &other)
Constructs a matrix with expression template.
jet::MatrixMxN::operator-=
MatrixMxN & operator-=(const E &m)
Subtraction assignment with input matrix (element-wise).
jet::MatrixMxN::strictUpperTri
MatrixTriangular< T, MatrixMxN > strictUpperTri() const
Returns strictly upper triangle part of this matrix.
jet::MatrixMxN::operator()
T & operator()(size_t i, size_t j)
Returns reference of (i,j) element.
jet::MatrixVectorMul
Vector expression for matrix-vector multiplication.
Definition: matrix_expression.h:295
jet::MatrixMxN::operator!=
bool operator!=(const MatrixExpression< T, E > &m) const
Returns true if is not equal to m.
jet::MatrixBinaryOp
Matrix expression for binary operation.
Definition: matrix_expression.h:225
jet::MatrixMxN::MatrixMxN
MatrixMxN(size_t m, size_t n, const T &s=T(0))
Constructs m x n constant value matrix.
jet::MatrixMxN::max
T max() const
Returns maximum among all elements.
jet::MatrixMxN::set
void set(const T &s)
Sets whole matrix with input scalar.
jet::MatrixMxN::isSquare
bool isSquare() const
Returns true if this matrix is a square matrix.
jet::MatrixMxN::min
T min() const
Returns minimum among all elements.
jet::MatrixMxN::makeIdentity
static MatrixIdentity< T > makeIdentity(size_t m)
jet::MatrixMxN::radd
MatrixScalarAdd< T, MatrixMxN > radd(const T &s) const
Returns input scalar + this matrix.
jet::MatrixMxN::MatrixMxN
MatrixMxN(MatrixMxN &&other)
Move constructor.
jet::MatrixMxN::lowerTri
MatrixTriangular< T, MatrixMxN > lowerTri() const
Returns lower triangle part of this matrix (including the diagonal).
jet::MatrixMxN::setOffDiagonal
void setOffDiagonal(const T &s)
Sets off-diagonal elements with input scalar.
jet::MatrixMxN::MatrixMxN
MatrixMxN(const std::initializer_list< std::initializer_list< T >> &lst)
Constructs a matrix with given initializer list lst.
jet::MatrixMxN::operator/=
MatrixMxN & operator/=(const T &s)
Division assignment with input scalar.
jet::MatrixMxN::inverse
MatrixMxN inverse() const
Returns inverse matrix.
jet::Array< T, 2 >
2-D array class.
Definition: array2.h:42
jet::MatrixMxN::div
MatrixScalarDiv< T, MatrixMxN > div(const T &s) const
Returns this matrix / input scalar.
jet::MatrixMxN::operator=
MatrixMxN & operator=(const E &m)
Assigns input matrix.
jet::MatrixMxN::operator+=
MatrixMxN & operator+=(const T &s)
Addition assignment with input scalar.
jet::MatrixMxN::parallelForEachIndex
void parallelForEachIndex(Callback func) const
Iterates the matrix and invoke given func for each index in parallel using multi-threading.
jet::MatrixMxN::set
void set(const MatrixExpression< T, E > &other)
Copies from input matrix expression.
jet::MatrixMxN::operator+=
MatrixMxN & operator+=(const E &m)
Addition assignment with input matrix (element-wise).
jet
Definition: advection_solver2.h:18
jet::MatrixMxN::operator-=
MatrixMxN & operator-=(const T &s)
Subtraction assignment with input scalar.
jet::MatrixMxN::begin
Iterator begin()
Returns the begin iterator of the matrix.
jet::MatrixMxN::parallelForEach
void parallelForEach(Callback func)
Iterates the matrix and invoke given func for each index in parallel.
jet::MatrixMxN::operator[]
T & operator[](size_t i)
Returns reference of i-th element.
jet::MatrixMxN::isub
void isub(const E &m)
Subtracts input matrix from this matrix (element-wise).
jet::MatrixExpression
Base class for matrix expression.
Definition: matrix_expression.h:26
jet::MatrixMxN::end
ConstIterator end() const
Returns the end const iterator of the matrix.
jet::MatrixMxN::begin
ConstIterator begin() const
Returns the begin const iterator of the matrix.
jet::MatrixMxN::MatrixMxN
MatrixMxN()
Constructs an empty matrix.
jet::MatrixMxN::absmax
T absmax() const
Returns absolute maximum among all elements.
matrix_expression.h
jet::MatrixMxN::rdiv
MatrixScalarRDiv< T, MatrixMxN > rdiv(const T &s) const
Returns input matrix / this scalar.
jet::MatrixScalarBinaryOp
Matrix expression for matrix-scalar binary operation.
Definition: matrix_expression.h:261
jet::MatrixMxN::isub
void isub(const T &s)
Subtracts input scalar from this matrix.
jet::MatrixMxN::MatrixMxN
MatrixMxN(const MatrixMxN &other)
Copy constructor.
jet::MatrixMxN::operator=
MatrixMxN & operator=(const MatrixMxN &other)
Copies to this matrix.
jet::MatrixMxN::set
void set(const std::initializer_list< std::initializer_list< T >> &lst)
Sets a matrix with given initializer list lst.
jet::MatrixMxN::upperTri
MatrixTriangular< T, MatrixMxN > upperTri() const
Returns upper triangle part of this matrix (including the diagonal).
jet::MatrixMxN::add
MatrixAdd< T, MatrixMxN, E > add(const E &m) const
Returns this matrix + input matrix (element-wise).
jet::MatrixMxN::rows
size_t rows() const
Returns number of rows of this matrix.
jet::MatrixMxN::ContainerType
Array2< T > ContainerType
Definition: matrix_mxn.h:30
jet::MatrixMxN::castTo
MatrixTypeCast< U, MatrixMxN, T > castTo() const
Type-casts to different value-typed matrix.
jet::MatrixMxNF
MatrixMxN< float > MatrixMxNF
Float-type M x N matrix.
Definition: matrix_mxn.h:491
jet::MatrixMxN::end
Iterator end()
Returns the end iterator of the matrix.
jet::MatrixMxN::absmin
T absmin() const
Returns absolute minimum among all elements.
jet::MatrixIdentity
Identity matrix expression.
Definition: matrix_expression.h:80
jet::MatrixMxN::transpose
void transpose()
Transposes this matrix.
jet::MatrixMxN::setDiagonal
void setDiagonal(const T &s)
Sets diagonal elements with input scalar.
jet::MatrixMxN::operator*=
MatrixMxN & operator*=(const E &m)
Multiplication assignment with input matrix.
jet::MatrixMxND
MatrixMxN< double > MatrixMxND
Double-type M x N matrix.
Definition: matrix_mxn.h:494
jet::Size2
2-D size class.
Definition: size2.h:19
jet::MatrixMxN::sub
MatrixSub< T, MatrixMxN, E > sub(const E &m) const
Returns this matrix - input matrix (element-wise).
jet::MatrixMxN::setColumn
void setColumn(size_t j, const VectorExpression< T, E > &col)
Sets j-th column with input vector.
jet::MatrixTriangular
Triangular matrix expression.
Definition: matrix_expression.h:179
jet::MatrixMxN::forEach
void forEach(Callback func) const
Iterates the matrix and invoke given func for each index.
jet::MatrixMxN::Iterator
ContainerType::Iterator Iterator
Definition: matrix_mxn.h:33
jet::MatrixMxN::operator=
MatrixMxN & operator=(MatrixMxN &&other)
Moves to this matrix.
jet::MatrixMxN::radd
MatrixAdd< T, MatrixMxN, E > radd(const E &m) const
Returns input matrix + this matrix (element-wise).
jet::MatrixMxN::strictLowerTri
MatrixTriangular< T, MatrixMxN > strictLowerTri() const
Returns strictly lower triangle part of this matrix.
jet::MatrixMxN::MatrixMxN
MatrixMxN(size_t m, size_t n, const T *arr)
jet::MatrixMul
Matrix expression for matrix-matrix multiplication.
Definition: matrix_expression.h:321
jet::MatrixMxN::operator()
const T & operator()(size_t i, size_t j) const
Returns constant reference of (i,j) element.
jet::MatrixMxN::set
void set(size_t m, size_t n, const T *arr)
jet::MatrixMxN::setRow
void setRow(size_t i, const VectorExpression< T, E > &row)
Sets i-th row with input vector.
jet::MatrixMxN::mul
MatrixScalarMul< T, MatrixMxN > mul(const T &s) const
Returns this matrix * input scalar.
jet::MatrixMxN::iadd
void iadd(const T &s)
Adds input scalar to this matrix.
jet::MatrixMxN::imul
void imul(const T &s)
Multiplies input scalar to this matrix.
vector_n.h
jet::MatrixDiagonal
Diagonal matrix expression.
Definition: matrix_expression.h:146
jet::MatrixMxN::sub
MatrixScalarSub< T, MatrixMxN > sub(const T &s) const
Returns this matrix - input scalar.
jet::MatrixUnaryOp
Matrix expression for unary operation.
Definition: matrix_expression.h:114
jet::MatrixMxN::transposed
MatrixMxN transposed() const
Returns transposed matrix.
jet::Array< T, 2 >::Iterator
ContainerType::iterator Iterator
Definition: array2.h:45
jet::MatrixMxN::rsub
MatrixScalarRSub< T, MatrixMxN > rsub(const T &s) const
Returns input scalar - this matrix.
jet::MatrixMxN::makeZero
static MatrixConstant< T > makeZero(size_t m, size_t n)
Makes a m x n matrix with zeros.
jet::MatrixMxN::diagonal
MatrixDiagonal< T, MatrixMxN > diagonal() const
Returns diagonal part of this matrix.
jet::MatrixMxN::add
MatrixScalarAdd< T, MatrixMxN > add(const T &s) const
Returns this matrix + input scalar.
jet::MatrixMxN::operator[]
const T & operator[](size_t i) const
Returns constant reference of i-th element.
jet::MatrixMxN::trace
T trace() const
jet::MatrixMxN::invert
void invert()
Inverts this matrix.
jet::MatrixMxN::isSimilar
bool isSimilar(const MatrixExpression< T, E > &other, double tol=std::numeric_limits< double >::epsilon()) const
jet::MatrixMxN::operator*=
MatrixMxN & operator*=(const T &s)
Multiplication assignment with input scalar.
jet::MatrixMxN::mul
MatrixVectorMul< T, MatrixMxN, VE > mul(const VectorExpression< T, VE > &v) const
Returns this matrix * input vector.
jet::MatrixMxN::avg
T avg() const
Returns average of all elements.