Compare commits
16 Commits
6eb4b00c12
...
main
Author | SHA1 | Date | |
---|---|---|---|
fba09a5c40 | |||
7a2b42e06d | |||
91348e7c09 | |||
2d2d6afcfb | |||
e8f843a420 | |||
a4dabb247d | |||
a3db9391d3 | |||
44b726f3f6 | |||
077702eb73 | |||
57fa219441 | |||
3c596e9601 | |||
e4026b8d60 | |||
b919f4a249 | |||
7e35b218d9 | |||
e0d6d480da | |||
78a8185e19 |
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,2 +1,4 @@
|
|||||||
build/*
|
build/*
|
||||||
.vscode/*
|
.vscode/*
|
||||||
|
main.cpp
|
||||||
|
main.exe
|
19
README.md
19
README.md
@ -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
|
||||||
|
|
||||||
a.Set(2.0F); // Fill both matrices
|
Matrix b = a;
|
||||||
b.Set(3.0F);
|
Matrix result = a + &b; // Basic operations are possible with pointers
|
||||||
|
|
||||||
a.Add(&b);
|
result.Print("A + B"); // Is full of 4s !
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
18
main.cpp
18
main.cpp
@ -1,18 +0,0 @@
|
|||||||
#include<iostream>
|
|
||||||
#include "matrices.h"
|
|
||||||
// using namespace std;
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
srand(time(0));
|
|
||||||
|
|
||||||
Matrix a(3,1);
|
|
||||||
a.Randomize();
|
|
||||||
|
|
||||||
Matrix b = a;
|
|
||||||
|
|
||||||
a.Print("A");
|
|
||||||
b.Print("B");
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
163
matrices.h
163
matrices.h
@ -14,33 +14,77 @@ class Matrix{
|
|||||||
|
|
||||||
inline void Set(float);
|
inline void Set(float);
|
||||||
|
|
||||||
inline void Multiply(float);
|
inline Matrix Swap(const Matrix*);
|
||||||
inline Matrix Multiply(Matrix*);
|
|
||||||
|
|
||||||
inline void Hadamard(Matrix*);
|
inline Matrix Multiply(float);
|
||||||
|
inline Matrix Multiply(const Matrix*);
|
||||||
|
|
||||||
inline void Add(float);
|
inline void Hadamard(const Matrix*);
|
||||||
inline void Add(Matrix*);
|
|
||||||
|
|
||||||
inline void Substract(float);
|
inline Matrix Add(float);
|
||||||
inline void Substract(Matrix*);
|
inline Matrix Add(const Matrix*);
|
||||||
|
|
||||||
|
inline Matrix Substract(float);
|
||||||
|
inline Matrix Substract(const Matrix*);
|
||||||
|
|
||||||
|
inline Matrix Function(float (*f)(float));
|
||||||
|
|
||||||
inline void Print(std::string_view);
|
inline void Print(std::string_view);
|
||||||
|
|
||||||
inline void 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+(float);
|
||||||
|
inline Matrix operator-(float);
|
||||||
|
inline Matrix operator*(float);
|
||||||
|
|
||||||
// Constructors
|
// Constructors
|
||||||
inline Matrix(int, int);
|
inline Matrix(int, int);
|
||||||
inline Matrix(Matrix*);
|
inline Matrix(const Matrix*);
|
||||||
};
|
};
|
||||||
|
|
||||||
Matrix Matrix::operator=(const Matrix* other){
|
Matrix Matrix::operator=(const Matrix* other){
|
||||||
this->values = other->values;
|
return this->Swap(other);
|
||||||
return *this;
|
}
|
||||||
|
|
||||||
|
Matrix Matrix::operator+(const Matrix* 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
|
||||||
@ -54,11 +98,16 @@ Matrix::Matrix(int rows, int cols){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix::Matrix(Matrix* other){
|
Matrix::Matrix(const Matrix* other){
|
||||||
this->values = other->values;
|
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
|
// 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(),
|
||||||
@ -71,7 +120,7 @@ void Matrix::Hadamard(Matrix* other){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Multiply 2 matrices (AxB = this x 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
|
// 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");
|
||||||
|
|
||||||
@ -91,34 +140,42 @@ Matrix Matrix::Multiply(Matrix* other){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add 2 matrices
|
// Add 2 matrices
|
||||||
void Matrix::Add(Matrix* other){
|
Matrix Matrix::Add(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;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Substract 2 matrices
|
// Substract 2 matrices
|
||||||
void Matrix::Substract(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++){
|
||||||
@ -129,51 +186,59 @@ void Matrix::Print(std::string_view titre){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add a constant value to every matrix case
|
// Add a constant value to every matrix case
|
||||||
void Matrix::Add(float value){
|
Matrix Matrix::Add(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;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
void Matrix::Transpose(){
|
Matrix Matrix::Transpose(){
|
||||||
std::vector<std::vector<float>> buffer = this->values;
|
// Transposed matrix size is inverted
|
||||||
this->values = {};
|
Matrix result(this->values[0].size(), this->values.size());
|
||||||
|
|
||||||
// Invert matrix size
|
for(int m = 0; m < result.values.size(); m++){
|
||||||
for(int m = 0; m < buffer[0].size(); m++){
|
for(int n = 0; n < result.values[m].size(); n++){
|
||||||
std::vector<float> row = {};
|
result.values[m][n] = this->values[n][m];
|
||||||
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
|
||||||
|
Reference in New Issue
Block a user