forked from leto/LeMA
Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
fba09a5c40 | ||
![]() |
7a2b42e06d | ||
91348e7c09 | |||
2d2d6afcfb | |||
e8f843a420 | |||
a4dabb247d | |||
a3db9391d3 |
4
.gitignore
vendored
4
.gitignore
vendored
@ -1,2 +1,4 @@
|
||||
build/*
|
||||
.vscode/*
|
||||
.vscode/*
|
||||
main.cpp
|
||||
main.exe
|
12
main.cpp
12
main.cpp
@ -1,12 +0,0 @@
|
||||
#include "matrices.h"
|
||||
|
||||
int main() {
|
||||
Matrix a(3, 3);
|
||||
Matrix b(3, 1); // Let's multiply by a vector
|
||||
|
||||
Matrix result = a * &b;
|
||||
|
||||
result.Print("A x B"); // Result is a 3 by 1 vector
|
||||
|
||||
return 0;
|
||||
}
|
42
matrices.h
42
matrices.h
@ -16,7 +16,7 @@ class Matrix{
|
||||
|
||||
inline Matrix Swap(const Matrix*);
|
||||
|
||||
inline void Multiply(float); // TODO : Make this return a Matrix
|
||||
inline Matrix Multiply(float);
|
||||
inline Matrix Multiply(const Matrix*);
|
||||
|
||||
inline void Hadamard(const Matrix*);
|
||||
@ -27,6 +27,8 @@ class Matrix{
|
||||
inline Matrix Substract(float);
|
||||
inline Matrix Substract(const Matrix*);
|
||||
|
||||
inline Matrix Function(float (*f)(float));
|
||||
|
||||
inline void Print(std::string_view);
|
||||
|
||||
inline Matrix Transpose();
|
||||
@ -36,7 +38,10 @@ class Matrix{
|
||||
inline Matrix operator+(const Matrix*);
|
||||
inline Matrix operator-(const Matrix*);
|
||||
inline Matrix operator*(const Matrix*);
|
||||
// TODO : Add float parameters for these
|
||||
|
||||
inline Matrix operator+(float);
|
||||
inline Matrix operator-(float);
|
||||
inline Matrix operator*(float);
|
||||
|
||||
// Constructors
|
||||
inline Matrix(int, int);
|
||||
@ -59,6 +64,29 @@ 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
|
||||
Matrix::Matrix(int rows, int cols){
|
||||
for(int m = 0; m < rows; m++){
|
||||
@ -180,12 +208,14 @@ Matrix Matrix::Substract(float value){
|
||||
}
|
||||
|
||||
// Multiply every matrix case by a given factor
|
||||
void Matrix::Multiply(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::Multiply(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;
|
||||
}
|
||||
|
||||
// Set a matrix to a given value
|
||||
|
Loading…
x
Reference in New Issue
Block a user