Vérifier si une matrice est orthogonale en C

CBeginner
Pratiquer maintenant

Introduction

Dans ce laboratoire, vous apprendrez à vérifier si une matrice carrée est orthogonale en programmation C. Le laboratoire couvre les étapes suivantes : la lecture d'une matrice carrée, le calcul de la transposée de la matrice et la vérification de la condition AᵀA = I pour déterminer si la matrice est orthogonale. À la fin de ce laboratoire, vous aurez une compréhension complète des opérations matricielles et de leurs applications en algèbre linéaire en utilisant le langage C.

Le laboratoire fournit un guide étape par étape, commençant par la lecture d'une matrice carrée, puis le calcul de la transposée de la matrice, et enfin la vérification si la matrice est orthogonale en vérifiant la condition AᵀA = I. Ce laboratoire est conçu pour vous aider à développer vos compétences en matrices et en algèbre linéaire en utilisant la programmation C, une compétence précieuse dans divers domaines, notamment l'analyse de données, le calcul scientifique et l'ingénierie.

Lecture d'une matrice carrée

Dans cette étape, vous apprendrez à lire une matrice carrée en programmation C. Une matrice carrée est une matrice avec un nombre égal de lignes et de colonnes. Nous allons créer un programme qui permet aux utilisateurs d'entrer dynamiquement une matrice carrée.

Tout d'abord, créons un fichier source C pour nos opérations matricielles :

cd ~/project
nano matrix_orthogonal.c

Maintenant, ajoutez le code suivant pour lire une matrice carrée :

#include <stdio.h>
#define MAX_SIZE 100

void readMatrix(int matrix[MAX_SIZE][MAX_SIZE], int size) {
    printf("Entrez les éléments de la matrice ligne par ligne :\n");
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }
}

int main() {
    int size, matrix[MAX_SIZE][MAX_SIZE];

    printf("Entrez la taille de la matrice carrée : ");
    scanf("%d", &size);

    readMatrix(matrix, size);

    // Affichage de la matrice pour vérifier l'entrée
    printf("\nMatrice d'entrée :\n");
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Compilez et exécutez le programme :

gcc matrix_orthogonal.c -o matrix_orthogonal
./matrix_orthogonal

Exemple de sortie :

Entrez la taille de la matrice carrée : 3
Entrez les éléments de la matrice ligne par ligne :
1 0 0
0 1 0
0 0 1

Matrice d'entrée :
1 0 0
0 1 0
0 0 1
Explication
  • MAX_SIZE définit la taille maximale possible de la matrice.
  • La fonction readMatrix() prend un tableau 2D et sa taille en paramètres.
  • L'utilisateur entre la taille de la matrice et les éléments ligne par ligne.
  • Le programme affiche la matrice d'entrée pour vérifier l'entrée correcte.

Calcul de Aᵀ et vérification de AᵀA = I

Dans cette étape, vous étendrez le programme précédent pour calculer la transposée de la matrice et vérifier si la matrice est orthogonale en vérifiant la condition AᵀA = I.

Mettez à jour le fichier matrix_orthogonal.c avec le code suivant :

nano ~/project/matrix_orthogonal.c

Ajoutez la mise en œuvre suivante :

#include <stdio.h>
#include <stdbool.h>
#define MAX_SIZE 100

void readMatrix(int matrix[MAX_SIZE][MAX_SIZE], int size) {
    printf("Entrez les éléments de la matrice ligne par ligne :\n");
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }
}

void transposeMatrix(int original[MAX_SIZE][MAX_SIZE],
                     int transpose[MAX_SIZE][MAX_SIZE],
                     int size) {
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            transpose[j][i] = original[i][j];
        }
    }
}

bool checkOrthogonality(int matrix[MAX_SIZE][MAX_SIZE],
                        int transpose[MAX_SIZE][MAX_SIZE],
                        int size) {
    int result[MAX_SIZE][MAX_SIZE] = {0};

    // Multiplication de la transposée et de la matrice originale
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            for (int k = 0; k < size; k++) {
                result[i][j] += transpose[i][k] * matrix[k][j];
            }
        }
    }

    // Vérification si la matrice résultat est la matrice identité
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            if (i == j && result[i][j] != 1) return false;
            if (i != j && result[i][j] != 0) return false;
        }
    }

    return true;
}

int main() {
    int size, matrix[MAX_SIZE][MAX_SIZE], transpose[MAX_SIZE][MAX_SIZE];

    printf("Entrez la taille de la matrice carrée : ");
    scanf("%d", &size);

    readMatrix(matrix, size);

    // Calcul de la transposée
    transposeMatrix(matrix, transpose, size);

    // Vérification de l'orthogonalité
    bool isOrthogonal = checkOrthogonality(matrix, transpose, size);

    printf("\nMatrice transposée :\n");
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            printf("%d ", transpose[i][j]);
        }
        printf("\n");
    }

    printf("\nLa matrice est-elle orthogonale ? %s\n",
           isOrthogonal ? "Oui" : "Non");

    return 0;
}

Compilez et exécutez le programme :

gcc matrix_orthogonal.c -o matrix_orthogonal
./matrix_orthogonal

Exemple de sortie :

Entrez la taille de la matrice carrée : 3
Entrez les éléments de la matrice ligne par ligne :
1 0 0
0 1 0
0 0 1

Matrice transposée :
1 0 0
0 1 0
0 0 1

La matrice est-elle orthogonale ? Oui
Explication
  • La fonction transposeMatrix() calcule la transposée de la matrice.
  • La fonction checkOrthogonality() vérifie la condition AᵀA = I.
  • Le programme vérifie si la matrice est orthogonale en multipliant la transposée et la matrice originale.

Affichage si orthogonal ou non

Dans cette dernière étape, vous améliorerez le programme précédent pour fournir des informations plus détaillées sur l'orthogonalité de la matrice et démontrer différents scénarios matriciels.

Mettez à jour le fichier matrix_orthogonal.c avec la mise en œuvre améliorée suivante :

nano ~/project/matrix_orthogonal.c

Remplacez la mise en œuvre précédente par :

#include <stdio.h>
#include <stdbool.h>
#define MAX_SIZE 100

void readMatrix(int matrix[MAX_SIZE][MAX_SIZE], int size) {
    printf("Entrez les éléments de la matrice ligne par ligne :\n");
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }
}

void transposeMatrix(int original[MAX_SIZE][MAX_SIZE],
                     int transpose[MAX_SIZE][MAX_SIZE],
                     int size) {
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            transpose[j][i] = original[i][j];
        }
    }
}

bool checkOrthogonality(int matrix[MAX_SIZE][MAX_SIZE],
                        int transpose[MAX_SIZE][MAX_SIZE],
                        int size) {
    int result[MAX_SIZE][MAX_SIZE] = {0};

    // Multiplication de la transposée et de la matrice originale
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            for (int k = 0; k < size; k++) {
                result[i][j] += transpose[i][k] * matrix[k][j];
            }
        }
    }

    // Vérification si la matrice résultat est la matrice identité
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            if (i == j && result[i][j] != 1) return false;
            if (i != j && result[i][j] != 0) return false;
        }
    }

    return true;
}

void printDetailedOrthogonalityInfo(int matrix[MAX_SIZE][MAX_SIZE],
                                    int transpose[MAX_SIZE][MAX_SIZE],
                                    int size) {
    bool isOrthogonal = checkOrthogonality(matrix, transpose, size);

    printf("\n--- Analyse de l'orthogonalité de la matrice ---\n");
    printf("Matrice originale :\n");
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }

    printf("\nMatrice transposée :\n");
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            printf("%d ", transpose[i][j]);
        }
        printf("\n");
    }

    printf("\nStatut d'orthogonalité : %s\n",
           isOrthogonal ? "✓ Matrice orthogonale" : "✗ Matrice non orthogonale");

    if (isOrthogonal) {
        printf("Explication : A * Aᵀ = I (Matrice identité)\n");
    } else {
        printf("Explication : A * Aᵀ ≠ I (Non matrice identité)\n");
    }
}

int main() {
    int size, matrix[MAX_SIZE][MAX_SIZE], transpose[MAX_SIZE][MAX_SIZE];

    printf("Entrez la taille de la matrice carrée : ");
    scanf("%d", &size);

    readMatrix(matrix, size);
    transposeMatrix(matrix, transpose, size);
    printDetailedOrthogonalityInfo(matrix, transpose, size);

    return 0;
}

Compilez et exécutez le programme :

gcc matrix_orthogonal.c -o matrix_orthogonal
./matrix_orthogonal

Exemple de sortie pour une matrice orthogonale :

... (sortie identique au précédent exemple)

Exemple de sortie pour une matrice non orthogonale :

... (sortie identique au précédent exemple)
Explication
  • Fonction printDetailedOrthogonalityInfo() ajoutée pour une sortie complète.
  • Affichage de la matrice originale, de la matrice transposée et du statut d'orthogonalité.
  • Explication claire de la condition d'orthogonalité.

Résumé

Dans ce laboratoire, vous avez d'abord appris à lire une matrice carrée en programmation C. Vous avez créé un programme permettant aux utilisateurs d'entrer dynamiquement une matrice carrée, puis d'afficher la matrice entrée pour vérifier la saisie correcte. Ensuite, vous avez étendu le programme pour calculer la transposée de la matrice d'entrée et vérifier si la matrice est orthogonale en vérifiant la condition AᵀA = I. Le programme calcule le produit de la transposée et de la matrice originale, et vérifie si le résultat est la matrice identité, indiquant que la matrice d'entrée est orthogonale.