MatrixMxN-Impl.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: MatrixMxN-Impl.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_IMPL_H
10 #define CUBBYFLOW_MATRIXMXN_IMPL_H
11 
12 namespace CubbyFlow
13 {
14  // MARK: MatrixMxN
15  template <typename T>
17  {
18  // Do nothing
19  }
20 
21  template <typename T>
22  MatrixMxN<T>::MatrixMxN(size_t m, size_t n, const T& s)
23  {
24  Resize(m, n, s);
25  }
26 
27  template <typename T>
28  MatrixMxN<T>::MatrixMxN(const std::initializer_list<std::initializer_list<T>>& list)
29  {
30  m_elements.Set(list);
31  }
32 
33  template <typename T>
34  template <typename E>
36  {
37  Set(other);
38  }
39 
40  template <typename T>
42  {
43  Set(other);
44  }
45 
46  template <typename T>
48  {
49  (*this) = std::move(other);
50  }
51 
52  template <typename T>
53  MatrixMxN<T>::MatrixMxN(size_t m, size_t n, const T* arr)
54  {
55  Set(m, n, arr);
56  }
57 
58  template <typename T>
59  void MatrixMxN<T>::Resize(size_t m, size_t n, const T& s)
60  {
61  // Note that m and n are flipped.
62  m_elements.Resize(n, m, s);
63  }
64 
65  template <typename T>
66  void MatrixMxN<T>::Set(const T& s)
67  {
68  m_elements.Set(s);
69  }
70 
71  template <typename T>
72  void MatrixMxN<T>::Set(const std::initializer_list<std::initializer_list<T>>& list)
73  {
74  m_elements.Set(list);
75  }
76 
77  template <typename T>
78  template <typename E>
80  {
81  Resize(other.Rows(), other.Cols());
82 
83  // Parallel evaluation of the expression
84  const E& expression = other();
85  ParallelForEachIndex([&](size_t i, size_t j) { (*this)(i, j) = expression(i, j); });
86  }
87 
88  template <typename T>
89  void MatrixMxN<T>::Set(size_t m, size_t n, const T* arr)
90  {
91  Resize(m, n);
92 
93  const size_t sz = m * n;
94  for (size_t i = 0; i < sz; ++i)
95  {
96  m_elements[i] = arr[i];
97  }
98  }
99 
100  template <typename T>
101  void MatrixMxN<T>::SetDiagonal(const T& s)
102  {
103  const size_t l = std::min(Rows(), Cols());
104  for (size_t i = 0; i < l; ++i)
105  {
106  (*this)(i, i) = s;
107  }
108  }
109 
110  template <typename T>
112  {
113  ParallelForEachIndex([&](size_t i, size_t j)
114  {
115  if (i != j)
116  {
117  (*this)(i, j) = s;
118  }
119  });
120  }
121 
122  template <typename T>
123  template <typename E>
124  void MatrixMxN<T>::SetRow(size_t i, const VectorExpression<T, E>& row)
125  {
126  assert(Cols() == row.size());
127 
128  const E& e = row();
129  ParallelFor(ZERO_SIZE, Cols(), [&](size_t j) { (*this)(i, j) = e[j]; });
130  }
131 
132  template <typename T>
133  template <typename E>
135  {
136  assert(Rows() == col.size());
137 
138  const E& e = col();
139  ParallelFor(ZERO_SIZE, Rows(), [&](size_t i) { (*this)(i, j) = e[i]; });
140  }
141 
142  template <typename T>
143  template <typename E>
145  {
146  if (size() != other.size())
147  {
148  return false;
149  }
150 
151  const E& e = other();
152  for (size_t i = 0; i < Rows(); ++i)
153  {
154  for (size_t j = 0; j < Cols(); ++j)
155  {
156  if ((*this)(i, j) != e(i, j))
157  {
158  return false;
159  }
160  }
161  }
162 
163  return true;
164  }
165 
166  template <typename T>
167  template <typename E>
168  bool MatrixMxN<T>::IsSimilar(const MatrixExpression<T, E>& other, double tol) const
169  {
170  if (size() != other.size())
171  {
172  return false;
173  }
174 
175  const E& e = other();
176  for (size_t i = 0; i < Rows(); ++i)
177  {
178  for (size_t j = 0; j < Cols(); ++j)
179  {
180  if (std::fabs((*this)(i, j) - e(i, j)) > tol)
181  {
182  return false;
183  }
184  }
185  }
186 
187  return true;
188  }
189 
190  template <typename T>
192  {
193  return Rows() == Cols();
194  }
195 
196  template <typename T>
198  {
199  return Size2(Rows(), Cols());
200  }
201 
202  template <typename T>
203  size_t MatrixMxN<T>::Rows() const
204  {
205  return m_elements.Height();
206  }
207 
208  template <typename T>
209  size_t MatrixMxN<T>::Cols() const
210  {
211  return m_elements.Width();
212  }
213 
214  template <typename T>
216  {
217  return m_elements.data();
218  }
219 
220  template <typename T>
221  const T* MatrixMxN<T>::data() const
222  {
223  return m_elements.data();
224  }
225 
226  template <typename T>
228  {
229  return m_elements.begin();
230  }
231 
232  template <typename T>
234  {
235  return m_elements.begin();
236  }
237 
238  template <typename T>
240  {
241  return m_elements.end();
242  }
243 
244  template <typename T>
246  {
247  return m_elements.end();
248  }
249 
250  template <typename T>
252  {
253  return MatrixScalarAdd<T, MatrixMxN<T>>(*this, s);
254  }
255 
256  template <typename T>
257  template <typename E>
259  {
260  return MatrixAdd<T, MatrixMxN, E>(*this, m);
261  }
262 
263  template <typename T>
265  {
266  return MatrixScalarSub<T, MatrixMxN<T>>(*this, s);
267  }
268 
269  template <typename T>
270  template <typename E>
272  {
273  return MatrixSub<T, MatrixMxN, E>(*this, m);
274  }
275 
276  template <typename T>
278  {
279  return MatrixScalarMul<T, MatrixMxN>(*this, s);
280  }
281 
282  template <typename T>
283  template <typename VE>
285  {
286  return MatrixVectorMul<T, MatrixMxN<T>, VE>(*this, v());
287  }
288 
289  template <typename T>
290  template <typename E>
292  {
293  return MatrixMul<T, MatrixMxN, E>(*this, m);
294  }
295 
296  template <typename T>
298  {
299  return MatrixScalarDiv<T, MatrixMxN>(*this, s);
300  }
301 
302  template <typename T>
304  {
305  return MatrixScalarAdd<T, MatrixMxN<T>>(*this, s);
306  }
307 
308  template <typename T>
309  template <typename E>
311  {
312  return MatrixAdd<T, MatrixMxN<T>, E>(m, *this);
313  }
314 
315  template <typename T>
317  {
318  return MatrixScalarRSub<T, MatrixMxN<T>>(*this, s);
319  }
320 
321  template <typename T>
322  template <typename E>
324  {
325  return MatrixSub<T, MatrixMxN<T>, E>(m, *this);
326  }
327 
328  template <typename T>
330  {
331  return MatrixScalarMul<T, MatrixMxN<T>>(*this, s);
332  }
333 
334  template <typename T>
335  template <typename E>
337  {
338  return MatrixMul<T, E, MatrixMxN<T>>(m, *this);
339  }
340 
341  template <typename T>
343  {
344  return MatrixScalarRDiv<T, MatrixMxN<T>>(*this, s);
345  }
346 
347  template <typename T>
348  void MatrixMxN<T>::IAdd(const T& s)
349  {
350  Set(Add(s));
351  }
352 
353  template <typename T>
354  template <typename E>
355  void MatrixMxN<T>::IAdd(const E& m)
356  {
357  Set(Add(m));
358  }
359 
360  template <typename T>
361  void MatrixMxN<T>::ISub(const T& s)
362  {
363  Set(Sub(s));
364  }
365 
366  template <typename T>
367  template <typename E>
368  void MatrixMxN<T>::ISub(const E& m)
369  {
370  Set(Sub(m));
371  }
372 
373  template <typename T>
374  void MatrixMxN<T>::IMul(const T& s)
375  {
376  Set(Mul(s));
377  }
378 
379  template <typename T>
380  template <typename E>
381  void MatrixMxN<T>::IMul(const E& m)
382  {
383  MatrixMxN tmp = Mul(m);
384  Set(tmp);
385  }
386 
387  template <typename T>
388  void MatrixMxN<T>::IDiv(const T& s)
389  {
390  Set(Div(s));
391  }
392 
393  template <typename T>
395  {
396  Set(Transposed());
397  }
398 
399  template <typename T>
401  {
402  assert(IsSquare());
403 
404  // Computes inverse matrix using Gaussian elimination method.
405  // https://martin-thoma.com/solving-linear-equations-with-gaussian-elimination/
406  size_t n = Rows();
407  MatrixMxN& a = *this;
408  MatrixMxN rhs = MakeIdentity(n);
409 
410  for (size_t i = 0; i < n; ++i)
411  {
412  // Search for maximum in this column
413  T maxEl = std::fabs(a(i, i));
414  size_t maxRow = i;
415 
416  for (size_t k = i + 1; k < n; ++k)
417  {
418  if (std::fabs(a(k, i)) > maxEl)
419  {
420  maxEl = std::fabs(a(k, i));
421  maxRow = k;
422  }
423  }
424 
425  // Swap maximum row with current row (column by column)
426  if (maxRow != i)
427  {
428  for (size_t k = i; k < n; ++k)
429  {
430  std::swap(a(maxRow, k), a(i, k));
431  std::swap(rhs(maxRow, k), rhs(i, k));
432  }
433  }
434 
435  // Make all rows except this one 0 in current column
436  for (size_t k = 0; k < n; ++k)
437  {
438  if (k == i)
439  {
440  continue;
441  }
442 
443  T c = -a(k, i) / a(i, i);
444 
445  for (size_t j = 0; j < n; ++j)
446  {
447  rhs(k, j) += c * rhs(i, j);
448 
449  if (i == j)
450  {
451  a(k, j) = 0;
452  }
453  else if (i < j)
454  {
455  a(k, j) += c * a(i, j);
456  }
457  }
458  }
459 
460  // Scale
461  for (size_t k = 0; k < n; ++k)
462  {
463  T c = 1 / a(k, k);
464 
465  for (size_t j = 0; j < n; ++j)
466  {
467  a(k, j) *= c;
468  rhs(k, j) *= c;
469  }
470  }
471  }
472 
473  Set(rhs);
474  }
475 
476  template <typename T>
478  {
479  return ParallelReduce(ZERO_SIZE, Rows() * Cols(), T(0),
480  [&](size_t start, size_t end, T init)
481  {
482  T result = init;
483 
484  for (size_t i = start; i < end; ++i)
485  {
486  result += m_elements[i];
487  }
488 
489  return result;
490  }, std::plus<T>());
491  }
492 
493  template <typename T>
495  {
496  return Sum() / (Rows() * Cols());
497  }
498 
499  template <typename T>
501  {
502  const T& (*_min)(const T&, const T&) = std::min<T>;
503 
504  return ParallelReduce(ZERO_SIZE, Rows() * Cols(), std::numeric_limits<T>::max(),
505  [&](size_t start, size_t end, T init)
506  {
507  T result = init;
508 
509  for (size_t i = start; i < end; ++i)
510  {
511  result = std::min(result, m_elements[i]);
512  }
513 
514  return result;
515  }, _min);
516  }
517 
518  template <typename T>
520  {
521  const T& (*_max)(const T&, const T&) = std::max<T>;
522 
523  return ParallelReduce(ZERO_SIZE, Rows() * Cols(), std::numeric_limits<T>::min(),
524  [&](size_t start, size_t end, T init)
525  {
526  T result = init;
527 
528  for (size_t i = start; i < end; ++i)
529  {
530  result = std::max(result, m_elements[i]);
531  }
532 
533  return result;
534  }, _max);
535  }
536 
537  template <typename T>
539  {
540  return ParallelReduce(ZERO_SIZE, Rows() * Cols(), std::numeric_limits<T>::max(),
541  [&](size_t start, size_t end, T init)
542  {
543  T result = init;
544 
545  for (size_t i = start; i < end; ++i)
546  {
547  result = CubbyFlow::AbsMin(result, m_elements[i]);
548  }
549 
550  return result;
551  }, CubbyFlow::AbsMin<T>);
552  }
553 
554  template <typename T>
556  {
557  return ParallelReduce(ZERO_SIZE, Rows() * Cols(), T(0), [&](size_t start, size_t end, T init)
558  {
559  T result = init;
560 
561  for (size_t i = start; i < end; ++i)
562  {
563  result = CubbyFlow::AbsMax(result, m_elements[i]);
564  }
565 
566  return result;
567  }, CubbyFlow::AbsMax<T>);
568  }
569 
570  template <typename T>
572  {
573  assert(IsSquare());
574 
575  return ParallelReduce(ZERO_SIZE, Rows(), T(0), [&](size_t start, size_t end, T init)
576  {
577  T result = init;
578 
579  for (size_t i = start; i < end; ++i)
580  {
581  result += m_elements(i, i);
582  }
583 
584  return result;
585  }, std::plus<T>());
586  }
587 
588  template <typename T>
590  {
591  assert(IsSquare());
592 
593  // Computes inverse matrix using Gaussian elimination method.
594  // https://martin-thoma.com/solving-linear-equations-with-gaussian-elimination/
595  size_t n = Rows();
596  MatrixMxN a(*this);
597  T result = 1;
598 
599  for (size_t i = 0; i < n; ++i)
600  {
601  // Search for maximum in this column
602  T maxEl = std::fabs(a(i, i));
603  size_t maxRow = i;
604 
605  for (size_t k = i + 1; k < n; ++k)
606  {
607  if (std::fabs(a(k, i)) > maxEl)
608  {
609  maxEl = std::fabs(a(k, i));
610  maxRow = k;
611  }
612  }
613 
614  // Swap maximum row with current row (column by column)
615  if (maxRow != i)
616  {
617  for (size_t k = i; k < n; ++k)
618  {
619  std::swap(a(maxRow, k), a(i, k));
620  }
621 
622  result *= -1;
623  }
624 
625  // Make all rows below this one 0 in current column
626  for (size_t k = i + 1; k < n; ++k)
627  {
628  T c = -a(k, i) / a(i, i);
629 
630  for (size_t j = i; j < n; ++j)
631  {
632  if (i == j)
633  {
634  a(k, j) = 0;
635  }
636  else
637  {
638  a(k, j) += c * a(i, j);
639  }
640  }
641  }
642  }
643 
644  for (size_t i = 0; i < n; ++i)
645  {
646  result *= a(i, i);
647  }
648 
649  return result;
650  }
651 
652  template <typename T>
654  {
655  return MatrixDiagonal<T, MatrixMxN>(*this, true);
656  }
657 
658  template <typename T>
660  {
661  return MatrixDiagonal<T, MatrixMxN>(*this, false);
662  }
663 
664  template <typename T>
666  {
667  return MatrixTriangular<T, MatrixMxN<T>>(*this, false, true);
668  }
669 
670  template <typename T>
672  {
673  return MatrixTriangular<T, MatrixMxN<T>>(*this, true, true);
674  }
675 
676  template <typename T>
678  {
679  return MatrixTriangular<T, MatrixMxN<T>>(*this, false, false);
680  }
681 
682  template <typename T>
684  {
685  return MatrixTriangular<T, MatrixMxN<T>>(*this, true, false);
686  }
687 
688  template <typename T>
690  {
691  MatrixMxN mt(Cols(), Rows());
692  ParallelForEachIndex([&](size_t i, size_t j) { mt(j, i) = (*this)(i, j); });
693  return mt;
694  }
695 
696  template <typename T>
698  {
699  MatrixMxN mInv(*this);
700  mInv.Invert();
701  return mInv;
702  }
703 
704  template <typename T>
705  template <typename U>
707  {
708  return MatrixTypeCast<U, MatrixMxN, T>(*this);
709  }
710 
711  template <typename T>
712  template <typename E>
714  {
715  Set(m);
716  return *this;
717  }
718 
719  template <typename T>
721  {
722  Set(other);
723  return *this;
724  }
725 
726  template <typename T>
728  {
729  m_elements = std::move(other.m_elements);
730  return *this;
731  }
732 
733  template <typename T>
735  {
736  IAdd(s);
737  return *this;
738  }
739 
740  template <typename T>
741  template <typename E>
743  {
744  IAdd(m);
745  return *this;
746  }
747 
748  template <typename T>
750  {
751  ISub(s);
752  return *this;
753  }
754 
755  template <typename T>
756  template <typename E>
758  {
759  ISub(m);
760  return *this;
761  }
762 
763  template <typename T>
765  {
766  IMul(s);
767  return *this;
768  }
769 
770  template <typename T>
771  template <typename E>
773  {
774  IMul(m);
775  return *this;
776  }
777 
778  template <typename T>
780  {
781  IDiv(s);
782  return *this;
783  }
784 
785  template <typename T>
787  {
788  return m_elements[i];
789  }
790 
791  template <typename T>
792  const T& MatrixMxN<T>::operator[](size_t i) const
793  {
794  return m_elements[i];
795  }
796 
797  template <typename T>
798  T& MatrixMxN<T>::operator()(size_t i, size_t j)
799  {
800  return m_elements(j, i);
801  }
802 
803  template <typename T>
804  const T& MatrixMxN<T>::operator()(size_t i, size_t j) const
805  {
806  return m_elements(j, i);
807  }
808 
809  template <typename T>
810  template <typename E>
812  {
813  return IsEqual(m);
814  }
815 
816  template <typename T>
817  template <typename E>
819  {
820  return !IsEqual(m);
821  }
822 
823  template <typename T>
824  template <typename Callback>
825  void MatrixMxN<T>::ForEach(Callback func) const
826  {
827  for (size_t i = 0; i < Rows(); ++i)
828  {
829  for (size_t j = 0; j < Cols(); ++j)
830  {
831  func((*this)(i, j));
832  }
833  }
834  }
835 
836  template <typename T>
837  template <typename Callback>
838  void MatrixMxN<T>::ForEachIndex(Callback func) const
839  {
840  for (size_t i = 0; i < Rows(); ++i)
841  {
842  for (size_t j = 0; j < Cols(); ++j)
843  {
844  func(i, j);
845  }
846  }
847  }
848 
849  template <typename T>
850  template <typename Callback>
851  void MatrixMxN<T>::ParallelForEach(Callback func)
852  {
853  ParallelFor(ZERO_SIZE, Cols(), ZERO_SIZE, Rows(), [&](size_t j, size_t i) { func((*this)(i, j)); });
854  }
855 
856  template <typename T>
857  template <typename Callback>
858  void MatrixMxN<T>::ParallelForEachIndex(Callback func) const
859  {
860  ParallelFor(ZERO_SIZE, Cols(), ZERO_SIZE, Rows(), [&](size_t j, size_t i) { func(i, j); });
861  }
862 
863  // MARK: Builders
864  template <typename T>
866  {
867  return MatrixConstant<T>(m, n, 0);
868  }
869 
870  template <typename T>
872  {
873  return MatrixIdentity<T>(m);
874  }
875 }
876 
877 #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
T AbsMin(T x, T y)
Returns the absolute minimum value among the two inputs.
Definition: MathUtils-Impl.h:39
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
Point2< size_t > Size2
Definition: Size2.h:16
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
void ParallelFor(IndexType beginIndex, IndexType endIndex, const Function &function, ExecutionPolicy policy)
Makes a for-loop from beginIndex to endIndex in parallel.
Definition: Parallel-Impl.h:201
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
Size2 size() const
Size of the matrix.
Definition: MatrixExpression-Impl.h:16
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
size_t Rows() const
Number of rows.
Definition: MatrixExpression-Impl.h:22
void Set(const T &s)
Sets whole matrix with input scalar.
Definition: MatrixMxN-Impl.h:66
size_t size() const
Size of the vector.
Definition: VectorExpression-Impl.h:18
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
constexpr size_t ZERO_SIZE
Zero size_t.
Definition: Constants.h:18
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
MatrixScalarSub< T, MatrixMxN > Sub(const T &s) const
Returns this matrix - input scalar.
Definition: MatrixMxN-Impl.h:264
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
T AbsMax(T x, T y)
Returns the absolute maximum value among the two inputs.
Definition: MathUtils-Impl.h:45
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
size_t Cols() const
Number of columns.
Definition: MatrixExpression-Impl.h:28
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
Value ParallelReduce(IndexType beginIndex, IndexType endIndex, const Value &identity, const Function &function, const Reduce &reduce, ExecutionPolicy policy)
Performs reduce operation in parallel.
Definition: Parallel-Impl.h:407
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