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) {
666
667 if(rows() != n) {
668 TH_MATH_ERROR("mat::resize", n, MathError::InvalidArgument);
669 algebra::mat_error(*this);
670 } else if(cols() != k) {
671 TH_MATH_ERROR("mat::resize", k, MathError::InvalidArgument);
672 algebra::mat_error(*this);
673 }
674
675 return *this;
676 }
677
678
679#ifndef THEORETICA_NO_PRINT
680
685 inline std::string to_string(
686 std::string separator = ", ", bool parenthesis = true) const {
687
688 std::stringstream res;
689
690 for (unsigned int i = 0; i < rows(); ++i) {
691
692 if(parenthesis)
693 res << "(";
694
695 for (unsigned int j = 0; j < cols(); ++j) {
696
697 if(j)
698 res << separator;
699
700 if(abs(get(i, j)) < MACH_EPSILON)
701 res << "0";
702 else
703 res << get(i, j);
704 }
705
706 if(parenthesis)
707 res << ")" << std::endl;
708 }
709
710 return res.str();
711 }
712
713
715 inline operator std::string() {
716 return to_string();
717 }
718
719
724 inline friend std::ostream& operator<<(
725 std::ostream& out, const mat<Type, N, K>& obj) {
726 return out << obj.to_string();
727 }
728
729#endif
730
731 };
732
733
734
740 template<typename Type>
741 class mat<Type, 0, 0> {
742 public:
743
745 std::vector<Type> data;
746
748 size_t row_sz {0};
749
751 size_t col_sz {0};
752
753
755 mat() : row_sz(0), col_sz(0) {}
756
757
761 mat(unsigned int n, unsigned int k) {
762 resize(n, k);
763 algebra::mat_zeroes(*this);
764 }
765
766
770 template <
771 typename Matrix, enable_matrix<Matrix>
772 >
773 mat(const Matrix& m) {
774 resize(m.rows(), m.cols());
775 algebra::mat_copy(*this, m);
776 }
777
778
782 template<typename T = Type>
783 inline mat(const std::initializer_list<std::initializer_list<T>>& rows) {
784
785 unsigned int i = 0;
786 unsigned int j = 0;
787
788 for (const auto& row : rows) {
789
790 for (const auto& x : row) {
791
792 if (!i && !j) {
793 this->resize(rows.size(), row.size());
794 }
795 else if (row.size() != col_sz) {
796 TH_MATH_ERROR("mat::mat", row.size(), MathError::InvalidArgument);
797 algebra::mat_error(*this);
798 return;
799 }
800
801 get(i, j) = x;
802 j = (j + 1) % col_sz;
803 }
804
805 i++;
806 }
807 }
808
809
813 template<typename Matrix>
814 inline mat<Type>& operator=(const Matrix& other) {
815 resize(other.rows(), other.cols());
816 return algebra::mat_copy(*this, other);
817 }
818
819
824 mat(Type diagonal, unsigned int n, unsigned int k) {
825
826 resize(n, k);
827 algebra::mat_zeroes(*this);
828 const unsigned int m = min(n, k);
829
830 for (unsigned int i = 0; i < m; ++i)
831 get(i, i) = diagonal;
832 }
833
834
837 row_sz = 0;
838 col_sz = 0;
839 }
840
841
843 inline void make_zeroes() {
844 algebra::mat_zeroes(*this);
845 }
846
847
852 template<typename Matrix>
853 inline mat<Type> operator+(const Matrix& other) const {
854 mat<Type> res;
855 res.resize(rows(), cols());
856 return algebra::mat_sum(res, *this, other);
857 }
858
859
864 template<typename Matrix>
865 inline mat<Type> operator-(const Matrix& other) const {
866 mat<Type> res;
867 res.resize(rows(), cols());
868 return algebra::mat_diff(res, *this, other);
869 }
870
871
875 inline mat<Type> operator*(Type scalar) const {
876 mat<Type> res;
877 res.resize(rows(), cols());
878 return algebra::mat_scalmul(res, scalar, *this);
879 }
880
881
886 inline friend mat<Type> operator*(Type a, const mat<Type>& B) {
887 return B * a;
888 }
889
890
897 template<typename VecType, unsigned int M>
898 inline friend vec<VecType, 0> operator*(
899 const vec<VecType, M>& a, const mat<Type, 0, 0>& B) {
900 return B * a;
901 }
902
903
907 inline mat<Type> operator/(Type scalar) const {
908
909 mat<Type> res;
910 res.resize(rows(), cols());
911
912 if(abs(scalar) < MACH_EPSILON) {
913 TH_MATH_ERROR("mat::operator/", scalar, MathError::DivByZero);
914 return algebra::mat_error(res);
915 }
916
917 return algebra::mat_scalmul(res, 1.0 / scalar, *this);
918 }
919
920
925 template<typename Vector>
926 inline Vector transform(const Vector& v) const {
927
928 if(v.size() != rows()) {
929 TH_MATH_ERROR("mat::transform", v.size(), MathError::InvalidArgument);
930 Vector res;
931 res.resize(rows());
932 return algebra::vec_error(res);
933 }
934
935 return algebra::transform(*this, v);
936 }
937
938
943 template<unsigned int N = 0, unsigned int K = 0>
944 inline vec<Type, N> transform(const vec<Type, K>& v) const {
945 return algebra::transform(*this, v);
946 }
947
948
953 template<unsigned int N = 0, unsigned int K = 0>
954 inline vec<Type, N> operator*(const vec<Type, K>& v) const {
955 return transform(v);
956 }
957
958
960 // inline vec<Type> operator*(const vec<Type>& v) const {
961 // return transform(v);
962 // }
963
964
976 inline mat<Type> mul(const mat<Type>& B) const {
977
978 mat<Type> res;
979 res.resize(rows(), B.cols());
980
981 if(B.rows() != cols()) {
984 return res;
985 }
986
987 return algebra::mat_mul(res, *this, B);
988 }
989
990
1002 template<typename Matrix>
1003 inline Matrix mul(const Matrix& B) const {
1004
1005 Matrix res;
1006 res.resize(rows(), B.cols());
1007
1008 if(B.rows() != cols()) {
1009 TH_MATH_ERROR("mat::mul", B.rows(), MathError::InvalidArgument);
1010 algebra::mat_error(res);
1011 return res;
1012 }
1013
1014 return algebra::mat_mul(res, *this, B);
1015 }
1016
1017
1024 template<typename Matrix>
1025 inline auto operator*(const Matrix& B) const {
1026 return mul(B);
1027 }
1028
1029
1037 template<typename Matrix>
1038 inline mat<Type>& operator+=(const Matrix& other) {
1039 return algebra::mat_sum(*this, other);
1040 }
1041
1042
1050 template<typename Matrix>
1051 inline mat<Type>& operator-=(const Matrix& other) {
1052 return algebra::mat_diff(*this, other);
1053 }
1054
1055
1062 inline mat<Type>& operator*=(Type scalar) {
1063 return algebra::mat_scalmul(scalar, *this);
1064 }
1065
1066
1073 inline mat<Type>& operator/=(Type scalar) {
1074
1075 if(abs(scalar) < MACH_EPSILON) {
1076 TH_MATH_ERROR("mat::operator/", scalar, MathError::DivByZero);
1077 return algebra::mat_error(*this);
1078 }
1079
1080 return algebra::mat_scalmul(1.0 / scalar, *this);
1081 }
1082
1083
1091 template<typename Matrix>
1092 inline mat<Type>& operator*=(const Matrix& B) {
1093 return (*this = this->operator*(B));
1094 }
1095
1096
1103 inline mat<Type>& transpose() {
1104 return algebra::make_transposed(*this);
1105 }
1106
1107
1113 inline Type& get(unsigned int i, unsigned int j) {
1114
1115#ifdef THEORETICA_ROW_FIRST
1116 return data[j + i * row_sz];
1117#else
1118 return data[i + j * col_sz];
1119#endif
1120 }
1121
1122
1128 inline const Type& get(unsigned int i, unsigned int j) const {
1129
1130#ifdef THEORETICA_ROW_FIRST
1131 return data[j + i * row_sz];
1132#else
1133 return data[i + j * col_sz];
1134#endif
1135 }
1136
1137
1144 inline Type& at(unsigned int i, unsigned int j) {
1145
1146 if (i >= rows()) {
1147 throw std::out_of_range(
1148 "The provided row index in mat::at() is out of range"
1149 );
1150 }
1151
1152 if (j >= cols()) {
1153 throw std::out_of_range(
1154 "The provided column index in mat::at() is out of range"
1155 );
1156 }
1157
1158 return get(i, j);
1159 }
1160
1161
1168 inline const Type& at(unsigned int i, unsigned int j) const {
1169
1170 if (i >= rows()) {
1171 throw std::out_of_range(
1172 "The provided row index in mat::at() is out of range"
1173 );
1174 }
1175
1176 if (j >= cols()) {
1177 throw std::out_of_range(
1178 "The provided column index in mat::at() is out of range"
1179 );
1180 }
1181
1182 return get(i, j);
1183 }
1184
1185
1193 inline Type& operator()(unsigned int i, unsigned int j) {
1194 return get(i, j);
1195 }
1196
1197
1205 inline const Type& operator()(unsigned int i, unsigned int j) const {
1206 return get(i, j);
1207 }
1208
1209
1211 using iterator = mat_iterator<mat<Type, 0, 0>, Type&>;
1212
1213
1215 using const_iterator = mat_iterator<const mat<Type, 0, 0>, const Type&>;
1216
1217
1223 inline auto begin() {
1224 return iterator(*this, 0, 0);
1225 }
1226
1227
1234 inline auto end() {
1235 return iterator(*this, rows(), 0);
1236 }
1237
1238
1244 inline auto begin() const {
1245 return const_iterator(*this, 0, 0);
1246 }
1247
1248
1254 inline auto end() const {
1255 return const_iterator(*this, rows(), 0);
1256 }
1257
1258
1261 TH_CONSTEXPR inline unsigned int rows() const {
1262 return row_sz;
1263 }
1264
1265
1268 TH_CONSTEXPR inline unsigned int cols() const {
1269 return col_sz;
1270 }
1271
1272
1275 inline unsigned int size() const {
1276 return rows() * cols();
1277 }
1278
1279
1286 template<typename Matrix>
1287 inline bool operator==(const Matrix& other) const {
1288 return algebra::mat_equals(*this, other);
1289 }
1290
1291
1298 template<typename Matrix>
1299 inline bool operator!=(const Matrix& other) const {
1300 return !algebra::mat_equals(*this, other);
1301 }
1302
1303
1308 inline mat<Type>& invert() {
1309 return algebra::invert(*this);
1310 }
1311
1312
1316 inline mat<Type>& resize(unsigned int rows, unsigned int cols) {
1317
1318 // Do nothing if the size is already correct
1319 if (row_sz == rows && col_sz == cols)
1320 return *this;
1321
1322 std::vector<Type> new_data (rows * cols);
1323
1324 if (data.size()) {
1325
1326 size_t min_elements = min(rows, row_sz) * min(cols, col_sz);
1327
1328 // Copy the overlapping elements
1329 for (unsigned int i = 0; i < min_elements; ++i)
1330 new_data[i] = data[i];
1331 }
1332
1333 data = new_data;
1334 row_sz = rows;
1335 col_sz = cols;
1336
1337 return *this;
1338 }
1339
1340
1341#ifndef THEORETICA_NO_PRINT
1342
1344 inline std::string to_string(
1345 std::string separator = ", ", bool parenthesis = true) const {
1346
1347 std::stringstream res;
1348
1349 for (unsigned int i = 0; i < rows(); ++i) {
1350
1351 if(parenthesis)
1352 res << "(";
1353
1354 for (unsigned int j = 0; j < cols(); ++j) {
1355
1356 if(j)
1357 res << separator;
1358
1359 if(abs(get(i, j)) < MACH_EPSILON)
1360 res << "0";
1361 else
1362 res << get(i, j);
1363 }
1364
1365 if(parenthesis)
1366 res << ")" << std::endl;
1367 }
1368
1369 return res.str();
1370 }
1371
1372
1374 inline operator std::string() {
1375 return to_string();
1376 }
1377
1378
1380 inline friend std::ostream& operator<<(
1381 std::ostream& out, const mat<Type>& obj) {
1382 return out << obj.to_string();
1383 }
1384
1385#endif
1386
1387 };
1388
1389}
1390
1391#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:1380
mat_iterator< mat< Type, 0, 0 >, Type & > iterator
Iterator for dynamically allocated matrices.
Definition mat.h:1211
mat< Type > operator-(const Matrix &other) const
Subtracts another matrix element-wise.
Definition mat.h:865
auto operator*(const Matrix &B) const
Matrix multiplication with any matrix type.
Definition mat.h:1025
mat(unsigned int n, unsigned int k)
Constructor that initializes a matrix with the specified number of rows and columns.
Definition mat.h:761
mat< Type > & invert()
Invert a generic square matrix.
Definition mat.h:1308
mat< Type > operator/(Type scalar) const
Divides each element in the matrix by a scalar.
Definition mat.h:907
bool operator==(const Matrix &other) const
Check if two matrices are equal element by element.
Definition mat.h:1287
mat< Type > & operator/=(Type scalar)
Scalar division of the matrix.
Definition mat.h:1073
mat< Type > & operator+=(const Matrix &other)
Matrix addition with another matrix.
Definition mat.h:1038
mat(Type diagonal, unsigned int n, unsigned int k)
Constructor that initializes a diagonal matrix with equal entries on the diagonal.
Definition mat.h:824
vec< Type, N > transform(const vec< Type, K > &v) const
Transforms a vector by multiplying it with the matrix.
Definition mat.h:944
Type & at(unsigned int i, unsigned int j)
Access a modifiable element at a specific row and column.
Definition mat.h:1144
friend mat< Type > operator*(Type a, const mat< Type > &B)
Friend operator to enable equations of the form (T) * (mat)
Definition mat.h:886
bool operator!=(const Matrix &other) const
Check if two matrices are unequal element by element.
Definition mat.h:1299
mat< Type > & operator*=(const Matrix &B)
Matrix multiplication with any matrix type.
Definition mat.h:1092
mat_iterator< const mat< Type, 0, 0 >, const Type & > const_iterator
Const iterator for dynamically allocated matrices.
Definition mat.h:1215
const Type & at(unsigned int i, unsigned int j) const
Access a constant element at a specific row and column.
Definition mat.h:1168
mat(const std::initializer_list< std::initializer_list< T > > &rows)
Constructor that initializes a matrix from a list of rows.
Definition mat.h:783
mat(const Matrix &m)
Copy constructor for creating a matrix from another matrix.
Definition mat.h:773
mat< Type > & resize(unsigned int rows, unsigned int cols)
Set or change the size of the matrix.
Definition mat.h:1316
auto end() const
Get a const iterator to one past the last element of the matrix.
Definition mat.h:1254
TH_CONSTEXPR unsigned int cols() const
Get the number of columns in the matrix.
Definition mat.h:1268
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:898
TH_CONSTEXPR unsigned int rows() const
Get the number of rows in the matrix.
Definition mat.h:1261
Vector transform(const Vector &v) const
Transforms a vector by multiplying it with the matrix.
Definition mat.h:926
unsigned int size() const
Get the total number of elements of the matrix, equal to (rows * columns).
Definition mat.h:1275
vec< Type, N > operator*(const vec< Type, K > &v) const
Transforms a vector by multiplying it with the matrix.
Definition mat.h:954
Type & get(unsigned int i, unsigned int j)
Access the element at the given row and column, by reference.
Definition mat.h:1113
mat< Type > operator*(Type scalar) const
Multiplies the matrix by a scalar.
Definition mat.h:875
auto end()
Get an iterator to one past the last element of the matrix.
Definition mat.h:1234
mat< Type > & operator*=(Type scalar)
Scalar multiplication of the matrix.
Definition mat.h:1062
auto begin()
Get an iterator to the first element of the matrix.
Definition mat.h:1223
mat()
Default constructor.
Definition mat.h:755
mat< Type > & transpose()
Transpose the current matrix in place.
Definition mat.h:1103
std::string to_string(std::string separator=", ", bool parenthesis=true) const
Convert the matrix to string representation.
Definition mat.h:1344
mat< Type > & operator-=(const Matrix &other)
Matrix subtraction with another matrix.
Definition mat.h:1051
void make_zeroes()
Sets all elements in the matrix to zero.
Definition mat.h:843
mat< Type > operator+(const Matrix &other) const
Adds two matrices element-wise.
Definition mat.h:853
mat< Type > & operator=(const Matrix &other)
Copy assignment operator for copying from another matrix.
Definition mat.h:814
std::vector< Type > data
Dynamically allocated array of the elements.
Definition mat.h:745
mat< Type > mul(const mat< Type > &B) const
Transform a vector by the matrix.
Definition mat.h:976
Matrix mul(const Matrix &B) const
Multiplies the matrix by another matrix of any compatible type.
Definition mat.h:1003
const Type & get(unsigned int i, unsigned int j) const
Get the element at the given row and column.
Definition mat.h:1128
auto begin() const
Get a const iterator to the first element of the matrix.
Definition mat.h:1244
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:1193
~mat()
Destructor that resets the matrix size.
Definition mat.h:836
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:1205
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:685
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:724
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
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
mat< Type, N, K > resize(unsigned int n, unsigned int k)
Compatibility function to allow for allocation or resizing of dynamic matrices.
Definition mat.h:665
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:170
#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:776
Matrix & mat_scalmul(Field a, Matrix &m)
Multiply a matrix by a scalar of any compatible type.
Definition algebra.h:609
Matrix & invert(Matrix &m)
Invert the given matrix and overwrite it.
Definition algebra.h:1977
Matrix2 & mat_diff(Matrix1 &A, const Matrix2 &B)
Subtract two matrices and store the result in the first matrix.
Definition algebra.h:838
Vector transform(const Matrix &A, const Vector &v)
Returns the matrix transformation of a vector.
Definition algebra.h:717
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:972
Matrix & mat_zeroes(Matrix &m)
Overwrite a matrix with all zeroes.
Definition algebra.h:120
Matrix1 & mat_copy(Matrix1 &dest, const Matrix2 &src)
Copy a matrix by overwriting another.
Definition algebra.h:152
Matrix & make_transposed(Matrix &m)
Transpose the given matrix.
Definition algebra.h:445
Vector & vec_error(Vector &v)
Overwrite the given vector with the error vector with NaN values.
Definition algebra.h:62
bool mat_equals(const Matrix1 &A, const Matrix2 &B, real tolerance=ALGEBRA_ELEMENT_TOL)
Checks whether two matrices are equal.
Definition algebra.h:1100
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:216
Linear transformations such as rotations and projective geometry.
Vector class and operations.