From 89007bfa9f5671014fad054b974e546dbb3334cc Mon Sep 17 00:00:00 2001 From: Leto Date: Mon, 23 Dec 2024 21:04:43 +0100 Subject: [PATCH] Added Matrix class and basic functions --- main.cpp | 15 ++++++++ matrices.cpp | 0 matrices.h | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 main.cpp create mode 100644 matrices.cpp create mode 100644 matrices.h diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..60b2481 --- /dev/null +++ b/main.cpp @@ -0,0 +1,15 @@ +#include +#include "matrices.h" +// using namespace std; + +int main() +{ + srand(time(0)); + + Matrix test(3,2); + test.Randomize(); + test.Print(""); + test.Transpose(); + test.Print(""); + return 0; +} \ No newline at end of file diff --git a/matrices.cpp b/matrices.cpp new file mode 100644 index 0000000..e69de29 diff --git a/matrices.h b/matrices.h new file mode 100644 index 0000000..244bdb5 --- /dev/null +++ b/matrices.h @@ -0,0 +1,99 @@ +#include +#include + +class Matrix{ + private: + std::vector> values; + public: + void Randomize(); + void Randomize(float, float); + + void Set(float); + + void Factor(float); + + void Add(float); + + void Print(std::string_view); + + void Transpose(); + + // Constructors + Matrix(int, int); +}; + +// Constructs a zero matrix +Matrix::Matrix(int rows, int cols){ + for(int m = 0; m < rows; m++){ + std::vector buffer = {}; + for(int n = 0; n < cols; n++){ + buffer.push_back(0.0F); + } + this->values.push_back(buffer); + } +} + +// Print a matrix in terminal, with a title +void Matrix::Print(std::string_view titre){ + std::cout << titre << std::endl; + for(int m = 0; m < values.size(); m++){ + std::cout << '|'; + for(int n = 0; n < values[m].size(); n++){ + std::cout << values[m][n] << "|"; + } + std::cout << std::endl; + } +} + +// 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; + } + } +} + +// Multiply every matrix case by a given factor +void Matrix::Factor(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; + } + } +} + +// Set a matrix to a given value +void Matrix::Set(float value){ + this->Factor(0.0F); + this->Add(value); +} + +// Transpose a matrix +void Matrix::Transpose(){ + std::vector> buffer = this->values; + this->values = {}; + + // Invert matrix size + for(int m = 0; m < buffer[0].size(); m++){ + std::vector line = {}; + for(int n = 0; n < buffer.size(); n++){ + line.push_back(buffer[n][m]); + } + this->values.push_back(line); + } +} + +// Randomize a matrix from 0.0F to 10.0F +void Matrix::Randomize(){ + this->Randomize(0.0F, 10.0F); +} + +// Randomize a matrix from min to max +void Matrix::Randomize(float min, float max){ + for(int m = 0; m < this->values.size(); m++){ + for(int n = 0; n < this->values[m].size(); n++){ + this->values[m][n] = min + ((float)rand()/(float)(RAND_MAX)) * (max - min); + } + } +} \ No newline at end of file