Check If a Matrix is Orthogonal in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to check if a square matrix is orthogonal in C programming. The lab covers the following steps: reading a square matrix, computing the transpose of the matrix, and verifying the condition AᵀA = I to determine if the matrix is orthogonal. By the end of this lab, you will have a comprehensive understanding of matrix operations and their applications in linear algebra using C.

The lab provides a step-by-step guide, starting with reading a square matrix, then computing the transpose of the matrix, and finally, checking if the matrix is orthogonal by verifying the condition AᵀA = I. This lab is designed to help you develop your skills in matrix and linear algebra using C programming, a valuable skill in various fields, including data analysis, scientific computing, and engineering.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/CompoundTypesGroup(["`Compound Types`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/ControlFlowGroup -.-> c/for_loop("`For Loop`") c/CompoundTypesGroup -.-> c/arrays("`Arrays`") subgraph Lab Skills c/output -.-> lab-435139{{"`Check If a Matrix is Orthogonal in C`"}} c/for_loop -.-> lab-435139{{"`Check If a Matrix is Orthogonal in C`"}} c/arrays -.-> lab-435139{{"`Check If a Matrix is Orthogonal in C`"}} end

Read a Square Matrix

In this step, you will learn how to read a square matrix in C programming. A square matrix is a matrix with an equal number of rows and columns. We'll create a program that allows users to input a square matrix dynamically.

First, let's create a C source file for our matrix operations:

cd ~/project
nano matrix_orthogonal.c

Now, add the following code to read a square matrix:

#include <stdio.h>
#define MAX_SIZE 100

void readMatrix(int matrix[MAX_SIZE][MAX_SIZE], int size) {
    printf("Enter matrix elements row by row:\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("Enter the size of the square matrix: ");
    scanf("%d", &size);

    readMatrix(matrix, size);

    // Print the matrix to verify input
    printf("\nInput Matrix:\n");
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Compile and run the program:

gcc matrix_orthogonal.c -o matrix_orthogonal
./matrix_orthogonal

Example output:

Enter the size of the square matrix: 3
Enter matrix elements row by row:
1 0 0
0 1 0
0 0 1

Input Matrix:
1 0 0
0 1 0
0 0 1
Explanation
  • MAX_SIZE defines the maximum possible size of the matrix
  • readMatrix() function takes a 2D array and its size as parameters
  • User inputs matrix size and elements row by row
  • The program prints the input matrix to verify correct input

Compute Aᵀ and Check if AᵀA = I

In this step, you will extend the previous program to compute the transpose of the matrix and check if the matrix is orthogonal by verifying the condition AᵀA = I.

Update the matrix_orthogonal.c file with the following code:

nano ~/project/matrix_orthogonal.c

Add the following implementation:

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

void readMatrix(int matrix[MAX_SIZE][MAX_SIZE], int size) {
    printf("Enter matrix elements row by row:\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};

    // Multiply transpose and original matrix
    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];
            }
        }
    }

    // Check if result is identity matrix
    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("Enter the size of the square matrix: ");
    scanf("%d", &size);

    readMatrix(matrix, size);

    // Compute transpose
    transposeMatrix(matrix, transpose, size);

    // Check orthogonality
    bool isOrthogonal = checkOrthogonality(matrix, transpose, size);

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

    printf("\nIs the matrix orthogonal? %s\n",
           isOrthogonal ? "Yes" : "No");

    return 0;
}

Compile and run the program:

gcc matrix_orthogonal.c -o matrix_orthogonal
./matrix_orthogonal

Example output:

Enter the size of the square matrix: 3
Enter matrix elements row by row:
1 0 0
0 1 0
0 0 1

Transpose Matrix:
1 0 0
0 1 0
0 0 1

Is the matrix orthogonal? Yes
Explanation
  • transposeMatrix() function computes the matrix transpose
  • checkOrthogonality() verifies AᵀA = I condition
  • The program checks if the matrix is orthogonal by multiplying transpose and original matrix

Print If Orthogonal or Not

In this final step, you will enhance the previous program to provide more detailed output about matrix orthogonality and demonstrate different matrix scenarios.

Update the matrix_orthogonal.c file with the following improved implementation:

nano ~/project/matrix_orthogonal.c

Replace the previous implementation with:

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

void readMatrix(int matrix[MAX_SIZE][MAX_SIZE], int size) {
    printf("Enter matrix elements row by row:\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};

    // Multiply transpose and original matrix
    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];
            }
        }
    }

    // Check if result is identity matrix
    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--- Matrix Orthogonality Analysis ---\n");
    printf("Original Matrix:\n");
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }

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

    printf("\nOrthogonality Status: %s\n",
           isOrthogonal ? "✓ Orthogonal Matrix" : "✗ Not an Orthogonal Matrix");

    if (isOrthogonal) {
        printf("Explanation: A * Aᵀ = I (Identity Matrix)\n");
    } else {
        printf("Explanation: A * Aᵀ ≠ I (Not an Identity Matrix)\n");
    }
}

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

    printf("Enter the size of the square matrix: ");
    scanf("%d", &size);

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

    return 0;
}

Compile and run the program:

gcc matrix_orthogonal.c -o matrix_orthogonal
./matrix_orthogonal

Example output for an orthogonal matrix:

Enter the size of the square matrix: 3
Enter matrix elements row by row:
1 0 0
0 1 0
0 0 1

--- Matrix Orthogonality Analysis ---
Original Matrix:
1 0 0
0 1 0
0 0 1

Transpose Matrix:
1 0 0
0 1 0
0 0 1

Orthogonality Status: ✓ Orthogonal Matrix
Explanation: A * Aᵀ = I (Identity Matrix)

Example output for a non-orthogonal matrix:

Enter the size of the square matrix: 3
Enter matrix elements row by row:
1 2 3
4 5 6
7 8 9

--- Matrix Orthogonality Analysis ---
Original Matrix:
1 2 3
4 5 6
7 8 9

Transpose Matrix:
1 4 7
2 5 8
3 6 9

Orthogonality Status: ✗ Not an Orthogonal Matrix
Explanation: A * Aᵀ ≠ I (Not an Identity Matrix)
Explanation
  • Added printDetailedOrthogonalityInfo() function for comprehensive output
  • Displays original matrix, transpose matrix, and orthogonality status
  • Provides clear explanation of orthogonality condition

Summary

In this lab, you first learned how to read a square matrix in C programming. You created a program that allows users to input a square matrix dynamically, and then prints the input matrix to verify the correct input. Next, you extended the program to compute the transpose of the input matrix and check if the matrix is orthogonal by verifying the condition AᵀA = I. The program computes the product of the transpose and the original matrix, and checks if the result is the identity matrix, indicating that the input matrix is orthogonal.

Other C Tutorials you may like