Theoretica
Mathematical Library
Loading...
Searching...
No Matches
mat.h
Go to the documentation of this file.
1
5
6#ifndef THEORETICA_MATRIX_H
7#define THEORETICA_MATRIX_H
8
9#ifndef THEORETICA_NO_PRINT
10#include <sstream>
11#include <ostream>
12#endif
13
14#include <array>
15#include <vector>
16
17#include "../core/error.h"
18#include "../core/constants.h"
19#include "../core/real_analysis.h"
20#include "./algebra.h"
21#include "./transform.h"
22#include "./vec.h"
23
24
25namespace theoretica {
26
27
33 template<typename Matrix, typename ReturnType = matrix_element_t<Matrix>&>
35
36 private:
37
39 Matrix& matrix;
40
42 size_t row;
43
45 size_t col;
46
47 public:
48
49 using iterator_category = std::forward_iterator_tag;
50 using value_type = matrix_element_t<Matrix>;
51 using pointer = value_type*;
52 using reference = value_type&;
53
54
64 Matrix& matrix,
65 size_t row = 0,
66 size_t col = 0)
67 : matrix(matrix), row(row), col(col) {}
68
69
74 ReturnType operator*() {
75 return matrix(row, col);
76 }
77
78
85
86 col++;
87
88 if(col == matrix.cols()) {
89 col = 0;
90 row++;
91 }
92
93 return *this;
94 }
95
96
99 size_t row_index() {
100 return row;
101 }
102
103
106 size_t col_index() {
107 return col;
108 }
109
110
114 bool operator==(const mat_iterator& other) const {
115 return (row == other.row) &&
116 (col == other.col);
117 }
118
119
123 bool operator!=(const mat_iterator& other) const {
124 return !(*this == other);
125 }
126 };
127
128
135 template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
136 class mat {
137 public:
138
139#ifdef THEORETICA_ROW_FIRST
140 Type data[N][K];
141#else
142 Type data[K][N];
143#endif
144
145
149 mat() {
150 algebra::mat_zeroes(*this);
151 }
152
153
159 template<typename Matrix>
160 mat(const Matrix& m) {
161 algebra::mat_copy(*this, m);
162 }
163
164
171 template<typename T = Type>
172 inline mat(const std::initializer_list<std::initializer_list<T>>& rows) {
173
174 if(rows.size() != N) {
175 TH_MATH_ERROR("mat::mat", rows.size(), INVALID_ARGUMENT);
176 algebra::mat_error(*this);
177 return;
178 }
179
180 unsigned int i = 0;
181 unsigned int j = 0;
182
183 for (const auto& row : rows) {
184
185 if (row.size() != K) {
186 TH_MATH_ERROR("mat::mat", rows.size(), INVALID_ARGUMENT);
187 algebra::mat_error(*this);
188 return;
189 }
190
191 for (const auto& x : row) {
192
193 get(i, j) = x;
194 j = (j + 1) % K;
195 }
196
197 i++;
198 }
199 }
200
201
209 mat(Type diagonal, unsigned int n = 0, unsigned int k = 0) {
210
211 if (n && k)
212 resize(n, k);
213
214 algebra::mat_zeroes(*this);
215 const unsigned int m = min(n, k);
216
217 for (unsigned int i = 0; i < m; ++i)
218 get(i, i) = diagonal;
219 }
220
221
226 template<typename Matrix>
227 inline mat<Type, N, K>& operator=(const Matrix& other) {
228 return algebra::mat_copy(*this, other);
229 }
230
231
236 template<typename Matrix>
237 inline mat<Type, N, K> operator+(const Matrix& other) const {
238 mat<Type, N, K> res;
239 return algebra::mat_sum(res, *this, other);
240 }
241
242
247 template<typename Matrix>
248 inline mat<Type, N, K> operator-(const Matrix& other) const {
249 mat<Type, N, K> res;
250 return algebra::mat_diff(res, *this, other);
251 }
252
253
257 inline mat<Type, N, K> operator*(Type scalar) const {
258 mat<Type, N, K> res;
259 return algebra::mat_scalmul(res, scalar, *this);
260 }
261
262
267 inline friend mat<Type, N, K> operator*(Type a, const mat<Type, N, K>& B) {
268 return B * a;
269 }
270
271
278 template<typename VecType, unsigned int M>
279 inline friend vec<VecType, K> operator*(
280 const vec<VecType, M>& a, const mat<Type, N, K>& B) {
281 return B * a;
282 }
283
284
290 inline mat<Type, N, K> operator/(Type scalar) const {
291
292 mat<Type, N, K> res;
293
294 if(abs(scalar) < MACH_EPSILON) {
295 TH_MATH_ERROR("mat::operator/", scalar, DIV_BY_ZERO);
296 return algebra::mat_error(res);
297 }
298
299 return algebra::mat_scalmul(res, 1.0 / scalar, *this);
300 }
301
302
310 template<typename Vector>
311 inline Vector transform(const Vector& v) const {
312
313 if(v.size() != cols()) {
314 TH_MATH_ERROR("mat::transform", v.size(), INVALID_ARGUMENT);
315 Vector res;
316 res.resize(cols());
318 return res;
319 }
320
321 return algebra::transform(*this, v);
322 }
323
324
328 inline vec<Type, N> transform(const vec<Type, K>& v) const {
329 return algebra::transform(*this, v);
330 }
331
332
336 inline vec<Type, N> operator*(const vec<Type, K>& v) const {
337 return transform(v);
338 }
339
340
345 template<unsigned int M>
346 inline mat<Type, N, M> mul(const mat<Type, K, M>& B) const {
347 mat<Type, N, M> res;
348 return algebra::mat_mul(res, *this, B);
349 }
350
351
359 template<typename Matrix>
360 inline Matrix mul(const Matrix& B) const {
361
362 Matrix res;
363 res.resize(N, B.cols());
364
365 if(B.rows() != K) {
366 TH_MATH_ERROR("mat::transform", B.rows(), INVALID_ARGUMENT);
368 return res;
369 }
370
371 return algebra::mat_mul(res, *this, B);
372 }
373
374
379 template<typename Matrix>
380 inline auto operator*(const Matrix& B) const {
381
382 Matrix res;
383 res.resize(N, B.cols());
384
385 if(B.rows() != K) {
386 TH_MATH_ERROR("mat::transform", B.rows(), INVALID_ARGUMENT);
388 return res;
389 }
390
391 return algebra::mat_mul(res, *this, B);
392 }
393
394
399 template<typename Matrix>
400 inline mat<Type, N, K>& operator+=(const Matrix& other) {
401 return algebra::mat_sum(*this, other);
402 }
403
404
409 template<typename Matrix>
410 inline mat<Type, N, K>& operator-=(const Matrix& other) {
411 return algebra::mat_diff(*this, other);
412 }
413
414
418 inline mat<Type, N, K>& operator*=(Type scalar) {
419 return algebra::mat_scalmul(scalar, *this);
420 }
421
422
428 inline mat<Type, N, K>& operator/=(Type scalar) {
429
430 if(abs(scalar) < MACH_EPSILON) {
431 TH_MATH_ERROR("mat::operator/", scalar, DIV_BY_ZERO);
432 return algebra::mat_error(*this);
433 }
434
435 return algebra::mat_scalmul(1.0 / scalar, *this);
436 }
437
438
443 template<typename Matrix>
444 inline mat<Type, N, K>& operator*=(const Matrix& B) {
445 return (*this = this->operator*(B));
446 }
447
448
454 inline mat<Type, N, K>& transpose() {
455 static_assert(
456 N == K, "The matrix must be square to be transposed in place.");
457 return algebra::make_transposed(*this);
458 }
459
460
466 inline Type& get(unsigned int i, unsigned int j) {
467
468#ifdef THEORETICA_ROW_FIRST
469 return data[i][j];
470#else
471 return data[j][i];
472#endif
473 }
474
475
481 inline const Type& get(unsigned int i, unsigned int j) const {
482
483#ifdef THEORETICA_ROW_FIRST
484 return data[i][j];
485#else
486 return data[j][i];
487#endif
488 }
489
490
496 inline Type& at(unsigned int i, unsigned int j) {
497
498 if (i >= rows()) {
499 throw std::out_of_range(
500 "The provided row index in mat::at() is out of range"
501 );
502 }
503
504 if (j >= cols()) {
505 throw std::out_of_range(
506 "The provided column index in mat::at() is out of range"
507 );
508 }
509
510 return get(i, j);
511 }
512
513
519 inline const Type& at(unsigned int i, unsigned int j) const {
520
521 if (i >= rows()) {
522 throw std::out_of_range(
523 "The provided row index in mat::at() is out of range"
524 );
525 }
526
527 if (j >= cols()) {
528 throw std::out_of_range(
529 "The provided column index in mat::at() is out of range"
530 );
531 }
532
533 return get(i, j);
534 }
535
536
541 inline Type& operator()(unsigned int i, unsigned int j) {
542 return get(i, j);
543 }
544
545
550 inline const Type& operator()(unsigned int i, unsigned int j) const {
551 return get(i, j);
552 }
553
554
556 using iterator = mat_iterator<mat<Type, N, K>, Type&>;
557
558
560 using const_iterator = mat_iterator<const mat<Type, N, K>, const Type&>;
561
562
565 inline auto begin() {
566 return iterator(*this, 0, 0);
567 }
568
569
572 inline auto end() {
573 return iterator(*this, rows(), 0);
574 }
575
576
579 inline auto begin() const {
580 return const_iterator(*this, 0, 0);
581 }
582
583
586 inline auto end() const {
587 return const_iterator(*this, rows(), 0);
588 }
589
590
593 TH_CONSTEXPR inline unsigned int rows() const {
594 return N;
595 }
596
597
600 TH_CONSTEXPR inline unsigned int cols() const {
601 return K;
602 }
603
604
607 inline unsigned int size() const {
608 return N * K;
609 }
610
611
616 template<typename Matrix>
617 inline bool operator==(const Matrix& other) const {
618 return algebra::mat_equals(*this, other);
619 }
620
621
626 template<typename Matrix>
627 inline bool operator!=(const Matrix& other) const {
628 return !algebra::mat_equals(*this, other);
629 }
630
631
636 inline mat<Type, N, K>& invert() {
637 static_assert(N == K, "The matrix must be square to be invertible.");
638 return algebra::invert(*this);
639 }
640
641
647 inline mat<Type, N, K> resize(unsigned int n, unsigned int k) const {
648
649 if(rows() != n) {
650 TH_MATH_ERROR("mat::resize", n, INVALID_ARGUMENT);
651 } else if(cols() != k) {
652 TH_MATH_ERROR("mat::resize", k, INVALID_ARGUMENT);
653 }
654
655 return *this;
656 }
657
658
659#ifndef THEORETICA_NO_PRINT
660
665 inline std::string to_string(
666 std::string separator = ", ", bool parenthesis = true) const {
667
668 std::stringstream res;
669
670 for (unsigned int i = 0; i < rows(); ++i) {
671
672 if(parenthesis)
673 res << "(";
674
675 for (unsigned int j = 0; j < cols(); ++j) {
676
677 if(j)
678 res << separator;
679
680 if(abs(get(i, j)) < MACH_EPSILON)
681 res << "0";
682 else
683 res << get(i, j);
684 }
685
686 if(parenthesis)
687 res << ")" << std::endl;
688 }
689
690 return res.str();
691 }
692
693
695 inline operator std::string() {
696 return to_string();
697 }
698
699
704 inline friend std::ostream& operator<<(
705 std::ostream& out, const mat<Type, N, K>& obj) {
706 return out << obj.to_string();
707 }
708
709#endif
710
711 };
712
713
714
720 template<typename Type>
721 class mat<Type, 0, 0> {
722 public:
723
725 std::vector<Type> data;
726
728 size_t row_sz {0};
729
731 size_t col_sz {0};
732
733
735 mat() : row_sz(0), col_sz(0) {}
736
737
741 mat(unsigned int n, unsigned int k) {
742 resize(n, k);
743 algebra::mat_zeroes(*this);
744 }
745
746
750 template <
751 typename Matrix, enable_matrix<Matrix>
752 >
753 mat(const Matrix& m) {
754 resize(m.rows(), m.cols());
755 algebra::mat_copy(*this, m);
756 }
757
758
762 template<typename T = Type>
763 inline mat(const std::initializer_list<std::initializer_list<T>>& rows) {
764
765 unsigned int i = 0;
766 unsigned int j = 0;
767
768 for (const auto& row : rows) {
769
770 for (const auto& x : row) {
771
772 if (!i && !j) {
773 this->resize(rows.size(), row.size());
774 }
775 else if (row.size() != col_sz) {
776 TH_MATH_ERROR("mat::mat", row.size(), INVALID_ARGUMENT);
777 algebra::mat_error(*this);
778 return;
779 }
780
781 get(i, j) = x;
782 j = (j + 1) % col_sz;
783 }
784
785 i++;
786 }
787 }
788
789
793 template<typename Matrix>
794 inline mat<Type>& operator=(const Matrix& other) {
795 resize(other.rows(), other.cols());
796 return algebra::mat_copy(*this, other);
797 }
798
799
804 mat(Type diagonal, unsigned int n, unsigned int k) {
805
806 resize(n, k);
807 algebra::mat_zeroes(*this);
808 const unsigned int m = min(n, k);
809
810 for (unsigned int i = 0; i < m; ++i)
811 get(i, i) = diagonal;
812 }
813
814
817 row_sz = 0;
818 col_sz = 0;
819 }
820
821
823 inline void make_zeroes() {
824 algebra::mat_zeroes(*this);
825 }
826
827
832 template<typename Matrix>
833 inline mat<Type> operator+(const Matrix& other) const {
834 mat<Type> res;
835 res.resize(rows(), cols());
836 return algebra::mat_sum(res, *this, other);
837 }
838
839
844 template<typename Matrix>
845 inline mat<Type> operator-(const Matrix& other) const {
846 mat<Type> res;
847 res.resize(rows(), cols());
848 return algebra::mat_diff(res, *this, other);
849 }
850
851
855 inline mat<Type> operator*(Type scalar) const {
856 mat<Type> res;
857 res.resize(rows(), cols());
858 return algebra::mat_scalmul(res, scalar, *this);
859 }
860
861
866 inline friend mat<Type> operator*(Type a, const mat<Type>& B) {
867 return B * a;
868 }
869
870
877 template<typename VecType, unsigned int M>
878 inline friend vec<VecType, 0> operator*(
879 const vec<VecType, M>& a, const mat<Type, 0, 0>& B) {
880 return B * a;
881 }
882
883
887 inline mat<Type> operator/(Type scalar) const {
888
889 mat<Type> res;
890 res.resize(rows(), cols());
891
892 if(abs(scalar) < MACH_EPSILON) {
893 TH_MATH_ERROR("mat::operator/", scalar, DIV_BY_ZERO);
894 return algebra::mat_error(res);
895 }
896
897 return algebra::mat_scalmul(res, 1.0 / scalar, *this);
898 }
899
900
905 template<typename Vector>
906 inline Vector transform(const Vector& v) const {
907
908 if(v.size() != rows()) {
909 TH_MATH_ERROR("mat::transform", v.size(), INVALID_ARGUMENT);
910 Vector res;
911 res.resize(rows());
912 return algebra::vec_error(res);
913 }
914
915 return algebra::transform(*this, v);
916 }
917
918
923 template<unsigned int N = 0, unsigned int K = 0>
924 inline vec<Type, N> transform(const vec<Type, K>& v) const {
925 return algebra::transform(*this, v);
926 }
927
928
933 template<unsigned int N = 0, unsigned int K = 0>
934 inline vec<Type, N> operator*(const vec<Type, K>& v) const {
935 return transform(v);
936 }
937
938
940 // inline vec<Type> operator*(const vec<Type>& v) const {
941 // return transform(v);
942 // }
943
944
956 inline mat<Type> mul(const mat<Type>& B) const {
957
958 mat<Type> res;
959 res.resize(rows(), B.cols());
960
961 if(B.rows() != cols()) {
962 TH_MATH_ERROR("mat::mul", B.rows(), INVALID_ARGUMENT);
964 return res;
965 }
966
967 return algebra::mat_mul(res, *this, B);
968 }
969
970
982 template<typename Matrix>
983 inline Matrix mul(const Matrix& B) const {
984
985 Matrix res;
986 res.resize(rows(), B.cols());
987
988 if(B.rows() != cols()) {
989 TH_MATH_ERROR("mat::mul", B.rows(), INVALID_ARGUMENT);
991 return res;
992 }
993
994 return algebra::mat_mul(res, *this, B);
995 }
996
997
1004 template<typename Matrix>
1005 inline auto operator*(const Matrix& B) const {
1006 return mul(B);
1007 }
1008
1009
1017 template<typename Matrix>
1018 inline mat<Type>& operator+=(const Matrix& other) {
1019 return algebra::mat_sum(*this, other);
1020 }
1021
1022
1030 template<typename Matrix>
1031 inline mat<Type>& operator-=(const Matrix& other) {
1032 return algebra::mat_diff(*this, other);
1033 }
1034
1035
1042 inline mat<Type>& operator*=(Type scalar) {
1043 return algebra::mat_scalmul(scalar, *this);
1044 }
1045
1046
1053 inline mat<Type>& operator/=(Type scalar) {
1054
1055 if(abs(scalar) < MACH_EPSILON) {
1056 TH_MATH_ERROR("mat::operator/", scalar, DIV_BY_ZERO);
1057 return algebra::mat_error(*this);
1058 }
1059
1060 return algebra::mat_scalmul(1.0 / scalar, *this);
1061 }
1062
1063
1071 template<typename Matrix>
1072 inline mat<Type>& operator*=(const Matrix& B) {
1073 return (*this = this->operator*(B));
1074 }
1075
1076
1083 inline mat<Type>& transpose() {
1084 return algebra::make_transposed(*this);
1085 }
1086
1087
1093 inline Type& get(unsigned int i, unsigned int j) {
1094
1095#ifdef THEORETICA_ROW_FIRST
1096 return data[j + i * row_sz];
1097#else
1098 return data[i + j * col_sz];
1099#endif
1100 }
1101
1102
1108 inline const Type& get(unsigned int i, unsigned int j) const {
1109
1110#ifdef THEORETICA_ROW_FIRST
1111 return data[j + i * row_sz];
1112#else
1113 return data[i + j * col_sz];
1114#endif
1115 }
1116
1117
1124 inline Type& at(unsigned int i, unsigned int j) {
1125
1126 if (i >= rows()) {
1127 throw std::out_of_range(
1128 "The provided row index in mat::at() is out of range"
1129 );
1130 }
1131
1132 if (j >= cols()) {
1133 throw std::out_of_range(
1134 "The provided column index in mat::at() is out of range"
1135 );
1136 }
1137
1138 return get(i, j);
1139 }
1140
1141
1148 inline const Type& at(unsigned int i, unsigned int j) const {
1149
1150 if (i >= rows()) {
1151 throw std::out_of_range(
1152 "The provided row index in mat::at() is out of range"
1153 );
1154 }
1155
1156 if (j >= cols()) {
1157 throw std::out_of_range(
1158 "The provided column index in mat::at() is out of range"
1159 );
1160 }
1161
1162 return get(i, j);
1163 }
1164
1165
1173 inline Type& operator()(unsigned int i, unsigned int j) {
1174 return get(i, j);
1175 }
1176
1177
1185 inline const Type& operator()(unsigned int i, unsigned int j) const {
1186 return get(i, j);
1187 }
1188
1189
1191 using iterator = mat_iterator<mat<Type, 0, 0>, Type&>;
1192
1193
1195 using const_iterator = mat_iterator<const mat<Type, 0, 0>, const Type&>;
1196
1197
1203 inline auto begin() {
1204 return iterator(*this, 0, 0);
1205 }
1206
1207
1214 inline auto end() {
1215 return iterator(*this, rows(), 0);
1216 }
1217
1218
1224 inline auto begin() const {
1225 return const_iterator(*this, 0, 0);
1226 }
1227
1228
1234 inline auto end() const {
1235 return const_iterator(*this, rows(), 0);
1236 }
1237
1238
1241 TH_CONSTEXPR inline unsigned int rows() const {
1242 return row_sz;
1243 }
1244
1245
1248 TH_CONSTEXPR inline unsigned int cols() const {
1249 return col_sz;
1250 }
1251
1252
1255 inline unsigned int size() const {
1256 return rows() * cols();
1257 }
1258
1259
1266 template<typename Matrix>
1267 inline bool operator==(const Matrix& other) const {
1268 return algebra::mat_equals(*this, other);
1269 }
1270
1271
1278 template<typename Matrix>
1279 inline bool operator!=(const Matrix& other) const {
1280 return !algebra::mat_equals(*this, other);
1281 }
1282
1283
1288 inline mat<Type>& invert() {
1289 return algebra::invert(*this);
1290 }
1291
1292
1296 inline mat<Type>& resize(unsigned int rows, unsigned int cols) {
1297
1298 // Do nothing if the size is already correct
1299 if (row_sz == rows && col_sz == cols)
1300 return *this;
1301
1302 std::vector<Type> new_data (rows * cols);
1303
1304 if (data.size()) {
1305
1306 size_t min_elements = min(rows, row_sz) * min(cols, col_sz);
1307
1308 // Copy the overlapping elements
1309 for (unsigned int i = 0; i < min_elements; ++i)
1310 new_data[i] = data[i];
1311 }
1312
1313 data = new_data;
1314 row_sz = rows;
1315 col_sz = cols;
1316
1317 return *this;
1318 }
1319
1320
1321#ifndef THEORETICA_NO_PRINT
1322
1324 inline std::string to_string(
1325 std::string separator = ", ", bool parenthesis = true) const {
1326
1327 std::stringstream res;
1328
1329 for (unsigned int i = 0; i < rows(); ++i) {
1330
1331 if(parenthesis)
1332 res << "(";
1333
1334 for (unsigned int j = 0; j < cols(); ++j) {
1335
1336 if(j)
1337 res << separator;
1338
1339 if(abs(get(i, j)) < MACH_EPSILON)
1340 res << "0";
1341 else
1342 res << get(i, j);
1343 }
1344
1345 if(parenthesis)
1346 res << ")" << std::endl;
1347 }
1348
1349 return res.str();
1350 }
1351
1352
1354 inline operator std::string() {
1355 return to_string();
1356 }
1357
1358
1360 inline friend std::ostream& operator<<(
1361 std::ostream& out, const mat<Type>& obj) {
1362 return out << obj.to_string();
1363 }
1364
1365#endif
1366
1367 };
1368
1369}
1370
1371#endif
Linear algebra routines.
friend std::ostream & operator<<(std::ostream &out, const mat< Type > &obj)
Stream the matrix in string representation to an output stream (std::ostream)
Definition mat.h:1360
mat_iterator< mat< Type, 0, 0 >, Type & > iterator
Iterator for dynamically allocated matrices.
Definition mat.h:1191
mat< Type > operator-(const Matrix &other) const
Subtracts another matrix element-wise.
Definition mat.h:845
auto operator*(const Matrix &B) const
Matrix multiplication with any matrix type.
Definition mat.h:1005
mat(unsigned int n, unsigned int k)
Constructor that initializes a matrix with the specified number of rows and columns.
Definition mat.h:741
mat< Type > & invert()
Invert a generic square matrix.
Definition mat.h:1288
mat< Type > operator/(Type scalar) const
Divides each element in the matrix by a scalar.
Definition mat.h:887
bool operator==(const Matrix &other) const
Check if two matrices are equal element by element.
Definition mat.h:1267
mat< Type > & operator/=(Type scalar)
Scalar division of the matrix.
Definition mat.h:1053
mat< Type > & operator+=(const Matrix &other)
Matrix addition with another matrix.
Definition mat.h:1018
mat(Type diagonal, unsigned int n, unsigned int k)
Constructor that initializes a diagonal matrix with equal entries on the diagonal.
Definition mat.h:804
vec< Type, N > transform(const vec< Type, K > &v) const
Transforms a vector by multiplying it with the matrix.
Definition mat.h:924
Type & at(unsigned int i, unsigned int j)
Access a modifiable element at a specific row and column.
Definition mat.h:1124
friend mat< Type > operator*(Type a, const mat< Type > &B)
Friend operator to enable equations of the form (T) * (mat)
Definition mat.h:866
bool operator!=(const Matrix &other) const
Check if two matrices are unequal element by element.
Definition mat.h:1279
mat< Type > & operator*=(const Matrix &B)
Matrix multiplication with any matrix type.
Definition mat.h:1072
mat_iterator< const mat< Type, 0, 0 >, const Type & > const_iterator
Const iterator for dynamically allocated matrices.
Definition mat.h:1195
const Type & at(unsigned int i, unsigned int j) const
Access a constant element at a specific row and column.
Definition mat.h:1148
mat(const std::initializer_list< std::initializer_list< T > > &rows)
Constructor that initializes a matrix from a list of rows.
Definition mat.h:763
mat(const Matrix &m)
Copy constructor for creating a matrix from another matrix.
Definition mat.h:753
mat< Type > & resize(unsigned int rows, unsigned int cols)
Set or change the size of the matrix.
Definition mat.h:1296
auto end() const
Get a const iterator to one past the last element of the matrix.
Definition mat.h:1234
TH_CONSTEXPR unsigned int cols() const
Get the number of columns in the matrix.
Definition mat.h:1248
friend vec< VecType, 0 > operator*(const vec< VecType, M > &a, const mat< Type, 0, 0 > &B)
Friend operator to enable equations of the form (vec) * (mat) (Enables vector-matrix multiplication....
Definition mat.h:878
TH_CONSTEXPR unsigned int rows() const
Get the number of rows in the matrix.
Definition mat.h:1241
Vector transform(const Vector &v) const
Transforms a vector by multiplying it with the matrix.
Definition mat.h:906
unsigned int size() const
Get the total number of elements of the matrix, equal to (rows * columns).
Definition mat.h:1255
vec< Type, N > operator*(const vec< Type, K > &v) const
Transforms a vector by multiplying it with the matrix.
Definition mat.h:934
Type & get(unsigned int i, unsigned int j)
Access the element at the given row and column, by reference.
Definition mat.h:1093
mat< Type > operator*(Type scalar) const
Multiplies the matrix by a scalar.
Definition mat.h:855
auto end()
Get an iterator to one past the last element of the matrix.
Definition mat.h:1214
mat< Type > & operator*=(Type scalar)
Scalar multiplication of the matrix.
Definition mat.h:1042
auto begin()
Get an iterator to the first element of the matrix.
Definition mat.h:1203
mat()
Default constructor.
Definition mat.h:735
mat< Type > & transpose()
Transpose the current matrix in place.
Definition mat.h:1083
std::string to_string(std::string separator=", ", bool parenthesis=true) const
Convert the matrix to string representation.
Definition mat.h:1324
mat< Type > & operator-=(const Matrix &other)
Matrix subtraction with another matrix.
Definition mat.h:1031
void make_zeroes()
Sets all elements in the matrix to zero.
Definition mat.h:823
mat< Type > operator+(const Matrix &other) const
Adds two matrices element-wise.
Definition mat.h:833
mat< Type > & operator=(const Matrix &other)
Copy assignment operator for copying from another matrix.
Definition mat.h:794
std::vector< Type > data
Dynamically allocated array of the elements.
Definition mat.h:725
mat< Type > mul(const mat< Type > &B) const
Transform a vector by the matrix.
Definition mat.h:956
Matrix mul(const Matrix &B) const
Multiplies the matrix by another matrix of any compatible type.
Definition mat.h:983
const Type & get(unsigned int i, unsigned int j) const
Get the element at the given row and column.
Definition mat.h:1108
auto begin() const
Get a const iterator to the first element of the matrix.
Definition mat.h:1224
Type & operator()(unsigned int i, unsigned int j)
Access a modifiable element at a specific row and column using the function call operator.
Definition mat.h:1173
~mat()
Destructor that resets the matrix size.
Definition mat.h:816
const Type & operator()(unsigned int i, unsigned int j) const
Access a constant element at a specific row and column using the function call operator.
Definition mat.h:1185
A sequential iterator for matrices.
Definition mat.h:34
mat_iterator & operator++()
Advances the iterator to the next element in row-major order.
Definition mat.h:84
mat_iterator(Matrix &matrix, size_t row=0, size_t col=0)
Constructs an iterator for a matrix, optionally starting at a specified row and column.
Definition mat.h:63
size_t col_index()
Retrieves the current column index of the iterator.
Definition mat.h:106
bool operator!=(const mat_iterator &other) const
Inequality operator to compare two iterators.
Definition mat.h:123
ReturnType operator*()
Dereferences the iterator to access the current matrix element.
Definition mat.h:74
bool operator==(const mat_iterator &other) const
Equality operator to compare two iterators.
Definition mat.h:114
size_t row_index()
Retrieves the current row index of the iterator.
Definition mat.h:99
A generic matrix with a fixed number of rows and columns.
Definition mat.h:136
mat< Type, N, K > operator+(const Matrix &other) const
Matrix addition.
Definition mat.h:237
mat< Type, N, K > & operator=(const Matrix &other)
Assignment operator to copy from another matrix.
Definition mat.h:227
mat< Type, N, M > mul(const mat< Type, K, M > &B) const
Matrix multiplication for matrices with different column counts.
Definition mat.h:346
std::string to_string(std::string separator=", ", bool parenthesis=true) const
Converts the matrix to a string representation.
Definition mat.h:665
mat< Type, N, K > operator*(Type scalar) const
Scalar multiplication.
Definition mat.h:257
const Type & get(unsigned int i, unsigned int j) const
Get the element at the given row and column.
Definition mat.h:481
mat(const std::initializer_list< std::initializer_list< T > > &rows)
Constructs a matrix from an initializer list.
Definition mat.h:172
bool operator!=(const Matrix &other) const
Checks whether this matrix is not equal to another matrix element-wise.
Definition mat.h:627
mat< Type, N, K > operator-(const Matrix &other) const
Matrix subtraction.
Definition mat.h:248
auto end() const
Returns a const iterator to one past the last element of the matrix.
Definition mat.h:586
mat_iterator< mat< Type, N, K >, Type & > iterator
Iterator for statically allocated matrices.
Definition mat.h:556
friend std::ostream & operator<<(std::ostream &out, const mat< Type, N, K > &obj)
Outputs the matrix to an output stream in string format.
Definition mat.h:704
mat< Type, N, K > & invert()
Inverts the matrix in place.
Definition mat.h:636
vec< Type, N > transform(const vec< Type, K > &v) const
Transforms a fixed-size vector by multiplying it with the matrix.
Definition mat.h:328
mat< Type, N, K > & operator/=(Type scalar)
Scalar division.
Definition mat.h:428
mat< Type, N, K > resize(unsigned int n, unsigned int k) const
Compatibility function to allow for allocation or resizing of dynamic matrices.
Definition mat.h:647
friend vec< VecType, K > operator*(const vec< VecType, M > &a, const mat< Type, N, K > &B)
Friend operator for vector-matrix multiplication.
Definition mat.h:279
mat< Type, N, K > & operator*=(const Matrix &B)
Matrix multiplication with an assignment operator.
Definition mat.h:444
mat< Type, N, K > & transpose()
Transposes the matrix in place.
Definition mat.h:454
TH_CONSTEXPR unsigned int rows() const
Returns the number of rows in the matrix.
Definition mat.h:593
mat< Type, N, K > & operator*=(Type scalar)
Scalar multiplication.
Definition mat.h:418
mat(Type diagonal, unsigned int n=0, unsigned int k=0)
Constructor that initializes a diagonal matrix with equal entries on the diagonal.
Definition mat.h:209
Type & at(unsigned int i, unsigned int j)
Accesses the element at the given row and column with bound checking.
Definition mat.h:496
mat_iterator< const mat< Type, N, K >, const Type & > const_iterator
Const iterator for statically allocated matrices.
Definition mat.h:560
unsigned int size() const
Returns the total number of elements in the matrix.
Definition mat.h:607
Type & get(unsigned int i, unsigned int j)
Access the element at the given row and column, by reference.
Definition mat.h:466
TH_CONSTEXPR unsigned int cols() const
Returns the number of columns in the matrix.
Definition mat.h:600
mat< Type, N, K > & operator-=(const Matrix &other)
Matrix subtraction.
Definition mat.h:410
mat()
Default constructor.
Definition mat.h:149
auto end()
Returns an iterator to one past the last element of the matrix.
Definition mat.h:572
friend mat< Type, N, K > operator*(Type a, const mat< Type, N, K > &B)
Friend operator for scalar multiplication (T * mat).
Definition mat.h:267
auto begin() const
Returns a const iterator to the first element of the matrix.
Definition mat.h:579
mat< Type, N, K > & operator+=(const Matrix &other)
Matrix addition.
Definition mat.h:400
mat(const Matrix &m)
Copy constructor.
Definition mat.h:160
vec< Type, N > operator*(const vec< Type, K > &v) const
Overloads the * operator to transform a fixed-size vector by the matrix.
Definition mat.h:336
auto operator*(const Matrix &B) const
Overloads the * operator for matrix multiplication.
Definition mat.h:380
Vector transform(const Vector &v) const
Transforms a vector by multiplying it with the matrix.
Definition mat.h:311
Matrix mul(const Matrix &B) const
Matrix multiplication for matrices with any type.
Definition mat.h:360
mat< Type, N, K > operator/(Type scalar) const
Scalar division.
Definition mat.h:290
bool operator==(const Matrix &other) const
Checks whether this matrix is equal to another matrix element-wise.
Definition mat.h:617
const Type & at(unsigned int i, unsigned int j) const
Accesses the element at the given row and column with bound checking.
Definition mat.h:519
const Type & operator()(unsigned int i, unsigned int j) const
Overloads the () operator to access an element by value.
Definition mat.h:550
Type & operator()(unsigned int i, unsigned int j)
Overloads the () operator to access an element by reference.
Definition mat.h:541
auto begin()
Returns an iterator to the first element of the matrix.
Definition mat.h:565
#define TH_CONSTEXPR
Enable constexpr in function declarations if C++14 is supported.
Definition constants.h:161
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compiling opti...
Definition error.h:225
Matrix2 & mat_sum(Matrix1 &A, const Matrix2 &B)
Sum two matrices and store the result in the first matrix.
Definition algebra.h:764
Matrix & mat_scalmul(Field a, Matrix &m)
Multiply a matrix by a scalar of any compatible type.
Definition algebra.h:590
Matrix & invert(Matrix &m)
Invert the given matrix and overwrite it.
Definition algebra.h:1997
Matrix2 & mat_diff(Matrix1 &A, const Matrix2 &B)
Subtract two matrices and store the result in the first matrix.
Definition algebra.h:832
bool mat_equals(const Matrix1 &A, const Matrix2 &B, real tolerance=10 *MACH_EPSILON)
Checks whether two matrices are equal.
Definition algebra.h:1109
Vector transform(const Matrix &A, const Vector &v)
Returns the matrix transformation of a vector.
Definition algebra.h:702
Matrix & mat_error(Matrix &m)
Overwrite the given matrix with the error matrix with NaN values on the diagonal and zeroes everywher...
Definition algebra.h:44
Matrix3 mat_mul(const Matrix1 &A, const Matrix2 &B)
Multiply two matrices and store the result in the first matrix, equivalent to the operation .
Definition algebra.h:977
Matrix & mat_zeroes(Matrix &m)
Overwrite a matrix with all zeroes.
Definition algebra.h:93
Matrix1 & mat_copy(Matrix1 &dest, const Matrix2 &src)
Copy a matrix by overwriting another.
Definition algebra.h:125
Matrix & make_transposed(Matrix &m)
Transpose the given matrix.
Definition algebra.h:420
Vector & vec_error(Vector &v)
Overwrite the given vector with the error vector with NaN values.
Definition algebra.h:62
Main namespace of the library which contains all functions and objects.
Definition algebra.h:27
auto min(const Vector &X)
Finds the minimum value inside a dataset.
Definition dataset.h:351
dual2 abs(dual2 x)
Compute the absolute value of a second order dual number.
Definition dual2_functions.h:198
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition constants.h:207
Linear transformations such as rotations and projective geometry.
Vector class and operations.