Added - operator
This commit is contained in:
parent
7e35b218d9
commit
b919f4a249
4
main.cpp
4
main.cpp
@ -12,8 +12,8 @@ int main()
|
|||||||
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 - B");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
32
matrices.h
32
matrices.h
@ -24,8 +24,8 @@ class Matrix{
|
|||||||
inline Matrix Add(float);
|
inline Matrix Add(float);
|
||||||
inline Matrix Add(const Matrix*);
|
inline Matrix Add(const Matrix*);
|
||||||
|
|
||||||
inline void Substract(float);
|
inline Matrix Substract(float);
|
||||||
inline void Substract(const Matrix*);
|
inline Matrix Substract(const Matrix*);
|
||||||
|
|
||||||
inline void Print(std::string_view);
|
inline void Print(std::string_view);
|
||||||
|
|
||||||
@ -35,6 +35,7 @@ class Matrix{
|
|||||||
// Assign
|
// Assign
|
||||||
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);
|
||||||
@ -49,6 +50,10 @@ Matrix Matrix::operator+(const Matrix* other){
|
|||||||
return this->Add(other);
|
return this->Add(other);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Matrix Matrix::operator-(const Matrix* other){
|
||||||
|
return this->Substract(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++){
|
||||||
@ -118,16 +123,21 @@ Matrix Matrix::Add(const Matrix* other){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Substract 2 matrices
|
// Substract 2 matrices
|
||||||
void Matrix::Substract(const Matrix* other){
|
Matrix Matrix::Substract(const Matrix* other){
|
||||||
// Matrices need to be the same size
|
// Matrices need to be the same size
|
||||||
assertm(this->values.size() == other->values.size() &&
|
assertm(this->values.size() == other->values.size() &&
|
||||||
this->values[0].size() == other->values[0].size(),
|
this->values[0].size() == other->values[0].size(),
|
||||||
"Wrong matrix size");
|
"Wrong matrix size");
|
||||||
for(int m = 0; m < this->values.size(); m++){
|
|
||||||
for(int n = 0; n < this->values[m].size(); n++){
|
Matrix result = this;
|
||||||
this->values[m][n] -= other->values[m][n];
|
|
||||||
|
for(int m = 0; m < result.values.size(); m++){
|
||||||
|
for(int n = 0; n < result.values[m].size(); n++){
|
||||||
|
result.values[m][n] -= other->values[m][n];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print a matrix in terminal, with a title
|
// Print a matrix in terminal, with a title
|
||||||
@ -154,12 +164,14 @@ Matrix Matrix::Add(float value){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Substract a constant value to every matrix case
|
// Substract a constant value to every matrix case
|
||||||
void Matrix::Substract(float value){
|
Matrix Matrix::Substract(float value){
|
||||||
for(int m = 0; m < this->values.size(); m++){
|
Matrix result = this;
|
||||||
for(int n = 0; n < this->values[m].size(); n++){
|
for(int m = 0; m < result.values.size(); m++){
|
||||||
this->values[m][n] -= value;
|
for(int n = 0; n < result.values[m].size(); n++){
|
||||||
|
result.values[m][n] -= value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Multiply every matrix case by a given factor
|
// Multiply every matrix case by a given factor
|
||||||
|
Loading…
x
Reference in New Issue
Block a user