forked from leto/LeMA
Added * operator and fixed Matrix.Set
This commit is contained in:
parent
e4026b8d60
commit
3c596e9601
9
main.cpp
9
main.cpp
@ -5,15 +5,16 @@ int main()
|
|||||||
{
|
{
|
||||||
srand(time(0));
|
srand(time(0));
|
||||||
|
|
||||||
Matrix a(3,2);
|
Matrix a(3,3);
|
||||||
a.Randomize();
|
a.Randomize();
|
||||||
Matrix b = a;
|
Matrix b(3, 1);
|
||||||
|
b.Set(2.0F);
|
||||||
|
|
||||||
a.Print("A");
|
a.Print("A");
|
||||||
b.Print("B");
|
b.Print("B");
|
||||||
|
|
||||||
Matrix result = a - &b;
|
Matrix result = a * &b;
|
||||||
result.Print("A - B");
|
result.Print("A x B");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
12
matrices.h
12
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*);
|
||||||
inline Matrix operator-(const Matrix*);
|
inline Matrix operator-(const Matrix*);
|
||||||
|
inline Matrix operator*(const Matrix*);
|
||||||
|
|
||||||
// Constructors
|
// Constructors
|
||||||
inline Matrix(int, int);
|
inline Matrix(int, int);
|
||||||
@ -54,6 +55,10 @@ Matrix Matrix::operator-(const Matrix* other){
|
|||||||
return this->Substract(other);
|
return this->Substract(other);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Matrix Matrix::operator*(const Matrix* other){
|
||||||
|
return this->Multiply(other);
|
||||||
|
}
|
||||||
|
|
||||||
// Constructs a zero matrix
|
// Constructs a zero matrix
|
||||||
Matrix::Matrix(int rows, int cols){
|
Matrix::Matrix(int rows, int cols){
|
||||||
for(int m = 0; m < rows; m++){
|
for(int m = 0; m < rows; m++){
|
||||||
@ -185,8 +190,11 @@ void Matrix::Multiply(float value){
|
|||||||
|
|
||||||
// Set a matrix to a given value
|
// Set a matrix to a given value
|
||||||
void Matrix::Set(float value){
|
void Matrix::Set(float value){
|
||||||
this->Multiply(0.0F);
|
for(int m = 0; m < this->values.size(); m++){
|
||||||
this->Add(value);
|
for(int n = 0; n < this->values[m].size(); n++){
|
||||||
|
this->values[m][n] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Transpose a matrix
|
// Transpose a matrix
|
||||||
|
Loading…
x
Reference in New Issue
Block a user