Theoretica
A C++ numerical and automatic 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
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 at(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 at(i, i) = diagonal;
219 }
220
221
226 template<typename Matrix>
228 return algebra::mat_copy(*this, other);
229 }
230
231
233 inline void make_zeroes() {
234 algebra::mat_zeroes(*this);
235 }
236
237
242 template<typename Matrix>
243 inline mat<Type, N, K> operator+(const Matrix& other) const {
245 return algebra::mat_sum(res, *this, other);
246 }
247
248
253 template<typename Matrix>
254 inline mat<Type, N, K> operator-(const Matrix& other) const {
256 return algebra::mat_diff(res, *this, other);
257 }
258
259
265 return algebra::mat_scalmul(res, scalar, *this);
266 }
267
268
273 inline friend mat<Type, N, K> operator*(Type a, const mat<Type, N, K>& B) {
274 return B * a;
275 }
276
277
284 template<typename VecType, unsigned int M>
286 const vec<VecType, M>& a, const mat<Type, N, K>& B) {
287 return B * a;
288 }
289
290
297
299
300 if(abs(scalar) < MACH_EPSILON) {
301 TH_MATH_ERROR("mat::operator/", scalar, DIV_BY_ZERO);
302 return algebra::mat_error(res);
303 }
304
305 return algebra::mat_scalmul(res, 1.0 / scalar, *this);
306 }
307
308
316 template<typename Vector>
317 inline Vector transform(const Vector& v) const {
318
319 if(v.size() != cols()) {
320 TH_MATH_ERROR("mat::transform", v.size(), INVALID_ARGUMENT);
321 Vector res;
322 res.resize(cols());
324 return res;
325 }
326
327 return algebra::transform(*this, v);
328 }
329
330
334 inline vec<Type, N> transform(const vec<Type, K>& v) const {
335 return algebra::transform(*this, v);
336 }
337
338
342 inline vec<Type, N> operator*(const vec<Type, K>& v) const {
343 return transform(v);
344 }
345
346
351 template<unsigned int M>
352 inline mat<Type, N, M> mul(const mat<Type, K, M>& B) const {
354 return algebra::mat_mul(res, *this, B);
355 }
356
357
365 template<typename Matrix>
366 inline Matrix mul(const Matrix& B) const {
367
368 Matrix res;
369 res.resize(N, B.cols());
370
371 if(B.rows() != K) {
372 TH_MATH_ERROR("mat::transform", B.rows(), INVALID_ARGUMENT);
374 return res;
375 }
376
377 return algebra::mat_mul(res, *this, B);
378 }
379
380
385 template<typename Matrix>
386 inline auto operator*(const Matrix& B) const {
387
388 Matrix res;
389 res.resize(N, B.cols());
390
391 if(B.rows() != K) {
392 TH_MATH_ERROR("mat::transform", B.rows(), INVALID_ARGUMENT);
394 return res;
395 }
396
397 return algebra::mat_mul(res, *this, B);
398 }
399
400
405 template<typename Matrix>
407 return algebra::mat_sum(*this, other);
408 }
409
410
415 template<typename Matrix>
417 return algebra::mat_diff(*this, other);
418 }
419
420
425 return algebra::mat_scalmul(scalar, *this);
426 }
427
428
435
436 if(abs(scalar) < MACH_EPSILON) {
437 TH_MATH_ERROR("mat::operator/", scalar, DIV_BY_ZERO);
438 return algebra::mat_error(*this);
439 }
440
441 return algebra::mat_scalmul(1.0 / scalar, *this);
442 }
443
444
449 template<typename Matrix>
451 return (*this = this->operator*(B));
452 }
453
454
461 static_assert(
462 N == K, "The matrix must be square to be transposed in place.");
463 return algebra::make_transposed(*this);
464 }
465
466
471 inline Type& at(unsigned int i, unsigned int j) {
472
473#ifdef THEORETICA_ROW_FIRST
474 return data[i][j];
475#else
476 return data[j][i];
477#endif
478 }
479
480
485 inline const Type& at(unsigned int i, unsigned int j) const {
486
487#ifdef THEORETICA_ROW_FIRST
488 return data[i][j];
489#else
490 return data[j][i];
491#endif
492 }
493
494
499 inline Type& operator()(unsigned int i, unsigned int j) {
500 return at(i, j);
501 }
502
503
508 inline const Type& operator()(unsigned int i, unsigned int j) const {
509 return at(i, j);
510 }
511
512
517 inline Type get(unsigned int i, unsigned int j) const {
518
519#ifdef THEORETICA_ROW_FIRST
520 return data[i][j];
521#else
522 return data[j][i];
523#endif
524 }
525
526
529
530
533
534
537 inline auto begin() {
538 return iterator(*this, 0, 0);
539 }
540
541
544 inline auto end() {
545 return iterator(*this, rows(), 0);
546 }
547
548
551 inline auto begin() const {
552 return const_iterator(*this, 0, 0);
553 }
554
555
558 inline auto end() const {
559 return const_iterator(*this, rows(), 0);
560 }
561
562
565 TH_CONSTEXPR inline unsigned int rows() const {
566 return N;
567 }
568
569
572 TH_CONSTEXPR inline unsigned int cols() const {
573 return K;
574 }
575
576
579 inline unsigned int size() const {
580 return N * K;
581 }
582
583
588 template<typename Matrix>
589 inline bool operator==(const Matrix& other) const {
590 return algebra::mat_equals(*this, other);
591 }
592
593
598 template<typename Matrix>
599 inline bool operator!=(const Matrix& other) const {
600 return !algebra::mat_equals(*this, other);
601 }
602
603
609 static_assert(N == K, "The matrix must be square to be invertible.");
610 return algebra::invert(*this);
611 }
612
613
619 inline mat<Type, N, K> resize(unsigned int n, unsigned int k) const {
620
621 if(rows() != n) {
622 TH_MATH_ERROR("mat::resize", n, INVALID_ARGUMENT);
623 } else if(cols() != k) {
624 TH_MATH_ERROR("mat::resize", k, INVALID_ARGUMENT);
625 }
626
627 return *this;
628 }
629
630
631#ifndef THEORETICA_NO_PRINT
632
637 inline std::string to_string(
638 std::string separator = ", ", bool parenthesis = true) const {
639
640 std::stringstream res;
641
642 for (unsigned int i = 0; i < rows(); ++i) {
643
644 if(parenthesis)
645 res << "(";
646
647 for (unsigned int j = 0; j < cols(); ++j) {
648
649 if(j)
650 res << separator;
651
652 if(abs(get(i, j)) < MACH_EPSILON)
653 res << "0";
654 else
655 res << get(i, j);
656 }
657
658 if(parenthesis)
659 res << ")" << std::endl;
660 }
661
662 return res.str();
663 }
664
665
667 inline operator std::string() {
668 return to_string();
669 }
670
671
676 inline friend std::ostream& operator<<(
677 std::ostream& out, const mat<Type, N, K>& obj) {
678 return out << obj.to_string();
679 }
680
681#endif
682
683 };
684
685
686
692 template<typename Type>
693 class mat<Type, 0, 0> {
694 public:
695
697 std::vector<Type> data;
698
700 size_t row_sz {0};
701
703 size_t col_sz {0};
704
705
707 mat() : row_sz(0), col_sz(0) {}
708
709
713 mat(unsigned int n, unsigned int k) {
714 resize(n, k);
715 algebra::mat_zeroes(*this);
716 }
717
718
722 template <
724 >
725 mat(const Matrix& m) {
726 resize(m.rows(), m.cols());
727 algebra::mat_copy(*this, m);
728 }
729
730
734 template<typename T = Type>
735 inline mat(const std::initializer_list<std::initializer_list<T>>& rows) {
736
737 unsigned int i = 0;
738 unsigned int j = 0;
739
740 for (const auto& row : rows) {
741
742 for (const auto& x : row) {
743
744 if (!i && !j) {
745 this->resize(rows.size(), row.size());
746 }
747 else if (row.size() != col_sz) {
748 TH_MATH_ERROR("mat::mat", row.size(), INVALID_ARGUMENT);
749 algebra::mat_error(*this);
750 return;
751 }
752
753 at(i, j) = x;
754 j = (j + 1) % col_sz;
755 }
756
757 i++;
758 }
759 }
760
761
765 template<typename Matrix>
766 inline mat<Type>& operator=(const Matrix& other) {
767 resize(other.rows(), other.cols());
768 return algebra::mat_copy(*this, other);
769 }
770
771
776 mat(Type diagonal, unsigned int n, unsigned int k) {
777
778 resize(n, k);
779 algebra::mat_zeroes(*this);
780 const unsigned int m = min(n, k);
781
782 for (unsigned int i = 0; i < m; ++i)
783 at(i, i) = diagonal;
784 }
785
786
789 row_sz = 0;
790 col_sz = 0;
791 }
792
793
795 inline void make_zeroes() {
796 algebra::mat_zeroes(*this);
797 }
798
799
804 template<typename Matrix>
805 inline mat<Type> operator+(const Matrix& other) const {
807 res.resize(rows(), cols());
808 return algebra::mat_sum(res, *this, other);
809 }
810
811
816 template<typename Matrix>
817 inline mat<Type> operator-(const Matrix& other) const {
819 res.resize(rows(), cols());
820 return algebra::mat_diff(res, *this, other);
821 }
822
823
829 res.resize(rows(), cols());
830 return algebra::mat_scalmul(res, scalar, *this);
831 }
832
833
838 inline friend mat<Type> operator*(Type a, const mat<Type>& B) {
839 return B * a;
840 }
841
842
849 template<typename VecType, unsigned int M>
851 const vec<VecType, M>& a, const mat<Type, 0, 0>& B) {
852 return B * a;
853 }
854
855
860
862 res.resize(rows(), cols());
863
864 if(abs(scalar) < MACH_EPSILON) {
865 TH_MATH_ERROR("mat::operator/", scalar, DIV_BY_ZERO);
866 return algebra::mat_error(res);
867 }
868
869 return algebra::mat_scalmul(res, 1.0 / scalar, *this);
870 }
871
872
877 template<typename Vector>
878 inline Vector transform(const Vector& v) const {
879
880 if(v.size() != rows()) {
881 TH_MATH_ERROR("mat::transform", v.size(), INVALID_ARGUMENT);
882 Vector res;
883 res.resize(rows());
884 return algebra::vec_error(res);
885 }
886
887 return algebra::transform(*this, v);
888 }
889
890
895 template<unsigned int N = 0, unsigned int K = 0>
896 inline vec<Type, N> transform(const vec<Type, K>& v) const {
897 return algebra::transform(*this, v);
898 }
899
900
905 template<unsigned int N = 0, unsigned int K = 0>
906 inline vec<Type, N> operator*(const vec<Type, K>& v) const {
907 return transform(v);
908 }
909
910
912 // inline vec<Type> operator*(const vec<Type>& v) const {
913 // return transform(v);
914 // }
915
916
928 inline mat<Type> mul(const mat<Type>& B) const {
929
931 res.resize(rows(), B.cols());
932
933 if(B.rows() != cols()) {
934 TH_MATH_ERROR("mat::mul", B.rows(), INVALID_ARGUMENT);
936 return res;
937 }
938
939 return algebra::mat_mul(res, *this, B);
940 }
941
942
954 template<typename Matrix>
955 inline Matrix mul(const Matrix& B) const {
956
957 Matrix res;
958 res.resize(rows(), B.cols());
959
960 if(B.rows() != cols()) {
961 TH_MATH_ERROR("mat::mul", B.rows(), INVALID_ARGUMENT);
963 return res;
964 }
965
966 return algebra::mat_mul(res, *this, B);
967 }
968
969
976 template<typename Matrix>
977 inline auto operator*(const Matrix& B) const {
978 return mul(B);
979 }
980
981
989 template<typename Matrix>
991 return algebra::mat_sum(*this, other);
992 }
993
994
1002 template<typename Matrix>
1004 return algebra::mat_diff(*this, other);
1005 }
1006
1007
1015 return algebra::mat_scalmul(scalar, *this);
1016 }
1017
1018
1026
1027 if(abs(scalar) < MACH_EPSILON) {
1028 TH_MATH_ERROR("mat::operator/", scalar, DIV_BY_ZERO);
1029 return algebra::mat_error(*this);
1030 }
1031
1032 return algebra::mat_scalmul(1.0 / scalar, *this);
1033 }
1034
1035
1043 template<typename Matrix>
1044 inline mat<Type>& operator*=(const Matrix& B) {
1045 return (*this = this->operator*(B));
1046 }
1047
1048
1056 return algebra::make_transposed(*this);
1057 }
1058
1059
1066 inline Type& at(unsigned int i, unsigned int j) {
1067
1068#ifdef THEORETICA_ROW_FIRST
1069 return data[j + i * row_sz];
1070#else
1071 return data[i + j * col_sz];
1072#endif
1073 }
1074
1075
1082 inline const Type& at(unsigned int i, unsigned int j) const {
1083
1084#ifdef THEORETICA_ROW_FIRST
1085 return data[j + i * row_sz];
1086#else
1087 return data[i + j * col_sz];
1088#endif
1089 }
1090
1091
1099 inline Type& operator()(unsigned int i, unsigned int j) {
1100 return at(i, j);
1101 }
1102
1103
1111 inline const Type& operator()(unsigned int i, unsigned int j) const {
1112 return at(i, j);
1113 }
1114
1115
1122 inline Type get(unsigned int i, unsigned int j) const {
1123 return at(i, j);
1124 }
1125
1126
1129
1130
1133
1134
1140 inline auto begin() {
1141 return iterator(*this, 0, 0);
1142 }
1143
1144
1151 inline auto end() {
1152 return iterator(*this, rows(), 0);
1153 }
1154
1155
1161 inline auto begin() const {
1162 return const_iterator(*this, 0, 0);
1163 }
1164
1165
1171 inline auto end() const {
1172 return const_iterator(*this, rows(), 0);
1173 }
1174
1175
1178 TH_CONSTEXPR inline unsigned int rows() const {
1179 return row_sz;
1180 }
1181
1182
1185 TH_CONSTEXPR inline unsigned int cols() const {
1186 return col_sz;
1187 }
1188
1189
1193 inline unsigned int size() const {
1194 return rows() * cols();
1195 }
1196
1197
1204 template<typename Matrix>
1205 inline bool operator==(const Matrix& other) const {
1206 return algebra::mat_equals(*this, other);
1207 }
1208
1209
1216 template<typename Matrix>
1217 inline bool operator!=(const Matrix& other) const {
1218 return !algebra::mat_equals(*this, other);
1219 }
1220
1221
1226 inline mat<Type>& invert() {
1227 return algebra::invert(*this);
1228 }
1229
1230
1234 inline mat<Type>& resize(unsigned int rows, unsigned int cols) {
1235
1236 // Do nothing if the size is already correct
1237 if (row_sz == rows && col_sz == cols)
1238 return *this;
1239
1240 std::vector<Type> new_data (rows * cols);
1241
1242 if (data.size()) {
1243
1244 size_t min_elements = min(rows, row_sz) * min(cols, col_sz);
1245
1246 // Copy the overlapping elements
1247 for (unsigned int i = 0; i < min_elements; ++i)
1248 new_data[i] = data[i];
1249 }
1250
1251 data = new_data;
1252 row_sz = rows;
1253 col_sz = cols;
1254
1255 return *this;
1256 }
1257
1258
1259#ifndef THEORETICA_NO_PRINT
1260
1262 inline std::string to_string(
1263 std::string separator = ", ", bool parenthesis = true) const {
1264
1265 std::stringstream res;
1266
1267 for (unsigned int i = 0; i < rows(); ++i) {
1268
1269 if(parenthesis)
1270 res << "(";
1271
1272 for (unsigned int j = 0; j < cols(); ++j) {
1273
1274 if(j)
1275 res << separator;
1276
1277 if(abs(get(i, j)) < MACH_EPSILON)
1278 res << "0";
1279 else
1280 res << get(i, j);
1281 }
1282
1283 if(parenthesis)
1284 res << ")" << std::endl;
1285 }
1286
1287 return res.str();
1288 }
1289
1290
1292 inline operator std::string() {
1293 return to_string();
1294 }
1295
1296
1298 inline friend std::ostream& operator<<(
1299 std::ostream& out, const mat<Type>& obj) {
1300 return out << obj.to_string();
1301 }
1302
1303#endif
1304
1305 };
1306
1307}
1308
1309#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:1298
mat_iterator< mat< Type, 0, 0 >, Type & > iterator
Iterator for dynamically allocated matrices.
Definition mat.h:1128
mat< Type > operator-(const Matrix &other) const
Subtracts another matrix element-wise.
Definition mat.h:817
auto operator*(const Matrix &B) const
Matrix multiplication with any matrix type.
Definition mat.h:977
mat(unsigned int n, unsigned int k)
Constructor that initializes a matrix with the specified number of rows and columns.
Definition mat.h:713
mat< Type > & invert()
Invert a generic square matrix.
Definition mat.h:1226
mat< Type > operator/(Type scalar) const
Divides each element in the matrix by a scalar.
Definition mat.h:859
bool operator==(const Matrix &other) const
Check if two matrices are equal element by element.
Definition mat.h:1205
mat< Type > & operator/=(Type scalar)
Scalar division of the matrix.
Definition mat.h:1025
mat< Type > & operator+=(const Matrix &other)
Matrix addition with another matrix.
Definition mat.h:990
mat(Type diagonal, unsigned int n, unsigned int k)
Constructor that initializes a diagonal matrix with equal entries on the diagonal.
Definition mat.h:776
vec< Type, N > transform(const vec< Type, K > &v) const
Transforms a vector by multiplying it with the matrix.
Definition mat.h:896
Type & at(unsigned int i, unsigned int j)
Access a modifiable element at a specific row and column.
Definition mat.h:1066
friend mat< Type > operator*(Type a, const mat< Type > &B)
Friend operator to enable equations of the form (T) * (mat)
Definition mat.h:838
bool operator!=(const Matrix &other) const
Check if two matrices are unequal element by element.
Definition mat.h:1217
mat< Type > & operator*=(const Matrix &B)
Matrix multiplication with any matrix type.
Definition mat.h:1044
mat_iterator< const mat< Type, 0, 0 >, const Type & > const_iterator
Const iterator for dynamically allocated matrices.
Definition mat.h:1132
const Type & at(unsigned int i, unsigned int j) const
Access a constant element at a specific row and column.
Definition mat.h:1082
mat(const std::initializer_list< std::initializer_list< T > > &rows)
Constructor that initializes a matrix from a list of rows.
Definition mat.h:735
mat(const Matrix &m)
Copy constructor for creating a matrix from another matrix.
Definition mat.h:725
mat< Type > & resize(unsigned int rows, unsigned int cols)
Set or change the size of the matrix.
Definition mat.h:1234
auto end() const
Get a const iterator to one past the last element of the matrix.
Definition mat.h:1171
TH_CONSTEXPR unsigned int cols() const
Get the number of columns in the matrix.
Definition mat.h:1185
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:850
TH_CONSTEXPR unsigned int rows() const
Get the number of rows in the matrix.
Definition mat.h:1178
Vector transform(const Vector &v) const
Transforms a vector by multiplying it with the matrix.
Definition mat.h:878
unsigned int size() const
Get the total number of elements of the matrix (rows * columns)
Definition mat.h:1193
vec< Type, N > operator*(const vec< Type, K > &v) const
Transforms a vector by multiplying it with the matrix.
Definition mat.h:906
mat< Type > operator*(Type scalar) const
Multiplies the matrix by a scalar.
Definition mat.h:827
auto end()
Get an iterator to one past the last element of the matrix.
Definition mat.h:1151
mat< Type > & operator*=(Type scalar)
Scalar multiplication of the matrix.
Definition mat.h:1014
auto begin()
Get an iterator to the first element of the matrix.
Definition mat.h:1140
mat()
Default constructor.
Definition mat.h:707
mat< Type > & transpose()
Transpose the current matrix in place.
Definition mat.h:1055
std::string to_string(std::string separator=", ", bool parenthesis=true) const
Convert the matrix to string representation.
Definition mat.h:1262
mat< Type > & operator-=(const Matrix &other)
Matrix subtraction with another matrix.
Definition mat.h:1003
void make_zeroes()
Sets all elements in the matrix to zero.
Definition mat.h:795
mat< Type > operator+(const Matrix &other) const
Adds two matrices element-wise.
Definition mat.h:805
mat< Type > & operator=(const Matrix &other)
Copy assignment operator for copying from another matrix.
Definition mat.h:766
std::vector< Type > data
Dynamically allocated array of the elements.
Definition mat.h:697
mat< Type > mul(const mat< Type > &B) const
Transform a vector by the matrix.
Definition mat.h:928
Matrix mul(const Matrix &B) const
Multiplies the matrix by another matrix of any compatible type.
Definition mat.h:955
auto begin() const
Get a const iterator to the first element of the matrix.
Definition mat.h:1161
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:1099
~mat()
Destructor that resets the matrix size.
Definition mat.h:788
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:1111
Type get(unsigned int i, unsigned int j) const
Get a copy of the element at a specific row and column.
Definition mat.h:1122
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:243
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:352
std::string to_string(std::string separator=", ", bool parenthesis=true) const
Converts the matrix to a string representation.
Definition mat.h:637
mat< Type, N, K > operator*(Type scalar) const
Scalar multiplication.
Definition mat.h:263
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:599
mat< Type, N, K > operator-(const Matrix &other) const
Matrix subtraction.
Definition mat.h:254
void make_zeroes()
Sets all elements of the matrix to zero.
Definition mat.h:233
auto end() const
Returns a const iterator to one past the last element of the matrix.
Definition mat.h:558
mat_iterator< mat< Type, N, K >, Type & > iterator
Iterator for statically allocated matrices.
Definition mat.h:528
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:676
Type get(unsigned int i, unsigned int j) const
Gets the element at the specified row and column.
Definition mat.h:517
mat< Type, N, K > & invert()
Inverts the matrix in place.
Definition mat.h:608
vec< Type, N > transform(const vec< Type, K > &v) const
Transforms a fixed-size vector by multiplying it with the matrix.
Definition mat.h:334
mat< Type, N, K > & operator/=(Type scalar)
Scalar division.
Definition mat.h:434
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:619
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:285
mat< Type, N, K > & operator*=(const Matrix &B)
Matrix multiplication with an assignment operator.
Definition mat.h:450
mat< Type, N, K > & transpose()
Transposes the matrix in place.
Definition mat.h:460
TH_CONSTEXPR unsigned int rows() const
Returns the number of rows in the matrix.
Definition mat.h:565
mat< Type, N, K > & operator*=(Type scalar)
Scalar multiplication.
Definition mat.h:424
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.
Definition mat.h:471
mat_iterator< const mat< Type, N, K >, const Type & > const_iterator
Const iterator for statically allocated matrices.
Definition mat.h:532
unsigned int size() const
Returns the total number of elements in the matrix.
Definition mat.h:579
TH_CONSTEXPR unsigned int cols() const
Returns the number of columns in the matrix.
Definition mat.h:572
mat< Type, N, K > & operator-=(const Matrix &other)
Matrix subtraction.
Definition mat.h:416
mat()
Default constructor.
Definition mat.h:149
auto end()
Returns an iterator to one past the last element of the matrix.
Definition mat.h:544
friend mat< Type, N, K > operator*(Type a, const mat< Type, N, K > &B)
Friend operator for scalar multiplication (T * mat).
Definition mat.h:273
auto begin() const
Returns a const iterator to the first element of the matrix.
Definition mat.h:551
mat< Type, N, K > & operator+=(const Matrix &other)
Matrix addition.
Definition mat.h:406
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:342
auto operator*(const Matrix &B) const
Overloads the * operator for matrix multiplication.
Definition mat.h:386
Vector transform(const Vector &v) const
Transforms a vector by multiplying it with the matrix.
Definition mat.h:317
Matrix mul(const Matrix &B) const
Matrix multiplication for matrices with any type.
Definition mat.h:366
mat< Type, N, K > operator/(Type scalar) const
Scalar division.
Definition mat.h:296
bool operator==(const Matrix &other) const
Checks whether this matrix is equal to another matrix element-wise.
Definition mat.h:589
const Type & at(unsigned int i, unsigned int j) const
Accesses the element at the given row and column.
Definition mat.h:485
const Type & operator()(unsigned int i, unsigned int j) const
Overloads the () operator to access an element.
Definition mat.h:508
Type & operator()(unsigned int i, unsigned int j)
Overloads the () operator to access an element.
Definition mat.h:499
auto begin()
Returns an iterator to the first element of the matrix.
Definition mat.h:537
#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:219
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
std::remove_reference_t< decltype(std::declval< Structure >()[0])> vector_element_t
Extract the type of a vector (or any indexable container) from its operator[].
Definition core_traits.h:134
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.