From 922321e9cb497dfb5397335574932ca64be1deff Mon Sep 17 00:00:00 2001 From: LeLeLeLeto Date: Tue, 31 Dec 2024 17:22:20 +0100 Subject: [PATCH] Matrix extend functions and made hadamard return a Matrix --- matrices.h | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/matrices.h b/matrices.h index 3ca3ace..1e5df46 100644 --- a/matrices.h +++ b/matrices.h @@ -9,9 +9,8 @@ #define assertm(exp, msg) assert((void(msg), exp)) class Matrix{ - private: - std::vector> values; public: + std::vector> values; inline void Randomize(); inline void Randomize(float, float); @@ -22,7 +21,7 @@ class Matrix{ inline Matrix Multiply(float); inline Matrix Multiply(const Matrix*); - inline void Hadamard(const Matrix*); + inline Matrix Hadamard(const Matrix*); inline Matrix Add(float); inline Matrix Add(const Matrix*); @@ -32,6 +31,9 @@ class Matrix{ inline Matrix Function(float (*f)(float)); + inline Matrix ExtendRight(int); + inline Matrix ExtendDown(int); + inline void Print(std::string_view); inline Matrix Transpose(); @@ -56,6 +58,28 @@ Matrix::Matrix(){ } +Matrix Matrix::ExtendRight(int new_size){ + // Extend the matrix to the right + Matrix result(this->values.size(), new_size); + for(int n = 0; n < result.values.size(); n++){ + for(int m = 0; m < result.values[n].size(); m++){ + result.values[n][m] = this->values[n][0]; + } + } + return result; +} + +Matrix Matrix::ExtendDown(int new_size){ + // Extend the matrix down + Matrix result(new_size, this->values[0].size()); + for(int n = 0; n < result.values.size(); n++){ + for(int m = 0; m < result.values[n].size(); m++){ + result.values[n][m] = this->values[0][m]; + } + } + return result; +} + Matrix Matrix::operator=(const Matrix* other){ return this->Swap(other); } @@ -115,16 +139,18 @@ Matrix Matrix::Swap(const Matrix* other){ return *this; } -void Matrix::Hadamard(const Matrix* other){ +Matrix Matrix::Hadamard(const Matrix* other){ // Matrices need to be the same size assertm(this->values.size() == other->values.size() && this->values[0].size() == other->values[0].size(), "Matrices need to be the same size"); - for(int m = 0; m < this->values.size(); m++){ - for(int n = 0; n < this->values[m].size(); n++){ - this->values[m][n] *= other->values[m][n]; + Matrix result = this; + for(int m = 0; m < result.values.size(); m++){ + for(int n = 0; n < result.values[m].size(); n++){ + result.values[m][n] = this->values[m][n] * other->values[m][n]; } } + return result; } // Multiply 2 matrices (AxB = this x other)