11 #ifndef CUBBYFLOW_MATRIX_IMPL_HPP 12 #define CUBBYFLOW_MATRIX_IMPL_HPP 23 template <
typename M1,
typename M2,
size_t J>
26 constexpr
static auto call(
const M1& a,
const M2& b,
size_t i,
size_t j)
32 template <
typename M1,
typename M2>
35 constexpr
static auto call(
const M1& a,
const M2& b,
size_t i,
size_t j)
37 return a(i, 0) * b(0, j);
42 template <
typename T,
size_t Rows,
size_t Cols,
typename ReduceOperation,
43 typename UnaryOperation,
size_t I>
47 template <
typename U = T>
48 constexpr
static std::enable_if_t<(Cols == 1), U>
Call(
59 template <
typename U = T>
60 constexpr
static std::enable_if_t<(Cols == 1), U>
Call(
71 ReduceOperation op, UnaryOperation uop)
91 ReduceOperation op, UnaryOperation uop)
93 return op(
Reduce<T, Rows, Cols, ReduceOperation, UnaryOperation,
94 I - 1>::CallDiag(a, init, op, uop),
99 template <
typename T,
size_t Rows,
size_t Cols,
typename ReduceOperation,
100 typename UnaryOperation>
101 struct Reduce<T, Rows, Cols, ReduceOperation, UnaryOperation, 0>
104 template <
typename U = T>
105 constexpr
static std::enable_if_t<(Cols > 1), U>
Call(
109 return op(uop(a(0, 0)), init);
113 template <
typename U = T>
114 constexpr
static std::enable_if_t<(Cols == 1), U>
Call(
122 ReduceOperation op, UnaryOperation uop)
124 return op(uop(a[0]), init);
136 ReduceOperation op, UnaryOperation uop)
138 return op(uop(a(0, 0)), init);
146 template <
typename T,
size_t Rows,
size_t Cols,
typename BinaryOperation,
160 template <
typename T,
size_t Rows,
size_t Cols,
typename BinaryOperation>
167 return op(a[0], b[0]);
173 template <
typename T,
size_t Rows,
size_t Cols>
179 template <
typename T,
size_t Rows,
size_t Cols>
180 template <
size_t R,
size_t C,
typename E>
183 assert(expression.
GetRows() == Rows && expression.
GetCols() == Cols);
185 CopyFrom(expression);
188 template <
typename T,
size_t Rows,
size_t Cols>
193 for (
auto rows : lst)
199 for (
auto col : rows)
211 template <
typename T,
size_t Rows,
size_t Cols>
216 for (
size_t i = 0; i < Rows; ++i)
218 for (
size_t j = 0; j < Cols; ++j)
220 (*this)(i, j) = ptr[cnt++];
225 template <
typename T,
size_t Rows,
size_t Cols>
228 m_elements.fill(val);
231 template <
typename T,
size_t Rows,
size_t Cols>
234 for (
size_t i = 0; i < Rows * Cols; ++i)
236 m_elements[i] = func(i);
240 template <
typename T,
size_t Rows,
size_t Cols>
242 const std::function<T(
size_t i,
size_t j)>& func)
244 for (
size_t i = 0; i < Rows; ++i)
246 for (
size_t j = 0; j < Cols; ++j)
248 (*this)(i, j) = func(i, j);
253 template <
typename T,
size_t Rows,
size_t Cols>
256 m_elements.swap(other.m_elements);
259 template <
typename T,
size_t Rows,
size_t Cols>
265 template <
typename T,
size_t Rows,
size_t Cols>
271 template <
typename T,
size_t Rows,
size_t Cols>
274 return &m_elements[0];
277 template <
typename T,
size_t Rows,
size_t Cols>
281 return &m_elements[0];
284 template <
typename T,
size_t Rows,
size_t Cols>
287 return begin() + Rows * Cols;
290 template <
typename T,
size_t Rows,
size_t Cols>
294 return begin() + Rows * Cols;
297 template <
typename T,
size_t Rows,
size_t Cols>
300 return &m_elements[0];
303 template <
typename T,
size_t Rows,
size_t Cols>
307 return &m_elements[0];
310 template <
typename T,
size_t Rows,
size_t Cols>
314 assert(i < Rows * Cols);
316 return m_elements[i];
319 template <
typename T,
size_t Rows,
size_t Cols>
323 assert(i < Rows * Cols);
325 return m_elements[i];
328 template <
typename T>
329 template <
size_t R,
size_t C,
typename E>
334 x = expression.
Eval(0, 0);
337 template <
typename T>
340 assert(lst.size() > 0);
345 template <
typename T>
351 template <
typename T>
357 template <
typename T>
363 template <
typename T>
366 std::swap(x, other.x);
369 template <
typename T>
375 template <
typename T>
381 template <
typename T>
387 template <
typename T>
393 template <
typename T>
399 template <
typename T>
405 template <
typename T>
411 template <
typename T>
417 template <
typename T>
425 template <
typename T>
434 template <
typename T>
440 template <
typename T>
446 template <
typename T>
447 template <
size_t R,
size_t C,
typename E>
452 x = expression.
Eval(0, 0);
453 y = expression.
Eval(1, 0);
456 template <
typename T>
459 assert(lst.size() > 1);
461 auto iter = lst.begin();
466 template <
typename T>
472 template <
typename T>
479 template <
typename T>
486 template <
typename T>
489 std::swap(x, other.x);
490 std::swap(y, other.y);
493 template <
typename T>
499 template <
typename T>
505 template <
typename T>
511 template <
typename T>
517 template <
typename T>
523 template <
typename T>
529 template <
typename T>
535 template <
typename T>
541 template <
typename T>
549 template <
typename T>
558 template <
typename T>
564 template <
typename T>
570 template <
typename T>
576 template <
typename T>
577 template <
size_t R,
size_t C,
typename E>
582 x = expression.
Eval(0, 0);
583 y = expression.
Eval(1, 0);
584 z = expression.
Eval(2, 0);
587 template <
typename T>
590 assert(lst.size() > 2);
592 auto iter = lst.begin();
598 template <
typename T>
604 template <
typename T>
612 template <
typename T>
620 template <
typename T>
623 std::swap(x, other.x);
624 std::swap(y, other.y);
625 std::swap(z, other.z);
628 template <
typename T>
634 template <
typename T>
640 template <
typename T>
646 template <
typename T>
652 template <
typename T>
658 template <
typename T>
664 template <
typename T>
670 template <
typename T>
676 template <
typename T>
684 template <
typename T>
693 template <
typename T>
699 template <
typename T>
705 template <
typename T>
711 template <
typename T>
717 template <
typename T>
718 template <
size_t R,
size_t C,
typename E>
723 x = expression.
Eval(0, 0);
724 y = expression.
Eval(1, 0);
725 z = expression.
Eval(2, 0);
726 w = expression.
Eval(3, 0);
729 template <
typename T>
732 assert(lst.size() > 3);
734 auto iter = lst.begin();
741 template <
typename T>
747 template <
typename T>
756 template <
typename T>
765 template <
typename T>
768 std::swap(x, other.x);
769 std::swap(y, other.y);
770 std::swap(z, other.z);
771 std::swap(w, other.w);
774 template <
typename T>
780 template <
typename T>
786 template <
typename T>
792 template <
typename T>
798 template <
typename T>
804 template <
typename T>
810 template <
typename T>
816 template <
typename T>
822 template <
typename T>
830 template <
typename T>
839 template <
typename T>
845 template <
typename T>
851 template <
typename T>
857 template <
typename T>
863 template <
typename T>
869 template <
typename T>
875 template <
typename T>
879 m_elements.resize(rows * cols);
886 template <
typename T>
887 template <
size_t R,
size_t C,
typename E>
890 :
Matrix(expression.GetRows(), expression.GetCols())
895 template <
typename T>
901 for (
auto rows : lst)
905 for (
auto col : rows)
916 m_elements.resize(m_rows * m_cols);
920 for (
auto rows : lst)
926 for (
auto col : rows)
938 template <
typename T>
946 for (
size_t i = 0; i < rows; ++i)
948 for (
size_t j = 0; j < cols; ++j)
950 (*this)(i, j) = ptr[cnt++];
955 template <
typename T>
957 : m_elements(other.m_elements), m_rows(other.m_rows), m_cols(other.m_cols)
962 template <
typename T>
965 : m_elements(std::move(other.m_elements)),
966 m_rows(other.m_rows),
972 template <
typename T>
977 m_elements = other.m_elements;
978 m_rows = other.m_rows;
979 m_cols = other.m_cols;
983 template <
typename T>
988 m_elements = std::move(other.m_elements);
989 m_rows = other.m_rows;
990 m_cols = other.m_cols;
996 template <
typename T>
999 std::fill(m_elements.begin(), m_elements.end(), val);
1002 template <
typename T>
1004 const std::function<T(
size_t i)>& func)
1006 for (
size_t i = 0; i < m_elements.size(); ++i)
1008 m_elements[i] = func(i);
1012 template <
typename T>
1014 const std::function<T(
size_t i,
size_t j)>& func)
1016 for (
size_t i = 0; i <
GetRows(); ++i)
1018 for (
size_t j = 0; j <
GetCols(); ++j)
1020 (*this)(i, j) = func(i, j);
1025 template <
typename T>
1028 m_elements.swap(other.m_elements);
1030 std::swap(m_rows, other.m_rows);
1031 std::swap(m_cols, other.m_cols);
1034 template <
typename T>
1038 Matrix newMatrix{ rows, cols, val };
1039 const size_t minRows = std::min(rows, m_rows);
1040 const size_t minCols = std::min(cols, m_cols);
1042 for (
size_t i = 0; i < minRows; ++i)
1044 for (
size_t j = 0; j < minCols; ++j)
1046 newMatrix(i, j) = (*this)(i, j);
1050 *
this = std::move(newMatrix);
1053 template <
typename T>
1062 template <
typename T>
1068 template <
typename T>
1074 template <
typename T>
1078 return &m_elements[0];
1081 template <
typename T>
1085 return &m_elements[0];
1088 template <
typename T>
1092 return begin() + m_rows * m_cols;
1095 template <
typename T>
1099 return begin() + m_rows * m_cols;
1102 template <
typename T>
1106 return &m_elements[0];
1109 template <
typename T>
1113 return &m_elements[0];
1116 template <
typename T>
1120 assert(i < m_rows * m_cols);
1122 return m_elements[i];
1125 template <
typename T>
1129 assert(i < m_rows * m_cols);
1131 return m_elements[i];
1134 template <
typename T>
1140 template <
typename T>
1143 m_elements.resize(rows, value);
1146 template <
typename T>
1147 template <
size_t R,
size_t C,
typename E>
1155 template <
typename T>
1158 size_t sz = lst.size();
1159 m_elements.resize(sz);
1163 for (
auto row : lst)
1165 m_elements[i] =
static_cast<T
>(row);
1170 template <
typename T>
1176 for (
size_t i = 0; i < rows; ++i)
1178 (*this)[i] = ptr[cnt++];
1182 template <
typename T>
1184 : m_elements(other.m_elements)
1189 template <
typename T>
1191 : m_elements(std::move(other.m_elements))
1196 template <
typename T>
1200 m_elements = other.m_elements;
1204 template <
typename T>
1208 m_elements = std::move(other.m_elements);
1212 template <
typename T>
1215 std::fill(m_elements.begin(), m_elements.end(), val);
1218 template <
typename T>
1220 const std::function<T(
size_t i)>& func)
1222 for (
size_t i = 0; i < m_elements.size(); ++i)
1224 m_elements[i] = func(i);
1228 template <
typename T>
1230 const std::function<T(
size_t i,
size_t j)>& func)
1232 for (
size_t i = 0; i <
GetRows(); ++i)
1234 m_elements[i] = func(i, 0);
1238 template <
typename T>
1241 m_elements.swap(other.m_elements);
1244 template <
typename T>
1247 m_elements.resize(rows, val);
1250 template <
typename T>
1253 m_elements.push_back(newElem);
1256 template <
typename T>
1259 m_elements.insert(m_elements.end(), newElems.m_elements.begin(),
1260 newElems.m_elements.end());
1263 template <
typename T>
1269 template <
typename T>
1272 return m_elements.size();
1275 template <
typename T>
1281 template <
typename T>
1285 return &m_elements[0];
1288 template <
typename T>
1292 return &m_elements[0];
1295 template <
typename T>
1299 return begin() + m_elements.size();
1302 template <
typename T>
1306 return begin() + m_elements.size();
1309 template <
typename T>
1313 return &m_elements[0];
1316 template <
typename T>
1320 return &m_elements[0];
1323 template <
typename T>
1327 assert(i < m_elements.size());
1329 return m_elements[i];
1332 template <
typename T>
1336 assert(i < m_elements.size());
1338 return m_elements[i];
1341 template <
typename T,
size_t R1,
size_t C1,
size_t R2,
size_t C2,
typename M2>
1347 template <
typename T,
size_t Rows,
size_t Cols>
1353 template <
typename T,
size_t R1,
size_t C1,
size_t R2,
size_t C2,
typename M2>
1359 template <
typename T,
size_t Rows,
size_t Cols>
1365 template <
typename T,
size_t R1,
size_t C1,
size_t R2,
size_t C2,
typename M2>
1374 template <
typename T,
size_t R1,
size_t C1,
size_t R2,
size_t C2,
typename M2>
1384 template <
typename T,
size_t Rows,
size_t Cols>
1392 template <
typename T,
size_t R1,
size_t C1,
size_t R2,
size_t C2,
typename M2>
1399 template <
typename T,
size_t Rows,
size_t Cols>
1407 template <
typename T,
size_t Rows,
size_t Cols,
typename M1,
typename M2>
1408 constexpr std::enable_if_t<IsMatrixSizeStatic<Rows, Cols>(),
bool>
operator==(
1413 Rows * Cols - 1>::Call(a, b,
1414 std::equal_to<T>());
1417 template <
typename T,
size_t R1,
size_t C1,
size_t R2,
size_t C2,
typename M1,
1427 for (
size_t i = 0; i < a.
GetRows(); ++i)
1429 for (
size_t j = 0; j < a.
GetCols(); ++j)
1441 template <
typename T,
size_t R1,
size_t C1,
size_t R2,
size_t C2,
typename M1,
1449 template <
typename T,
size_t Rows,
size_t Cols,
typename M1,
1450 typename BinaryOperation>
1451 constexpr std::enable_if_t<TraitIsMatrixSizeStatic<Rows, Cols>::value, T>
1456 Rows * Cols - 1>::Call(a, init, op,
NoOp<T>());
1459 template <
typename T,
size_t Rows,
size_t Cols,
typename M1>
1460 constexpr std::enable_if_t<TraitIsMatrixSizeStatic<Rows, Cols>::value, T>
1464 Rows * Cols - 1>::Call(a, init, std::plus<T>(),
1468 template <
typename T,
size_t Rows,
size_t Cols,
typename M1>
1469 constexpr std::enable_if_t<TraitIsMatrixSizeStatic<Rows, Cols>::value, T>
1473 Rows * Cols - 1>::Call(a, std::plus<T>(),
1477 template <
typename T,
size_t Rows,
size_t Cols,
typename M1,
1478 typename BinaryOperation>
1479 constexpr std::enable_if_t<TraitIsMatrixSizeDynamic<Rows, Cols>::value, T>
1483 return std::accumulate(a.begin(), a.end(), init, op);
1486 template <
typename T,
size_t Rows,
size_t Cols,
typename M1>
1487 constexpr std::enable_if_t<TraitIsMatrixSizeDynamic<Rows, Cols>::value, T>
1490 return std::accumulate(a.begin(), a.end(), init, std::plus<T>());
1493 template <
typename T,
size_t Rows,
size_t Cols,
typename M1>
1494 constexpr std::enable_if_t<TraitIsMatrixSizeDynamic<Rows, Cols>::value, T>
1497 return std::accumulate(a.begin(), a.end(), T{}, std::plus<T>());
1502 template <
typename T,
size_t Rows,
size_t Cols,
typename M1>
1505 return Accumulate(a, init, std::multiplies<T>());
1509 template <
typename T,
size_t Rows,
size_t Cols,
typename M1,
typename M2,
1510 typename M3,
typename M4>
1519 for (
size_t i = 0; i < f0.
GetRows(); ++i)
1521 for (
size_t j = 0; j < f0.
GetCols(); ++j)
constexpr Matrix()
Definition: Matrix.hpp:47
Matrix expression for element-wise binary operation.
Definition: MatrixExpression.hpp:452
constexpr size_t GetCols() const
Returns the number of columns.
Definition: MatrixExpression-Impl.hpp:27
const T * ConstPointer
Definition: Matrix.hpp:622
static constexpr std::enable_if_t<(Cols==1), U > Call(const Matrix< T, Rows, 1 > &a, ReduceOperation op, UnaryOperation uop)
Definition: Matrix-Impl.hpp:114
static constexpr bool Call(const Matrix< T, Rows, Cols > &a, const Matrix< T, Rows, Cols > &b, BinaryOperation op)
Definition: Matrix-Impl.hpp:163
const double & ConstReference
Definition: Matrix.hpp:41
Definition: Matrix-Impl.hpp:148
constexpr size_t GetRows() const
Returns the number of rows.
Definition: MatrixExpression-Impl.hpp:21
static constexpr T Call(const Matrix< T, Rows, Cols > &a, const T &init, ReduceOperation op, UnaryOperation uop)
Definition: Matrix-Impl.hpp:121
T * Pointer
Definition: Matrix.hpp:42
constexpr std::enable_if_t< IsMatrixSizeStatic< Rows, Cols >), bool > operator==(const MatrixExpression< T, Rows, Cols, M1 > &a, const MatrixExpression< T, Rows, Cols, M2 > &b)
Definition: Matrix-Impl.hpp:1408
static constexpr T CallDiag(const Matrix< T, Rows, Cols > &a, const T &init, ReduceOperation op, UnaryOperation uop)
Definition: Matrix-Impl.hpp:90
void CopyFrom(const MatrixExpression< T, R, C, E > &expression)
Copies from generic expression.
Definition: MatrixDenseBase-Impl.hpp:21
const T * ConstPointer
Definition: Matrix.hpp:544
static constexpr std::enable_if_t<(Cols > 1), U > Call(const Matrix< T, Rows, 1 > &a, const T &init, ReduceOperation op, UnaryOperation uop)
Definition: Matrix-Impl.hpp:105
T Eval(size_t i, size_t j) const
Returns the evaluated value for (i, j).
Definition: MatrixExpression-Impl.hpp:33
const double * ConstPointer
Definition: Matrix.hpp:43
static constexpr std::enable_if_t<(Cols==1), U > Call(const Matrix< T, Rows, 1 > &a, const T &init, ReduceOperation op, UnaryOperation uop)
Definition: Matrix-Impl.hpp:48
Definition: Matrix-Impl.hpp:24
void ElemIDiv(Matrix< T, R1, C1 > &a, const MatrixExpression< T, R2, C2, M2 > &b)
Definition: Matrix-Impl.hpp:1393
Pointer data()
Definition: Matrix-Impl.hpp:298
void Fill(const T &val)
Definition: Matrix-Impl.hpp:226
Iterator end()
Definition: Matrix-Impl.hpp:285
Definition: Matrix.hpp:420
static constexpr bool Call(const Matrix< T, Rows, Cols > &a, const Matrix< T, Rows, Cols > &b, BinaryOperation op)
Definition: Matrix-Impl.hpp:150
Definition: Matrix.hpp:27
constexpr T Product(const MatrixExpression< T, Rows, Cols, M1 > &a, const T &init)
Definition: Matrix-Impl.hpp:1503
T & Reference
Definition: Matrix.hpp:40
Matrix & operator=(const Matrix &other)
Definition: Matrix.hpp:81
const T & ConstReference
Definition: Matrix.hpp:620
void operator+=(Matrix< T, R1, C1 > &a, const MatrixExpression< T, R2, C2, M2 > &b)
Definition: Matrix-Impl.hpp:1342
Definition: pybind11Utils.hpp:20
const T & ConstReference
Definition: Matrix.hpp:542
Definition: Matrix.hpp:531
static constexpr std::enable_if_t<(Cols==1), U > Call(const Matrix< T, Rows, 1 > &a, ReduceOperation op, UnaryOperation uop)
Definition: Matrix-Impl.hpp:60
void Fill(ArrayView< T, N > a, const Vector< size_t, N > &begin, const Vector< size_t, N > &end, const T &val)
Definition: ArrayUtils-Impl.hpp:19
No-op operator.
Definition: Functors.hpp:20
static constexpr T Call(const Matrix< T, Rows, Cols > &a, ReduceOperation op, UnaryOperation uop)
Definition: Matrix-Impl.hpp:128
Pointer Iterator
Definition: Matrix.hpp:44
Definition: Matrix.hpp:216
void operator/=(Matrix< T, Rows, Cols > &a, const T &b)
Definition: Matrix-Impl.hpp:1400
ConstPointer ConstIterator
Definition: Matrix.hpp:45
Definition: Matrix-Impl.hpp:44
Definition: Matrix.hpp:312
static constexpr auto call(const M1 &a, const M2 &b, size_t i, size_t j)
Definition: Matrix-Impl.hpp:26
constexpr size_t GetCols() const
Definition: Matrix-Impl.hpp:266
static constexpr T Call(const Matrix< T, Rows, Cols > &a, ReduceOperation op, UnaryOperation uop)
Definition: Matrix-Impl.hpp:80
Derived & GetDerived()
Returns actual implementation (the subclass).
Definition: MatrixExpression-Impl.hpp:509
void operator*=(Matrix< T, R1, C1 > &a, const MatrixExpression< T, R2, C2, M2 > &b)
Definition: Matrix-Impl.hpp:1366
Definition: Matrix.hpp:126
Base class for matrix expression.
Definition: MatrixExpression.hpp:93
Definition: Matrix.hpp:611
static constexpr T Call(const Matrix< T, Rows, Cols > &a, const T &init, ReduceOperation op, UnaryOperation uop)
Definition: Matrix-Impl.hpp:70
static constexpr T CallDiag(const Matrix< T, Rows, Cols > &a, const T &init, ReduceOperation op, UnaryOperation uop)
Definition: Matrix-Impl.hpp:135
constexpr std::enable_if_t< TraitIsMatrixSizeStatic< Rows, Cols >::value, T > Accumulate(const MatrixExpression< T, Rows, Cols, M1 > &a, const T &init, BinaryOperation op)
Definition: Matrix-Impl.hpp:1452
bool operator!=(const MatrixExpression< T, R1, C1, M1 > &a, const MatrixExpression< T, R2, C2, M2 > &b)
Definition: Matrix-Impl.hpp:1443
void operator-=(Matrix< T, R1, C1 > &a, const MatrixExpression< T, R2, C2, M2 > &b)
Definition: Matrix-Impl.hpp:1354
Reference operator[](size_t i)
Definition: Matrix-Impl.hpp:311
typename NestedInitializerLists< T, N >::Type NestedInitializerListsT
Definition: NestedInitializerList.hpp:57
Iterator begin()
Definition: Matrix-Impl.hpp:272
std::enable_if_t< std::is_arithmetic< T >::value, T > MonotonicCatmullRom(const T &f0, const T &f1, const T &f2, const T &f3, T t)
Computes monotonic Catmull-Rom interpolation.
Definition: MathUtils-Impl.hpp:336
constexpr size_t GetRows() const
Definition: Matrix-Impl.hpp:260
void Swap(Matrix &other)
Definition: Matrix-Impl.hpp:254
void ElemIMul(Matrix< T, R1, C1 > &a, const MatrixExpression< T, R2, C2, M2 > &b)
Definition: Matrix-Impl.hpp:1375
static constexpr auto call(const M1 &a, const M2 &b, size_t i, size_t j)
Definition: Matrix-Impl.hpp:35
Definition: MatrixExpression.hpp:79