Matrix-Impl.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: Matrix-Impl.h
3 > Project Name: CubbyFlow
4 > Author: Dongmin Kim
5 > Purpose: Static-sized M x N matrix class.
6 > Created Time: 2017/03/06
7 > Copyright (c) 2018, Dongmin Kim
8 *************************************************************************/
9 #ifndef CUBBYFLOW_MATRIX_IMPL_H
10 #define CUBBYFLOW_MATRIX_IMPL_H
11 
12 namespace CubbyFlow
13 {
14  template<typename T, size_t M, size_t N>
16  {
17  for (auto& elem : m_elements)
18  {
19  elem = 0;
20  }
21  }
22 
23  template <typename T, size_t M, size_t N>
24  template <typename... Params>
25  Matrix<T, M, N>::Matrix(Params... params)
26  {
27  static_assert(sizeof...(params) == M * N, "Invalid number of elements.");
28 
29  SetRowAt(0, params...);
30  }
31 
32  template <typename T, size_t M, size_t N>
33  Matrix<T, M, N>::Matrix(const std::initializer_list<std::initializer_list<T>>& list)
34  {
35  Set(list);
36  }
37 
38  template <typename T, size_t M, size_t N>
39  template <typename E>
41  {
42  Set(other);
43  }
44 
45  template <typename T, size_t M, size_t N>
47  {
48  Set(other);
49  }
50 
51  template <typename T, size_t M, size_t N>
52  void Matrix<T, M, N>::Set(const T& s)
53  {
54  m_elements.fill(s);
55  }
56 
57  template <typename T, size_t M, size_t N>
58  void Matrix<T, M, N>::Set(const std::initializer_list<std::initializer_list<T>>& list)
59  {
60  size_t rows = list.size();
61  size_t cols = (rows > 0) ? list.begin()->size() : 0;
62 
63  assert(rows == M);
64  assert(cols == N);
65 
66  auto rowIter = list.begin();
67  for (size_t i = 0; i < rows; ++i)
68  {
69  assert(cols == rowIter->size());
70 
71  auto colIter = rowIter->begin();
72 
73  for (size_t j = 0; j < cols; ++j)
74  {
75  (*this)(i, j) = *colIter;
76  ++colIter;
77  }
78 
79  ++rowIter;
80  }
81  }
82 
83  template <typename T, size_t M, size_t N>
84  template <typename E>
86  {
87  const E& expression = other();
88  ForEachIndex([&](size_t i, size_t j) { (*this)(i, j) = expression(i, j); });
89  }
90 
91  template <typename T, size_t M, size_t N>
93  {
94  const size_t l = std::min(Rows(), Cols());
95 
96  for (size_t i = 0; i < l; ++i)
97  {
98  (*this)(i, i) = s;
99  }
100  }
101 
102  template <typename T, size_t M, size_t N>
104  {
105  ForEachIndex([&](size_t i, size_t j)
106  {
107  if (i != j)
108  {
109  (*this)(i, j) = s;
110  }
111  });
112  }
113 
114  template <typename T, size_t M, size_t N>
115  template <typename E>
117  {
118  assert(Cols() == row.size());
119 
120  const E& e = row();
121 
122  for (size_t j = 0; j < N; ++j)
123  {
124  (*this)(i, j) = e[j];
125  }
126  }
127 
128  template <typename T, size_t M, size_t N>
129  template <typename E>
131  {
132  assert(Rows() == col.size());
133 
134  const E& e = col();
135 
136  for (size_t i = 0; i < M; ++i)
137  {
138  (*this)(i, j) = e[j];
139  }
140  }
141 
142  template <typename T, size_t M, size_t N>
143  template <typename E>
145  {
146  if (size() != other.size())
147  {
148  return false;
149  }
150 
151  const E& e = other();
152 
153  for (size_t i = 0; i < Rows(); ++i)
154  {
155  for (size_t j = 0; j < Cols(); ++j)
156  {
157  if ((*this)(i, j) != e(i, j))
158  {
159  return false;
160  }
161  }
162  }
163 
164  return true;
165  }
166 
167  template <typename T, size_t M, size_t N>
168  template <typename E>
169  bool Matrix<T, M, N>::IsSimilar(const MatrixExpression<T, E>& other, double tol) const
170  {
171  if (size() != other.size())
172  {
173  return false;
174  }
175 
176  const E& e = other();
177 
178  for (size_t i = 0; i < Rows(); ++i)
179  {
180  for (size_t j = 0; j < Cols(); ++j)
181  {
182  if (std::fabs((*this)(i, j) - e(i, j)) > tol)
183  {
184  return false;
185  }
186  }
187  }
188 
189  return true;
190  }
191 
192  template <typename T, size_t M, size_t N>
193  constexpr bool Matrix<T, M, N>::IsSquare() const
194  {
195  return M == N;
196  }
197 
198  template <typename T, size_t M, size_t N>
199  constexpr Size2 Matrix<T, M, N>::size() const
200  {
201  return Size2(M, N);
202  }
203 
204  template <typename T, size_t M, size_t N>
205  constexpr size_t Matrix<T, M, N>::Rows() const
206  {
207  return M;
208  }
209 
210  template <typename T, size_t M, size_t N>
211  constexpr size_t Matrix<T, M, N>::Cols() const
212  {
213  return N;
214  }
215 
216  template <typename T, size_t M, size_t N>
218  {
219  return m_elements.data();
220  }
221 
222  template <typename T, size_t M, size_t N>
223  const T* Matrix<T, M, N>::data() const
224  {
225  return m_elements.data();
226  }
227 
228  template <typename T, size_t M, size_t N>
230  {
231  return m_elements.begin();
232  }
233 
234  template <typename T, size_t M, size_t N>
236  {
237  return m_elements.begin();
238  }
239 
240  template <typename T, size_t M, size_t N>
242  {
243  return m_elements.end();
244  }
245 
246  template <typename T, size_t M, size_t N>
248  {
249  return m_elements.end();
250  }
251 
252  template <typename T, size_t M, size_t N>
254  {
255  return MatrixScalarAdd<T, Matrix<T, M, N>>(*this, s);
256  }
257 
258  template <typename T, size_t M, size_t N>
259  template <typename E>
261  {
262  return MatrixAdd<T, Matrix, E>(*this, m);
263  }
264 
265  template <typename T, size_t M, size_t N>
267  {
268  return MatrixScalarSub<T, Matrix<T, M, N>>(*this, s);
269  }
270 
271  template <typename T, size_t M, size_t N>
272  template <typename E>
274  {
275  return MatrixSub<T, Matrix, E>(*this, m);
276  }
277 
278  template <typename T, size_t M, size_t N>
280  {
281  return MatrixScalarMul<T, Matrix>(*this, s);
282  }
283 
284  template <typename T, size_t M, size_t N>
285  template <typename VE>
287  {
288  return MatrixVectorMul<T, Matrix<T, M, N>, VE>(*this, v());
289  }
290 
291  template <typename T, size_t M, size_t N>
292  template <size_t L>
294  {
295  return MatrixMul<T, Matrix, Matrix<T, N, L>>(*this, m);
296  }
297 
298  template <typename T, size_t M, size_t N>
300  {
301  return MatrixScalarDiv<T, Matrix>(*this, s);
302  }
303 
304  template <typename T, size_t M, size_t N>
306  {
307  return MatrixScalarAdd<T, Matrix<T, M, N>>(*this, s);
308  }
309 
310  template <typename T, size_t M, size_t N>
311  template <typename E>
313  {
314  return MatrixAdd<T, Matrix<T, M, N>, E>(m, *this);
315  }
316 
317  template <typename T, size_t M, size_t N>
319  {
320  return MatrixScalarRSub<T, Matrix<T, M, N>>(*this, s);
321  }
322 
323  template <typename T, size_t M, size_t N>
324  template <typename E>
326  {
327  return MatrixSub<T, Matrix<T, M, N>, E>(m, *this);
328  }
329 
330  template <typename T, size_t M, size_t N>
332  {
333  return MatrixScalarMul<T, Matrix<T, M, N>>(*this, s);
334  }
335 
336  template <typename T, size_t M, size_t N>
337  template <size_t L>
339  {
340  return MatrixMul<T, Matrix<T, N, L>, Matrix>(m, *this);
341  }
342 
343  template <typename T, size_t M, size_t N>
345  {
346  return MatrixScalarRDiv<T, Matrix<T, M, N>>(*this, s);
347  }
348 
349  template <typename T, size_t M, size_t N>
350  void Matrix<T, M, N>::IAdd(const T& s)
351  {
352  Set(Add(s));
353  }
354 
355  template <typename T, size_t M, size_t N>
356  template <typename E>
357  void Matrix<T, M, N>::IAdd(const E& m)
358  {
359  Set(Add(m));
360  }
361 
362  template <typename T, size_t M, size_t N>
363  void Matrix<T, M, N>::ISub(const T& s)
364  {
365  Set(Sub(s));
366  }
367 
368  template <typename T, size_t M, size_t N>
369  template <typename E>
370  void Matrix<T, M, N>::ISub(const E& m)
371  {
372  Set(Sub(m));
373  }
374 
375  template <typename T, size_t M, size_t N>
376  void Matrix<T, M, N>::IMul(const T& s)
377  {
378  Set(Mul(s));
379  }
380 
381  template <typename T, size_t M, size_t N>
382  template <typename E>
383  void Matrix<T, M, N>::IMul(const E& m)
384  {
385  Matrix tmp = Mul(m);
386  Set(tmp);
387  }
388 
389  template <typename T, size_t M, size_t N>
390  void Matrix<T, M, N>::IDiv(const T& s)
391  {
392  Set(Div(s));
393  }
394 
395  template <typename T, size_t M, size_t N>
397  {
398  Set(Transposed());
399  }
400 
401  template <typename T, size_t M, size_t N>
403  {
404  assert(IsSquare());
405 
406  // Computes inverse matrix using Gaussian elimination method.
407  // https://martin-thoma.com/solving-linear-equations-with-gaussian-elimination/
408  size_t n = Rows();
409  Matrix& a = *this;
410  Matrix rhs = MakeIdentity();
411 
412  for (size_t i = 0; i < n; ++i)
413  {
414  // Search for maximum in this column
415  T maxEl = std::fabs(a(i, i));
416  size_t maxRow = i;
417 
418  for (size_t k = i + 1; k < n; ++k)
419  {
420  if (std::fabs(a(k, i)) > maxEl)
421  {
422  maxEl = std::fabs(a(k, i));
423  maxRow = k;
424  }
425  }
426 
427  // Swap maximum row with current row (column by column)
428  if (maxRow != i)
429  {
430  for (size_t k = i; k < n; ++k)
431  {
432  std::swap(a(maxRow, k), a(i, k));
433  }
434 
435  for (size_t k = 0; k < n; ++k)
436  {
437  std::swap(rhs(maxRow, k), rhs(i, k));
438  }
439  }
440 
441  // Make all rows except this one 0 in current column
442  for (size_t k = 0; k < n; ++k)
443  {
444  if (k == i)
445  {
446  continue;
447  }
448 
449  T c = -a(k, i) / a(i, i);
450 
451  for (size_t j = 0; j < n; ++j)
452  {
453  rhs(k, j) += c * rhs(i, j);
454 
455  if (i == j)
456  {
457  a(k, j) = 0;
458  }
459  else if (i < j)
460  {
461  a(k, j) += c * a(i, j);
462  }
463  }
464  }
465 
466  // Scale
467  for (size_t k = 0; k < n; ++k)
468  {
469  T c = 1 / a(k, k);
470 
471  for (size_t j = 0; j < n; ++j)
472  {
473  a(k, j) *= c;
474  rhs(k, j) *= c;
475  }
476  }
477  }
478 
479  Set(rhs);
480  }
481 
482  template <typename T, size_t M, size_t N>
484  {
485  T ret = 0;
486 
487  for (auto v : m_elements)
488  {
489  ret += v;
490  }
491 
492  return ret;
493  }
494 
495  template <typename T, size_t M, size_t N>
497  {
498  return Sum() / (Rows() * Cols());
499  }
500 
501  template <typename T, size_t M, size_t N>
503  {
504  T ret = m_elements.front();
505 
506  for (auto v : m_elements)
507  {
508  ret = std::min(ret, v);
509  }
510 
511  return ret;
512  }
513 
514  template <typename T, size_t M, size_t N>
516  {
517  T ret = m_elements.front();
518 
519  for (auto v : m_elements)
520  {
521  ret = std::max(ret, v);
522  }
523 
524  return ret;
525  }
526 
527  template <typename T, size_t M, size_t N>
529  {
530  T ret = m_elements.front();
531 
532  for (auto v : m_elements)
533  {
534  ret = CubbyFlow::AbsMin(ret, v);
535  }
536 
537  return ret;
538  }
539 
540  template <typename T, size_t M, size_t N>
542  {
543  T ret = m_elements.front();
544 
545  for (auto v : m_elements)
546  {
547  ret = CubbyFlow::AbsMax(ret, v);
548  }
549 
550  return ret;
551  }
552 
553  template <typename T, size_t M, size_t N>
555  {
556  assert(IsSquare());
557 
558  T ret = 0;
559 
560  for (size_t i = 0; i < M; ++i)
561  {
562  ret += (*this)(i, i);
563  }
564 
565  return ret;
566  }
567 
568  template <typename T, size_t M, size_t N>
570  {
571  assert(IsSquare());
572 
573  // Computes inverse matrix using Gaussian elimination method.
574  // https://martin-thoma.com/solving-linear-equations-with-gaussian-elimination/
575  size_t n = Rows();
576  Matrix a(*this);
577  T result = 1;
578 
579  for (size_t i = 0; i < n; ++i)
580  {
581  // Search for maximum in this column
582  T maxEl = std::fabs(a(i, i));
583  size_t maxRow = i;
584 
585  for (size_t k = i + 1; k < n; ++k)
586  {
587  if (std::fabs(a(k, i)) > maxEl)
588  {
589  maxEl = std::fabs(a(k, i));
590  maxRow = k;
591  }
592  }
593 
594  // Swap maximum row with current row (column by column)
595  if (maxRow != i)
596  {
597  for (size_t k = i; k < n; ++k)
598  {
599  std::swap(a(maxRow, k), a(i, k));
600  }
601 
602  result *= -1;
603  }
604 
605  // Make all rows below this one 0 in current column
606  for (size_t k = i + 1; k < n; ++k)
607  {
608  T c = -a(k, i) / a(i, i);
609 
610  for (size_t j = i; j < n; ++j)
611  {
612  if (i == j)
613  {
614  a(k, j) = 0;
615  }
616  else
617  {
618  a(k, j) += c * a(i, j);
619  }
620  }
621  }
622  }
623 
624  for (size_t i = 0; i < n; ++i)
625  {
626  result *= a(i, i);
627  }
628 
629  return result;
630  }
631 
632  template <typename T, size_t M, size_t N>
634  {
635  return MatrixDiagonal<T, Matrix>(*this, true);
636  }
637 
638  template <typename T, size_t M, size_t N>
640  {
641  return MatrixDiagonal<T, Matrix>(*this, false);
642  }
643 
644  template <typename T, size_t M, size_t N>
646  {
647  return MatrixTriangular<T, Matrix<T, M, N>>(*this, false, true);
648  }
649 
650  template <typename T, size_t M, size_t N>
652  {
653  return MatrixTriangular<T, Matrix<T, M, N>>(*this, true, true);
654  }
655 
656  template <typename T, size_t M, size_t N>
658  {
659  return MatrixTriangular<T, Matrix<T, M, N>>(*this, false, false);
660  }
661 
662  template <typename T, size_t M, size_t N>
664  {
665  return MatrixTriangular<T, Matrix<T, M, N>>(*this, true, false);
666  }
667 
668  template <typename T, size_t M, size_t N>
670  {
671  Matrix<T, N, M> mt;
672  ForEachIndex([&](size_t i, size_t j) { mt(j, i) = (*this)(i, j); });
673  return mt;
674  }
675 
676  template <typename T, size_t M, size_t N>
678  {
679  Matrix mInv(*this);
680  mInv.Invert();
681  return mInv;
682  }
683 
684  template <typename T, size_t M, size_t N>
685  template <typename U>
687  {
688  return MatrixTypeCast<U, Matrix, T>(*this);
689  }
690 
691  template <typename T, size_t M, size_t N>
692  template <typename E>
694  {
695  Set(m);
696  return *this;
697  }
698 
699  template <typename T, size_t M, size_t N>
701  {
702  Set(other);
703  return *this;
704  }
705 
706  template <typename T, size_t M, size_t N>
708  {
709  IAdd(s);
710  return *this;
711  }
712 
713  template <typename T, size_t M, size_t N>
714  template <typename E>
716  {
717  IAdd(m);
718  return *this;
719  }
720 
721  template <typename T, size_t M, size_t N>
723  {
724  ISub(s);
725  return *this;
726  }
727 
728  template <typename T, size_t M, size_t N>
729  template <typename E>
731  {
732  ISub(m);
733  return *this;
734  }
735 
736  template <typename T, size_t M, size_t N>
738  {
739  IMul(s);
740  return *this;
741  }
742 
743  template <typename T, size_t M, size_t N>
744  template <typename E>
746  {
747  IMul(m);
748  return *this;
749  }
750 
751  template <typename T, size_t M, size_t N>
753  {
754  IDiv(s);
755  return *this;
756  }
757 
758  template <typename T, size_t M, size_t N>
760  {
761  return m_elements[i];
762  }
763 
764  template <typename T, size_t M, size_t N>
765  const T& Matrix<T, M, N>::operator[](size_t i) const
766  {
767  return m_elements[i];
768  }
769 
770  template <typename T, size_t M, size_t N>
771  T& Matrix<T, M, N>::operator()(size_t i, size_t j)
772  {
773  return m_elements[i * N + j];
774  }
775 
776  template <typename T, size_t M, size_t N>
777  const T& Matrix<T, M, N>::operator()(size_t i, size_t j) const
778  {
779  return m_elements[i * N + j];
780  }
781 
782  template <typename T, size_t M, size_t N>
783  template <typename E>
785  {
786  return IsEqual(m);
787  }
788 
789  template <typename T, size_t M, size_t N>
790  template <typename E>
792  {
793  return !IsEqual(m);
794  }
795 
796  template <typename T, size_t M, size_t N>
797  template <typename Callback>
798  void Matrix<T, M, N>::ForEach(Callback func) const
799  {
800  for (size_t i = 0; i < Rows(); ++i)
801  {
802  for (size_t j = 0; j < Cols(); ++j)
803  {
804  func((*this)(i, j));
805  }
806  }
807  }
808 
809  template <typename T, size_t M, size_t N>
810  template <typename Callback>
811  void Matrix<T, M, N>::ForEachIndex(Callback func) const
812  {
813  for (size_t i = 0; i < Rows(); ++i)
814  {
815  for (size_t j = 0; j < Cols(); ++j)
816  {
817  func(i, j);
818  }
819  }
820  }
821 
822  template <typename T, size_t M, size_t N>
824  {
825  return MatrixConstant<T>(M, N, 0);
826  }
827 
828  template <typename T, size_t M, size_t N>
830  {
831  static_assert(M == N, "Should be a square matrix.");
832  return MatrixIdentity<T>(M);
833  }
834 
835  template <typename T, size_t M, size_t N>
836  template <typename... Params>
837  void Matrix<T, M, N>::SetRowAt(size_t i, T v, Params... params)
838  {
839  m_elements[i] = v;
840  SetRowAt(i + 1, params...);
841  }
842 
843  template <typename T, size_t M, size_t N>
844  void Matrix<T, M, N>::SetRowAt(size_t i, T v)
845  {
846  m_elements[i] = v;
847  }
848 }
849 
850 #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
T AbsMin(T x, T y)
Returns the absolute minimum value among the two inputs.
Definition: MathUtils-Impl.h:39
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
Point2< size_t > Size2
Definition: Size2.h:16
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
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
MatrixTriangular< T, Matrix > StrictUpperTri() const
Returns strictly upper triangle part of this matrix.
Definition: Matrix-Impl.h:651
Size2 size() const
Size of the matrix.
Definition: MatrixExpression-Impl.h:16
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
size_t size() const
Size of the vector.
Definition: VectorExpression-Impl.h:18
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
T AbsMax(T x, T y)
Returns the absolute maximum value among the two inputs.
Definition: MathUtils-Impl.h:45
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