Compare commits
No commits in common. "7e35b218d94688bee3df716f9f17278a16c26f58" and "6eb4b00c12837dec254fd4e41659694cdcd896d0" have entirely different histories.
7e35b218d9
...
6eb4b00c12
7
main.cpp
7
main.cpp
@ -1,3 +1,4 @@
|
|||||||
|
#include<iostream>
|
||||||
#include "matrices.h"
|
#include "matrices.h"
|
||||||
// using namespace std;
|
// using namespace std;
|
||||||
|
|
||||||
@ -5,15 +6,13 @@ int main()
|
|||||||
{
|
{
|
||||||
srand(time(0));
|
srand(time(0));
|
||||||
|
|
||||||
Matrix a(3,2);
|
Matrix a(3,1);
|
||||||
a.Randomize();
|
a.Randomize();
|
||||||
|
|
||||||
Matrix b = a;
|
Matrix b = a;
|
||||||
|
|
||||||
a.Print("A");
|
a.Print("A");
|
||||||
b.Print("B");
|
b.Print("B");
|
||||||
|
|
||||||
Matrix result = a + &b;
|
|
||||||
result.Print("A + B");
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
75
matrices.h
75
matrices.h
@ -14,39 +14,33 @@ class Matrix{
|
|||||||
|
|
||||||
inline void Set(float);
|
inline void Set(float);
|
||||||
|
|
||||||
inline Matrix Swap(const Matrix*);
|
|
||||||
|
|
||||||
inline void Multiply(float);
|
inline void Multiply(float);
|
||||||
inline Matrix Multiply(const Matrix*);
|
inline Matrix Multiply(Matrix*);
|
||||||
|
|
||||||
inline void Hadamard(const Matrix*);
|
inline void Hadamard(Matrix*);
|
||||||
|
|
||||||
inline Matrix Add(float);
|
inline void Add(float);
|
||||||
inline Matrix Add(const Matrix*);
|
inline void Add(Matrix*);
|
||||||
|
|
||||||
inline void Substract(float);
|
inline void Substract(float);
|
||||||
inline void Substract(const Matrix*);
|
inline void Substract(Matrix*);
|
||||||
|
|
||||||
inline void Print(std::string_view);
|
inline void Print(std::string_view);
|
||||||
|
|
||||||
inline Matrix Transpose();
|
inline void Transpose();
|
||||||
|
|
||||||
// --- Operators
|
// --- Operators
|
||||||
// Assign
|
// Assign
|
||||||
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);
|
||||||
inline Matrix(const Matrix*);
|
inline Matrix(Matrix*);
|
||||||
};
|
};
|
||||||
|
|
||||||
Matrix Matrix::operator=(const Matrix* other){
|
Matrix Matrix::operator=(const Matrix* other){
|
||||||
return this->Swap(other);
|
this->values = other->values;
|
||||||
}
|
return *this;
|
||||||
|
|
||||||
Matrix Matrix::operator+(const Matrix* other){
|
|
||||||
return this->Add(other);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Constructs a zero matrix
|
// Constructs a zero matrix
|
||||||
@ -60,16 +54,11 @@ Matrix::Matrix(int rows, int cols){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix::Matrix(const Matrix* other){
|
Matrix::Matrix(Matrix* other){
|
||||||
this->values = other->values;
|
this->values = other->values;
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix Matrix::Swap(const Matrix* other){
|
void Matrix::Hadamard(Matrix* other){
|
||||||
this->values = other->values;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Matrix::Hadamard(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(),
|
||||||
@ -82,7 +71,7 @@ void Matrix::Hadamard(const Matrix* other){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Multiply 2 matrices (AxB = this x other)
|
// Multiply 2 matrices (AxB = this x other)
|
||||||
Matrix Matrix::Multiply(const Matrix* other){
|
Matrix Matrix::Multiply(Matrix* other){
|
||||||
// Matrices need to be of right size
|
// Matrices need to be of right size
|
||||||
assertm(this->values[0].size() == other->values.size(),"Wrong matrix size");
|
assertm(this->values[0].size() == other->values.size(),"Wrong matrix size");
|
||||||
|
|
||||||
@ -102,23 +91,20 @@ Matrix Matrix::Multiply(const Matrix* other){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add 2 matrices
|
// Add 2 matrices
|
||||||
Matrix Matrix::Add(const Matrix* other){
|
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(),
|
||||||
"Wrong matrix size");
|
"Wrong matrix size");
|
||||||
|
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] += other->values[m][n];
|
||||||
for(int n = 0; n < result.values[m].size(); n++){
|
|
||||||
result.values[m][n] += other->values[m][n];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Substract 2 matrices
|
// Substract 2 matrices
|
||||||
void Matrix::Substract(const Matrix* other){
|
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(),
|
||||||
@ -143,14 +129,12 @@ void Matrix::Print(std::string_view titre){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add a constant value to every matrix case
|
// Add a constant value to every matrix case
|
||||||
Matrix Matrix::Add(float value){
|
void Matrix::Add(float value){
|
||||||
Matrix result = this;
|
for(int m = 0; m < this->values.size(); m++){
|
||||||
for(int m = 0; m < result.values.size(); m++){
|
for(int n = 0; n < this->values[m].size(); n++){
|
||||||
for(int n = 0; n < result.values[m].size(); n++){
|
this->values[m][n] += value;
|
||||||
result.values[m][n] += value;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Substract a constant value to every matrix case
|
// Substract a constant value to every matrix case
|
||||||
@ -178,17 +162,18 @@ void Matrix::Set(float value){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Transpose a matrix
|
// Transpose a matrix
|
||||||
Matrix Matrix::Transpose(){
|
void Matrix::Transpose(){
|
||||||
// Transposed matrix size is inverted
|
std::vector<std::vector<float>> buffer = this->values;
|
||||||
Matrix result(this->values[0].size(), this->values.size());
|
this->values = {};
|
||||||
|
|
||||||
for(int m = 0; m < result.values.size(); m++){
|
// Invert matrix size
|
||||||
for(int n = 0; n < result.values[m].size(); n++){
|
for(int m = 0; m < buffer[0].size(); m++){
|
||||||
result.values[m][n] = this->values[n][m];
|
std::vector<float> row = {};
|
||||||
|
for(int n = 0; n < buffer.size(); n++){
|
||||||
|
row.push_back(buffer[n][m]);
|
||||||
}
|
}
|
||||||
|
this->values.push_back(row);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Randomize a matrix from 0.0F to 10.0F
|
// Randomize a matrix from 0.0F to 10.0F
|
||||||
|
Loading…
x
Reference in New Issue
Block a user