13#include <initializer_list>
47 Matrix(
long dim_x,
long dim_y);
60 Matrix(
long dim_x,
long dim_y, std::initializer_list<T> l);
88 T* getData() {
return this->data;}
89 long get_rows()
const {
return rows;}
90 long get_cols()
const {
return cols;}
222 T&
operator()(
const long i,
const long j ) {
return data[i*cols + j]; }
234 const T&
operator()(
const long i,
const long j )
const {
return data[i*cols + j]; }
256 const T&
operator()(
const long n )
const {
return data[n]; }
263 Matrix operator*(T constant);
264 Matrix operator/(T constant);
265 Matrix operator^(T constant);
266 Matrix operator+(T constant);
267 Matrix operator-(T constant);
364 for(
long i = 0; i < rows * cols; i ++) {
370Matrix<T>::Matrix(
long dim_x,
long dim_y, std::initializer_list<T> l) : rows(dim_x), cols(dim_y), data(new T[rows * cols]) {
372 if(rows * cols != l.size()) {
373 std::cout<<
"The size of the list ("<<l.size()<<
") does not match the matrix shape ("<<dim_x<<
", "<<dim_y<<
")\n";
376 std::uninitialized_copy(l.begin(), l.end(), data);
391 this->rows = other.rows;
392 this->cols = other.cols;
393 this->data =
new T[rows * cols];
395 for(
long i = 0; i < rows * cols; i ++){
396 this->data[i] = other.data[i];
404 this->rows = other.rows;
405 this->cols = other.cols;
409 this->data = other.data;
410 other.data =
nullptr;
419 this->rows = other.rows;
420 this->cols = other.cols;
421 data =
new T[rows * cols];
423 for(
long i = 0; i < rows * cols; i ++) {
424 this->data[i] = other.data[i];
434 this->rows = other.rows;
435 this->cols = other.cols;
436 this->data = other.data;
439 other.data =
nullptr;
446 for(
long i = 0; i < rows * cols; i ++) {
447 this->data[i] = constant;
454 if(this->rows != other.rows || this->cols != other.cols) {
455 std::cout<<
"Matrix dimensions do not match!\n";
458 for(
long i = 0; i < rows * cols; i ++){
459 this->data[i] += other.data[i];
467 if(this->rows != other.rows || this->cols != other.cols) {
468 std::cout<<
"Matrix dimensions do not match!\n";
471 for(
long i = 0; i < rows * cols; i ++){
472 this->data[i] -= other.data[i];
480 if(this->rows != other.rows || this->cols != other.cols) {
481 std::cout<<
"Matrix dimensions do not match!\n";
484 for(
long i = 0; i < rows * cols; i ++){
485 this->data[i] *= other.data[i];
493 if(this->rows != other.rows || this->cols != other.cols) {
494 std::cout<<
"Matrix dimensions do not match!\n";
497 for(
long i = 0; i < rows * cols; i ++){
498 this->data[i] /= other.data[i];
506 for(
long i = 0; i < rows * cols; i ++){
514 for(
long i = 0; i < rows * cols; i ++){
522 for(
long i = 0; i < rows * cols; i ++){
530 for(
long i = 0; i < rows * cols; i ++){
595 for(
long i = 0; i < rows; i ++){
596 for(
long j = 0; j < cols; j ++) {
597 res(i, j) = pow(this->data[i * cols + j], constant);
608 if(this->cols != other.rows) {
609 std::cout<<
"Matrix dimensions of ("<<this->rows<<
", "<<this->cols<<
") and ("<<other.rows<<
", "<<other.cols<<
") are not compatible!\n";
612 for(
long i = 0; i < this->rows; i ++) {
613 for(
long j = 0; j < other.cols; j ++) {
614 for(
long k = 0; k < this->cols; k ++) {
615 res.data[i * other.cols + j] += this->data[i * this->cols + k] * other.data[k * other.cols + j];
627 if(this->rows != 3 || this->cols != 1 || other.rows != 3 || other.cols != 1) {
628 std::cerr<<
"Dimensions incompatible with the cross product operation!\n";
631 result(0, 0) = this->data[1 * cols + 0] * other(2, 0) - this->data[2 * cols + 0] * other(1, 0);
632 result(1, 0) = this->data[2 * cols + 0] * other(0, 0) - this->data[0 * cols + 0] * other(2, 0);
633 result(2, 0) = this->data[0 * cols + 0] * other(1, 0) - this->data[1 * cols + 0] * other(0, 0);
641 Matrix<T> result(this->rows, other.rows);
642 if(this->cols != 1 || other.cols != 1) {
643 std::cerr<<
"Dimensions incompatible with the outer product operation!\n";
646 for(
long i = 0; i < this->rows; i ++) {
647 for(
long j = 0; j < other.rows; j ++) {
648 result(i, j) = this->data[j] * other(i);
658 Matrix<T> mat1(this->rows, other.rows), mat2(this->rows, other.rows);
659 if(this->cols != 1 || other.cols != 1) {
660 std::cerr<<
"Dimensions incompatible with the outer product operation!\n";
663 for(
long i = 0; i < this->rows; i ++) {
664 for(
long j = 0; j < other.rows; j ++) {
665 mat1(i, j) = this->data[i];
666 mat2(i, j) = other(j);
670 return std::make_tuple(mat1, mat2);
677 for(
long i = 0; i < rows * cols; i ++) {
678 res += this->data[i] * this->data[i];
688 for(
auto i = 0; i < rows * cols; i ++) {
689 if(max_value < data[i]) max_value = data[i];
698 for(
auto i = 0; i < rows * cols; i ++) {
699 if(min_value > data[i]) min_value = data[i];
708 for(
auto i = 0; i < rows; i ++) {
709 for(
auto j = 0; j < cols; j ++) {
710 new_matrix(j, i) = data[i * cols + j];
720 Matrix<T> result(other.get_rows(), other.get_cols());
722 for(
long i = 0; i < other.get_rows() * other.get_cols(); i ++){
723 result(i) = std::cos(other(i));
731 Matrix<T> result(other.get_rows(), other.get_cols());
733 for(
long i = 0; i < other.get_rows() * other.get_cols(); i ++){
734 result(i) = std::sin(other(i));
742 Matrix<T> result(other.get_rows(), other.get_cols());
744 for(
long i = 0; i < other.get_rows() * other.get_cols(); i ++){
745 result(i) = std::tan(other(i));
753 Matrix<T> result(other.get_rows(), other.get_cols());
755 for(
long i = 0; i < other.get_rows() * other.get_cols(); i ++){
756 result(i) = std::exp(other(i));
764 Matrix<T> result(other.get_rows(), other.get_cols());
766 for(
long i = 0; i < other.get_rows() * other.get_cols(); i ++){
767 result(i) = std::erf(other(i));
775 Matrix<T> result(other.get_rows(), other.get_cols());
777 for(
long i = 0; i < other.get_rows() * other.get_cols(); i ++){
778 result(i) = std::erfc(other(i));
786 for(
long i = 0; i < other.get_rows() * other.get_cols(); i ++){
794 for(
auto i = 0; i < rows; i ++) {
795 for(
auto j = 0; j < cols; j ++) {
796 std::cout<<data[i * cols + j] <<
" ";
This class implements a matrix object used for linear algebra and vectorized operations.
Definition Matrix.h:25
Matrix cross(const Matrix &other)
Calculates the cross product of two matrices.
Definition Matrix.h:624
T & operator()(const long n)
Subscript operator for element access in a single row (read/write).
Definition Matrix.h:245
T & operator()(const long i, const long j)
Subscript operator for element access (read/write).
Definition Matrix.h:222
Matrix & operator+=(const Matrix &other)
Compound assignment operator (addition). Adds another Matrix to this Matrix.
Definition Matrix.h:453
std::tuple< Matrix< T >, Matrix< T > > meshgrid(const Matrix &other)
Calculates the meshgrid of 2 1D matrices.
Definition Matrix.h:656
Matrix & operator-=(const Matrix &other)
Compound assignment operator (subtraction). Subtracts another Matrix from this Matrix.
Definition Matrix.h:466
T max()
Finds the maximum element in the matrix.
Definition Matrix.h:685
Matrix dot(const Matrix &other)
Calculates the dot product of two matrices.
Definition Matrix.h:604
const T & operator()(const long n) const
Subscript operator for element access in a single row (read-only).
Definition Matrix.h:256
Matrix & operator*=(const Matrix &other)
Compound assignment operator (multiplication). Multiplies this Matrix with another Matrix.
Definition Matrix.h:479
T norm()
Calculates the Frobenius norm of the matrix.
Definition Matrix.h:674
~Matrix()
Destructor. Cleans up the allocated memory.
Definition Matrix.h:381
T min()
Finds the minimum element in the matrix.
Definition Matrix.h:695
Matrix outer(const Matrix &other)
Calculates the outer product of two matrices.
Definition Matrix.h:639
Matrix()
Default constructor. Initializes an empty matrix.
Definition Matrix.h:360
const T & operator()(const long i, const long j) const
Subscript operator for element access (read-only).
Definition Matrix.h:234
Matrix & operator=(const Matrix &other)
Copy assignment operator. Copies the data from the given Matrix object.
Definition Matrix.h:416
void print()
Prints the matrix to the console.
Definition Matrix.h:793
Matrix & operator/=(const Matrix &other)
Compound assignment operator (division). Divides this Matrix by another Matrix.
Definition Matrix.h:492
Matrix tr()
Transposes the matrix.
Definition Matrix.h:705