From 2a412ddb13ef96cb88efc412c425e27c3d3d7376 Mon Sep 17 00:00:00 2001 From: FyloZ Date: Mon, 29 Jan 2024 14:27:22 -0500 Subject: [PATCH] Fix row/col des matrices --- labo01/Matrix.h | 20 ++++++++++---------- labo01/Operators.h | 8 ++++---- labo01/main.cpp | 36 ++++++++---------------------------- 3 files changed, 22 insertions(+), 42 deletions(-) diff --git a/labo01/Matrix.h b/labo01/Matrix.h index 378e016..345dba4 100644 --- a/labo01/Matrix.h +++ b/labo01/Matrix.h @@ -66,7 +66,7 @@ namespace gti320 { for (int col = 0; col < this->cols(); col++) { for (int row = 0; row < this->rows(); row++) { - this(col, row) = submatrix(col, row); + this(row, col) = submatrix(row, col); } } return *this; @@ -76,7 +76,7 @@ namespace gti320 { * Accesseur à une entrée de la matrice (lecture seule) */ _Scalar operator()(int i, int j) const { - int index = i * this->rows() + j; + int index = i + j * this->rows(); return this->m_storage.data()[index]; } @@ -84,7 +84,7 @@ namespace gti320 { * Accesseur à une entrée de la matrice (lecture ou écriture) */ _Scalar &operator()(int i, int j) { - int index = i * this->rows() + j; + int index = i + j * this->rows(); return this->m_storage.data()[index]; } @@ -111,7 +111,7 @@ namespace gti320 { auto transposed = Matrix<_OtherScalar, _OtherRows, _OtherCols, _OtherStorage>(this->cols(), this->rows()); for (int col = 0; col < this->cols(); col++) { for (int row = 0; row < this->rows(); row++) { - transposed(row, col) = (*this)(col, row); + transposed(col, row) = (*this)(row, col); } } return transposed; @@ -172,7 +172,7 @@ namespace gti320 { for (int row = 0; row < this->rows(); row++) { for (int col = 0; col < this->cols(); col++) { - this(col, row) = submatrix(col, row); + this(row, col) = submatrix(row, col); } } return *this; @@ -182,7 +182,7 @@ namespace gti320 { * Accesseur à une entrée de la matrice (lecture seule) */ _Scalar operator()(int i, int j) const { - int index = i + j * this->cols(); + int index = i * this->cols() + j; return this->m_storage.data()[index]; } @@ -190,7 +190,7 @@ namespace gti320 { * Accesseur à une entrée de la matrice (lecture ou écriture) */ _Scalar &operator()(int i, int j) { - int index = i + j * this->cols(); + int index = i * this->cols() + j; return this->m_storage.data()[index]; } @@ -217,7 +217,7 @@ namespace gti320 { for (int row = 0; row < this->rows(); row++) { for (int col = 0; col < this->cols(); col++) { - t(row, col) = (*this)(col, row); + t(col, row) = (*this)(row, col); } } @@ -310,7 +310,7 @@ namespace gti320 { for (int col = 0; col < this->cols(); col++) { for (int row = 0; row < this->rows(); row++) { - (*this)(col, row) = matrix(col, row); + (*this)(row, col) = matrix(row, col); } } @@ -362,7 +362,7 @@ namespace gti320 { for (int col = 0; col < this->cols(); col++) { for (int row = 0; row < this->rows(); row++) { - t(row, col) = (*this)(col, row); + t(col, row) = (*this)(row, col); } } diff --git a/labo01/Operators.h b/labo01/Operators.h index 64c6b28..615770f 100644 --- a/labo01/Operators.h +++ b/labo01/Operators.h @@ -32,7 +32,7 @@ namespace gti320 { for (int col = 0; col < B.cols(); col++) { for (int row = 0; row < A.rows(); row++) { for (int k = 0; k < A.cols(); k++) { - result(col, row) += A(k, row) * B(col, k); + result(row, col) += A(row, k) * B(k, col); } } } @@ -57,7 +57,7 @@ namespace gti320 { for (int col = 0; col < A.cols(); col++) { for (int row = 0; row < B.rows(); row++) { for (int k = 0; k < B.cols(); k++) { - result(k, row) += A(col, row) * B(k, col); + result(row, k) += A(row, col) * B(col, k); } } } @@ -82,7 +82,7 @@ namespace gti320 { for (int col = 0; col < B.cols(); col++) { for (int row = 0; row < A.rows(); row++) { for (int k = 0; k < A.cols(); k++) { - result(col, row) += A(k, row) * B(col, k); + result(row, col) += A(row, k) * B(k, col); } } } @@ -127,7 +127,7 @@ namespace gti320 { for (int col = 0; col < A.cols(); col++) { for (int row = 0; row < A.rows(); row++) { - result(row, col) = A(row, col) + B(row, col); + result(col, row) = A(col, row) + B(col, row); } } diff --git a/labo01/main.cpp b/labo01/main.cpp index 45782f1..e48c202 100644 --- a/labo01/main.cpp +++ b/labo01/main.cpp @@ -224,31 +224,11 @@ TEST(TestLabo1, MatrixMatrixOperators) { { // Création d'un matrice à stockage par lignes Matrix A(5, 5); - A(0, 0) = 0.8147; - A(0, 1) = 0.0975; - A(0, 2) = 0.1576; - A(0, 3) = 0.1419; - A(0, 4) = 0.6557; - A(1, 0) = 0.9058; - A(1, 1) = 0.2785; - A(1, 2) = 0.9706; - A(1, 3) = 0.4218; - A(1, 4) = 0.0357; - A(2, 0) = 0.1270; - A(2, 1) = 0.5469; - A(2, 2) = 0.9572; - A(2, 3) = 0.9157; - A(2, 4) = 0.8491; - A(3, 0) = 0.9134; - A(3, 1) = 0.9575; - A(3, 2) = 0.4854; - A(3, 3) = 0.7922; - A(3, 4) = 0.9340; - A(4, 0) = 0.6324; - A(4, 1) = 0.9649; - A(4, 2) = 0.8003; - A(4, 3) = 0.9595; - A(4, 4) = 0.6787; + A(0, 0) = 0.8147; A(0, 1) = 0.0975; A(0, 2) = 0.1576; A(0, 3) = 0.1419; A(0, 4) = 0.6557; + A(1, 0) = 0.9058; A(1, 1) = 0.2785; A(1, 2) = 0.9706; A(1, 3) = 0.4218; A(1, 4) = 0.0357; + A(2, 0) = 0.1270; A(2, 1) = 0.5469; A(2, 2) = 0.9572; A(2, 3) = 0.9157; A(2, 4) = 0.8491; + A(3, 0) = 0.9134; A(3, 1) = 0.9575; A(3, 2) = 0.4854; A(3, 3) = 0.7922; A(3, 4) = 0.9340; + A(4, 0) = 0.6324; A(4, 1) = 0.9649; A(4, 2) = 0.8003; A(4, 3) = 0.9595; A(4, 4) = 0.6787; // Test : transposée (le résultat est une matrice à stockage par // colonnes) @@ -598,7 +578,7 @@ TEST(TestLabo1, Supplementaires) { expected = 1; } - EXPECT_EQ(MC(col, row), expected); + EXPECT_EQ(MC(row, col), expected); } } @@ -632,7 +612,7 @@ TEST(TestLabo1, Supplementaires) { MC(1, 0) = 5; MC(0, 1) = 6; EXPECT_EQ(MC.data()[0], MC(0, 0)); - EXPECT_EQ(MC.data()[1], MC(0, 1)); + EXPECT_EQ(MC.data()[1], MC(1, 0)); // Test 9: Stockage par rangée Matrix MS(4, 6); @@ -640,7 +620,7 @@ TEST(TestLabo1, Supplementaires) { MS(1, 0) = 5; MS(0, 1) = 6; EXPECT_EQ(MS.data()[0], MS(0, 0)); - EXPECT_EQ(MS.data()[1], MS(1, 0)); + EXPECT_EQ(MS.data()[1], MS(0, 1)); // === Vecteurs === // Test 10: Copie