1
0
forked from leto/LeMA

Transpose now returns Matrix object

This commit is contained in:
Leto 2024-12-24 14:08:43 +01:00
parent 78a8185e19
commit e0d6d480da
2 changed files with 11 additions and 16 deletions

View File

@ -5,15 +5,11 @@ int main()
{ {
srand(time(0)); srand(time(0));
Matrix a(3,1); Matrix a(3,2);
a.Randomize(); a.Randomize();
Matrix b = a;
a.Print("A"); a.Print("A");
b.Print("B"); a.Transpose().Print("Transposed");
a.Add(&b).Print("A + B");
return 0; return 0;
} }

View File

@ -27,7 +27,7 @@ class Matrix{
inline void Print(std::string_view); inline void Print(std::string_view);
inline void Transpose(); inline Matrix Transpose();
// --- Operators // --- Operators
// Assign // Assign
@ -168,18 +168,17 @@ void Matrix::Set(float 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