Added operator = overload

This commit is contained in:
Leto 2024-12-24 13:58:59 +01:00
parent 96cea58568
commit 6eb4b00c12
2 changed files with 27 additions and 20 deletions

View File

@ -6,16 +6,13 @@ int main()
{ {
srand(time(0)); srand(time(0));
Matrix a(2,1); Matrix a(3,1);
Matrix b(1,2);
a.Randomize(); a.Randomize();
b.Randomize();
Matrix b = a;
a.Print("A"); a.Print("A");
b.Print("B"); b.Print("B");
a.Multiply(&b).Print("A x B");
return 0; return 0;
} }

View File

@ -1,6 +1,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <cassert> #include <cassert>
#include <iostream>
#define assertm(exp, msg) assert((void(msg), exp)) #define assertm(exp, msg) assert((void(msg), exp))
@ -8,31 +9,40 @@ class Matrix{
private: private:
std::vector<std::vector<float>> values; std::vector<std::vector<float>> values;
public: public:
void Randomize(); inline void Randomize();
void Randomize(float, float); inline void Randomize(float, float);
void Set(float); inline void Set(float);
void Multiply(float); inline void Multiply(float);
Matrix Multiply(Matrix*); inline Matrix Multiply(Matrix*);
void Hadamard(Matrix*); inline void Hadamard(Matrix*);
void Add(float); inline void Add(float);
void Add(Matrix*); inline void Add(Matrix*);
void Substract(float); inline void Substract(float);
void Substract(Matrix*); inline void Substract(Matrix*);
void Print(std::string_view); inline void Print(std::string_view);
void Transpose(); inline void Transpose();
// --- Operators
// Assign
inline Matrix operator=(const Matrix*);
// Constructors // Constructors
Matrix(int, int); inline Matrix(int, int);
Matrix(Matrix*); inline Matrix(Matrix*);
}; };
Matrix Matrix::operator=(const Matrix* other){
this->values = other->values;
return *this;
}
// 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++){