9 #ifndef CUBBYFLOW_MATRIX_IMPL_H 10 #define CUBBYFLOW_MATRIX_IMPL_H 14 template<
typename T,
size_t M,
size_t N>
17 for (
auto& elem : m_elements)
23 template <
typename T,
size_t M,
size_t N>
24 template <
typename... Params>
27 static_assert(
sizeof...(params) == M * N,
"Invalid number of elements.");
29 SetRowAt(0, params...);
32 template <
typename T,
size_t M,
size_t N>
38 template <
typename T,
size_t M,
size_t N>
45 template <
typename T,
size_t M,
size_t N>
51 template <
typename T,
size_t M,
size_t N>
57 template <
typename T,
size_t M,
size_t N>
60 size_t rows = list.size();
61 size_t cols = (rows > 0) ? list.begin()->size() : 0;
66 auto rowIter = list.begin();
67 for (
size_t i = 0; i < rows; ++i)
69 assert(cols == rowIter->size());
71 auto colIter = rowIter->begin();
73 for (
size_t j = 0; j < cols; ++j)
75 (*this)(i, j) = *colIter;
83 template <
typename T,
size_t M,
size_t N>
87 const E& expression = other();
88 ForEachIndex([&](
size_t i,
size_t j) { (*this)(i, j) = expression(i, j); });
91 template <
typename T,
size_t M,
size_t N>
94 const size_t l = std::min(Rows(), Cols());
96 for (
size_t i = 0; i < l; ++i)
102 template <
typename T,
size_t M,
size_t N>
105 ForEachIndex([&](
size_t i,
size_t j)
114 template <
typename T,
size_t M,
size_t N>
115 template <
typename E>
118 assert(Cols() == row.
size());
122 for (
size_t j = 0; j < N; ++j)
124 (*this)(i, j) = e[j];
128 template <
typename T,
size_t M,
size_t N>
129 template <
typename E>
132 assert(Rows() == col.
size());
136 for (
size_t i = 0; i < M; ++i)
138 (*this)(i, j) = e[j];
142 template <
typename T,
size_t M,
size_t N>
143 template <
typename E>
146 if (size() != other.
size())
151 const E& e = other();
153 for (
size_t i = 0; i < Rows(); ++i)
155 for (
size_t j = 0; j < Cols(); ++j)
157 if ((*
this)(i, j) != e(i, j))
167 template <
typename T,
size_t M,
size_t N>
168 template <
typename E>
171 if (size() != other.
size())
176 const E& e = other();
178 for (
size_t i = 0; i < Rows(); ++i)
180 for (
size_t j = 0; j < Cols(); ++j)
182 if (std::fabs((*
this)(i, j) - e(i, j)) > tol)
192 template <
typename T,
size_t M,
size_t N>
198 template <
typename T,
size_t M,
size_t N>
204 template <
typename T,
size_t M,
size_t N>
210 template <
typename T,
size_t M,
size_t N>
216 template <
typename T,
size_t M,
size_t N>
219 return m_elements.data();
222 template <
typename T,
size_t M,
size_t N>
225 return m_elements.data();
228 template <
typename T,
size_t M,
size_t N>
231 return m_elements.begin();
234 template <
typename T,
size_t M,
size_t N>
237 return m_elements.begin();
240 template <
typename T,
size_t M,
size_t N>
243 return m_elements.end();
246 template <
typename T,
size_t M,
size_t N>
249 return m_elements.end();
252 template <
typename T,
size_t M,
size_t N>
258 template <
typename T,
size_t M,
size_t N>
259 template <
typename E>
265 template <
typename T,
size_t M,
size_t N>
271 template <
typename T,
size_t M,
size_t N>
272 template <
typename E>
278 template <
typename T,
size_t M,
size_t N>
284 template <
typename T,
size_t M,
size_t N>
285 template <
typename VE>
291 template <
typename T,
size_t M,
size_t N>
293 MatrixMul<T, Matrix<T, M, N>,
Matrix<T, N, L>>
Matrix<T, M, N>::Mul(
const Matrix<T, N, L>& m)
const 298 template <
typename T,
size_t M,
size_t N>
304 template <
typename T,
size_t M,
size_t N>
310 template <
typename T,
size_t M,
size_t N>
311 template <
typename E>
317 template <
typename T,
size_t M,
size_t N>
323 template <
typename T,
size_t M,
size_t N>
324 template <
typename E>
330 template <
typename T,
size_t M,
size_t N>
336 template <
typename T,
size_t M,
size_t N>
338 MatrixMul<T, Matrix<T, N, L>,
Matrix<T, M, N>>
Matrix<T, M, N>::RMul(
const Matrix<T, N, L>& m)
const 343 template <
typename T,
size_t M,
size_t N>
349 template <
typename T,
size_t M,
size_t N>
355 template <
typename T,
size_t M,
size_t N>
356 template <
typename E>
362 template <
typename T,
size_t M,
size_t N>
368 template <
typename T,
size_t M,
size_t N>
369 template <
typename E>
375 template <
typename T,
size_t M,
size_t N>
381 template <
typename T,
size_t M,
size_t N>
382 template <
typename E>
389 template <
typename T,
size_t M,
size_t N>
395 template <
typename T,
size_t M,
size_t N>
401 template <
typename T,
size_t M,
size_t N>
410 Matrix rhs = MakeIdentity();
412 for (
size_t i = 0; i < n; ++i)
415 T maxEl = std::fabs(a(i, i));
418 for (
size_t k = i + 1; k < n; ++k)
420 if (std::fabs(a(k, i)) > maxEl)
422 maxEl = std::fabs(a(k, i));
430 for (
size_t k = i; k < n; ++k)
432 std::swap(a(maxRow, k), a(i, k));
435 for (
size_t k = 0; k < n; ++k)
437 std::swap(rhs(maxRow, k), rhs(i, k));
442 for (
size_t k = 0; k < n; ++k)
449 T c = -a(k, i) / a(i, i);
451 for (
size_t j = 0; j < n; ++j)
453 rhs(k, j) += c * rhs(i, j);
461 a(k, j) += c * a(i, j);
467 for (
size_t k = 0; k < n; ++k)
471 for (
size_t j = 0; j < n; ++j)
482 template <
typename T,
size_t M,
size_t N>
487 for (
auto v : m_elements)
495 template <
typename T,
size_t M,
size_t N>
498 return Sum() / (Rows() * Cols());
501 template <
typename T,
size_t M,
size_t N>
504 T ret = m_elements.front();
506 for (
auto v : m_elements)
508 ret = std::min(ret, v);
514 template <
typename T,
size_t M,
size_t N>
517 T ret = m_elements.front();
519 for (
auto v : m_elements)
521 ret = std::max(ret, v);
527 template <
typename T,
size_t M,
size_t N>
530 T ret = m_elements.front();
532 for (
auto v : m_elements)
540 template <
typename T,
size_t M,
size_t N>
543 T ret = m_elements.front();
545 for (
auto v : m_elements)
553 template <
typename T,
size_t M,
size_t N>
560 for (
size_t i = 0; i < M; ++i)
562 ret += (*this)(i, i);
568 template <
typename T,
size_t M,
size_t N>
579 for (
size_t i = 0; i < n; ++i)
582 T maxEl = std::fabs(a(i, i));
585 for (
size_t k = i + 1; k < n; ++k)
587 if (std::fabs(a(k, i)) > maxEl)
589 maxEl = std::fabs(a(k, i));
597 for (
size_t k = i; k < n; ++k)
599 std::swap(a(maxRow, k), a(i, k));
606 for (
size_t k = i + 1; k < n; ++k)
608 T c = -a(k, i) / a(i, i);
610 for (
size_t j = i; j < n; ++j)
618 a(k, j) += c * a(i, j);
624 for (
size_t i = 0; i < n; ++i)
632 template <
typename T,
size_t M,
size_t N>
638 template <
typename T,
size_t M,
size_t N>
644 template <
typename T,
size_t M,
size_t N>
650 template <
typename T,
size_t M,
size_t N>
656 template <
typename T,
size_t M,
size_t N>
662 template <
typename T,
size_t M,
size_t N>
668 template <
typename T,
size_t M,
size_t N>
672 ForEachIndex([&](
size_t i,
size_t j) { mt(j, i) = (*this)(i, j); });
676 template <
typename T,
size_t M,
size_t N>
684 template <
typename T,
size_t M,
size_t N>
685 template <
typename U>
691 template <
typename T,
size_t M,
size_t N>
692 template <
typename E>
699 template <
typename T,
size_t M,
size_t N>
706 template <
typename T,
size_t M,
size_t N>
713 template <
typename T,
size_t M,
size_t N>
714 template <
typename E>
721 template <
typename T,
size_t M,
size_t N>
728 template <
typename T,
size_t M,
size_t N>
729 template <
typename E>
736 template <
typename T,
size_t M,
size_t N>
743 template <
typename T,
size_t M,
size_t N>
744 template <
typename E>
751 template <
typename T,
size_t M,
size_t N>
758 template <
typename T,
size_t M,
size_t N>
761 return m_elements[i];
764 template <
typename T,
size_t M,
size_t N>
767 return m_elements[i];
770 template <
typename T,
size_t M,
size_t N>
773 return m_elements[i * N + j];
776 template <
typename T,
size_t M,
size_t N>
779 return m_elements[i * N + j];
782 template <
typename T,
size_t M,
size_t N>
783 template <
typename E>
789 template <
typename T,
size_t M,
size_t N>
790 template <
typename E>
796 template <
typename T,
size_t M,
size_t N>
797 template <
typename Callback>
800 for (
size_t i = 0; i < Rows(); ++i)
802 for (
size_t j = 0; j < Cols(); ++j)
809 template <
typename T,
size_t M,
size_t N>
810 template <
typename Callback>
813 for (
size_t i = 0; i < Rows(); ++i)
815 for (
size_t j = 0; j < Cols(); ++j)
822 template <
typename T,
size_t M,
size_t N>
828 template <
typename T,
size_t M,
size_t N>
831 static_assert(M == N,
"Should be a square matrix.");
835 template <
typename T,
size_t M,
size_t N>
836 template <
typename... Params>
840 SetRowAt(i + 1, params...);
843 template <
typename T,
size_t M,
size_t N>
844 void Matrix<T, M, N>::SetRowAt(
size_t i, T v)
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