Fix row/col des matrices

This commit is contained in:
FyloZ 2024-01-29 13:59:46 -05:00
parent 9a93c8e529
commit 92d337fcc6
Signed by: william
GPG Key ID: 835378AE9AF4AE97
2 changed files with 466 additions and 413 deletions

View File

@ -64,9 +64,9 @@ namespace gti320 {
this->resize(submatrix->rows(), submatrix->cols());
}
for (int i = 0; i < this->rows(); i++) {
for (int j = 0; j < this->cols(); j++) {
this(i, j) = submatrix(i, j);
for (int col = 0; col < this->cols(); col++) {
for (int row = 0; row < this->rows(); row++) {
this(col, row) = submatrix(col, row);
}
}
return *this;
@ -109,9 +109,9 @@ namespace gti320 {
template<typename _OtherScalar, int _OtherRows, int _OtherCols, int _OtherStorage>
Matrix<_OtherScalar, _OtherRows, _OtherCols, _OtherStorage> transpose() const {
auto transposed = Matrix<_OtherScalar, _OtherRows, _OtherCols, _OtherStorage>(this->cols(), this->rows());
for (int i = 0; i < this->rows(); i++) {
for (int j = 0; j < this->cols(); j++) {
transposed(j, i) = (*this)(i, j);
for (int col = 0; col < this->cols(); col++) {
for (int row = 0; row < this->rows(); row++) {
transposed(row, col) = (*this)(col, row);
}
}
return transposed;
@ -170,9 +170,9 @@ namespace gti320 {
this->resize(submatrix->rows(), submatrix->cols());
}
for (int j = 0; j < this->cols(); j++) {
for (int i = 0; i < this->rows(); i++) {
this(i, j) = submatrix(i, j);
for (int row = 0; row < this->rows(); row++) {
for (int col = 0; col < this->cols(); col++) {
this(col, row) = submatrix(col, row);
}
}
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 = j * this->cols() + i;
int index = i + j * this->cols();
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 = j * this->cols() + i;
int index = i + j * this->cols();
return this->m_storage.data()[index];
}
@ -215,9 +215,9 @@ namespace gti320 {
Matrix<_Scalar, _RowsAtCompile, _ColsAtCompile, ColumnStorage> transpose() const {
auto t = Matrix<_Scalar, _RowsAtCompile, _ColsAtCompile, ColumnStorage>(this->cols(), this->rows());
for (int i = 0; i < this->rows(); i++) {
for (int j = 0; j < this->cols(); j++) {
t(j, i) = (*this)(i, j);
for (int row = 0; row < this->rows(); row++) {
for (int col = 0; col < this->cols(); col++) {
t(row, col) = (*this)(col, row);
}
}
@ -308,9 +308,9 @@ namespace gti320 {
assert(this->rows() == matrix.rows());
assert(this->cols() == matrix.cols());
for (int i = 0; i < this->rows(); i++) {
for (int j = 0; j < this->cols(); j++) {
(*this)(i, j) = matrix(i, j);
for (int col = 0; col < this->cols(); col++) {
for (int row = 0; row < this->rows(); row++) {
(*this)(col, row) = matrix(col, row);
}
}
@ -349,6 +349,7 @@ namespace gti320 {
int full_i = this->m_i + i;
int full_j = this->m_j + j;
return this->m_matrix(full_i, full_j);
}
@ -359,9 +360,9 @@ namespace gti320 {
Matrix<_OtherScalar, _OtherRows, _OtherCols, _OtherStorage> transpose() const {
auto t = Matrix<_OtherScalar, _OtherRows, _OtherCols, _OtherStorage>(this->rows(), this->cols());
for (int i = 0; i < this->rows(); i++) {
for (int j = 0; j < this->cols(); j++) {
t(j, i) = (*this)(i, j);
for (int col = 0; col < this->cols(); col++) {
for (int row = 0; row < this->rows(); row++) {
t(row, col) = (*this)(col, row);
}
}

View File

@ -23,8 +23,8 @@ using namespace gti320;
* Multiplication matrice * vecteur, utilisant une implémentation naive
*/
template<typename _Scalar>
static inline Vector<_Scalar, Dynamic> naiveMatrixMult(const Matrix<_Scalar, Dynamic, Dynamic, ColumnStorage>& A, const Vector<_Scalar, Dynamic>& v)
{
static inline Vector<_Scalar, Dynamic>
naiveMatrixMult(const Matrix<_Scalar, Dynamic, Dynamic, ColumnStorage> &A, const Vector<_Scalar, Dynamic> &v) {
assert(A.cols() == v.rows());
Vector<_Scalar, Dynamic> b(A.rows());
@ -44,8 +44,9 @@ static inline Vector<_Scalar, Dynamic> naiveMatrixMult(const Matrix<_Scalar, Dyn
* Addition matrice + matrice, utilisant une implémentation naive
*/
template<typename _Scalar>
static inline Matrix<_Scalar, Dynamic, Dynamic, ColumnStorage> naiveMatrixAddition(const Matrix<_Scalar, Dynamic, Dynamic, ColumnStorage>& A, const Matrix<_Scalar, Dynamic, Dynamic, ColumnStorage>& B)
{
static inline Matrix<_Scalar, Dynamic, Dynamic, ColumnStorage>
naiveMatrixAddition(const Matrix<_Scalar, Dynamic, Dynamic, ColumnStorage> &A,
const Matrix<_Scalar, Dynamic, Dynamic, ColumnStorage> &B) {
assert(A.cols() == B.cols() && A.rows() == B.rows());
Matrix<_Scalar, Dynamic, Dynamic, ColumnStorage> C(A.rows(), A.cols());
@ -62,16 +63,14 @@ static inline Matrix<_Scalar, Dynamic, Dynamic, ColumnStorage> naiveMatrixAdditi
* Multiplication matrice * matrice, utilisant une implémentation naive.
*/
template<typename _Scalar, int _Storage>
static inline Matrix<_Scalar, Dynamic, Dynamic, _Storage> naiveMatrixMult(const Matrix<_Scalar, Dynamic, Dynamic, _Storage>& A, const Matrix<_Scalar, Dynamic, Dynamic, _Storage>& B)
{
static inline Matrix<_Scalar, Dynamic, Dynamic, _Storage>
naiveMatrixMult(const Matrix<_Scalar, Dynamic, Dynamic, _Storage> &A,
const Matrix<_Scalar, Dynamic, Dynamic, _Storage> &B) {
assert(A.cols() == B.rows());
Matrix<_Scalar, Dynamic, Dynamic> product(A.rows(), B.cols());
for (int i = 0; i < A.rows(); ++i)
{
for (int j = 0; j < B.cols(); ++j)
{
for (int k = 0; k < A.cols(); ++k)
{
for (int i = 0; i < A.rows(); ++i) {
for (int j = 0; j < B.cols(); ++j) {
for (int k = 0; k < A.cols(); ++k) {
product(i, j) += A(i, k) * B(k, j);
}
}
@ -80,8 +79,7 @@ static inline Matrix<_Scalar, Dynamic, Dynamic, _Storage> naiveMatrixMult(const
}
// Test les matrice avec redimensionnement dynamique
TEST(TestLabo1, DynamicMatrixTests)
{
TEST(TestLabo1, DynamicMatrixTests) {
// Crée une matrice à taille dynamique
// (note : les valeurs par défaut du patron de la classe `Matrix` mettent le
// le nombre de ligne et de colonnes à `Dynamic`)
@ -132,8 +130,7 @@ TEST(TestLabo1, DynamicMatrixTests)
/**
* Test pour les vecteurs à taille dynamique
*/
TEST(TestLabo1, DynamicVectorSizeTest)
{
TEST(TestLabo1, DynamicVectorSizeTest) {
Vector<double> v(5);
v.setZero();
@ -161,8 +158,7 @@ TEST(TestLabo1, DynamicVectorSizeTest)
/**
* Test pour les matrice à taille fixe
*/
TEST(TestLabo1, Matrix4x4SizeTest)
{
TEST(TestLabo1, Matrix4x4SizeTest) {
Matrix4d M;
M.setZero();
@ -173,67 +169,86 @@ TEST(TestLabo1, Matrix4x4SizeTest)
/**
* Test pour les opérateurs d'arithmétique matricielle.
*/
TEST(TestLabo1, MatrixMatrixOperators)
{
TEST(TestLabo1, MatrixMatrixOperators) {
// Opérations arithmétiques avec matrices à taille dynamique
// {
// // Test : matrice identité
// Matrix<double> A(6, 6);
// A.setIdentity();
// EXPECT_DOUBLE_EQ(A(0, 0), 1.0);
// EXPECT_DOUBLE_EQ(A(1, 1), 1.0);
// EXPECT_DOUBLE_EQ(A(2, 2), 1.0);
// EXPECT_DOUBLE_EQ(A(3, 3), 1.0);
// EXPECT_DOUBLE_EQ(A(4, 4), 1.0);
// EXPECT_DOUBLE_EQ(A(5, 5), 1.0);
// EXPECT_DOUBLE_EQ(A(0, 1), 0.0);
// EXPECT_DOUBLE_EQ(A(1, 0), 0.0);
//
// // Test : produit scalaire * matrice
// const double alpha = 2.5;
// Matrix<double> B = alpha * A;
// EXPECT_DOUBLE_EQ(B(0, 0), alpha);
// EXPECT_DOUBLE_EQ(B(1, 1), alpha);
// EXPECT_DOUBLE_EQ(B(2, 2), alpha);
// EXPECT_DOUBLE_EQ(B(3, 3), alpha);
// EXPECT_DOUBLE_EQ(B(4, 4), alpha);
// EXPECT_DOUBLE_EQ(B(5, 5), alpha);
// EXPECT_DOUBLE_EQ(B(0, 1), 0.0);
// EXPECT_DOUBLE_EQ(B(1, 0), 0.0);
//
// // Test : produit matrice * matrice
// Matrix<double> C = A * B;
// EXPECT_DOUBLE_EQ(C(0, 0), A(0, 0) * B(0, 0));
// EXPECT_DOUBLE_EQ(C(1, 1), A(1, 1) * B(1, 1));
// EXPECT_DOUBLE_EQ(C(2, 2), A(2, 2) * B(2, 2));
// EXPECT_DOUBLE_EQ(C(3, 3), A(3, 3) * B(3, 3));
// EXPECT_DOUBLE_EQ(C(4, 4), A(4, 4) * B(4, 4));
// EXPECT_DOUBLE_EQ(C(5, 5), A(5, 5) * B(5, 5));
// EXPECT_DOUBLE_EQ(C(0, 1), 0.0);
// EXPECT_DOUBLE_EQ(C(2, 3), 0.0);
//
// // Test : addition matrice * matrice
// Matrix<double> A_plus_B = A + B;
// EXPECT_DOUBLE_EQ(A_plus_B(0, 0), A(0, 0) + B(0, 0));
// EXPECT_DOUBLE_EQ(A_plus_B(1, 1), A(1, 1) + B(1, 1));
// EXPECT_DOUBLE_EQ(A_plus_B(2, 2), A(2, 2) + B(2, 2));
// EXPECT_DOUBLE_EQ(A_plus_B(3, 3), A(3, 3) + B(3, 3));
// EXPECT_DOUBLE_EQ(A_plus_B(4, 4), A(4, 4) + B(4, 4));
// EXPECT_DOUBLE_EQ(A_plus_B(5, 5), A(5, 5) + B(5, 5));
// EXPECT_DOUBLE_EQ(A_plus_B(0, 1), 0.0);
// EXPECT_DOUBLE_EQ(A_plus_B(2, 3), 0.0);
// }
{
// Test : matrice identité
Matrix<double> A(6, 6);
A.setIdentity();
EXPECT_DOUBLE_EQ(A(0, 0), 1.0);
EXPECT_DOUBLE_EQ(A(1, 1), 1.0);
EXPECT_DOUBLE_EQ(A(2, 2), 1.0);
EXPECT_DOUBLE_EQ(A(3, 3), 1.0);
EXPECT_DOUBLE_EQ(A(4, 4), 1.0);
EXPECT_DOUBLE_EQ(A(5, 5), 1.0);
EXPECT_DOUBLE_EQ(A(0, 1), 0.0);
EXPECT_DOUBLE_EQ(A(1, 0), 0.0);
// Test : produit scalaire * matrice
const double alpha = 2.5;
Matrix<double> B = alpha * A;
EXPECT_DOUBLE_EQ(B(0, 0), alpha);
EXPECT_DOUBLE_EQ(B(1, 1), alpha);
EXPECT_DOUBLE_EQ(B(2, 2), alpha);
EXPECT_DOUBLE_EQ(B(3, 3), alpha);
EXPECT_DOUBLE_EQ(B(4, 4), alpha);
EXPECT_DOUBLE_EQ(B(5, 5), alpha);
EXPECT_DOUBLE_EQ(B(0, 1), 0.0);
EXPECT_DOUBLE_EQ(B(1, 0), 0.0);
// Test : produit matrice * matrice
Matrix<double> C = A * B;
EXPECT_DOUBLE_EQ(C(0, 0), A(0, 0) * B(0, 0));
EXPECT_DOUBLE_EQ(C(1, 1), A(1, 1) * B(1, 1));
EXPECT_DOUBLE_EQ(C(2, 2), A(2, 2) * B(2, 2));
EXPECT_DOUBLE_EQ(C(3, 3), A(3, 3) * B(3, 3));
EXPECT_DOUBLE_EQ(C(4, 4), A(4, 4) * B(4, 4));
EXPECT_DOUBLE_EQ(C(5, 5), A(5, 5) * B(5, 5));
EXPECT_DOUBLE_EQ(C(0, 1), 0.0);
EXPECT_DOUBLE_EQ(C(2, 3), 0.0);
// Test : addition matrice * matrice
Matrix<double> A_plus_B = A + B;
EXPECT_DOUBLE_EQ(A_plus_B(0, 0), A(0, 0) + B(0, 0));
EXPECT_DOUBLE_EQ(A_plus_B(1, 1), A(1, 1) + B(1, 1));
EXPECT_DOUBLE_EQ(A_plus_B(2, 2), A(2, 2) + B(2, 2));
EXPECT_DOUBLE_EQ(A_plus_B(3, 3), A(3, 3) + B(3, 3));
EXPECT_DOUBLE_EQ(A_plus_B(4, 4), A(4, 4) + B(4, 4));
EXPECT_DOUBLE_EQ(A_plus_B(5, 5), A(5, 5) + B(5, 5));
EXPECT_DOUBLE_EQ(A_plus_B(0, 1), 0.0);
EXPECT_DOUBLE_EQ(A_plus_B(2, 3), 0.0);
}
// Opérations arithmétique avec matrices à stockage par lignes et par
// colonnes.
{
// Création d'un matrice à stockage par lignes
Matrix<double, Dynamic, Dynamic, RowStorage> 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)
@ -242,39 +257,59 @@ TEST(TestLabo1, MatrixMatrixOperators)
// Test : multiplication matrix(ligne) * matrice(colonne)
// Note : teste seulement la première et la dernière colonne
const auto C = A * B;
EXPECT_NEAR(C(0, 0), 1.14815820000000, 1e-3); EXPECT_NEAR(C(0, 4), 1.31659795000000, 1e-3);
EXPECT_NEAR(C(1, 0), 1.00133748000000, 1e-3); EXPECT_NEAR(C(1, 4), 2.04727044000000, 1e-3);
EXPECT_NEAR(C(2, 0), 0.99433707000000, 1e-3); EXPECT_NEAR(C(2, 4), 2.82896409000000, 1e-3);
EXPECT_NEAR(C(3, 0), 1.63883925000000, 1e-3); EXPECT_NEAR(C(3, 4), 3.28401323000000, 1e-3);
EXPECT_NEAR(C(4, 0), 1.31659795000000, 1e-3); EXPECT_NEAR(C(4, 4), 3.35271580000000, 1e-3);
EXPECT_NEAR(C(0, 0), 1.14815820000000, 1e-3);
EXPECT_NEAR(C(0, 4), 1.31659795000000, 1e-3);
EXPECT_NEAR(C(1, 0), 1.00133748000000, 1e-3);
EXPECT_NEAR(C(1, 4), 2.04727044000000, 1e-3);
EXPECT_NEAR(C(2, 0), 0.99433707000000, 1e-3);
EXPECT_NEAR(C(2, 4), 2.82896409000000, 1e-3);
EXPECT_NEAR(C(3, 0), 1.63883925000000, 1e-3);
EXPECT_NEAR(C(3, 4), 3.28401323000000, 1e-3);
EXPECT_NEAR(C(4, 0), 1.31659795000000, 1e-3);
EXPECT_NEAR(C(4, 4), 3.35271580000000, 1e-3);
// Test : multiplication matrice(colonne) * matrice(ligne)
// Note : teste seulement la première et la dernière colonne
const auto C2 = B * A;
EXPECT_NEAR(C2(0, 0), 2.73456805000000, 1e-3); EXPECT_NEAR(C2(0, 4), 1.95669703000000, 1e-3);
EXPECT_NEAR(C2(1, 0), 1.88593811000000, 1e-3); EXPECT_NEAR(C2(1, 4), 2.08742862000000, 1e-3);
EXPECT_NEAR(C2(2, 0), 2.07860468000000, 1e-3); EXPECT_NEAR(C2(2, 4), 1.94727447000000, 1e-3);
EXPECT_NEAR(C2(3, 0), 1.94434955000000, 1e-3); EXPECT_NEAR(C2(3, 4), 2.27675041000000, 1e-3);
EXPECT_NEAR(C2(4, 0), 1.95669703000000, 1e-3); EXPECT_NEAR(C2(4, 4), 2.48517748000000, 1e-3);
EXPECT_NEAR(C2(0, 0), 2.73456805000000, 1e-3);
EXPECT_NEAR(C2(0, 4), 1.95669703000000, 1e-3);
EXPECT_NEAR(C2(1, 0), 1.88593811000000, 1e-3);
EXPECT_NEAR(C2(1, 4), 2.08742862000000, 1e-3);
EXPECT_NEAR(C2(2, 0), 2.07860468000000, 1e-3);
EXPECT_NEAR(C2(2, 4), 1.94727447000000, 1e-3);
EXPECT_NEAR(C2(3, 0), 1.94434955000000, 1e-3);
EXPECT_NEAR(C2(3, 4), 2.27675041000000, 1e-3);
EXPECT_NEAR(C2(4, 0), 1.95669703000000, 1e-3);
EXPECT_NEAR(C2(4, 4), 2.48517748000000, 1e-3);
// Test : addition matrice(ligne) + matrice(ligne)
// Note : teste seulement la première et la dernière colonne
const auto A_plus_A = A + A;
EXPECT_DOUBLE_EQ(A_plus_A(0, 0), A(0, 0) + A(0, 0)); EXPECT_DOUBLE_EQ(A_plus_A(0, 4), A(0, 4) + A(0, 4));
EXPECT_DOUBLE_EQ(A_plus_A(1, 0), A(1, 0) + A(1, 0)); EXPECT_DOUBLE_EQ(A_plus_A(1, 4), A(1, 4) + A(1, 4));
EXPECT_DOUBLE_EQ(A_plus_A(2, 0), A(2, 0) + A(2, 0)); EXPECT_DOUBLE_EQ(A_plus_A(2, 4), A(2, 4) + A(2, 4));
EXPECT_DOUBLE_EQ(A_plus_A(3, 0), A(3, 0) + A(3, 0)); EXPECT_DOUBLE_EQ(A_plus_A(3, 4), A(3, 4) + A(3, 4));
EXPECT_DOUBLE_EQ(A_plus_A(4, 0), A(4, 0) + A(4, 0)); EXPECT_DOUBLE_EQ(A_plus_A(4, 4), A(4, 4) + A(4, 4));
EXPECT_DOUBLE_EQ(A_plus_A(0, 0), A(0, 0) + A(0, 0));
EXPECT_DOUBLE_EQ(A_plus_A(0, 4), A(0, 4) + A(0, 4));
EXPECT_DOUBLE_EQ(A_plus_A(1, 0), A(1, 0) + A(1, 0));
EXPECT_DOUBLE_EQ(A_plus_A(1, 4), A(1, 4) + A(1, 4));
EXPECT_DOUBLE_EQ(A_plus_A(2, 0), A(2, 0) + A(2, 0));
EXPECT_DOUBLE_EQ(A_plus_A(2, 4), A(2, 4) + A(2, 4));
EXPECT_DOUBLE_EQ(A_plus_A(3, 0), A(3, 0) + A(3, 0));
EXPECT_DOUBLE_EQ(A_plus_A(3, 4), A(3, 4) + A(3, 4));
EXPECT_DOUBLE_EQ(A_plus_A(4, 0), A(4, 0) + A(4, 0));
EXPECT_DOUBLE_EQ(A_plus_A(4, 4), A(4, 4) + A(4, 4));
// Test : addition matrice(colonne) + matrice(colonne)
// Note : teste seulement la première et la dernière colonne
const auto B_plus_B = B + B;
EXPECT_DOUBLE_EQ(B_plus_B(0, 0), B(0, 0) + B(0, 0)); EXPECT_DOUBLE_EQ(B_plus_B(0, 4), B(0, 4) + B(0, 4));
EXPECT_DOUBLE_EQ(B_plus_B(1, 0), B(1, 0) + B(1, 0)); EXPECT_DOUBLE_EQ(B_plus_B(1, 4), B(1, 4) + B(1, 4));
EXPECT_DOUBLE_EQ(B_plus_B(2, 0), B(2, 0) + B(2, 0)); EXPECT_DOUBLE_EQ(B_plus_B(2, 4), B(2, 4) + B(2, 4));
EXPECT_DOUBLE_EQ(B_plus_B(3, 0), B(3, 0) + B(3, 0)); EXPECT_DOUBLE_EQ(B_plus_B(3, 4), B(3, 4) + B(3, 4));
EXPECT_DOUBLE_EQ(B_plus_B(4, 0), B(4, 0) + B(4, 0)); EXPECT_DOUBLE_EQ(B_plus_B(4, 4), B(4, 4) + B(4, 4));
EXPECT_DOUBLE_EQ(B_plus_B(0, 0), B(0, 0) + B(0, 0));
EXPECT_DOUBLE_EQ(B_plus_B(0, 4), B(0, 4) + B(0, 4));
EXPECT_DOUBLE_EQ(B_plus_B(1, 0), B(1, 0) + B(1, 0));
EXPECT_DOUBLE_EQ(B_plus_B(1, 4), B(1, 4) + B(1, 4));
EXPECT_DOUBLE_EQ(B_plus_B(2, 0), B(2, 0) + B(2, 0));
EXPECT_DOUBLE_EQ(B_plus_B(2, 4), B(2, 4) + B(2, 4));
EXPECT_DOUBLE_EQ(B_plus_B(3, 0), B(3, 0) + B(3, 0));
EXPECT_DOUBLE_EQ(B_plus_B(3, 4), B(3, 4) + B(3, 4));
EXPECT_DOUBLE_EQ(B_plus_B(4, 0), B(4, 0) + B(4, 0));
EXPECT_DOUBLE_EQ(B_plus_B(4, 4), B(4, 4) + B(4, 4));
}
}
@ -282,8 +317,7 @@ TEST(TestLabo1, MatrixMatrixOperators)
/**
* Test pour la multiplication matrice * vecteur
*/
TEST(TestLabo1, MatrixVectorOperators)
{
TEST(TestLabo1, MatrixVectorOperators) {
// Vecteur à taille dynamique
Vector<double> v(5);
v(0) = 1.0;
@ -323,8 +357,7 @@ TEST(TestLabo1, MatrixVectorOperators)
/**
* Opérateurs d'arithmétique vectorielle
*/
TEST(TestLabo1, VectorOperators)
{
TEST(TestLabo1, VectorOperators) {
Vector<double> v(5);
v(0) = 0.1;
v(1) = 0.2;
@ -354,8 +387,7 @@ TEST(TestLabo1, VectorOperators)
/**
* Mathématiques 3D
*/
TEST(TestLabo1, Math3D)
{
TEST(TestLabo1, Math3D) {
// Test : norme d'un vecteur de dimension 3
Vector3d v;
v.setZero();
@ -397,27 +429,51 @@ TEST(TestLabo1, Math3D)
// Test : création d'une matrice de rotation de 45 degrés autour de l'axe des x
const auto Rx = makeRotation<double>(M_PI / 4.0, 0, 0);
EXPECT_NEAR(Rx(0, 0), 1, 1e-3); EXPECT_NEAR(Rx(0, 1), 0, 1e-3); EXPECT_NEAR(Rx(0, 2), 0, 1e-3);
EXPECT_NEAR(Rx(1, 0), 0, 1e-3); EXPECT_NEAR(Rx(1, 1), 0.7071, 1e-3); EXPECT_NEAR(Rx(1, 2), -0.7071, 1e-3);
EXPECT_NEAR(Rx(2, 0), 0, 1e-3); EXPECT_NEAR(Rx(2, 1), 0.7071, 1e-3); EXPECT_NEAR(Rx(2, 2), 0.7071, 1e-3);
EXPECT_NEAR(Rx(0, 0), 1, 1e-3);
EXPECT_NEAR(Rx(0, 1), 0, 1e-3);
EXPECT_NEAR(Rx(0, 2), 0, 1e-3);
EXPECT_NEAR(Rx(1, 0), 0, 1e-3);
EXPECT_NEAR(Rx(1, 1), 0.7071, 1e-3);
EXPECT_NEAR(Rx(1, 2), -0.7071, 1e-3);
EXPECT_NEAR(Rx(2, 0), 0, 1e-3);
EXPECT_NEAR(Rx(2, 1), 0.7071, 1e-3);
EXPECT_NEAR(Rx(2, 2), 0.7071, 1e-3);
// Test : création d'une matrice de rotation de 45 degrés autour de l'axe des y
const auto Ry = makeRotation<double>(0, M_PI / 4.0, 0);
EXPECT_NEAR(Ry(0, 0), 0.7071, 1e-3); EXPECT_NEAR(Ry(0, 1), 0, 1e-3); EXPECT_NEAR(Ry(0, 2), 0.7071, 1e-3);
EXPECT_NEAR(Ry(1, 0), 0, 1e-3); EXPECT_NEAR(Ry(1, 1), 1, 1e-3); EXPECT_NEAR(Ry(1, 2), 0, 1e-3);
EXPECT_NEAR(Ry(2, 0), -0.7071, 1e-3); EXPECT_NEAR(Ry(2, 1), 0, 1e-3); EXPECT_NEAR(Ry(2, 2), 0.7071, 1e-3);
EXPECT_NEAR(Ry(0, 0), 0.7071, 1e-3);
EXPECT_NEAR(Ry(0, 1), 0, 1e-3);
EXPECT_NEAR(Ry(0, 2), 0.7071, 1e-3);
EXPECT_NEAR(Ry(1, 0), 0, 1e-3);
EXPECT_NEAR(Ry(1, 1), 1, 1e-3);
EXPECT_NEAR(Ry(1, 2), 0, 1e-3);
EXPECT_NEAR(Ry(2, 0), -0.7071, 1e-3);
EXPECT_NEAR(Ry(2, 1), 0, 1e-3);
EXPECT_NEAR(Ry(2, 2), 0.7071, 1e-3);
// Test : création d'une matrice de rotation de 45 degrés autour de l'axe des z
const auto Rz = makeRotation<double>(0, 0, M_PI / 4.0);
EXPECT_NEAR(Rz(0, 0), 0.7071, 1e-3); EXPECT_NEAR(Rz(0, 1), -0.7071, 1e-3); EXPECT_NEAR(Rz(0, 2), 0, 1e-3);
EXPECT_NEAR(Rz(1, 0), 0.7071, 1e-3); EXPECT_NEAR(Rz(1, 1), 0.7071, 1e-3); EXPECT_NEAR(Rz(1, 2), 0, 1e-3);
EXPECT_NEAR(Rz(2, 0), 0, 1e-3); EXPECT_NEAR(Rz(2, 1), 0, 1e-3); EXPECT_NEAR(Rz(2, 2), 1, 1e-3);
EXPECT_NEAR(Rz(0, 0), 0.7071, 1e-3);
EXPECT_NEAR(Rz(0, 1), -0.7071, 1e-3);
EXPECT_NEAR(Rz(0, 2), 0, 1e-3);
EXPECT_NEAR(Rz(1, 0), 0.7071, 1e-3);
EXPECT_NEAR(Rz(1, 1), 0.7071, 1e-3);
EXPECT_NEAR(Rz(1, 2), 0, 1e-3);
EXPECT_NEAR(Rz(2, 0), 0, 1e-3);
EXPECT_NEAR(Rz(2, 1), 0, 1e-3);
EXPECT_NEAR(Rz(2, 2), 1, 1e-3);
// Test : création d'une matrice de rotation quelconque.
const auto Rxyz = makeRotation<double>(M_PI / 3.0, -M_PI / 6.0, M_PI / 4.0);
EXPECT_NEAR(Rxyz(0, 0), 0.6124, 1e-3); EXPECT_NEAR(Rxyz(0, 1), -0.6597, 1e-3); EXPECT_NEAR(Rxyz(0, 2), 0.4356, 1e-3);
EXPECT_NEAR(Rxyz(1, 0), 0.6124, 1e-3); EXPECT_NEAR(Rxyz(1, 1), 0.0474, 1e-3); EXPECT_NEAR(Rxyz(1, 2), -0.7891, 1e-3);
EXPECT_NEAR(Rxyz(2, 0), 0.5, 1e-3); EXPECT_NEAR(Rxyz(2, 1), 0.75, 1e-3); EXPECT_NEAR(Rxyz(2, 2), 0.4330, 1e-3);
EXPECT_NEAR(Rxyz(0, 0), 0.6124, 1e-3);
EXPECT_NEAR(Rxyz(0, 1), -0.6597, 1e-3);
EXPECT_NEAR(Rxyz(0, 2), 0.4356, 1e-3);
EXPECT_NEAR(Rxyz(1, 0), 0.6124, 1e-3);
EXPECT_NEAR(Rxyz(1, 1), 0.0474, 1e-3);
EXPECT_NEAR(Rxyz(1, 2), -0.7891, 1e-3);
EXPECT_NEAR(Rxyz(2, 0), 0.5, 1e-3);
EXPECT_NEAR(Rxyz(2, 1), 0.75, 1e-3);
EXPECT_NEAR(Rxyz(2, 2), 0.4330, 1e-3);
// Test : création d'une transformation homogène via la sous-matrice 3x3 en
// utilisant la fonction `block`
@ -458,8 +514,7 @@ TEST(TestLabo1, Math3D)
* Test des performance de la multiplication matrice * vecteur
* pour de grandes dimensions.
*/
TEST(TestLabo1, PerformanceMatrixVector)
{
TEST(TestLabo1, PerformanceMatrixVector) {
Matrix<double> A(16384, 16384); // grande matrice avec stockage colonne
Vector<double> v(16384); // grand vecteur
@ -484,8 +539,7 @@ TEST(TestLabo1, PerformanceMatrixVector)
* Test des performances de l'addition matrice + matrice
* pour de grandes dimensions.
*/
TEST(TestLabo1, PerformanceLargeMatrixMatrix)
{
TEST(TestLabo1, PerformanceLargeMatrixMatrix) {
// deux grandes matrices à stockage par colonnes
Matrix<double> A(16384, 16384);
Matrix<double> B(16384, 16384);
@ -505,8 +559,7 @@ TEST(TestLabo1, PerformanceLargeMatrixMatrix)
EXPECT_TRUE(optimal_t < 0.4 * naive_t);
}
TEST(TestLabo1, Supplementaires)
{
TEST(TestLabo1, Supplementaires) {
// === Stockage ===
// Test 1: Set zero
DenseStorage<int, Dynamic> S1(10);
@ -538,14 +591,14 @@ TEST(TestLabo1, Supplementaires)
// Test 5: Identité
Matrix<int, -1, -1, ColumnStorage> MC(4, 6);
MC.setIdentity();
for (int i = 0; i < MC.rows(); i++) {
for (int j = 0; j < MC.cols(); j++) {
for (int col = 0; col < MC.cols(); col++) {
for (int row = 0; row < MC.rows(); row++) {
int expected = 0;
if (i == j) {
if (row == col) {
expected = 1;
}
EXPECT_EQ(MC(i, j), expected);
EXPECT_EQ(MC(col, row), expected);
}
}
@ -560,17 +613,17 @@ TEST(TestLabo1, Supplementaires)
MC(3, 2) = 8;
MC(3, 3) = 9;
SubMatrix<int, -1, -1, ColumnStorage> s = MC.block(1, 1, 3, 3);
for (int i = 0; i < s.rows(); i++) {
for (int j = 0; j < s.cols(); j++) {
EXPECT_EQ(s(i, j), MC(i + 1, j + 1));
for (int col = 0; col < s.cols(); col++) {
for (int row = 0; row < s.rows(); row++) {
EXPECT_EQ(s(col, row), MC(col + 1, row + 1));
}
}
// Test 7: Transposée d'une sous-matrice
const Matrix<int, -1, -1, ColumnStorage> t = s.transpose<int, -1, -1, ColumnStorage>();
for (int i = 0; i < t.rows(); i++) {
for (int j = 0; j < t.cols(); j++) {
EXPECT_EQ(t(j, i), s(i, j));
for (int col = 0; col < t.cols(); col++) {
for (int row = 0; row < t.rows(); row++) {
EXPECT_EQ(t(col, row), s(row, col));
}
}
@ -599,8 +652,7 @@ TEST(TestLabo1, Supplementaires)
EXPECT_EQ(V1(12), V2(12));
}
int main(int argc, char** argv)
{
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
const int ret = RUN_ALL_TESTS();