From 3c596e96013cbdd8d3aac2c7b0864401f0ed2be6 Mon Sep 17 00:00:00 2001 From: Leto Date: Tue, 24 Dec 2024 14:25:14 +0100 Subject: [PATCH] Added * operator and fixed Matrix.Set --- main.cpp | 9 +++++---- matrices.h | 12 ++++++++++-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/main.cpp b/main.cpp index 69c991d..d215123 100644 --- a/main.cpp +++ b/main.cpp @@ -5,15 +5,16 @@ int main() { srand(time(0)); - Matrix a(3,2); + Matrix a(3,3); a.Randomize(); - Matrix b = a; + Matrix b(3, 1); + b.Set(2.0F); a.Print("A"); b.Print("B"); - Matrix result = a - &b; - result.Print("A - B"); + Matrix result = a * &b; + result.Print("A x B"); return 0; } \ No newline at end of file diff --git a/matrices.h b/matrices.h index f24bbe3..5099a5a 100644 --- a/matrices.h +++ b/matrices.h @@ -36,6 +36,7 @@ class Matrix{ inline Matrix operator=(const Matrix*); inline Matrix operator+(const Matrix*); inline Matrix operator-(const Matrix*); + inline Matrix operator*(const Matrix*); // Constructors inline Matrix(int, int); @@ -54,6 +55,10 @@ Matrix Matrix::operator-(const Matrix* other){ return this->Substract(other); } +Matrix Matrix::operator*(const Matrix* other){ + return this->Multiply(other); +} + // Constructs a zero matrix Matrix::Matrix(int rows, int cols){ for(int m = 0; m < rows; m++){ @@ -185,8 +190,11 @@ void Matrix::Multiply(float value){ // Set a matrix to a given value void Matrix::Set(float value){ - this->Multiply(0.0F); - this->Add(value); + for(int m = 0; m < this->values.size(); m++){ + for(int n = 0; n < this->values[m].size(); n++){ + this->values[m][n] = value; + } + } } // Transpose a matrix