Matrix.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: Matrix.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: Static-sized M x N matrix class.
6 > Created Time: 2017/03/06
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_MATRIX_H
10 #define CUBBYFLOW_MATRIX_H
11 
13 
14 #include <array>
15 #include <type_traits>
16 
17 namespace CubbyFlow
18 {
29  template <typename T, size_t M, size_t N>
30  class Matrix final : public MatrixExpression<T, Matrix<T, M, N>>
31  {
32  public:
33  static_assert(M > 0, "Number of rows for static-sized matrix should be greater than zero.");
34  static_assert(N > 0, "Number of columns for static-sized matrix should be greater than zero.");
35  static_assert(!(M == 2 && N == 2) && !(M == 3 && N == 3) && !(M == 4 && N == 4), "Use specialized matrix for 2z2, 3x3, and 4x4 matrices.");
36  static_assert(std::is_floating_point<T>::value, "Matrix only can be instantiated with floating point types.");
37 
38  using ContainerType = std::array<T, M * N>;
39  using Iterator = typename ContainerType::iterator;
40  using ConstIterator = typename ContainerType::const_iterator;
41 
44  Matrix();
45 
47  template <typename... Params>
48  explicit Matrix(Params... params);
49 
66  Matrix(const std::initializer_list<std::initializer_list<T>>& list);
67 
69  template <typename E>
70  Matrix(const MatrixExpression<T, E>& other);
71 
73  Matrix(const Matrix& other);
74 
75  // MARK: Basic setters
77  void Resize(size_t m, size_t n, const T& s = T(0));
78 
80  void Set(const T& s);
81 
99  void Set(const std::initializer_list<std::initializer_list<T>>& list);
100 
102  template <typename E>
103  void Set(const MatrixExpression<T, E>& other);
104 
106  void SetDiagonal(const T& s);
107 
109  void SetOffDiagonal(const T& s);
110 
112  template <typename E>
113  void SetRow(size_t i, const VectorExpression<T, E>& row);
114 
116  template <typename E>
117  void SetColumn(size_t j, const VectorExpression<T, E>& col);
118 
119  // MARK: Basic getters
120  template <typename E>
121  bool IsEqual(const MatrixExpression<T, E>& other) const;
122 
125  template <typename E>
126  bool IsSimilar(const MatrixExpression<T, E>& other, double tol = std::numeric_limits<double>::epsilon()) const;
127 
129  constexpr bool IsSquare() const;
130 
132  constexpr Size2 size() const;
133 
135  constexpr size_t Rows() const;
136 
138  constexpr size_t Cols() const;
139 
141  T* data();
142 
144  const T* data() const;
145 
147  Iterator begin();
148 
150  ConstIterator begin() const;
151 
153  Iterator end();
154 
156  ConstIterator end() const;
157 
158  // MARK: Binary operator methods - new instance = this instance (+) input
160  MatrixScalarAdd<T, Matrix> Add(const T& s) const;
161 
163  template <typename E>
164  MatrixAdd<T, Matrix, E> Add(const E& m) const;
165 
167  MatrixScalarSub<T, Matrix> Sub(const T& s) const;
168 
170  template <typename E>
171  MatrixSub<T, Matrix, E> Sub(const E& m) const;
172 
174  MatrixScalarMul<T, Matrix> Mul(const T& s) const;
175 
177  template <typename VE>
179 
181  template <size_t L>
183 
185  MatrixScalarDiv<T, Matrix> Div(const T& s) const;
186 
187  // MARK: Binary operator methods - new instance = input (+) this instance
189  MatrixScalarAdd<T, Matrix> RAdd(const T& s) const;
190 
192  template <typename E>
193  MatrixAdd<T, Matrix, E> RAdd(const E& m) const;
194 
196  MatrixScalarRSub<T, Matrix> RSub(const T& s) const;
197 
199  template <typename E>
200  MatrixSub<T, Matrix, E> RSub(const E& m) const;
201 
203  MatrixScalarMul<T, Matrix> RMul(const T& s) const;
204 
206  template <size_t L>
208 
210  MatrixScalarRDiv<T, Matrix> RDiv(const T& s) const;
211 
212  // MARK: Augmented operator methods - this instance (+)= input
214  void IAdd(const T& s);
215 
217  template <typename E>
218  void IAdd(const E& m);
219 
221  void ISub(const T& s);
222 
224  template <typename E>
225  void ISub(const E& m);
226 
228  void IMul(const T& s);
229 
231  template <typename E>
232  void IMul(const E& m);
233 
235  void IDiv(const T& s);
236 
237  // MARK: Modifiers
239  void Transpose();
240 
246  void Invert();
247 
248  // MARK: Complex getters
250  T Sum() const;
251 
253  T Avg() const;
254 
256  T Min() const;
257 
259  T Max() const;
260 
262  T AbsMin() const;
263 
265  T AbsMax() const;
266 
269  T Trace() const;
270 
272  T Determinant() const;
273 
276 
279 
282 
285 
288 
291 
293  Matrix<T, N, M> Transposed() const;
294 
296  Matrix Inverse() const;
297 
298  template <typename U>
300 
301  // MARK: Setter operators
303  template <typename E>
304  Matrix& operator=(const E& m);
305 
307  Matrix& operator=(const Matrix& other);
308 
310  Matrix& operator+=(const T& s);
311 
313  template <typename E>
314  Matrix& operator+=(const E& m);
315 
317  Matrix& operator-=(const T& s);
318 
320  template <typename E>
321  Matrix& operator-=(const E& m);
322 
324  Matrix& operator*=(const T& s);
325 
327  template <typename E>
328  Matrix& operator*=(const E& m);
329 
331  Matrix& operator/=(const T& s);
332 
333  // MARK: Getter operators
335  T& operator[](size_t i);
336 
338  const T& operator[](size_t i) const;
339 
341  T& operator()(size_t i, size_t j);
342 
344  const T& operator()(size_t i, size_t j) const;
345 
347  template <typename E>
348  bool operator==(const MatrixExpression<T, E>& m) const;
349 
351  template <typename E>
352  bool operator!=(const MatrixExpression<T, E>& m) const;
353 
354  // MARK: Helpers
384  template <typename Callback>
385  void ForEach(Callback func) const;
386 
416  template <typename Callback>
417  void ForEachIndex(Callback func) const;
418 
419  // MARK: Builders
421  static MatrixConstant<T> MakeZero();
422 
425 
426  private:
427  ContainerType m_elements;
428 
429  template <typename... Params>
430  void SetRowAt(size_t i, T v, Params... params);
431  void SetRowAt(size_t i, T v);
432  };
433 }
434 
435 #include <Core/Matrix/Matrix-Impl.h>
436 
437 #endif
bool IsSimilar(const MatrixExpression< T, E > &other, double tol=std::numeric_limits< double >::epsilon()) const
Definition: Matrix-Impl.h:169
static MatrixConstant< T > MakeZero()
Makes a M x N matrix with zeros.
Definition: Matrix-Impl.h:823
MatrixDiagonal< T, Matrix > Diagonal() const
Returns diagonal part of this matrix.
Definition: Matrix-Impl.h:633
MatrixDiagonal< T, Matrix > OffDiagonal() const
Returns off-diagonal part of this matrix.
Definition: Matrix-Impl.h:639
T Sum() const
Returns sum of all elements.
Definition: Matrix-Impl.h:483
Matrix & operator*=(const T &s)
Multiplication assignment with input scalar.
Definition: Matrix-Impl.h:737
Matrix & operator/=(const T &s)
Division assignment with input scalar.
Definition: Matrix-Impl.h:752
T * data()
Returns data pointer of this matrix.
Definition: Matrix-Impl.h:217
void SetDiagonal(const T &s)
Sets diagonal elements with input scalar.
Definition: Matrix-Impl.h:92
void IMul(const T &s)
Multiplies input scalar to this matrix.
Definition: Matrix-Impl.h:376
MatrixScalarRSub< T, Matrix > RSub(const T &s) const
Returns input scalar - this matrix.
Definition: Matrix-Impl.h:318
void IAdd(const T &s)
Adds input scalar to this matrix.
Definition: Matrix-Impl.h:350
void Set(const T &s)
Sets whole matrix with input scalar.
Definition: Matrix-Impl.h:52
Matrix expression for matrix-scalar binary operation.
Definition: MatrixExpression.h:261
MatrixScalarAdd< T, Matrix > Add(const T &s) const
Returns this matrix + input scalar.
Definition: Matrix-Impl.h:253
constexpr Size2 size() const
Returns the size of this matrix.
Definition: Matrix-Impl.h:199
void SetRow(size_t i, const VectorExpression< T, E > &row)
Sets i-th row with input vector.
Definition: Matrix-Impl.h:116
void IDiv(const T &s)
Divides this matrix with input scalar.
Definition: Matrix-Impl.h:390
Base class for vector expression.
Definition: VectorExpression.h:28
bool operator!=(const MatrixExpression< T, E > &m) const
Returns true if is not equal to m.
Definition: Matrix-Impl.h:791
Iterator begin()
Returns the begin iterator of the matrix.
Definition: Matrix-Impl.h:229
MatrixTriangular< T, Matrix > StrictLowerTri() const
Returns strictly lower triangle part of this matrix.
Definition: Matrix-Impl.h:645
Matrix expression for unary operation.
Definition: MatrixExpression.h:117
void ISub(const T &s)
Subtracts input scalar from this matrix.
Definition: Matrix-Impl.h:363
Vector expression for matrix-vector multiplication.
Definition: MatrixExpression.h:296
2-D point class.
Definition: Point2.h:25
constexpr size_t Cols() const
Returns number of columns of this matrix.
Definition: Matrix-Impl.h:211
T Max() const
Returns maximum among all elements.
Definition: Matrix-Impl.h:515
Matrix & operator+=(const T &s)
Addition assignment with input scalar.
Definition: Matrix-Impl.h:707
MatrixScalarDiv< T, Matrix > Div(const T &s) const
Returns this matrix / input scalar.
Definition: Matrix-Impl.h:299
Identity matrix expression.
Definition: MatrixExpression.h:83
MatrixScalarSub< T, Matrix > Sub(const T &s) const
Returns this matrix - input scalar.
Definition: Matrix-Impl.h:266
Matrix Inverse() const
Returns inverse matrix.
Definition: Matrix-Impl.h:677
MatrixScalarMul< T, Matrix > RMul(const T &s) const
Returns input scalar * this matrix.
Definition: Matrix-Impl.h:331
MatrixScalarAdd< T, Matrix > RAdd(const T &s) const
Returns input scalar + this matrix.
Definition: Matrix-Impl.h:305
Triangular matrix expression.
Definition: MatrixExpression.h:182
typename ContainerType::iterator Iterator
Definition: Matrix.h:39
Static-sized M x N matrix class.
Definition: Matrix.h:30
void Resize(size_t m, size_t n, const T &s=T(0))
Resizes to m x n matrix with initial value s.
MatrixScalarMul< T, Matrix > Mul(const T &s) const
Returns this matrix * input scalar.
Definition: Matrix-Impl.h:279
bool IsEqual(const MatrixExpression< T, E > &other) const
Definition: Matrix-Impl.h:144
void ForEach(Callback func) const
Iterates the matrix and invoke given func for each index.
Definition: Matrix-Impl.h:798
void Invert()
Inverts this matrix.
Definition: Matrix-Impl.h:402
Definition: pybind11Utils.h:24
T & operator[](size_t i)
Returns reference of i-th element.
Definition: Matrix-Impl.h:759
bool operator==(const MatrixExpression< T, E > &m) const
Returns true if is equal to m.
Definition: Matrix-Impl.h:784
std::array< T, M *N > ContainerType
Definition: Matrix.h:38
MatrixTriangular< T, Matrix > StrictUpperTri() const
Returns strictly upper triangle part of this matrix.
Definition: Matrix-Impl.h:651
constexpr bool IsSquare() const
Returns true if this matrix is a square matrix.
Definition: Matrix-Impl.h:193
Iterator end()
Returns the end iterator of the matrix.
Definition: Matrix-Impl.h:241
Matrix & operator-=(const T &s)
Subtraction assignment with input scalar.
Definition: Matrix-Impl.h:722
Matrix & operator=(const E &m)
Assigns input matrix.
const Matrix< T, M, N > & operator()() const
Returns actual implementation (the subclass).
Definition: MatrixExpression-Impl.h:34
T Trace() const
Definition: Matrix-Impl.h:554
T Avg() const
Returns average of all elements.
Definition: Matrix-Impl.h:496
typename ContainerType::const_iterator ConstIterator
Definition: Matrix.h:40
Base class for matrix expression.
Definition: MatrixExpression.h:27
Diagonal matrix expression.
Definition: MatrixExpression.h:149
T Determinant() const
Returns determinant of this matrix.
Definition: Matrix-Impl.h:569
Constant matrix expression.
Definition: MatrixExpression.h:51
MatrixTriangular< T, Matrix > UpperTri() const
Returns upper triangle part of this matrix (including the diagonal).
Definition: Matrix-Impl.h:663
void Transpose()
Transposes this matrix.
Definition: Matrix-Impl.h:396
void ForEachIndex(Callback func) const
Iterates the matrix and invoke given func for each index.
Definition: Matrix-Impl.h:811
static MatrixIdentity< T > MakeIdentity()
Makes a M x N matrix with all diagonal elements to 1, and other elements to 0.
Definition: Matrix-Impl.h:829
T Min() const
Returns minimum among all elements.
Definition: Matrix-Impl.h:502
T AbsMin() const
Returns absolute minimum among all elements.
Definition: Matrix-Impl.h:528
void SetOffDiagonal(const T &s)
Sets off-diagonal elements with input scalar.
Definition: Matrix-Impl.h:103
MatrixTypeCast< U, Matrix, T > CastTo() const
MatrixScalarRDiv< T, Matrix > RDiv(const T &s) const
Returns input matrix / this scalar.
Definition: Matrix-Impl.h:344
Matrix expression for binary operation.
Definition: MatrixExpression.h:226
constexpr size_t Rows() const
Returns number of rows of this matrix.
Definition: Matrix-Impl.h:205
Matrix< T, N, M > Transposed() const
Returns transposed matrix.
Definition: Matrix-Impl.h:669
T AbsMax() const
Returns absolute maximum among all elements.
Definition: Matrix-Impl.h:541
Matrix()
Definition: Matrix-Impl.h:15
void SetColumn(size_t j, const VectorExpression< T, E > &col)
Sets j-th column with input vector.
Definition: Matrix-Impl.h:130
MatrixTriangular< T, Matrix > LowerTri() const
Returns lower triangle part of this matrix (including the diagonal).
Definition: Matrix-Impl.h:657
Matrix expression for matrix-matrix multiplication.
Definition: MatrixExpression.h:323