Added Hadamard Product

This commit is contained in:
Leto 2024-12-23 21:52:51 +01:00
parent 2fe209fb2b
commit d126e6aa63

View File

@ -16,6 +16,8 @@ class Matrix{
void Multiply(float); void Multiply(float);
Matrix Multiply(Matrix*); Matrix Multiply(Matrix*);
void Hadamard(Matrix*);
void Add(float); void Add(float);
void Add(Matrix*); void Add(Matrix*);
@ -46,8 +48,23 @@ Matrix::Matrix(Matrix* other){
this->values = other->values; this->values = other->values;
} }
void Matrix::Hadamard(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];
}
}
}
// Multiply 2 matrices (AxB = this x other) // Multiply 2 matrices (AxB = this x other)
Matrix Matrix::Multiply(Matrix* other){ Matrix Matrix::Multiply(Matrix* other){
// Matrices need to be of right size
assertm(this->values[0].size() == other->values.size(),"Wrong matrix size");
// Resulting size is this->M x other->N // Resulting size is this->M x other->N
Matrix result(this->values.size(), other->values[0].size()); Matrix result(this->values.size(), other->values[0].size());
for(int m = 0; m < result.values.size(); m++){ for(int m = 0; m < result.values.size(); m++){
@ -68,7 +85,7 @@ void Matrix::Add(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(),
"Matrices need to be the same size"); "Wrong matrix size");
for(int m = 0; m < this->values.size(); m++){ for(int m = 0; m < this->values.size(); m++){
for(int n = 0; n < this->values[m].size(); n++){ for(int n = 0; n < this->values[m].size(); n++){
this->values[m][n] += other->values[m][n]; this->values[m][n] += other->values[m][n];
@ -81,7 +98,7 @@ void Matrix::Substract(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(),
"Matrices need to be the same size"); "Wrong matrix size");
for(int m = 0; m < this->values.size(); m++){ for(int m = 0; m < this->values.size(); m++){
for(int n = 0; n < this->values[m].size(); n++){ for(int n = 0; n < this->values[m].size(); n++){
this->values[m][n] -= other->values[m][n]; this->values[m][n] -= other->values[m][n];