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(), MathError::InvalidArgument);
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(), MathError::InvalidArgument);
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, unsigned int k) {
210
211 if (n && k)
212 resize(n, k);
213
214 if (n != rows() && k != cols()) {
216 return;
217 }
218
219 algebra::mat_zeroes(*this);
220 const unsigned int m = min(rows(), cols());
221
222 for (unsigned int i = 0; i < m; ++i)
223 get(i, i) = diagonal;
224 }
225
226
230 mat(Type diagonal) {
231
232 algebra::mat_zeroes(*this);
233 const unsigned int m = min(rows(), cols());
234
235 for (unsigned int i = 0; i < m; ++i)
236 get(i, i) = diagonal;
237 }
238
239
244 template<typename Matrix>
245 inline mat<Type, N, K>& operator=(const Matrix& other) {
246 return algebra::mat_copy(*this, other);
247 }
248
249
254 template<typename Matrix>
255 inline mat<Type, N, K> operator+(const Matrix& other) const {
256 mat<Type, N, K> res;
257 return algebra::mat_sum(res, *this, other);
258 }
259
260
265 template<typename Matrix>
266 inline mat<Type, N, K> operator-(const Matrix& other) const {
267 mat<Type, N, K> res;
268 return algebra::mat_diff(res, *this, other);
269 }
270
271
275 inline mat<Type, N, K> operator*(Type scalar) const {
276 mat<Type, N, K> res;
277 return algebra::mat_scalmul(res, scalar, *this);
278 }
279
280
285 inline friend mat<Type, N, K> operator*(Type a, const mat<Type, N, K>& B) {
286 return B * a;
287 }
288
289
296 template<typename VecType, unsigned int M>
297 inline friend vec<VecType, K> operator*(
298 const vec<VecType, M>& a, const mat<Type, N, K>& B) {
299 return B * a;
300 }
301
302
308 inline mat<Type, N, K> operator/(Type scalar) const {
309
310 mat<Type, N, K> res;
311
312 if(abs(scalar) < MACH_EPSILON) {
313 TH_MATH_ERROR("mat::operator/", scalar, MathError::DivByZero);
314 return algebra::mat_error(res);
315 }
316
317 return algebra::mat_scalmul(res, 1.0 / scalar, *this);
318 }
319
320
328 template<typename Vector>
329 inline Vector transform(const Vector& v) const {
330
331 if(v.size() != cols()) {
332 TH_MATH_ERROR("mat::transform", v.size(), MathError::InvalidArgument);
333 Vector res;
334 res.resize(cols());
336 return res;
337 }
338
339 return algebra::transform(*this, v);
340 }
341
342
346 inline vec<Type, N> transform(const vec<Type, K>& v) const {
347 return algebra::transform(*this, v);
348 }
349
350
354 inline vec<Type, N> operator*(const vec<Type, K>& v) const {
355 return transform(v);
356 }
357
358
363 template<unsigned int M>
364 inline mat<Type, N, M> mul(const mat<Type, K, M>& B) const {
365 mat<Type, N, M> res;
366 return algebra::mat_mul(res, *this, B);
367 }
368
369
377 template<typename Matrix>
378 inline Matrix mul(const Matrix& B) const {
379
380 Matrix res;
381 res.resize(N, B.cols());
382
383 if(B.rows() != K) {
384 TH_MATH_ERROR("mat::transform", B.rows(), MathError::InvalidArgument);
386 return res;
387 }
388
389 return algebra::mat_mul(res, *this, B);
390 }
391
392
397 template<typename Matrix>
398 inline auto operator*(const Matrix& B) const {
399
400 Matrix res;
401 res.resize(N, B.cols());
402
403 if(B.rows() != K) {
404 TH_MATH_ERROR("mat::transform", B.rows(), MathError::InvalidArgument);
406 return res;
407 }
408
409 return algebra::mat_mul(res, *this, B);
410 }
411
412
417 template<typename Matrix>
418 inline mat<Type, N, K>& operator+=(const Matrix& other) {
419 return algebra::mat_sum(*this, other);
420 }
421
422
427 template<typename Matrix>
428 inline mat<Type, N, K>& operator-=(const Matrix& other) {
429 return algebra::mat_diff(*this, other);
430 }
431
432
436 inline mat<Type, N, K>& operator*=(Type scalar) {
437 return algebra::mat_scalmul(scalar, *this);
438 }
439
440
446 inline mat<Type, N, K>& operator/=(Type scalar) {
447
448 if(abs(scalar) < MACH_EPSILON) {
449 TH_MATH_ERROR("mat::operator/", scalar, MathError::DivByZero);
450 return algebra::mat_error(*this);
451 }
452
453 return algebra::mat_scalmul(1.0 / scalar, *this);
454 }
455
456
461 template<typename Matrix>
462 inline mat<Type, N, K>& operator*=(const Matrix& B) {
463 return (*this = this->operator*(B));
464 }
465
466
472 inline mat<Type, N, K>& transpose() {
473 static_assert(
474 N == K, "The matrix must be square to be transposed in place.");
475 return algebra::make_transposed(*this);
476 }
477
478
484 inline Type& get(unsigned int i, unsigned int j) {
485
486#ifdef THEORETICA_ROW_FIRST
487 return data[i][j];
488#else
489 return data[j][i];
490#endif
491 }
492
493
499 inline const Type& get(unsigned int i, unsigned int j) const {
500
501#ifdef THEORETICA_ROW_FIRST
502 return data[i][j];
503#else
504 return data[j][i];
505#endif
506 }
507
508
514 inline Type& at(unsigned int i, unsigned int j) {
515
516 if (i >= rows()) {
517 throw std::out_of_range(
518 "The provided row index in mat::at() is out of range"
519 );
520 }
521
522 if (j >= cols()) {
523 throw std::out_of_range(
524 "The provided column index in mat::at() is out of range"
525 );
526 }
527
528 return get(i, j);
529 }
530
531
537 inline const Type& at(unsigned int i, unsigned int j) const {
538
539 if (i >= rows()) {
540 throw std::out_of_range(
541 "The provided row index in mat::at() is out of range"
542 );
543 }
544
545 if (j >= cols()) {
546 throw std::out_of_range(
547 "The provided column index in mat::at() is out of range"
548 );
549 }
550
551 return get(i, j);
552 }
553
554
559 inline Type& operator()(unsigned int i, unsigned int j) {
560 return get(i, j);
561 }
562
563
568 inline const Type& operator()(unsigned int i, unsigned int j) const {
569 return get(i, j);
570 }
571
572
574 using iterator = mat_iterator<mat<Type, N, K>, Type&>;
575
576
578 using const_iterator = mat_iterator<const mat<Type, N, K>, const Type&>;
579
580
583 inline auto begin() {
584 return iterator(*this, 0, 0);
585 }
586
587
590 inline auto end() {
591 return iterator(*this, rows(), 0);
592 }
593
594
597 inline auto begin() const {
598 return const_iterator(*this, 0, 0);
599 }
600
601
604 inline auto end() const {
605 return const_iterator(*this, rows(), 0);
606 }
607
608
611 TH_CONSTEXPR inline unsigned int rows() const {
612 return N;
613 }
614
615
618 TH_CONSTEXPR inline unsigned int cols() const {
619 return K;
620 }
621
622
625 inline unsigned int size() const {
626 return N * K;
627 }
628
629
634 template<typename Matrix>
635 inline bool operator==(const Matrix& other) const {
636 return algebra::mat_equals(*this, other);
637 }
638
639
644 template<typename Matrix>
645 inline bool operator!=(const Matrix& other) const {
646 return !algebra::mat_equals(*this, other);
647 }
648
649
654 inline mat<Type, N, K>& invert() {
655 static_assert(N == K, "The matrix must be square to be invertible.");
656 return algebra::invert(*this);
657 }
658
659
665 inline mat<Type, N, K> resize(unsigned int n, unsigned int k) const {
666
667 if(rows() != n) {
668 TH_MATH_ERROR("mat::resize", n, MathError::InvalidArgument);
669 } else if(cols() != k) {
670 TH_MATH_ERROR("mat::resize", k, MathError::InvalidArgument);
671 }
672
673 return *this;
674 }
675
676
677#ifndef THEORETICA_NO_PRINT
678
683 inline std::string to_string(
684 std::string separator = ", ", bool parenthesis = true) const {
685
686 std::stringstream res;
687
688 for (unsigned int i = 0; i < rows(); ++i) {
689
690 if(parenthesis)
691 res << "(";
692
693 for (unsigned int j = 0; j < cols(); ++j) {
694
695 if(j)
696 res << separator;
697
698 if(abs(get(i, j)) < MACH_EPSILON)
699 res << "0";
700 else
701 res << get(i, j);
702 }
703
704 if(parenthesis)
705 res << ")" << std::endl;
706 }
707
708 return res.str();
709 }
710
711
713 inline operator std::string() {
714 return to_string();
715 }
716
717
722 inline friend std::ostream& operator<<(
723 std::ostream& out, const mat<Type, N, K>& obj) {
724 return out << obj.to_string();
725 }
726
727#endif
728
729 };
730
731
732
738 template<typename Type>
739 class mat<Type, 0, 0> {
740 public:
741
743 std::vector<Type> data;
744
746 size_t row_sz {0};
747
749 size_t col_sz {0};
750
751
753 mat() : row_sz(0), col_sz(0) {}
754
755
759 mat(unsigned int n, unsigned int k) {
760 resize(n, k);
761 algebra::mat_zeroes(*this);
762 }
763
764
768 template <
769 typename Matrix, enable_matrix<Matrix>
770 >
771 mat(const Matrix& m) {
772 resize(m.rows(), m.cols());
773 algebra::mat_copy(*this, m);
774 }
775
776
780 template<typename T = Type>
781 inline mat(const std::initializer_list<std::initializer_list<T>>& rows) {
782
783 unsigned int i = 0;
784 unsigned int j = 0;
785
786 for (const auto& row : rows) {
787
788 for (const auto& x : row) {
789
790 if (!i && !j) {
791 this->resize(rows.size(), row.size());
792 }
793 else if (row.size() != col_sz) {
794 TH_MATH_ERROR("mat::mat", row.size(), MathError::InvalidArgument);
795 algebra::mat_error(*this);
796 return;
797 }
798
799 get(i, j) = x;
800 j = (j + 1) % col_sz;
801 }
802
803 i++;
804 }
805 }
806
807
811 template<typename Matrix>
812 inline mat<Type>& operator=(const Matrix& other) {
813 resize(other.rows(), other.cols());
814 return algebra::mat_copy(*this, other);
815 }
816
817
822 mat(Type diagonal, unsigned int n, unsigned int k) {
823
824 resize(n, k);
825 algebra::mat_zeroes(*this);
826 const unsigned int m = min(n, k);
827
828 for (unsigned int i = 0; i < m; ++i)
829 get(i, i) = diagonal;
830 }
831
832
835 row_sz = 0;
836 col_sz = 0;
837 }
838
839
841 inline void make_zeroes() {
842 algebra::mat_zeroes(*this);
843 }
844
845
850 template<typename Matrix>
851 inline mat<Type> operator+(const Matrix& other) const {
852 mat<Type> res;
853 res.resize(rows(), cols());
854 return algebra::mat_sum(res, *this, other);
855 }
856
857
862 template<typename Matrix>
863 inline mat<Type> operator-(const Matrix& other) const {
864 mat<Type> res;
865 res.resize(rows(), cols());
866 return algebra::mat_diff(res, *this, other);
867 }
868
869
873 inline mat<Type> operator*(Type scalar) const {
874 mat<Type> res;
875 res.resize(rows(), cols());
876 return algebra::mat_scalmul(res, scalar, *this);
877 }
878
879
884 inline friend mat<Type> operator*(Type a, const mat<Type>& B) {
885 return B * a;
886 }
887
888
895 template<typename VecType, unsigned int M>
896 inline friend vec<VecType, 0> operator*(
897 const vec<VecType, M>& a, const mat<Type, 0, 0>& B) {
898 return B * a;
899 }
900
901
905 inline mat<Type> operator/(Type scalar) const {
906
907 mat<Type> res;
908 res.resize(rows(), cols());
909
910 if(abs(scalar) < MACH_EPSILON) {
911 TH_MATH_ERROR("mat::operator/", scalar, MathError::DivByZero);
912 return algebra::mat_error(res);
913 }
914
915 return algebra::mat_scalmul(res, 1.0 / scalar, *this);
916 }
917
918
923 template<typename Vector>
924 inline Vector transform(const Vector& v) const {
925
926 if(v.size() != rows()) {
927 TH_MATH_ERROR("mat::transform", v.size(), MathError::InvalidArgument);
928 Vector res;
929 res.resize(rows());
930 return algebra::vec_error(res);
931 }
932
933 return algebra::transform(*this, v);
934 }
935
936
941 template<unsigned int N = 0, unsigned int K = 0>
942 inline vec<Type, N> transform(const vec<Type, K>& v) const {
943 return algebra::transform(*this, v);
944 }
945
946
951 template<unsigned int N = 0, unsigned int K = 0>
952 inline vec<Type, N> operator*(const vec<Type, K>& v) const {
953 return transform(v);
954 }
955
956
958 // inline vec<Type> operator*(const vec<Type>& v) const {
959 // return transform(v);
960 // }
961
962
974 inline mat<Type> mul(const mat<Type>& B) const {
975
976 mat<Type> res;
977 res.resize(rows(), B.cols());
978
979 if(B.rows() != cols()) {
982 return res;
983 }
984
985 return algebra::mat_mul(res, *this, B);
986 }
987
988
1000 template<typename Matrix>
1001 inline Matrix mul(const Matrix& B) const {
1002
1003 Matrix res;
1004 res.resize(rows(), B.cols());
1005
1006 if(B.rows() != cols()) {
1007 TH_MATH_ERROR("mat::mul", B.rows(), MathError::InvalidArgument);
1008 algebra::mat_error(res);
1009 return res;
1010 }
1011
1012 return algebra::mat_mul(res, *this, B);
1013 }
1014
1015
1022 template<typename Matrix>
1023 inline auto operator*(const Matrix& B) const {
1024 return mul(B);
1025 }
1026
1027
1035 template<typename Matrix>
1036 inline mat<Type>& operator+=(const Matrix& other) {
1037 return algebra::mat_sum(*this, other);
1038 }
1039
1040
1048 template<typename Matrix>
1049 inline mat<Type>& operator-=(const Matrix& other) {
1050 return algebra::mat_diff(*this, other);
1051 }
1052
1053
1060 inline mat<Type>& operator*=(Type scalar) {
1061 return algebra::mat_scalmul(scalar, *this);
1062 }
1063
1064
1071 inline mat<Type>& operator/=(Type scalar) {
1072
1073 if(abs(scalar) < MACH_EPSILON) {
1074 TH_MATH_ERROR("mat::operator/", scalar, MathError::DivByZero);
1075 return algebra::mat_error(*this);
1076 }
1077
1078 return algebra::mat_scalmul(1.0 / scalar, *this);
1079 }
1080
1081
1089 template<typename Matrix>
1090 inline mat<Type>& operator*=(const Matrix& B) {
1091 return (*this = this->operator*(B));
1092 }
1093
1094
1101 inline mat<Type>& transpose() {
1102 return algebra::make_transposed(*this);
1103 }
1104
1105
1111 inline Type& get(unsigned int i, unsigned int j) {
1112
1113#ifdef THEORETICA_ROW_FIRST
1114 return data[j + i * row_sz];
1115#else
1116 return data[i + j * col_sz];
1117#endif
1118 }
1119
1120
1126 inline const Type& get(unsigned int i, unsigned int j) const {
1127
1128#ifdef THEORETICA_ROW_FIRST
1129 return data[j + i * row_sz];
1130#else
1131 return data[i + j * col_sz];
1132#endif
1133 }
1134
1135
1142 inline Type& at(unsigned int i, unsigned int j) {
1143
1144 if (i >= rows()) {
1145 throw std::out_of_range(
1146 "The provided row index in mat::at() is out of range"
1147 );
1148 }
1149
1150 if (j >= cols()) {
1151 throw std::out_of_range(
1152 "The provided column index in mat::at() is out of range"
1153 );
1154 }
1155
1156 return get(i, j);
1157 }
1158
1159
1166 inline const Type& at(unsigned int i, unsigned int j) const {
1167
1168 if (i >= rows()) {
1169 throw std::out_of_range(
1170 "The provided row index in mat::at() is out of range"
1171 );
1172 }
1173
1174 if (j >= cols()) {
1175 throw std::out_of_range(
1176 "The provided column index in mat::at() is out of range"
1177 );
1178 }
1179
1180 return get(i, j);
1181 }
1182
1183
1191 inline Type& operator()(unsigned int i, unsigned int j) {
1192 return get(i, j);
1193 }
1194
1195
1203 inline const Type& operator()(unsigned int i, unsigned int j) const {
1204 return get(i, j);
1205 }
1206
1207
1209 using iterator = mat_iterator<mat<Type, 0, 0>, Type&>;
1210
1211
1213 using const_iterator = mat_iterator<const mat<Type, 0, 0>, const Type&>;
1214
1215
1221 inline auto begin() {
1222 return iterator(*this, 0, 0);
1223 }
1224
1225
1232 inline auto end() {
1233 return iterator(*this, rows(), 0);
1234 }
1235
1236
1242 inline auto begin() const {
1243 return const_iterator(*this, 0, 0);
1244 }
1245
1246
1252 inline auto end() const {
1253 return const_iterator(*this, rows(), 0);
1254 }
1255
1256
1259 TH_CONSTEXPR inline unsigned int rows() const {
1260 return row_sz;
1261 }
1262
1263
1266 TH_CONSTEXPR inline unsigned int cols() const {
1267 return col_sz;
1268 }
1269
1270
1273 inline unsigned int size() const {
1274 return rows() * cols();
1275 }
1276
1277
1284 template<typename Matrix>
1285 inline bool operator==(const Matrix& other) const {
1286 return algebra::mat_equals(*this, other);
1287 }
1288
1289
1296 template<typename Matrix>
1297 inline bool operator!=(const Matrix& other) const {
1298 return !algebra::mat_equals(*this, other);
1299 }
1300
1301
1306 inline mat<Type>& invert() {
1307 return algebra::invert(*this);
1308 }
1309
1310
1314 inline mat<Type>& resize(unsigned int rows, unsigned int cols) {
1315
1316 // Do nothing if the size is already correct
1317 if (row_sz == rows && col_sz == cols)
1318 return *this;
1319
1320 std::vector<Type> new_data (rows * cols);
1321
1322 if (data.size()) {
1323
1324 size_t min_elements = min(rows, row_sz) * min(cols, col_sz);
1325
1326 // Copy the overlapping elements
1327 for (unsigned int i = 0; i < min_elements; ++i)
1328 new_data[i] = data[i];
1329 }
1330
1331 data = new_data;
1332 row_sz = rows;
1333 col_sz = cols;
1334
1335 return *this;
1336 }
1337
1338
1339#ifndef THEORETICA_NO_PRINT
1340
1342 inline std::string to_string(
1343 std::string separator = ", ", bool parenthesis = true) const {
1344
1345 std::stringstream res;
1346
1347 for (unsigned int i = 0; i < rows(); ++i) {
1348
1349 if(parenthesis)
1350 res << "(";
1351
1352 for (unsigned int j = 0; j < cols(); ++j) {
1353
1354 if(j)
1355 res << separator;
1356
1357 if(abs(get(i, j)) < MACH_EPSILON)
1358 res << "0";
1359 else
1360 res << get(i, j);
1361 }
1362
1363 if(parenthesis)
1364 res << ")" << std::endl;
1365 }
1366
1367 return res.str();
1368 }
1369
1370
1372 inline operator std::string() {
1373 return to_string();
1374 }
1375
1376
1378 inline friend std::ostream& operator<<(
1379 std::ostream& out, const mat<Type>& obj) {
1380 return out << obj.to_string();
1381 }
1382
1383#endif
1384
1385 };
1386
1387}
1388
1389#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:1378
mat_iterator< mat< Type, 0, 0 >, Type & > iterator
Iterator for dynamically allocated matrices.
Definition mat.h:1209
mat< Type > operator-(const Matrix &other) const
Subtracts another matrix element-wise.
Definition mat.h:863
auto operator*(const Matrix &B) const
Matrix multiplication with any matrix type.
Definition mat.h:1023
mat(unsigned int n, unsigned int k)
Constructor that initializes a matrix with the specified number of rows and columns.
Definition mat.h:759
mat< Type > & invert()
Invert a generic square matrix.
Definition mat.h:1306
mat< Type > operator/(Type scalar) const
Divides each element in the matrix by a scalar.
Definition mat.h:905
bool operator==(const Matrix &other) const
Check if two matrices are equal element by element.
Definition mat.h:1285
mat< Type > & operator/=(Type scalar)
Scalar division of the matrix.
Definition mat.h:1071
mat< Type > & operator+=(const Matrix &other)
Matrix addition with another matrix.
Definition mat.h:1036
mat(Type diagonal, unsigned int n, unsigned int k)
Constructor that initializes a diagonal matrix with equal entries on the diagonal.
Definition mat.h:822
vec< Type, N > transform(const vec< Type, K > &v) const
Transforms a vector by multiplying it with the matrix.
Definition mat.h:942
Type & at(unsigned int i, unsigned int j)
Access a modifiable element at a specific row and column.
Definition mat.h:1142
friend mat< Type > operator*(Type a, const mat< Type > &B)
Friend operator to enable equations of the form (T) * (mat)
Definition mat.h:884
bool operator!=(const Matrix &other) const
Check if two matrices are unequal element by element.
Definition mat.h:1297
mat< Type > & operator*=(const Matrix &B)
Matrix multiplication with any matrix type.
Definition mat.h:1090
mat_iterator< const mat< Type, 0, 0 >, const Type & > const_iterator
Const iterator for dynamically allocated matrices.
Definition mat.h:1213
const Type & at(unsigned int i, unsigned int j) const
Access a constant element at a specific row and column.
Definition mat.h:1166
mat(const std::initializer_list< std::initializer_list< T > > &rows)
Constructor that initializes a matrix from a list of rows.
Definition mat.h:781
mat(const Matrix &m)
Copy constructor for creating a matrix from another matrix.
Definition mat.h:771
mat< Type > & resize(unsigned int rows, unsigned int cols)
Set or change the size of the matrix.
Definition mat.h:1314
auto end() const
Get a const iterator to one past the last element of the matrix.
Definition mat.h:1252
TH_CONSTEXPR unsigned int cols() const
Get the number of columns in the matrix.
Definition mat.h:1266
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:896
TH_CONSTEXPR unsigned int rows() const
Get the number of rows in the matrix.
Definition mat.h:1259
Vector transform(const Vector &v) const
Transforms a vector by multiplying it with the matrix.
Definition mat.h:924
unsigned int size() const
Get the total number of elements of the matrix, equal to (rows * columns).
Definition mat.h:1273
vec< Type, N > operator*(const vec< Type, K > &v) const
Transforms a vector by multiplying it with the matrix.
Definition mat.h:952
Type & get(unsigned int i, unsigned int j)
Access the element at the given row and column, by reference.
Definition mat.h:1111
mat< Type > operator*(Type scalar) const
Multiplies the matrix by a scalar.
Definition mat.h:873
auto end()
Get an iterator to one past the last element of the matrix.
Definition mat.h:1232
mat< Type > & operator*=(Type scalar)
Scalar multiplication of the matrix.
Definition mat.h:1060
auto begin()
Get an iterator to the first element of the matrix.
Definition mat.h:1221
mat()
Default constructor.
Definition mat.h:753
mat< Type > & transpose()
Transpose the current matrix in place.
Definition mat.h:1101
std::string to_string(std::string separator=", ", bool parenthesis=true) const
Convert the matrix to string representation.
Definition mat.h:1342
mat< Type > & operator-=(const Matrix &other)
Matrix subtraction with another matrix.
Definition mat.h:1049
void make_zeroes()
Sets all elements in the matrix to zero.
Definition mat.h:841
mat< Type > operator+(const Matrix &other) const
Adds two matrices element-wise.
Definition mat.h:851
mat< Type > & operator=(const Matrix &other)
Copy assignment operator for copying from another matrix.
Definition mat.h:812
std::vector< Type > data
Dynamically allocated array of the elements.
Definition mat.h:743
mat< Type > mul(const mat< Type > &B) const
Transform a vector by the matrix.
Definition mat.h:974
Matrix mul(const Matrix &B) const
Multiplies the matrix by another matrix of any compatible type.
Definition mat.h:1001
const Type & get(unsigned int i, unsigned int j) const
Get the element at the given row and column.
Definition mat.h:1126
auto begin() const
Get a const iterator to the first element of the matrix.
Definition mat.h:1242
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:1191
~mat()
Destructor that resets the matrix size.
Definition mat.h:834
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:1203
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:255
mat< Type, N, K > & operator=(const Matrix &other)
Assignment operator to copy from another matrix.
Definition mat.h:245
mat< Type, N, M > mul(const mat< Type, K, M > &B) const
Matrix multiplication for matrices with different column counts.
Definition mat.h:364
std::string to_string(std::string separator=", ", bool parenthesis=true) const
Converts the matrix to a string representation.
Definition mat.h:683
mat< Type, N, K > operator*(Type scalar) const
Scalar multiplication.
Definition mat.h:275
const Type & get(unsigned int i, unsigned int j) const
Get the element at the given row and column.
Definition mat.h:499
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:645
mat< Type, N, K > operator-(const Matrix &other) const
Matrix subtraction.
Definition mat.h:266
auto end() const
Returns a const iterator to one past the last element of the matrix.
Definition mat.h:604
mat_iterator< mat< Type, N, K >, Type & > iterator
Iterator for statically allocated matrices.
Definition mat.h:574
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:722
mat< Type, N, K > & invert()
Inverts the matrix in place.
Definition mat.h:654
vec< Type, N > transform(const vec< Type, K > &v) const
Transforms a fixed-size vector by multiplying it with the matrix.
Definition mat.h:346
mat(Type diagonal)
Constructor that initializes a diagonal matrix with equal entries on the diagonal.
Definition mat.h:230
mat< Type, N, K > & operator/=(Type scalar)
Scalar division.
Definition mat.h:446
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:665
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:297
mat< Type, N, K > & operator*=(const Matrix &B)
Matrix multiplication with an assignment operator.
Definition mat.h:462
mat< Type, N, K > & transpose()
Transposes the matrix in place.
Definition mat.h:472
TH_CONSTEXPR unsigned int rows() const
Returns the number of rows in the matrix.
Definition mat.h:611
mat< Type, N, K > & operator*=(Type scalar)
Scalar multiplication.
Definition mat.h:436
Type & at(unsigned int i, unsigned int j)
Accesses the element at the given row and column with bound checking.
Definition mat.h:514
mat_iterator< const mat< Type, N, K >, const Type & > const_iterator
Const iterator for statically allocated matrices.
Definition mat.h:578
unsigned int size() const
Returns the total number of elements in the matrix.
Definition mat.h:625
Type & get(unsigned int i, unsigned int j)
Access the element at the given row and column, by reference.
Definition mat.h:484
TH_CONSTEXPR unsigned int cols() const
Returns the number of columns in the matrix.
Definition mat.h:618
mat< Type, N, K > & operator-=(const Matrix &other)
Matrix subtraction.
Definition mat.h:428
mat()
Default constructor.
Definition mat.h:149
auto end()
Returns an iterator to one past the last element of the matrix.
Definition mat.h:590
friend mat< Type, N, K > operator*(Type a, const mat< Type, N, K > &B)
Friend operator for scalar multiplication (T * mat).
Definition mat.h:285
auto begin() const
Returns a const iterator to the first element of the matrix.
Definition mat.h:597
mat< Type, N, K > & operator+=(const Matrix &other)
Matrix addition.
Definition mat.h:418
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:354
mat(Type diagonal, unsigned int n, unsigned int k)
Constructor that initializes a diagonal matrix with equal entries on the diagonal.
Definition mat.h:209
auto operator*(const Matrix &B) const
Overloads the * operator for matrix multiplication.
Definition mat.h:398
Vector transform(const Vector &v) const
Transforms a vector by multiplying it with the matrix.
Definition mat.h:329
Matrix mul(const Matrix &B) const
Matrix multiplication for matrices with any type.
Definition mat.h:378
mat< Type, N, K > operator/(Type scalar) const
Scalar division.
Definition mat.h:308
bool operator==(const Matrix &other) const
Checks whether this matrix is equal to another matrix element-wise.
Definition mat.h:635
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:537
const Type & operator()(unsigned int i, unsigned int j) const
Overloads the () operator to access an element by value.
Definition mat.h:568
Type & operator()(unsigned int i, unsigned int j)
Overloads the () operator to access an element by reference.
Definition mat.h:559
auto begin()
Returns an iterator to the first element of the matrix.
Definition mat.h:583
#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:238
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
@ InvalidArgument
Invalid argument.
@ ImpossibleOperation
Mathematically impossible operation.
@ DivByZero
Division by zero.
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.