1
0
forked from leto/LeMA

Added * operator and fixed Matrix.Set

This commit is contained in:
Leto 2024-12-24 14:25:14 +01:00
parent e4026b8d60
commit 3c596e9601
2 changed files with 15 additions and 6 deletions

View File

@ -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;
}

View File

@ -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