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