9 #include "../Common/Definitions.hpp" 10 #include "../Common/Traits.hpp" 11 #include "../Vector.hpp" 23 template <
class T,
int Rows,
int Columns, eMatrixOrder Order = eMatrixOrder::FOLLOW_VECTOR, eMatrixLayout Layout = eMatrixLayout::ROW_MAJOR,
bool Packed = false>
46 std::array<Vector<T, StripeDim, Packed>, StripeCount>
stripes;
50 inline T& GetElement(
int row,
int col) {
53 return GetElementImpl(col, row, std::integral_constant<bool, Layout == eMatrixLayout::ROW_MAJOR>());
55 inline T GetElement(
int row,
int col)
const {
58 return GetElementImpl(col, row, std::integral_constant<bool, Layout == eMatrixLayout::ROW_MAJOR>());
62 inline T& GetElementImpl(
int col,
int row, std::true_type) {
63 return stripes[row][col];
65 inline T GetElementImpl(
int col,
int row, std::true_type)
const {
66 return stripes[row][col];
68 inline T& GetElementImpl(
int col,
int row, std::false_type) {
69 return stripes[col][row];
71 inline T GetElementImpl(
int col,
int row, std::false_type)
const {
72 return stripes[col][row];
81 template <
class MatrixT,
int SRows,
int SColumns>
85 template <
class,
int,
int>
87 static constexpr
int VecDim = std::max(SRows, SColumns);
88 static constexpr
bool VectorAssignable = std::min(SRows, SColumns) == 1;
91 SubmatrixHelper(MatrixT& mat,
int row,
int col) : mat(mat), row(row), col(col) {}
98 template <
class U, eMatrixOrder UOrder, eMatrixLayout ULayout,
bool UPacked>
101 for (
int i = 0; i < SRows; ++i) {
102 for (
int j = 0; j < SColumns; ++j) {
103 ret(i, j) = (*this)(i, j);
109 template <class U, bool Packed2, class = typename std::enable_if<VectorAssignable, U>::type>
113 for (
int i = 0; i < SRows; ++i) {
114 for (
int j = 0; j < SColumns; ++j) {
115 v(k) = (*this)(i, j);
123 template <
class U, eMatrixOrder UOrder, eMatrixLayout ULayout,
bool UPacked>
125 static_assert(!std::is_const<MatrixT>::value,
"Cannot assign to submatrix of const matrix.");
128 for (
int i = 0; i < SRows; ++i) {
129 for (
int j = 0; j < SColumns; ++j) {
130 mat(row + i, col + j) = rhs(i, j);
138 template <class U, bool Packed, class = typename std::enable_if<VectorAssignable, U>::type>
140 static_assert(!std::is_const<MatrixT>::value,
"Cannot assign to submatrix of const matrix.");
143 for (
int i = 0; i < SRows; ++i) {
144 for (
int j = 0; j < SColumns; ++j) {
145 mat(row + i, col + j) = v(k);
153 template <
class MatrixU>
155 static_assert(!std::is_const<MatrixT>::value,
"Cannot assign to submatrix of const matrix.");
158 if ((
void*)&mat == (
void*)&rhs.mat) {
170 for (
int i = 0; i < SRows; ++i) {
171 for (
int j = 0; j < SColumns; ++j) {
172 mat(row + i, col + j) = rhs(i, j);
179 static_assert(!std::is_const<MatrixT>::value,
"Cannot assign to submatrix of const matrix.");
180 return operator=<MatrixT>(rhs);
184 return mat(this->row + row, this->col + col);
188 return mat(this->row + row, this->col + col);
193 int row = -1, col = -1;
201 template <
class T,
int Rows,
int Columns, eMatrixOrder Order = eMatrixOrder::FOLLOW_VECTOR, eMatrixLayout Layout = eMatrixLayout::ROW_MAJOR,
bool Packed = false>
203 static_assert(Columns >= 1 && Rows >= 1,
"Dimensions must be positive integers.");
205 static constexpr
int VecDim = std::max(Rows, Columns);
206 static constexpr
bool VectorAssignable = std::min(Rows, Columns) == 1;
211 template <
class T2,
int Rows2,
int Columns2, eMatrixOrder Order2, eMatrixLayout Layout2,
bool Packed2>
227 template <
class T2, eMatrixLayout Layout2,
bool Packed2>
229 for (
int i = 0; i <
RowCount(); ++i) {
231 (*this)(i, j) = rhs(i, j);
237 template <
class T2, eMatrixLayout Layout2,
bool Packed2>
239 for (
int i = 0; i <
RowCount(); ++i) {
241 (*this)(i, j) = rhs(j, i);
246 template <
class H,
class... Args,
248 typename std::enable_if<1 +
sizeof...(Args) == Rows * Columns,
int>::type = 0>
250 Assign<0, 0>(h, args...);
254 template <class T2, bool Packed2, class = typename std::enable_if<VectorAssignable, T2>::type>
256 for (
int i = 0; i < v.
Dimension(); ++i) {
268 return GetElement(row, col);
271 return GetElement(row, col);
275 template <
class Q = T>
276 inline typename std::enable_if<(Columns == 1 && Rows > 1) || (Columns > 1 && Rows == 1), Q>::type&
operator()(
int idx) {
277 return GetElement(Rows == 1 ? 0 : idx, Columns == 1 ? 0 : idx);
279 template <
class Q = T>
280 inline typename std::enable_if<(Columns == 1 && Rows > 1) || (Columns > 1 && Rows == 1), Q>::type
operator()(
int idx)
const {
281 return GetElement(Rows == 1 ? 0 : idx, Columns == 1 ? 0 : idx);
285 template <
int Subrows,
int Subcolumns>
287 assert(Subrows + rowIdx <= Rows);
288 assert(Subcolumns + colIdx <= Columns);
293 template <
int Subrows,
int Subcolumns>
295 assert(Subrows + rowIdx <= Rows);
296 assert(Subcolumns + colIdx <= Columns);
303 return Submatrix<Rows, 1>(0, colIdx);
307 return Submatrix<1, Columns>(rowIdx, 0);
311 return Submatrix<Rows, 1>(0, colIdx);
314 auto Row(
int rowIdx)
const {
315 return Submatrix<1, Columns>(rowIdx, 0);
319 template <class T2, bool Packed2, class = typename std::enable_if<VectorAssignable, T2>::type>
323 for (
int i = 0; i < Rows; ++i) {
324 for (
int j = 0; j < Columns; ++j) {
325 v(k) = (*this)(i, j);
333 template <
int i,
int j,
class Head,
class... Args>
334 void Assign(Head head, Args... args) {
335 (*this)(i, j) = (T)head;
336 Assign<((j != Columns - 1) ? i : (i + 1)), ((j + 1) % Columns)>(args...);
T operator()(int row, int col) const
Definition: MatrixImpl.hpp:270
Props::Type operator()(int row, int col) const
Definition: MatrixImpl.hpp:187
#define MATHTER_EBCO
Definition: Definitions.hpp:82
auto Column(int colIdx)
Return the submatrix corresponding to the specified column.
Definition: MatrixImpl.hpp:302
SubmatrixHelper & operator=(const Vector< U, VecDim, Packed > &v)
Definition: MatrixImpl.hpp:139
SubmatrixHelper & operator=(const Matrix< U, SRows, SColumns, UOrder, ULayout, UPacked > &rhs)
Definition: MatrixImpl.hpp:124
std::enable_if<(Columns==1 &&Rows > 1)||(Columns > 1 &&Rows==1), Q >::type operator()(int idx) const
Definition: MatrixImpl.hpp:280
T & operator()(int row, int col)
Definition: MatrixImpl.hpp:267
auto Row(int rowIdx) const
Return the submatrix corresponding to the specified row.
Definition: MatrixImpl.hpp:314
mathter::SubmatrixHelper< Matrix, Subrows, Subcolumns > Submatrix(int rowIdx, int colIdx)
Definition: MatrixImpl.hpp:286
Represents a vector in N-dimensional space.
Definition: Definitions.hpp:57
Definition: Traits.hpp:48
Definition: MatrixImpl.hpp:24
static constexpr int StripeCount
Definition: MatrixImpl.hpp:44
auto Row(int rowIdx)
Return the submatrix corresponding to the specified row.
Definition: MatrixImpl.hpp:306
Definition: Traits.hpp:66
std::array< Vector< T, StripeDim, Packed >, StripeCount > stripes
Definition: MatrixImpl.hpp:46
Definition: Approx.hpp:11
Matrix(H h, Args... args)
Definition: MatrixImpl.hpp:249
Props::Type & operator()(int row, int col)
Definition: MatrixImpl.hpp:183
SubmatrixHelper & operator=(const SubmatrixHelper &rhs)
Definition: MatrixImpl.hpp:178
auto Column(int colIdx) const
Return the submatrix corresponding to the specified column.
Definition: MatrixImpl.hpp:310
Matrix(const Vector< T2, VecDim, Packed2 > &v)
Definition: MatrixImpl.hpp:255
Matrix(const Matrix< T2, Columns, Rows, Order==eMatrixOrder::FOLLOW_VECTOR ? eMatrixOrder::PRECEDE_VECTOR :eMatrixOrder::FOLLOW_VECTOR, Layout2, Packed2 > &rhs)
Definition: MatrixImpl.hpp:238
constexpr int Height() const
Returns the number of rows of the matrix.
Definition: MatrixImpl.hpp:39
static constexpr int StripeDim
Definition: MatrixImpl.hpp:43
Definition: Definitions.hpp:66
constexpr int ColumnCount() const
Returns the number of columns of the matrix.
Definition: MatrixImpl.hpp:27
mathter::SubmatrixHelper< const Matrix, Subrows, Subcolumns > Submatrix(int rowIdx, int colIdx) const
Definition: MatrixImpl.hpp:294
Definition: Traits.hpp:184
Definition: Definitions.hpp:63
constexpr int Width() const
Returns the number of columns of the matrix.
Definition: MatrixImpl.hpp:35
constexpr int Dimension() const
Returns the number of dimensions of the vector.
Definition: VectorImpl.hpp:402
SubmatrixHelper & operator=(const SubmatrixHelper< MatrixU, SRows, SColumns > &rhs)
Definition: MatrixImpl.hpp:154
std::enable_if<(Columns==1 &&Rows > 1)||(Columns > 1 &&Rows==1), Q >::type & operator()(int idx)
Definition: MatrixImpl.hpp:276
Matrix(const Matrix< T2, Rows, Columns, Order, Layout2, Packed2 > &rhs)
Definition: MatrixImpl.hpp:228
SubmatrixHelper(SubmatrixHelper &&rhs)
Definition: MatrixImpl.hpp:95
constexpr int RowCount() const
Returns the number of rows of the matrix.
Definition: MatrixImpl.hpp:31