Compare commits

...

3 Commits

2 changed files with 49 additions and 33 deletions

View File

@ -1,4 +1,3 @@
#include<iostream>
#include "matrices.h"
// using namespace std;
@ -6,13 +5,15 @@ int main()
{
srand(time(0));
Matrix a(3,1);
Matrix a(3,2);
a.Randomize();
Matrix b = a;
a.Print("A");
b.Print("B");
Matrix result = a + &b;
result.Print("A + B");
return 0;
}

View File

@ -14,33 +14,39 @@ class Matrix{
inline void Set(float);
inline Matrix Swap(const Matrix*);
inline void Multiply(float);
inline Matrix Multiply(Matrix*);
inline Matrix Multiply(const Matrix*);
inline void Hadamard(Matrix*);
inline void Hadamard(const Matrix*);
inline void Add(float);
inline void Add(Matrix*);
inline Matrix Add(float);
inline Matrix Add(const Matrix*);
inline void Substract(float);
inline void Substract(Matrix*);
inline void Substract(const Matrix*);
inline void Print(std::string_view);
inline void Transpose();
inline Matrix Transpose();
// --- Operators
// Assign
inline Matrix operator=(const Matrix*);
inline Matrix operator+(const Matrix*);
// Constructors
inline Matrix(int, int);
inline Matrix(Matrix*);
inline Matrix(const Matrix*);
};
Matrix Matrix::operator=(const Matrix* other){
this->values = other->values;
return *this;
return this->Swap(other);
}
Matrix Matrix::operator+(const Matrix* other){
return this->Add(other);
}
// Constructs a zero matrix
@ -54,11 +60,16 @@ Matrix::Matrix(int rows, int cols){
}
}
Matrix::Matrix(Matrix* other){
Matrix::Matrix(const Matrix* other){
this->values = other->values;
}
void Matrix::Hadamard(Matrix* other){
Matrix Matrix::Swap(const Matrix* other){
this->values = other->values;
return *this;
}
void Matrix::Hadamard(const Matrix* other){
// Matrices need to be the same size
assertm(this->values.size() == other->values.size() &&
this->values[0].size() == other->values[0].size(),
@ -71,7 +82,7 @@ void Matrix::Hadamard(Matrix* other){
}
// Multiply 2 matrices (AxB = this x other)
Matrix Matrix::Multiply(Matrix* other){
Matrix Matrix::Multiply(const Matrix* other){
// Matrices need to be of right size
assertm(this->values[0].size() == other->values.size(),"Wrong matrix size");
@ -91,20 +102,23 @@ Matrix Matrix::Multiply(Matrix* other){
}
// Add 2 matrices
void Matrix::Add(Matrix* other){
Matrix Matrix::Add(const Matrix* other){
// Matrices need to be the same size
assertm(this->values.size() == other->values.size() &&
this->values[0].size() == other->values[0].size(),
"Wrong matrix 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];
Matrix result = this;
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;
}
// Substract 2 matrices
void Matrix::Substract(Matrix* other){
void Matrix::Substract(const Matrix* other){
// Matrices need to be the same size
assertm(this->values.size() == other->values.size() &&
this->values[0].size() == other->values[0].size(),
@ -129,12 +143,14 @@ void Matrix::Print(std::string_view titre){
}
// Add a constant value to every matrix case
void Matrix::Add(float 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;
Matrix Matrix::Add(float value){
Matrix result = this;
for(int m = 0; m < result.values.size(); m++){
for(int n = 0; n < result.values[m].size(); n++){
result.values[m][n] += value;
}
}
return result;
}
// Substract a constant value to every matrix case
@ -162,18 +178,17 @@ void Matrix::Set(float value){
}
// Transpose a matrix
void Matrix::Transpose(){
std::vector<std::vector<float>> buffer = this->values;
this->values = {};
Matrix Matrix::Transpose(){
// Transposed matrix size is inverted
Matrix result(this->values[0].size(), this->values.size());
// Invert matrix size
for(int m = 0; m < buffer[0].size(); m++){
std::vector<float> row = {};
for(int n = 0; n < buffer.size(); n++){
row.push_back(buffer[n][m]);
for(int m = 0; m < result.values.size(); m++){
for(int n = 0; n < result.values[m].size(); n++){
result.values[m][n] = this->values[n][m];
}
this->values.push_back(row);
}
return result;
}
// Randomize a matrix from 0.0F to 10.0F