MatrixMxN.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: MatrixMxN.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: M x N matrix class.
6 > Created Time: 2017/09/27
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_MATRIXMXN_H
10 #define CUBBYFLOW_MATRIXMXN_H
11 
12 #include <Core/Array/Array2.h>
14 
15 namespace CubbyFlow
16 {
17  // MARK: MatrixMxN
25  template <typename T>
26  class MatrixMxN final : public MatrixExpression<T, MatrixMxN<T>>
27  {
28  public:
29  static_assert(std::is_floating_point<T>::value, "MatrixMxN only can be instantiated with floating point types");
30 
34 
35  // MARK: Constructors
37  MatrixMxN();
38 
40  MatrixMxN(size_t m, size_t n, const T& s = T(0));
41 
60  MatrixMxN(const std::initializer_list<std::initializer_list<T>>& list);
61 
63  template <typename E>
64  MatrixMxN(const MatrixExpression<T, E>& other);
65 
68  MatrixMxN(size_t m, size_t n, const T* arr);
69 
71  MatrixMxN(const MatrixMxN& other);
72 
74  MatrixMxN(MatrixMxN&& other) noexcept;
75 
76  // MARK: Basic setters
78  void Resize(size_t m, size_t n, const T& s = T(0));
79 
81  void Set(const T& s);
82 
102  void Set(const std::initializer_list<std::initializer_list<T>>& list);
103 
105  template <typename E>
106  void Set(const MatrixExpression<T, E>& other);
107 
110  void Set(size_t m, size_t n, const T* arr);
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>
133  bool IsSimilar(const MatrixExpression<T, E>& other, double tol = std::numeric_limits<double>::epsilon()) const;
134 
136  bool IsSquare() const;
137 
139  Size2 size() const;
140 
142  size_t Rows() const;
143 
145  size_t Cols() const;
146 
148  T* data();
149 
151  const T* data() const;
152 
154  Iterator begin();
155 
157  ConstIterator begin() const;
158 
160  Iterator end();
161 
163  ConstIterator end() const;
164 
165  // MARK: Binary operator methods - new instance = this instance (+) input
167  MatrixScalarAdd<T, MatrixMxN> Add(const T& s) const;
168 
170  template <typename E>
171  MatrixAdd<T, MatrixMxN, E> Add(const E& m) const;
172 
174  MatrixScalarSub<T, MatrixMxN> Sub(const T& s) const;
175 
177  template <typename E>
178  MatrixSub<T, MatrixMxN, E> Sub(const E& m) const;
179 
181  MatrixScalarMul<T, MatrixMxN> Mul(const T& s) const;
182 
184  template <typename VE>
186 
188  template <typename E>
189  MatrixMul<T, MatrixMxN, E> Mul(const E& m) const;
190 
192  MatrixScalarDiv<T, MatrixMxN> Div(const T& s) const;
193 
194  // MARK: Binary operator methods - new instance = input (+) this instance
196  MatrixScalarAdd<T, MatrixMxN> RAdd(const T& s) const;
197 
199  template <typename E>
200  MatrixAdd<T, MatrixMxN, E> RAdd(const E& m) const;
201 
203  MatrixScalarRSub<T, MatrixMxN> RSub(const T& s) const;
204 
206  template <typename E>
207  MatrixSub<T, MatrixMxN, E> RSub(const E& m) const;
208 
210  MatrixScalarMul<T, MatrixMxN> RMul(const T& s) const;
211 
213  template <typename E>
214  MatrixMul<T, E, MatrixMxN> RMul(const E& m) const;
215 
217  MatrixScalarRDiv<T, MatrixMxN> RDiv(const T& s) const;
218 
219  // MARK: Augmented operator methods - this instance (+)= input
221  void IAdd(const T& s);
222 
224  template <typename E>
225  void IAdd(const E& m);
226 
228  void ISub(const T& s);
229 
231  template <typename E>
232  void ISub(const E& m);
233 
235  void IMul(const T& s);
236 
238  template <typename E>
239  void IMul(const E& m);
240 
242  void IDiv(const T& s);
243 
244  // MARK: Modifiers
246  void Transpose();
247 
253  void Invert();
254 
255  // MARK: Complex getters
257  T Sum() const;
258 
260  T Avg() const;
261 
263  T Min() const;
264 
266  T Max() const;
267 
269  T AbsMin() const;
270 
272  T AbsMax() const;
273 
276  T Trace() const;
277 
279  T Determinant() const;
280 
283 
286 
289 
292 
295 
298 
300  MatrixMxN Transposed() const;
301 
303  MatrixMxN Inverse() const;
304 
306  template <typename U>
308 
309  // MARK: Setter operators
311  template <typename E>
312  MatrixMxN& operator=(const E& m);
313 
315  MatrixMxN& operator=(const MatrixMxN& other);
316 
318  MatrixMxN& operator=(MatrixMxN&& other) noexcept;
319 
321  MatrixMxN& operator+=(const T& s);
322 
324  template <typename E>
325  MatrixMxN& operator+=(const E& m);
326 
328  MatrixMxN& operator-=(const T& s);
329 
331  template <typename E>
332  MatrixMxN& operator-=(const E& m);
333 
335  MatrixMxN& operator*=(const T& s);
336 
338  template <typename E>
339  MatrixMxN& operator*=(const E& m);
340 
342  MatrixMxN& operator/=(const T& s);
343 
344  // MARK: Getter operators
346  T& operator[](size_t i);
347 
349  const T& operator[](size_t i) const;
350 
352  T& operator()(size_t i, size_t j);
353 
355  const T& operator()(size_t i, size_t j) const;
356 
358  template <typename E>
359  bool operator==(const MatrixExpression<T, E>& m) const;
360 
362  template <typename E>
363  bool operator!=(const MatrixExpression<T, E>& m) const;
364 
365  // MARK: Helpers
395  template <typename Callback>
396  void ForEach(Callback func) const;
397 
427  template <typename Callback>
428  void ForEachIndex(Callback func) const;
429 
449  template <typename Callback>
450  void ParallelForEach(Callback func);
451 
470  template <typename Callback>
471  void ParallelForEachIndex(Callback func) const;
472 
473  // MARK: Builders
475  static MatrixConstant<T> MakeZero(size_t m, size_t n);
476 
478  static MatrixIdentity<T> MakeIdentity(size_t m);
479 
480  private:
481  ContainerType m_elements;
482  };
483 
486 
489 }
490 
492 
493 #endif
T & operator[](size_t i)
Returns reference of i-th element.
Definition: MatrixMxN-Impl.h:786
typename ContainerType::Iterator Iterator
Definition: MatrixMxN.h:32
MatrixMxN & operator*=(const T &s)
Multiplication assignment with input scalar.
Definition: MatrixMxN-Impl.h:764
T Min() const
Returns minimum among all elements.
Definition: MatrixMxN-Impl.h:500
void IDiv(const T &s)
Divides this matrix with input scalar.
Definition: MatrixMxN-Impl.h:388
static MatrixConstant< T > MakeZero(size_t m, size_t n)
Makes a m x n matrix with zeros.
Definition: MatrixMxN-Impl.h:865
bool IsEqual(const MatrixExpression< T, E > &other) const
Definition: MatrixMxN-Impl.h:144
MatrixMxN Transposed() const
Returns transposed matrix.
Definition: MatrixMxN-Impl.h:689
bool IsSquare() const
Returns true if this matrix is a square matrix.
Definition: MatrixMxN-Impl.h:191
MatrixMxN & operator/=(const T &s)
Division assignment with input scalar.
Definition: MatrixMxN-Impl.h:779
MatrixScalarAdd< T, MatrixMxN > Add(const T &s) const
Returns this matrix + input scalar.
Definition: MatrixMxN-Impl.h:251
void ISub(const T &s)
Subtracts input scalar from this matrix.
Definition: MatrixMxN-Impl.h:361
MatrixMxN & operator=(const E &m)
Assigns input matrix.
MatrixScalarDiv< T, MatrixMxN > Div(const T &s) const
Returns this matrix / input scalar.
Definition: MatrixMxN-Impl.h:297
bool operator!=(const MatrixExpression< T, E > &m) const
Returns true if is not equal to m.
Definition: MatrixMxN-Impl.h:818
Matrix expression for matrix-scalar binary operation.
Definition: MatrixExpression.h:261
MatrixScalarRDiv< T, MatrixMxN > RDiv(const T &s) const
Returns input matrix / this scalar.
Definition: MatrixMxN-Impl.h:342
Base class for vector expression.
Definition: VectorExpression.h:28
T * data()
Returns data pointer of this matrix.
Definition: MatrixMxN-Impl.h:215
MatrixDiagonal< T, MatrixMxN > OffDiagonal() const
Returns off-diagonal part of this matrix.
Definition: MatrixMxN-Impl.h:659
MatrixMxN()
Constructs an empty matrix.
Definition: MatrixMxN-Impl.h:16
Matrix expression for unary operation.
Definition: MatrixExpression.h:117
Iterator end()
Returns the end iterator of the matrix.
Definition: MatrixMxN-Impl.h:239
Iterator begin()
Returns the begin iterator of the matrix.
Definition: MatrixMxN-Impl.h:227
Vector expression for matrix-vector multiplication.
Definition: MatrixExpression.h:296
T Sum() const
Returns sum of all elements.
Definition: MatrixMxN-Impl.h:477
2-D point class.
Definition: Point2.h:25
void ForEach(Callback func) const
Iterates the matrix and invoke given func for each index.
Definition: MatrixMxN-Impl.h:825
MatrixScalarMul< T, MatrixMxN > Mul(const T &s) const
Returns this matrix * input scalar.
Definition: MatrixMxN-Impl.h:277
MatrixScalarRSub< T, MatrixMxN > RSub(const T &s) const
Returns input scalar - this matrix.
Definition: MatrixMxN-Impl.h:316
Identity matrix expression.
Definition: MatrixExpression.h:83
MatrixDiagonal< T, MatrixMxN > Diagonal() const
Returns diagonal part of this matrix.
Definition: MatrixMxN-Impl.h:653
MatrixTriangular< T, MatrixMxN > StrictLowerTri() const
Returns strictly lower triangle part of this matrix.
Definition: MatrixMxN-Impl.h:665
MatrixMxN & operator+=(const T &s)
Addition assignment with input scalar.
Definition: MatrixMxN-Impl.h:734
MatrixScalarAdd< T, MatrixMxN > RAdd(const T &s) const
Returns input scalar + this matrix.
Definition: MatrixMxN-Impl.h:303
MatrixMxN Inverse() const
Returns inverse matrix.
Definition: MatrixMxN-Impl.h:697
size_t Cols() const
Returns number of columns of this matrix.
Definition: MatrixMxN-Impl.h:209
void IAdd(const T &s)
Adds input scalar to this matrix.
Definition: MatrixMxN-Impl.h:348
MatrixTriangular< T, MatrixMxN > UpperTri() const
Returns upper triangle part of this matrix (including the diagonal).
Definition: MatrixMxN-Impl.h:683
Triangular matrix expression.
Definition: MatrixExpression.h:182
void Resize(size_t m, size_t n, const T &s=T(0))
Resizes to m x n matrix with initial value s.
Definition: MatrixMxN-Impl.h:59
Definition: pybind11Utils.h:24
MatrixTypeCast< U, MatrixMxN, T > CastTo() const
Type-casts to different value-typed matrix.
static MatrixIdentity< T > MakeIdentity(size_t m)
Makes a m x m matrix with all diagonal elements to 1, and other elements to 0.
Definition: MatrixMxN-Impl.h:871
T Avg() const
Returns average of all elements.
Definition: MatrixMxN-Impl.h:494
Size2 size() const
Returns the size of this matrix.
Definition: MatrixMxN-Impl.h:197
MatrixScalarMul< T, MatrixMxN > RMul(const T &s) const
Returns input scalar * this matrix.
Definition: MatrixMxN-Impl.h:329
T AbsMin() const
Returns absolute minimum among all elements.
Definition: MatrixMxN-Impl.h:538
void IMul(const T &s)
Multiplies input scalar to this matrix.
Definition: MatrixMxN-Impl.h:374
T Determinant() const
Returns determinant of this matrix.
Definition: MatrixMxN-Impl.h:589
void Set(const T &s)
Sets whole matrix with input scalar.
Definition: MatrixMxN-Impl.h:66
typename ContainerType::iterator Iterator
Definition: Array2.h:46
const MatrixMxN< T > & operator()() const
Returns actual implementation (the subclass).
Definition: MatrixExpression-Impl.h:34
void SetOffDiagonal(const T &s)
Sets off-diagonal elements with input scalar.
Definition: MatrixMxN-Impl.h:111
bool IsSimilar(const MatrixExpression< T, E > &other, double tol=std::numeric_limits< double >::epsilon()) const
Definition: MatrixMxN-Impl.h:168
T Max() const
Returns maximum among all elements.
Definition: MatrixMxN-Impl.h:519
Base class for matrix expression.
Definition: MatrixExpression.h:27
void ParallelForEach(Callback func)
Iterates the matrix and invoke given func for each index in parallel.
Definition: MatrixMxN-Impl.h:851
Diagonal matrix expression.
Definition: MatrixExpression.h:149
Constant matrix expression.
Definition: MatrixExpression.h:51
void ForEachIndex(Callback func) const
Iterates the matrix and invoke given func for each index.
Definition: MatrixMxN-Impl.h:838
MatrixTriangular< T, MatrixMxN > LowerTri() const
Returns lower triangle part of this matrix (including the diagonal).
Definition: MatrixMxN-Impl.h:677
typename ContainerType::const_iterator ConstIterator
Definition: Array2.h:47
MatrixScalarSub< T, MatrixMxN > Sub(const T &s) const
Returns this matrix - input scalar.
Definition: MatrixMxN-Impl.h:264
2-D array class.
Definition: Array2.h:42
MatrixMxN & operator-=(const T &s)
Subtraction assignment with input scalar.
Definition: MatrixMxN-Impl.h:749
void Transpose()
Transposes this matrix.
Definition: MatrixMxN-Impl.h:394
T AbsMax() const
Returns absolute maximum among all elements.
Definition: MatrixMxN-Impl.h:555
M x N matrix class.
Definition: MatrixMxN.h:26
void SetRow(size_t i, const VectorExpression< T, E > &row)
Sets i-th row with input vector.
Definition: MatrixMxN-Impl.h:124
Matrix expression for binary operation.
Definition: MatrixExpression.h:226
size_t Rows() const
Returns number of rows of this matrix.
Definition: MatrixMxN-Impl.h:203
T Trace() const
Definition: MatrixMxN-Impl.h:571
void SetDiagonal(const T &s)
Sets diagonal elements with input scalar.
Definition: MatrixMxN-Impl.h:101
void SetColumn(size_t j, const VectorExpression< T, E > &col)
Sets j-th column with input vector.
Definition: MatrixMxN-Impl.h:134
void Invert()
Inverts this matrix.
Definition: MatrixMxN-Impl.h:400
void ParallelForEachIndex(Callback func) const
Iterates the matrix and invoke given func for each index in parallel using multi-threading.
Definition: MatrixMxN-Impl.h:858
MatrixTriangular< T, MatrixMxN > StrictUpperTri() const
Returns strictly upper triangle part of this matrix.
Definition: MatrixMxN-Impl.h:671
Matrix expression for matrix-matrix multiplication.
Definition: MatrixExpression.h:323
typename ContainerType::ConstIterator ConstIterator
Definition: MatrixMxN.h:33
bool operator==(const MatrixExpression< T, E > &m) const
Returns true if is equal to m.
Definition: MatrixMxN-Impl.h:811