philsupertramp/game-math
Loading...
Searching...
No Matches
Matrix.h
Go to the documentation of this file.
1#pragma once
2#include "Random.h"
3#include "vec/vec3.h"
4#include <cassert>
5#include <cmath>
6#include <cstdlib>
7#include <cstring>
8#include <functional>
9#include <initializer_list>
10#include <iostream>
11#include <memory>
12#include <type_traits>
13
14#include <signal.h>
15
16
22 size_t rows;
24 size_t columns;
26 size_t elemDim = 1;
27};
40template<typename T = double>
41class Matrix
42{
43public:
51 explicit Matrix(T val, size_t rowCount, size_t colCount, size_t elementDimension = 1) {
52 Resize(rowCount, colCount, elementDimension);
53 for(size_t i = 0; i < _rows * _columns * _element_size; ++i) { _data[i] = val; }
54 }
55
61 explicit Matrix(T* val, size_t colCount) {
62 Resize(1, colCount);
63 for(size_t j = 0; j < _columns; j++) { _data[GetIndex(0, j)] = val[j]; }
64 }
65
69 Matrix() { }
70
75 Matrix(const std::initializer_list<std::initializer_list<T>>& lst) {
76 int i = 0, j = 0;
77 auto rows = lst.size();
78 auto cols = lst.begin()->size();
79 Resize(rows, cols);
80 for(const auto& l : lst) {
81 for(const auto& v : l) {
82 _data[GetIndex(i, j)] = v;
83 ++j;
84 }
85 j = 0;
86 ++i;
87 }
88 }
89
94 Matrix(const std::initializer_list<std::initializer_list<std::initializer_list<T>>>& lst) {
95 int i = 0, j = 0, c = 0;
96 auto rows = lst.size();
97 auto cols = lst.begin()->size();
98 auto elems = lst.begin()->begin()->size();
99 Resize(rows, cols, elems);
100 for(const auto& l : lst) {
101 for(const auto& v : l) {
102 for(const auto k : v) {
103 _data[GetIndex(i, j, c)] = k;
104 ++c;
105 }
106 c = 0;
107 ++j;
108 }
109 j = 0;
110 ++i;
111 }
112 }
113
118 Matrix(Matrix const& other) {
119 Resize(other._rows, other._columns, other._element_size);
120 std::memcpy(this->_data, other._data, other._dataSize * sizeof(T));
121 this->needsFree = true;
122 };
123
130 template<typename V>
131 Matrix(const Matrix<V>& other) {
132 Resize(other.rows(), other.columns(), other.elements());
133 auto index_factor = _columns * _element_size;
134 for(size_t i = 0; i < (_rows * _columns * _element_size); ++i) {
135 _data[i] = static_cast<T>(other(i / (index_factor), i % (index_factor), i % _element_size));
136 }
137
138 this->needsFree = true;
139 }
140
145 if(needsFree) { free(_data); }
146 }
147
157 static Matrix
158 Random(size_t rows, size_t columns, size_t element_size = 1, double minValue = 0.0, double maxValue = 1.0) {
159 Matrix<T> matrix(0, rows, columns, element_size);
160 for(size_t i = 0; i < rows * columns * element_size; ++i) { matrix._data[i] = Random::Get(minValue, maxValue); }
161 return matrix;
162 }
163
171 static Matrix Normal(size_t rows, size_t columns, double mu, double sigma) {
172 assert(columns % 2 == 0);
173
174 constexpr double two_pi = 2.0 * M_PI;
175 Matrix out;
176 out.Resize(rows, columns);
177 for(size_t i = 0; i < rows * columns; i += 2) {
178 auto u1 = Random::Get();
179 auto u2 = Random::Get();
180
181 auto mag = sigma * sqrt(-2.0 * log(u1));
182 out._data[i] = mag * cos(two_pi * u2) + mu;
183 out._data[i + 1] = mag * sin(two_pi * u2) + mu;
184 }
185
186 return out;
187 }
188
193 [[nodiscard]] inline size_t rows() const { return _rows; }
198 [[nodiscard]] inline size_t columns() const { return _columns; }
199
204 [[nodiscard]] inline size_t elements() const { return _element_size; }
205
210 [[nodiscard]] inline size_t elements_total() const {return _rows * _columns * _element_size; }
211
216 [[nodiscard]] inline T Determinant() const {
217 if(!HasDet()) return 0;
218
219 if(_rows == 3 && _columns == 3) {
220 return (
221 _data[GetIndex(0, 0)] * _data[GetIndex(1, 1)] * _data[GetIndex(2, 2)]
222 + _data[GetIndex(0, 1)] * _data[GetIndex(1, 2)] * _data[GetIndex(2, 0)]
223 + _data[GetIndex(0, 2)] * _data[GetIndex(1, 0)] * _data[GetIndex(2, 1)]
224 - _data[GetIndex(0, 2)] * _data[GetIndex(1, 1)] * _data[GetIndex(2, 0)]
225 - _data[GetIndex(0, 1)] * _data[GetIndex(1, 0)] * _data[GetIndex(2, 2)]
226 - _data[GetIndex(0, 0)] * _data[GetIndex(1, 2)] * _data[GetIndex(2, 1)]);
227 }
228 if(_rows == 2 && _columns == 2) {
229 return _data[GetIndex(0, 0)] * _data[GetIndex(1, 1)] - _data[GetIndex(0, 1)] * _data[GetIndex(1, 0)];
230 }
231
232 Matrix<T> submat(0.0, _rows - 1, _columns - 1);
233 T d = 0;
234 {
235 for(size_t c = 0; c < _columns; c++) {
236 size_t subi = 0; //sub-matrix's i value
237 for(size_t i = 1; i < _rows; i++) {
238 size_t subj = 0;
239 for(size_t j = 0; j < _columns; j++) {
240 if(j == c) continue;
241 submat._data[submat.GetIndex(subi, subj)] = _data[GetIndex(i, j)];
242 subj++;
243 }
244 subi++;
245 }
246 d = d + (std::pow(-1, c) * _data[GetIndex(0, c)] * submat.Determinant());
247 }
248 }
249 return d;
250 }
251
256 [[nodiscard]] constexpr Matrix<T> Transpose() const {
258 int index_factor = _rows * _element_size;
259 for(size_t i = 0; i < (_rows * _columns * _element_size); ++i) {
260 res._data[i] = _data[GetIndex(i % (index_factor), i / (index_factor), i % _element_size)];
261 }
262 return res;
263 }
264
271 assert(this->rows() == other.rows());
272 auto result = new Matrix<T>(0, this->rows(), this->columns() + other.columns(), other.elements());
273 for(size_t i = 0; i < rows(); ++i) {
274 for(size_t j = 0; j < columns() + other.columns(); ++j) {
275 for(size_t elem = 0; elem < _element_size; ++elem) {
276 (*result)(i, j, elem) = j < columns() ? _data[GetIndex(i, j, elem)] : other(i, j - columns(), elem);
277 }
278 }
279 }
280 return *result;
281 }
282
285 // Comparison
286
292 bool operator==(const Matrix<T>& rhs) const {
293 // Just need to check element-wise
294 // Dimensions handled by implementation.
295 this->assertSize(rhs);
296 return elementWiseCompare(rhs);
297 }
303 bool operator!=(const Matrix<T>& rhs) const {
304 return !(rhs == *this); // NOLINT
305 }
306
307 bool operator<(const Matrix<T>& rhs) const {
308 assertSize(rhs);
309 for(size_t i = 0; i < _rows * _columns * _element_size; ++i) {
310 if(_data[i] > rhs._data[i]) { return false; }
311 }
312 return true;
313 }
314
315 bool operator>(const Matrix<T>& rhs) const {
316 assertSize(rhs);
317 for(size_t i = 0; i < _rows * _columns * _element_size; ++i) {
318 if(_data[i] < rhs._data[i]) { return false; }
319 }
320 return true;
321 }
322
328 [[nodiscard]] bool IsVector() const { return _columns == 1 || _rows == 1; }
329
334 void assertSize(const Matrix<T>& other) const {
335 assert(_columns == other.columns() && _rows == other.rows() && _element_size == other.elements());
336 }
337
343 [[nodiscard]] bool elementWiseCompare(const Matrix<T>& rhs) const {
344 assertSize(rhs);
345 for(size_t i = 0; i < _rows * _columns * _element_size; ++i) {
346 if(_data[i] != rhs._data[i]) { return false; }
347 }
348 return true;
349 }
350
351
352 // Assignment
361 if(this != &other) {
362 if((this == NULL) || (_rows != other.rows() || _columns != other.columns())) {
363 Resize(other.rows(), other.columns(), other.elements());
364 }
365 for(size_t i = 0; i < _rows * _columns * _element_size; ++i) { _data[i] = other._data[i]; }
366 }
367 return *this;
368 }
369
375 Matrix<T> Apply(const std::function<T(T)>& fun) const {
376 auto out = (*this);
377 for(size_t i = 0; i < _rows * _columns * _element_size; ++i) { out._data[i] = fun(_data[i]); }
378 return out;
379 }
380
381 // Math
382
389 Matrix& HadamardMulti(const Matrix& other) {
390 for(size_t i = 0; i < _rows * _columns * _element_size; ++i) { _data[i] *= other._data[i]; }
391 return *this;
392 }
393
401 assert(_element_size == other.elements());
402 auto result = new Matrix<T>(0, rows() * other.rows(), columns() * other.columns(), _element_size);
403 for(size_t m = 0; m < rows(); m++) {
404 for(size_t n = 0; n < columns(); n++) {
405 for(size_t p = 0; p < other.rows(); p++) {
406 for(size_t q = 0; q < other.columns(); q++) {
407 for(size_t elem = 0; elem < _element_size; ++elem) {
408 (*result)(m * other.rows() + p, n * other.columns() + q, elem) =
409 _data[GetIndex(m, n, elem)] * other(p, q, elem);
410 }
411 }
412 }
413 }
414 }
415 return *result;
416 }
417
422 T sumElements() const {
423 T result = T(0.0);
424 for(size_t i = 0; i < _rows * _columns * _element_size; ++i) { result += _data[i]; }
425 return result;
426 }
427
434 Matrix<T> sum(size_t axis) const {
435 Matrix<T> out(0, axis == 0 ? _rows : 1, axis == 1 ? _columns : 1);
436 for(size_t i = 0; i < (axis == 0 ? _rows : _columns); ++i) {
437 out(axis == 0 ? i : 0, axis == 1 ? i : 0) =
438 GetSlice(axis == 0 ? i : 0, axis == 0 ? i : _rows - 1, axis == 1 ? i : 0, axis == 1 ? i : _columns - 1)
439 .sumElements();
440 }
441 return out;
442 }
443
450 (*this) = *this * rhs;
451 return *this;
452 }
453
460 (*this) = (*this) + rhs;
461 return *this;
462 }
469 (*this) = (*this) - rhs;
470 return *this;
471 }
472
473 // Access
474
482 T& operator()(size_t row, size_t column, size_t elem = 0) { return _data[GetIndex(row, column, elem)]; }
490 T& operator()(size_t row, size_t column, size_t elem = 0) const { return _data[GetIndex(row, column, elem)]; }
491
500 Matrix<T> operator()(size_t row) { return GetSlice(row, row, 0, _columns - 1); }
506 Matrix<T> operator()(size_t row) const { return GetSlice(row, row, 0, _columns - 1); }
507
512 T& operator*() { return _data[0]; }
517 T& operator*() const { return _data[0]; }
518
524 void SetColumn(size_t index, const Matrix<T>& other) {
525 bool isInColumns = other.columns() > other.rows();
526 auto rowCount = isInColumns ? other.columns() : other.rows();
527 assert(rowCount == _rows);
528 assert(_element_size == other.elements());
529 for(size_t i = 0; i < rowCount; ++i) {
530 for(size_t elem = 0; elem < elements(); ++elem) {
531 _data[GetIndex(i, index, elem)] = other(isInColumns ? 0 : i, isInColumns ? i : 0, elem);
532 }
533 }
534 }
535
541 void SetRow(size_t index, const Matrix<T>& other) {
542 bool isInColumns = other.columns() > other.rows();
543 auto colCount = isInColumns ? other.columns() : other.rows();
544 assert(colCount == _columns);
545 assert(_element_size == other.elements());
546 for(size_t i = 0; i < colCount; ++i) {
547 for(size_t elem = 0; elem < elements(); ++elem) {
548 _data[GetIndex(index, i, elem)] = other(isInColumns ? 0 : i, isInColumns ? i : 0, elem);
549 }
550 }
551 }
552
559 friend std::ostream& operator<<(std::ostream& ostr, const Matrix& m) {
560 ostr.precision(17);
561 ostr << "[\n";
562 for(size_t row = 0; row < m.rows(); row++) {
563 ostr << '\t';
564 for(size_t col = 0; col < m.columns(); col++) {
565 if(m.elements() > 1) { ostr << "( "; }
566 for(size_t elem = 0; elem < m.elements(); elem++) {
567 ostr << m._data[m.GetIndex(row, col, elem)];
568 if(elem < m.elements() - 1) ostr << ", ";
569 }
570 if(m.elements() > 1) { ostr << " )"; }
571 if(col < m.columns() - 1) ostr << ", ";
572 }
573 ostr << "\n";
574 }
575 ostr << "]\n";
576 return ostr;
577 }
578
585 void Resize(size_t rows, size_t cols, size_t elementSize = 1) {
586 _rows = rows;
587 _columns = cols;
588 _element_size = elementSize;
589 if(_data != nullptr || needsFree) {
590 _data = (T*)realloc(_data, rows * cols * elementSize * sizeof(T));
591 } else {
592 _data = (T*)malloc(rows * cols * elementSize * sizeof(T));
593 }
594 _dataSize = rows * cols * elementSize;
595 needsFree = true;
596 }
597
605 [[nodiscard]] inline int GetIndex(size_t row, size_t col, size_t elem = 0) const {
606 // assert(row < _rows && col < _columns && elem < _element_size);
607 return elem + col * _element_size + row * _columns * _element_size;
608 }
609 [[nodiscard]] inline Matrix GetSlice(size_t rowStart) const { return GetSlice(rowStart, rowStart, 0, _columns - 1); }
610 [[nodiscard]] inline Matrix GetSlice(size_t rowStart, size_t rowEnd) const {
611 return GetSlice(rowStart, rowEnd, 0, _columns - 1);
612 }
613 [[nodiscard]] inline Matrix GetSlice(size_t rowStart, size_t rowEnd, size_t colStart) const {
614 return GetSlice(rowStart, rowEnd, colStart, _columns - 1);
615 }
625 [[nodiscard]] inline Matrix GetSlice(size_t rowStart, size_t rowEnd, size_t colStart, size_t colEnd) const {
626 size_t numRows = (rowEnd - rowStart) + 1;
627 size_t numCols = (colEnd - colStart) + 1;
628
629 Matrix out(0, numRows, numCols, _element_size);
630
631 for(size_t i = 0; i < numRows; ++i) {
632 for(size_t j = 0; j < numCols; ++j) {
633 for(size_t elem = 0; elem < _element_size; ++elem) {
634 out(i, j, elem) = _data[GetIndex(rowStart + i, colStart + j, elem)];
635 }
636 }
637 }
638 return out;
639 }
640
642 const size_t& row_start,
643 const size_t& row_end,
644 const size_t& col_start,
645 const size_t& col_end,
646 const Matrix<T>& slice) {
647 size_t numRows = (row_end - row_start) + 1;
648 size_t numCols = (col_end - col_start) + 1;
649 assert(numRows == slice.rows());
650 assert(numCols == slice.columns());
651
652 for(size_t i = 0; i < numRows; ++i) {
653 for(size_t j = 0; j < numCols; ++j) { _data[GetIndex(row_start + i, col_start + j)] = slice(i, j); }
654 }
655 }
656
657
661 void SetSlice(const size_t& row_start, const Matrix<T>& slice) {
662 return SetSlice(row_start, row_start + slice.rows() - 1, 0, slice.columns() - 1, slice);
663 }
664
670 Matrix<T> GetComponents(const size_t& index) const {
671 assert(index < _element_size);
672 Matrix<T> out(0, _rows, _columns, 1);
673 for(size_t i = 0; i < _rows; ++i) {
674 for(size_t j = 0; j < _columns; ++j) { out(i, j, 0) = _data[GetIndex(i, j, index)]; }
675 }
676 return out;
677 }
678
679 inline Matrix<T> GetSlicesByIndex(const Matrix<size_t>& indices) const {
680 assert(indices.IsVector());
681 auto _indices = indices.rows() > indices.columns() ? indices : indices.Transpose();
682 Matrix<T> out(0, _indices.rows(), _columns, _element_size);
683 for(size_t i = 0; i < _indices.rows(); ++i) {
684 auto idx = _indices(i, 0);
685 auto slice = GetSlice(idx);
686 out.SetSlice(i, slice);
687 }
688 return out;
689 }
690
691private:
696 [[nodiscard]] bool HasDet() const { return _columns > 1 && _rows > 1 && _element_size == 1; }
697
699 size_t _rows = 0;
701 size_t _columns = 0;
703 size_t _element_size = 0;
704
706 T* _data = nullptr;
708 size_t _dataSize = 0;
710 bool needsFree = false;
711};
712
723template<typename T>
724inline Matrix<T> operator+(const Matrix<T>& lhs, const Matrix<T>& rhs) {
725 if(rhs.IsVector() && !lhs.IsVector()) {
726 bool row_wise = rhs.rows() > rhs.columns();
727 if(row_wise) {
728 assert(rhs.rows() == lhs.rows());
729 } else {
730 assert(rhs.columns() == lhs.columns());
731 }
732 auto result = Matrix<T>(0, lhs.rows(), lhs.columns(), lhs.elements());
733 for(size_t i = 0; i < lhs.rows(); i++) {
734 for(size_t j = 0; j < lhs.columns(); j++) {
735 for(size_t elem = 0; elem < lhs.elements(); elem++) {
736 result(i, j, elem) = lhs(i, j, elem) + rhs(row_wise ? i : 0, row_wise ? 0 : j, elem);
737 }
738 }
739 }
740 return result;
741 }
742
743 lhs.assertSize(rhs);
744 auto result = Matrix<T>(0, lhs.rows(), lhs.columns(), lhs.elements());
745 for(size_t i = 0; i < lhs.rows(); i++) {
746 for(size_t j = 0; j < lhs.columns(); j++) { result(i, j) = lhs(i, j) + rhs(i, j); }
747 }
748 return result;
749}
757template<typename T>
758inline Matrix<T> operator-(const Matrix<T>& lhs, const Matrix<T>& rhs) {
759 if(rhs.IsVector() && !lhs.IsVector()) {
760 // Matrix-Vector substraction
761 // substracts vector row/column wise from given lhs matrix
762 // similar to numpy.
763 auto result = Matrix<T>(0, lhs.rows(), lhs.columns(), lhs.elements());
764 for(size_t i = 0; i < lhs.rows(); i++) {
765 for(size_t j = 0; j < lhs.columns(); j++) {
766 for(size_t elem = 0; elem < lhs.elements(); elem++) {
767 result(i, j, elem) =
768 lhs(i, j, elem) - rhs(rhs.rows() > rhs.columns() ? i : 0, rhs.rows() > rhs.columns() ? 0 : j, elem);
769 }
770 }
771 }
772 return result;
773 }
774 lhs.assertSize(rhs);
775 auto result = Matrix<T>(0, lhs.rows(), lhs.columns(), lhs.elements());
776 for(size_t i = 0; i < lhs.rows(); i++) {
777 for(size_t j = 0; j < lhs.columns(); j++) {
778 for(size_t elem = 0; elem < lhs.elements(); elem++) { result(i, j, elem) = lhs(i, j, elem) - rhs(i, j, elem); }
779 }
780 }
781 return result;
782}
790template<typename T, typename U>
791inline Matrix<T> operator/(U lhs, const Matrix<T>& rhs) {
792 auto result = Matrix<T>(0.0, rhs.rows(), rhs.columns(), rhs.elements());
793 for(size_t i = 0; i < rhs.rows(); i++) {
794 for(size_t j = 0; j < rhs.columns(); j++) {
795 for(size_t elem = 0; elem < rhs.elements(); elem++) { result(i, j, elem) = lhs / rhs(i, j, elem); }
796 }
797 }
798 return result;
799}
807template<typename T, typename U>
808inline Matrix<T> operator/(const Matrix<T>& lhs, const U& rhs) {
809 auto result = Matrix<T>(0.0, lhs.rows(), lhs.columns(), lhs.elements());
810 for(size_t i = 0; i < lhs.rows(); i++) {
811 for(size_t j = 0; j < lhs.columns(); j++) {
812 for(size_t elem = 0; elem < lhs.elements(); elem++) { result(i, j, elem) = lhs(i, j, elem) / rhs; }
813 }
814 }
815 return result;
816}
817
818
838template<typename T>
839inline Matrix<T> operator/(const Matrix<T>& lhs, const Matrix<T>& rhs) {
840 auto result = Matrix<T>(0.0, lhs.rows(), lhs.columns(), lhs.elements());
841 if(rhs.IsVector()) {
842 bool row_wise = rhs.rows() > rhs.columns();
843 for(size_t i = 0; i < lhs.rows(); i++) {
844 for(size_t j = 0; j < lhs.columns(); j++) {
845 for(size_t elem = 0; elem < lhs.elements(); elem++) {
846 result(i, j, elem) = lhs(i, j, elem) / rhs(row_wise ? i : 0, row_wise ? 0 : j, elem);
847 }
848 }
849 }
850 return result;
851 }
852
853 return lhs * (1.0 / rhs);
854}
855
862template<typename T, typename U>
863inline Matrix<T> operator*(const Matrix<T>& lhs, const U& rhs) {
864 auto result = Matrix<T>(0.0, lhs.rows(), lhs.columns(), lhs.elements());
865 for(size_t i = 0; i < lhs.rows(); i++) {
866 for(size_t j = 0; j < lhs.columns(); j++) {
867 for(size_t elem = 0; elem < lhs.elements(); elem++) { result(i, j, elem) = lhs(i, j, elem) * rhs; }
868 }
869 }
870 return result;
871}
878template<typename T, typename U>
879inline Matrix<T> operator*(U lambda, const Matrix<T>& A) {
880 auto result = Matrix<T>(0.0, A.rows(), A.columns(), A.elements());
881 for(size_t i = 0; i < A.rows(); i++) {
882 for(size_t j = 0; j < A.columns(); j++) {
883 for(size_t elem = 0; elem < A.elements(); elem++) { result(i, j, elem) = A(i, j, elem) * lambda; }
884 }
885 }
886 return result;
887}
888
896template<typename T>
897inline Matrix<T> operator*(const Matrix<T>& lhs, const Matrix<T>& rhs) {
898 if(lhs.columns() == rhs.rows() && lhs.elements() == rhs.elements()) {
899 auto result = Matrix<T>(0.0, lhs.rows(), rhs.columns(), rhs.elements());
900 for(size_t i = 0; i < lhs.rows(); i++) {
901 for(size_t j = 0; j < rhs.columns(); j++) {
902 for(size_t k = 0; k < rhs.rows(); k++) {
903 for(size_t elem = 0; elem < rhs.elements(); elem++) {
904 result(i, j, elem) += (T)(lhs(i, k, elem) * rhs(k, j, elem));
905 }
906 }
907 }
908 }
909 return result;
910 }
911 assert(rhs.IsVector() && !lhs.IsVector());
912
913 auto row_wise = rhs.rows() > rhs.columns();
914 auto result = Matrix<T>(0.0, lhs.rows(), lhs.columns(), lhs.elements());
915 for(size_t i = 0; i < lhs.rows(); i++) {
916 for(size_t j = 0; j < lhs.columns(); j++) {
917 for(size_t k = 0; k < lhs.elements(); k++) {
918 result(i, j, k) = (T)(lhs(i, j, k) * rhs(row_wise ? i : 0, row_wise ? 0 : j, k));
919 }
920 }
921 }
922 return result;
923}
924
Matrix< T > operator-(const Matrix< T > &lhs, const Matrix< T > &rhs)
Definition: Matrix.h:758
Matrix< T > operator/(U lhs, const Matrix< T > &rhs)
Definition: Matrix.h:791
Matrix< T > operator*(const Matrix< T > &lhs, const U &rhs)
Definition: Matrix.h:863
Matrix< T > operator+(const Matrix< T > &lhs, const Matrix< T > &rhs)
Definition: Matrix.h:724
Definition: Matrix.h:42
Matrix(Matrix const &other)
Definition: Matrix.h:118
constexpr Matrix< T > Transpose() const
Definition: Matrix.h:256
void SetRow(size_t index, const Matrix< T > &other)
Definition: Matrix.h:541
void SetSlice(const size_t &row_start, const size_t &row_end, const size_t &col_start, const size_t &col_end, const Matrix< T > &slice)
Definition: Matrix.h:641
Matrix(const std::initializer_list< std::initializer_list< T > > &lst)
Definition: Matrix.h:75
void SetSlice(const size_t &row_start, const Matrix< T > &slice)
Definition: Matrix.h:661
Matrix(const std::initializer_list< std::initializer_list< std::initializer_list< T > > > &lst)
Definition: Matrix.h:94
bool HasDet() const
Definition: Matrix.h:696
Matrix GetSlice(size_t rowStart, size_t rowEnd) const
Definition: Matrix.h:610
Matrix< T > & operator*=(T rhs)
Definition: Matrix.h:449
Matrix(const Matrix< V > &other)
Definition: Matrix.h:131
Matrix & HadamardMulti(const Matrix &other)
Definition: Matrix.h:389
T & operator*()
Definition: Matrix.h:512
Matrix< T > & operator+=(const Matrix< T > &rhs)
Definition: Matrix.h:459
bool operator==(const Matrix< T > &rhs) const
Definition: Matrix.h:292
static Matrix Normal(size_t rows, size_t columns, double mu, double sigma)
Definition: Matrix.h:171
size_t elements_total() const
Definition: Matrix.h:210
bool operator<(const Matrix< T > &rhs) const
Definition: Matrix.h:307
Matrix GetSlice(size_t rowStart, size_t rowEnd, size_t colStart, size_t colEnd) const
Definition: Matrix.h:625
bool operator!=(const Matrix< T > &rhs) const
Definition: Matrix.h:303
Matrix< T > & operator-=(const Matrix< T > &rhs)
Definition: Matrix.h:468
size_t rows() const
Definition: Matrix.h:193
Matrix(T val, size_t rowCount, size_t colCount, size_t elementDimension=1)
Definition: Matrix.h:51
T Determinant() const
Definition: Matrix.h:216
Matrix< T > operator()(size_t row)
Definition: Matrix.h:500
size_t columns() const
Definition: Matrix.h:198
Matrix GetSlice(size_t rowStart) const
Definition: Matrix.h:609
Matrix< T > operator=(const Matrix< T > &other)
Definition: Matrix.h:360
void assertSize(const Matrix< T > &other) const
Definition: Matrix.h:334
T & operator()(size_t row, size_t column, size_t elem=0) const
Definition: Matrix.h:490
bool needsFree
Definition: Matrix.h:710
T & operator()(size_t row, size_t column, size_t elem=0)
Definition: Matrix.h:482
size_t _element_size
number elements
Definition: Matrix.h:703
size_t _columns
number columns
Definition: Matrix.h:701
~Matrix()
Definition: Matrix.h:144
Matrix GetSlice(size_t rowStart, size_t rowEnd, size_t colStart) const
Definition: Matrix.h:613
void Resize(size_t rows, size_t cols, size_t elementSize=1)
Definition: Matrix.h:585
Matrix()
Definition: Matrix.h:69
Matrix< T > & KroneckerMulti(const Matrix< T > &other)
Definition: Matrix.h:400
T * _data
ongoing array representing data
Definition: Matrix.h:706
Matrix< T > GetComponents(const size_t &index) const
Definition: Matrix.h:670
Matrix< T > operator()(size_t row) const
Definition: Matrix.h:506
Matrix< T > Apply(const std::function< T(T)> &fun) const
Definition: Matrix.h:375
static Matrix Random(size_t rows, size_t columns, size_t element_size=1, double minValue=0.0, double maxValue=1.0)
Definition: Matrix.h:158
Matrix< T > sum(size_t axis) const
Definition: Matrix.h:434
friend std::ostream & operator<<(std::ostream &ostr, const Matrix &m)
Definition: Matrix.h:559
size_t _dataSize
total number of elements
Definition: Matrix.h:708
size_t elements() const
Definition: Matrix.h:204
Matrix(T *val, size_t colCount)
Definition: Matrix.h:61
Matrix< T > GetSlicesByIndex(const Matrix< size_t > &indices) const
Definition: Matrix.h:679
T & operator*() const
Definition: Matrix.h:517
bool operator>(const Matrix< T > &rhs) const
Definition: Matrix.h:315
int GetIndex(size_t row, size_t col, size_t elem=0) const
Definition: Matrix.h:605
size_t _rows
number rows
Definition: Matrix.h:699
void SetColumn(size_t index, const Matrix< T > &other)
Definition: Matrix.h:524
T sumElements() const
Definition: Matrix.h:422
bool IsVector() const
Definition: Matrix.h:328
Matrix< T > HorizontalConcat(const Matrix< T > &other)
Definition: Matrix.h:270
bool elementWiseCompare(const Matrix< T > &rhs) const
Definition: Matrix.h:343
static double Get(double l=0.0, double r=1.0)
Definition: Random.h:40
Definition: Matrix.h:20
size_t elemDim
number elements
Definition: Matrix.h:26
size_t rows
number rows
Definition: Matrix.h:22
size_t columns
number columns
Definition: Matrix.h:24