Compare commits

..

13 Commits

4 changed files with 83 additions and 53 deletions

4
.gitignore vendored
View File

@ -1,2 +1,4 @@
build/* build/*
.vscode/* .vscode/*
main.cpp
main.exe

View File

@ -9,6 +9,7 @@ LeMA will eventually be used for graphical and machine learning usage in my othe
- **Easy to Use**: Intuitive functions. - **Easy to Use**: Intuitive functions.
- **Somewhat complete**: Supports addition, subtraction, multiplication, and more. - **Somewhat complete**: Supports addition, subtraction, multiplication, and more.
- **Ready to Integrate**: Designed to work seamlessly with C++ code. - **Ready to Integrate**: Designed to work seamlessly with C++ code.
- **Not a movie**: Is **not** about the 1999 hit movie by the Wachowski transexual sisters.
## Getting Started ## Getting Started
@ -24,7 +25,7 @@ LeMA will eventually be used for graphical and machine learning usage in my othe
#include "matrices.h" #include "matrices.h"
``` ```
That's it! You're ready to use Lema. That's it! You're ready to use LeMA.
### Example Usage ### Example Usage
@ -45,18 +46,15 @@ int main() {
```cpp ```cpp
#include "matrices.h" #include "matrices.h"
#include <iostream>
int main() { int main() {
Matrix a(2,2); Matrix a(2,2);
Matrix b(2,2); a.Set(2.0F); // Fill A with 2s
Matrix b = a;
Matrix result = a + &b; // Basic operations are possible with pointers
a.Set(2.0F); // Fill both matrices result.Print("A + B"); // Is full of 4s !
b.Set(3.0F);
a.Add(&b);
a.Print("A + B");
return 0; return 0;
} }
``` ```
@ -65,15 +63,14 @@ int main() {
```cpp ```cpp
#include "matrices.h" #include "matrices.h"
#include <iostream>
int main() { int main() {
Matrix a(3, 3); Matrix a(3, 3);
Matrix b(3, 1); // Let's multiply by a vector Matrix b(3, 1); // Let's multiply by a vector
Matrix result = a.Multiply(&b); Matrix result = a * &b;
result.Print("A x B"); result.Print("A x B"); // Result is a 3 by 1 vector
return 0; return 0;
} }

View File

@ -1,19 +0,0 @@
#include "matrices.h"
// using namespace std;
int main()
{
srand(time(0));
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

@ -16,7 +16,7 @@ class Matrix{
inline Matrix Swap(const Matrix*); inline Matrix Swap(const Matrix*);
inline void Multiply(float); inline Matrix Multiply(float);
inline Matrix Multiply(const Matrix*); inline Matrix Multiply(const Matrix*);
inline void Hadamard(const Matrix*); inline void Hadamard(const Matrix*);
@ -24,17 +24,24 @@ 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 Matrix Function(float (*f)(float));
inline void Print(std::string_view); inline void Print(std::string_view);
inline Matrix Transpose(); inline Matrix Transpose();
// --- Operators // Operators
// 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*);
inline Matrix operator*(const Matrix*);
inline Matrix operator+(float);
inline Matrix operator-(float);
inline Matrix operator*(float);
// Constructors // Constructors
inline Matrix(int, int); inline Matrix(int, int);
@ -49,6 +56,37 @@ Matrix Matrix::operator+(const Matrix* other){
return this->Add(other); return this->Add(other);
} }
Matrix Matrix::operator-(const Matrix* other){
return this->Substract(other);
}
Matrix Matrix::operator*(const Matrix* other){
return this->Multiply(other);
}
Matrix Matrix::operator+(float value){
return this->Add(value);
}
Matrix Matrix::operator-(float value){
return this->Substract(value);
}
Matrix Matrix::operator*(float value){
return this->Multiply(value);
}
Matrix Matrix::Function(float (*f)(float)){
Matrix result = this;
for(int m = 0; m < result.values.size(); m++){
for(int n = 0; n < result.values[m].size(); n++){
// Execute function on every value
result.values[m][n] = f(result.values[m][n]);
}
}
return result;
}
// 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,21 +156,26 @@ 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
void Matrix::Print(std::string_view titre){ void Matrix::Print(std::string_view title){
std::cout << titre << std::endl; std::cout << title << std::endl;
for(int m = 0; m < values.size(); m++){ for(int m = 0; m < values.size(); m++){
std::cout << '|'; std::cout << '|';
for(int n = 0; n < values[m].size(); n++){ for(int n = 0; n < values[m].size(); n++){
@ -154,27 +197,34 @@ 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
void Matrix::Multiply(float value){ Matrix Matrix::Multiply(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;
} }
// 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