32#ifndef DOXYGEN_SHOULD_SKIP_THIS
59 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
61 #ifndef DOXYGEN_SHOULD_SKIP_THIS
62 static_assert(ROWS > 0,
"ROWS must be strictly positive");
63 static_assert(COLS > 0,
"COLS must be strictly positive");
74 static constexpr std::size_t Rows = ROWS;
79 static constexpr std::size_t Cols = COLS;
96 std::fill_n(data, ROWS * COLS, val);
101 std::copy_n(array, ROWS * COLS, data);
105 return grid[row][col];
109 return grid[row][col];
115 constexpr void zero() noexcept {
116 for (std::size_t i = 0; i < ROWS * COLS; ++i) {
157 static constexpr std::size_t Rows = 2;
162 static constexpr std::size_t Cols = 2;
174 explicit constexpr Matrix(T val) noexcept
180 explicit constexpr Matrix(
const T *array)
181 : m11(array[0]), m12(array[1])
182 , m21(array[2]), m22(array[3])
194 constexpr Matrix(T e11, T e12, T e21, T e22) noexcept
209 const T *grid[Rows][Cols] = {
213 return *grid[row][col];
217 T *grid[Rows][Cols] = {
221 return *grid[row][col];
224 constexpr void zero() noexcept {
277 static constexpr std::size_t Rows = 3;
282 static constexpr std::size_t Cols = 3;
294 explicit constexpr Matrix(T val) noexcept
295 : m11(val), m12(val), m13(val)
296 , m21(val), m22(val), m23(val)
297 , m31(val), m32(val), m33(val)
301 explicit constexpr Matrix(
const T *array)
302 : m11(array[0]), m12(array[1]), m13(array[2])
303 , m21(array[3]), m22(array[4]), m23(array[5])
304 , m31(array[6]), m32(array[7]), m33(array[8])
321 constexpr Matrix(T e11, T e12, T e13, T e21, T e22, T e23, T e31, T e32, T e33) noexcept
322 : m11(e11), m12(e12), m13(e13)
323 , m21(e21), m22(e22), m23(e23)
324 , m31(e31), m32(e32), m33(e33)
337 const T *grid[Rows][Cols] = {
338 { &m11, &m12, &m13 },
339 { &m21, &m22, &m23 },
342 return *grid[row][col];
346 T *grid[Rows][Cols] = {
347 { &m11, &m12, &m13 },
348 { &m21, &m22, &m23 },
351 return *grid[row][col];
354 constexpr void zero() noexcept {
355 m11 = m12 = m13 =
T{};
356 m21 = m22 = m23 =
T{};
357 m31 = m32 = m33 =
T{};
431 static constexpr std::size_t Rows = 4;
436 static constexpr std::size_t Cols = 4;
448 explicit constexpr Matrix(T val) noexcept
449 : m11(val), m12(val), m13(val), m14(val)
450 , m21(val), m22(val), m23(val), m24(val)
451 , m31(val), m32(val), m33(val), m34(val)
452 , m41(val), m42(val), m43(val), m44(val)
456 explicit constexpr Matrix(
const T *array)
457 : m11(array[ 0]), m12(array[ 1]), m13(array[ 2]), m14(array[ 3])
458 , m21(array[ 4]), m22(array[ 5]), m23(array[ 6]), m24(array[ 7])
459 , m31(array[ 8]), m32(array[ 9]), m33(array[10]), m34(array[11])
460 , m41(array[12]), m42(array[13]), m43(array[14]), m44(array[15])
484 constexpr Matrix(T e11, T e12, T e13, T e14, T e21, T e22, T e23, T e24, T e31, T e32, T e33, T e34, T e41, T e42, T e43, T e44) noexcept
485 : m11(e11), m12(e12), m13(e13), m14(e14)
486 , m21(e21), m22(e22), m23(e23), m24(e24)
487 , m31(e31), m32(e32), m33(e33), m34(e34)
488 , m41(e41), m42(e42), m43(e43), m44(e44)
501 const T *grid[Rows][Cols] = {
502 { &m11, &m12, &m13, &m14 },
503 { &m21, &m22, &m23, &m24 },
504 { &m31, &m32, &m33, &m34 },
505 { &m41, &m42, &m43, &m44 }
507 return *grid[row][col];
511 T *grid[Rows][Cols] = {
512 { &m11, &m12, &m13, &m14 },
513 { &m21, &m22, &m23, &m24 },
514 { &m31, &m32, &m33, &m34 },
515 { &m41, &m42, &m43, &m44 }
517 return *grid[row][col];
520 constexpr void zero() noexcept {
521 m11 = m12 = m13 = m14 =
T{};
522 m21 = m22 = m23 = m24 =
T{};
523 m31 = m32 = m33 = m34 =
T{};
524 m41 = m42 = m43 = m44 =
T{};
675 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
678 for (std::size_t i = 0; i < ROWS; ++i) {
679 for (std::size_t j = 0; j < COLS; ++j) {
680 if (lhs(i,j) != rhs(i,j)) {
693 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
696 return !(lhs == rhs);
707 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
712 for (std::size_t i = 0; i < ROWS; ++i) {
713 for (std::size_t j = 0; j < COLS; ++j) {
714 out(i,j) = - val(i,j);
729 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
734 for (std::size_t i = 0; i < ROWS; ++i) {
735 for (std::size_t j = 0; j < COLS; ++j) {
736 out(i,j) = lhs(i,j) + rhs(i,j);
747 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
750 for (std::size_t i = 0; i < ROWS; ++i) {
751 for (std::size_t j = 0; j < COLS; ++j) {
752 lhs(i,j) += rhs(i,j);
764 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
769 for (std::size_t i = 0; i < ROWS; ++i) {
770 for (std::size_t j = 0; j < COLS; ++j) {
771 out(i,j) = lhs(i,j) - rhs(i,j);
782 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
785 for (std::size_t i = 0; i < ROWS; ++i) {
786 for (std::size_t j = 0; j < COLS; ++j) {
787 lhs(i,j) -= rhs(i,j);
802 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
807 for (std::size_t i = 0; i < ROWS; ++i) {
808 for (std::size_t j = 0; j < COLS; ++j) {
809 out(i,j) = lhs(i,j) * rhs;
820 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
823 for (std::size_t i = 0; i < ROWS; ++i) {
824 for (std::size_t j = 0; j < COLS; ++j) {
836 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
841 for (std::size_t i = 0; i < ROWS; ++i) {
842 for (std::size_t j = 0; j < COLS; ++j) {
843 out(i,j) = lhs * rhs(i,j);
854 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
859 for (std::size_t i = 0; i < ROWS; ++i) {
860 for (std::size_t j = 0; j < COLS; ++j) {
861 out(i,j) = lhs(i,j) / rhs;
872 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
875 for (std::size_t i = 0; i < ROWS; ++i) {
876 for (std::size_t j = 0; j < COLS; ++j) {
888 template<
typename T, std::
size_t S1, std::
size_t S2>
893 for (std::size_t i = 0; i < S1; ++i) {
896 for (std::size_t j = 0; j < S2; ++j) {
897 val += lhs(i,j) * rhs[j];
910 template<
typename T, std::
size_t S1, std::
size_t S2>
915 for (std::size_t j = 0; j < S2; ++j) {
918 for (std::size_t i = 0; i < S1; ++i) {
919 val += lhs[i] * rhs(i,j);
933 template<
typename T, std::
size_t S1, std::
size_t S2, std::
size_t S3>
938 for (std::size_t i = 0; i < S1; ++i) {
939 for (std::size_t j = 0; j < S3; ++j) {
942 for (std::size_t k = 0; k < S2; ++k) {
943 val += lhs(i,k) * rhs(k,j);
959 template<
typename T, std::
size_t N>
976 template<
typename MatrixType>
979 static_assert(MatrixType::Rows == MatrixType::Cols,
"identity() is only defined for square matrices.");
983 typedef typename MatrixType::value_type
value_type;
985 for (std::size_t i = 0; i < MatrixType::Rows; ++i) {
986 for (std::size_t j = 0; j < MatrixType::Cols; ++j) {
998 template<
typename T, std::
size_t S1, std::
size_t S2>
1003 for (std::size_t i = 0; i < S1; ++i) {
1004 for (std::size_t j = 0; j < S2; ++j) {
1005 out(j,i) = mat(i,j);
1018 template<
typename T>
1028 T det = mat.
xx * mat.
yy - mat.
yx * mat.
xy;
1037 template<
typename T>
1043 out.
xy = - (mat.
xy * mat.
zz - mat.
zy * mat.
xz);
1045 out.
yx = - (mat.
yx * mat.
zz - mat.
zx * mat.
yz);
1047 out.
yz = - (mat.
xx * mat.
yz - mat.
yx * mat.
xz);
1049 out.
zy = - (mat.
xx * mat.
zy - mat.
zx * mat.
xy);
1052 T det = mat.
xx * out.
xx + mat.
xy * out.
yx + mat.
xz * out.
zx;
1057#ifndef DOXYGEN_SHOULD_SKIP_THIS
constexpr ZeroType Zero
Constant to represent "zero".
Definition: Types.h:93
The namespace for gf classes.
A 2x2 matrix.
Definition: Matrix.h:148
constexpr Matrix(ZeroType) noexcept
Definition: Matrix.h:169
T value_type
The value type of the elements of the matrix.
Definition: Matrix.h:152
T m11
Definition: Matrix.h:231
T * getData() noexcept
Definition: Matrix.h:204
T & operator()(std::size_t row, std::size_t col)
Definition: Matrix.h:216
T xy
Definition: Matrix.h:235
constexpr void zero() noexcept
Definition: Matrix.h:224
T m21
Definition: Matrix.h:241
constexpr Matrix(const T *array)
Definition: Matrix.h:180
constexpr Matrix(T e11, T e12, T e21, T e22) noexcept
Constructor that takes all the elements.
Definition: Matrix.h:194
T m12
Definition: Matrix.h:236
T operator()(std::size_t row, std::size_t col) const
Definition: Matrix.h:208
Matrix()=default
Default constructor.
T m22
Definition: Matrix.h:246
const T * getData() const noexcept
Definition: Matrix.h:200
T xx
Definition: Matrix.h:230
constexpr Matrix(T val) noexcept
Definition: Matrix.h:174
T yy
Definition: Matrix.h:245
T yx
Definition: Matrix.h:240
A 3x3 matrix.
Definition: Matrix.h:268
T & operator()(std::size_t row, std::size_t col)
Definition: Matrix.h:345
T value_type
The value type of the elements of the matrix.
Definition: Matrix.h:272
T xy
Definition: Matrix.h:366
T operator()(std::size_t row, std::size_t col) const
Definition: Matrix.h:336
T yy
Definition: Matrix.h:381
constexpr void zero() noexcept
Definition: Matrix.h:354
T m33
Definition: Matrix.h:402
T zz
Definition: Matrix.h:401
T yx
Definition: Matrix.h:376
T m32
Definition: Matrix.h:397
T m12
Definition: Matrix.h:367
constexpr Matrix(T val) noexcept
Definition: Matrix.h:294
T m21
Definition: Matrix.h:377
T yz
Definition: Matrix.h:386
T zy
Definition: Matrix.h:396
constexpr Matrix(T e11, T e12, T e13, T e21, T e22, T e23, T e31, T e32, T e33) noexcept
Constructor that takes all the elements.
Definition: Matrix.h:321
T * getData() noexcept
Definition: Matrix.h:332
T zx
Definition: Matrix.h:391
T m31
Definition: Matrix.h:392
T m11
Definition: Matrix.h:362
T xz
Definition: Matrix.h:371
T m23
Definition: Matrix.h:387
constexpr Matrix(ZeroType) noexcept
Definition: Matrix.h:289
const T * getData() const noexcept
Definition: Matrix.h:328
T m22
Definition: Matrix.h:382
T m13
Definition: Matrix.h:372
constexpr Matrix(const T *array)
Definition: Matrix.h:301
T xx
Definition: Matrix.h:361
Matrix()=default
Default constructor.
T yx
Definition: Matrix.h:548
T xz
Definition: Matrix.h:538
T xy
Definition: Matrix.h:533
T value_type
The value type of the elements of the matrix.
Definition: Matrix.h:426
T m33
Definition: Matrix.h:579
T m23
Definition: Matrix.h:559
constexpr Matrix(T val) noexcept
Definition: Matrix.h:448
Matrix()=default
Default constructor.
constexpr Matrix(const T *array)
Definition: Matrix.h:456
T yy
Definition: Matrix.h:553
const T * getData() const noexcept
Definition: Matrix.h:492
T wz
Definition: Matrix.h:598
T yw
Definition: Matrix.h:563
constexpr Matrix(ZeroType) noexcept
Definition: Matrix.h:443
T m43
Definition: Matrix.h:599
T m42
Definition: Matrix.h:594
T xw
Definition: Matrix.h:543
T m44
Definition: Matrix.h:604
T zz
Definition: Matrix.h:578
T m34
Definition: Matrix.h:584
T m11
Definition: Matrix.h:529
T zw
Definition: Matrix.h:583
T wx
Definition: Matrix.h:588
constexpr Matrix(T e11, T e12, T e13, T e14, T e21, T e22, T e23, T e24, T e31, T e32, T e33, T e34, T e41, T e42, T e43, T e44) noexcept
Constructor that takes all the elements.
Definition: Matrix.h:484
T ww
Definition: Matrix.h:603
T m32
Definition: Matrix.h:574
T m14
Definition: Matrix.h:544
T m12
Definition: Matrix.h:534
T operator()(std::size_t row, std::size_t col) const
Definition: Matrix.h:500
T m41
Definition: Matrix.h:589
T * getData() noexcept
Definition: Matrix.h:496
T wy
Definition: Matrix.h:593
T yz
Definition: Matrix.h:558
T zy
Definition: Matrix.h:573
T m24
Definition: Matrix.h:564
T & operator()(std::size_t row, std::size_t col)
Definition: Matrix.h:510
T m31
Definition: Matrix.h:569
T xx
Definition: Matrix.h:528
T m13
Definition: Matrix.h:539
T m22
Definition: Matrix.h:554
T zx
Definition: Matrix.h:568
constexpr void zero() noexcept
Definition: Matrix.h:520
T m21
Definition: Matrix.h:549
General purpose math matrix.
Definition: Matrix.h:60
constexpr Matrix< T, ROWS, COLS > operator*(T lhs, const Matrix< T, ROWS, COLS > &rhs)
Left scalar multiplication.
Definition: Matrix.h:838
constexpr Matrix< T, ROWS, COLS > & operator-=(Matrix< T, ROWS, COLS > &lhs, const Matrix< T, ROWS, COLS > &rhs)
Component-wise substraction and assignment.
Definition: Matrix.h:784
T & operator()(std::size_t row, std::size_t col)
Definition: Matrix.h:108
constexpr Matrix< T, ROWS, COLS > operator+(const Matrix< T, ROWS, COLS > &lhs, const Matrix< T, ROWS, COLS > &rhs)
Component-wise addition.
Definition: Matrix.h:731
constexpr bool operator==(const Matrix< T, ROWS, COLS > &lhs, const Matrix< T, ROWS, COLS > &rhs)
Equality operator between two matrices.
Definition: Matrix.h:677
constexpr Matrix< T, N, N > & operator*=(Matrix< T, N, N > &lhs, const Matrix< T, N, N > &rhs)
Matrix-matrix multiplication and assignment.
Definition: Matrix.h:961
Matrix(const T *array)
Definition: Matrix.h:99
constexpr Matrix< T, 2, 2 > invert(const Matrix< T, 2, 2 > &mat)
Inversion of a 2x2 matrix.
Definition: Matrix.h:1020
constexpr Matrix< T, ROWS, COLS > operator-(const Matrix< T, ROWS, COLS > &val)
Component-wise unary minus.
Definition: Matrix.h:709
constexpr void zero() noexcept
Zero out the matrix.
Definition: Matrix.h:115
constexpr MatrixType identity()
Identity matrix.
Definition: Matrix.h:978
T operator()(std::size_t row, std::size_t col) const
Definition: Matrix.h:104
constexpr Vector< T, S2 > operator*(const Vector< T, S1 > &lhs, const Matrix< T, S1, S2 > &rhs)
Vector-matrix multiplication.
Definition: Matrix.h:912
constexpr Matrix< T, 3, 3 > invert(const Matrix< T, 3, 3 > &mat)
Inversion of a 3x3 matrix.
Definition: Matrix.h:1039
constexpr Matrix< T, ROWS, COLS > operator-(const Matrix< T, ROWS, COLS > &lhs, const Matrix< T, ROWS, COLS > &rhs)
Component-wise substraction.
Definition: Matrix.h:766
constexpr Matrix< T, ROWS, COLS > & operator+=(Matrix< T, ROWS, COLS > &lhs, const Matrix< T, ROWS, COLS > &rhs)
Component-wise addition and assignment.
Definition: Matrix.h:749
constexpr Vector< T, S1 > operator*(const Matrix< T, S1, S2 > &lhs, const Vector< T, S2 > &rhs)
Matrix-vector multiplication.
Definition: Matrix.h:890
constexpr Matrix< T, ROWS, COLS > & operator*=(Matrix< T, ROWS, COLS > &lhs, T rhs)
Right scalar multiplication and assignment.
Definition: Matrix.h:822
Matrix(T val) noexcept
Constructor that fills the matrix with a value.
Definition: Matrix.h:94
constexpr Matrix< T, ROWS, COLS > operator/(const Matrix< T, ROWS, COLS > &lhs, T rhs)
Right scalar division.
Definition: Matrix.h:856
Matrix()=default
Default constructor.
constexpr Matrix< T, S2, S1 > transpose(const Matrix< T, S1, S2 > &mat)
Transposition of a matrix.
Definition: Matrix.h:1000
constexpr Matrix< T, ROWS, COLS > & operator/=(Matrix< T, ROWS, COLS > &lhs, T rhs)
Right scalar division and assignment.
Definition: Matrix.h:874
T value_type
The value type of the elements of the matrix.
Definition: Matrix.h:69
constexpr bool operator!=(const Matrix< T, ROWS, COLS > &lhs, const Matrix< T, ROWS, COLS > &rhs)
Inequality operator between two matrices.
Definition: Matrix.h:695
constexpr Matrix(ZeroType) noexcept
Definition: Matrix.h:86
constexpr Matrix< T, ROWS, COLS > operator*(const Matrix< T, ROWS, COLS > &lhs, T rhs)
Right scalar multiplication.
Definition: Matrix.h:804
constexpr Matrix< T, S1, S3 > operator*(const Matrix< T, S1, S2 > &lhs, const Matrix< T, S2, S3 > &rhs)
Matrix-matrix multiplication.
Definition: Matrix.h:935
General purpose math vector.
Definition: Vector.h:61
Semantic type to represent "zero".
Definition: Types.h:85