Skip to content
Snippets Groups Projects
matrices.cpp 3.87 KiB
Newer Older
Nicolas M. Thiéry's avatar
Nicolas M. Thiéry committed
#include <stdexcept>
#include <iostream>
#include <vector>
using namespace std;

/** Infrastructure minimale de test **/
#define CHECK(test) if (!(test)) cerr << "Test failed in file " << __FILE__ << " line " << __LINE__ << ": " #test << endl

using Matrice = vector<vector<int>>;

Matrice matriceVide (0);
Matrice matriceCarrée       = {  { 1,  4, 9 },
                                { 4, 11, 6 },
                                { 9, 12, 7 }};

Matrice matriceSymétrique = {   { 1,  2, 3 },
                                { 2, 11, 4 },
                                { 3,  4, 0 }};
Matrice matriceCreuse = {                      { 0,  0 },
                                               { 5,  0 },
                                               { 0,  2 }   };
Matrice matriceRectangulaire={  { 1,  2, 3 },
                                { 9, 12, 7 }};

Matrice matriceProduit =  {                    { 10,  6 },
                                               { 60, 14 }};
/// BEGIN initBizzare
Matrice matriceBizzare =    {   { 1,  2, 3 , 4},
                                { 2, 11, 4 , 5},
                                { 3, 4, 100000} };
/// END initBizzare

/// BEGIN estSymétrique
/** Teste si une matrice est symétrique
 * @param t une matrice carrée
 * @return true si t[i][j] == t[j][i] pour tout i,j, false sinon
 **/
bool estSymétrique(Matrice t) {
    // Remplacez cette ligne et la suivante par le code adéquat
    throw runtime_error("Fonction estSymétrique non implantée ligne 40");
}
/// END estSymétrique
/// BEGIN estSymétriqueTest
void estSymétriqueTest() {
    CHECK(     estSymétrique(matriceVide) );
    CHECK( not estSymétrique(matriceCarrée) );
    CHECK(     estSymétrique(matriceSymétrique) );
    CHECK( not estSymétrique(matriceProduit) );
}
/// END estSymétriqueTest

/// BEGIN estCarréeSymétrique
/** Teste si une matrice est symétrique
 * @param t une matrice
 * @return true si la matrice est carrée et symétrique
 * cad t[i][j] == t[j][i] pour tout i,j, false sinon
 **/

bool estCarréeSymétrique(Matrice t) {

    // Remplacez cette ligne et la suivante par le code adéquat
    throw runtime_error("code non implanté ligne 62");

}
/// END estCarréeSymétrique

/// BEGIN estCarréeSymétriqueTest
void estCarréeSymétriqueTest() {
    CHECK(     estCarréeSymétrique(matriceVide) );
    CHECK( not estCarréeSymétrique(matriceCarrée) );
    CHECK(     estCarréeSymétrique(matriceSymétrique) );

    CHECK( not    estCarréeSymétrique(matriceRectangulaire) );
    CHECK( not    estCarréeSymétrique(matriceBizzare) );

}
/// END estCarréeSymétriqueTest

/// BEGIN somme
/** somme deux matrices dont les dimensions sont identiques
 * @param t1 une matrice
 * @param t2 une matrice
 * @return la matrice t1 + t2
 **/
Matrice somme(Matrice t1, Matrice t2) {

    // Remplacez cette ligne et la suivante par le code adéquat
    throw runtime_error("code non implanté ligne 87");

}
/// END somme

void sommeTest() {
    CHECK( somme(matriceCarrée, matriceSymétrique) ==
    Matrice({{ 2, 6, 12 }, { 6, 22, 10 }, { 12, 16, 7 }}) );
}

/// BEGIN produit
/** produit de deux matrices dont les dimensions sont compatibles
 * @param t1 une matrice
 * @param t2 une matrice
 * @return la matrice t1 * t2 (produit matriciel)
 **/
Matrice produit(Matrice t1, Matrice t2) {

    // Remplacez cette ligne et la suivante par le code adéquat
    throw runtime_error("code non implanté ligne 105");

}
/// END produit

void produitTest() {
    CHECK( produit(matriceCarrée, matriceCarrée) == Matrice({{ 98, 156, 96 }, { 102, 209, 144 }, { 120, 252, 202 }}) );
    CHECK( produit(matriceRectangulaire, matriceCreuse) == matriceProduit );
}

int main() {
    cout << "Lancement des tests. Si tout marche bien, rien ne devrait s'afficher ci dessous."
         << endl;
    // Lance tous les tests
    estSymétriqueTest();
    estCarréeSymétriqueTest();
    sommeTest();
    produitTest();
}