- What is a symmetric matrix and its properties
- How to check if a matrix is symmetric
- How to use the transpose condition for verification
- Step-by-step explanation of the program
- Practice exercises to test your understanding
Introduction
In this tutorial, we will learn how to write a C program to check if a matrix is symmetric.
A symmetric matrix is a square matrix that is equal to its transpose. In other words, A[i][j] = A[j][i] for all i and j. This means the matrix is symmetric about its main diagonal.
š” Key Point: A symmetric matrix must be a square matrix (same number of rows and columns). Non-square matrices cannot be symmetric.
Symmetric matrices are important in many real-world applications, such as:
- Linear Algebra: Eigenvalue problems, quadratic forms
- Physics: Inertia tensors, stress tensors
- Statistics: Covariance and correlation matrices
- Computer Graphics: Rotation and transformation matrices
Visual Example
Symmetric Matrix (3Ć3):
[1 2 3] [2 4 5] [3 5 6]
Transpose of Symmetric Matrix:
[1 2 3] [2 4 5] [3 5 6]
ā Matrix = Transpose ā Symmetric
Check: A[0][1] = 2, A[1][0] = 2 ā
A[0][2] = 3, A[2][0] = 3 ā
A[1][2] = 5, A[2][1] = 5 ā
ā Non-Symmetric Matrix:
[1 2 3] [4 5 6] [7 8 9]
Check: A[0][1] = 2, A[1][0] = 4 ā Not equal!
C Program to Check if Matrix is Symmetric
#include <stdio.h>
int main() {
int n, i, j;
int symmetric = 1; // Flag: 1 = symmetric, 0 = not symmetric
// Ask user for matrix size
printf("Enter the size of the square matrix: ");
scanf("%d", &n);
// Declare matrix
int matrix[n][n];
// Read elements into the matrix
printf("\nEnter %d elements:\n", n * n);
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
printf("matrix[%d][%d] = ", i, j);
scanf("%d", &matrix[i][j]);
}
}
// Display the matrix
printf("\n=== The Matrix ===\n");
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
// Check if matrix is symmetric
// Only need to check upper triangle (i < j)
for(i = 0; i < n; i++) {
for(j = i + 1; j < n; j++) {
if(matrix[i][j] != matrix[j][i]) {
symmetric = 0;
break;
}
}
if(symmetric == 0) {
break;
}
}
// Display result
printf("\n=== Result ===\n");
if(symmetric == 1) {
printf("ā
The matrix is SYMMETRIC.\n");
printf("(Matrix is equal to its transpose)\n");
} else {
printf("ā The matrix is NOT symmetric.\n");
printf("(Matrix is not equal to its transpose)\n");
}
return 0;
}
Sample Output
Enter the size of the square matrix: 3 Enter 9 elements: matrix[0][0] = 1 matrix[0][1] = 2 matrix[0][2] = 3 matrix[1][0] = 2 matrix[1][1] = 4 matrix[1][2] = 5 matrix[2][0] = 3 matrix[2][1] = 5 matrix[2][2] = 6 === The Matrix === 1 2 3 2 4 5 3 5 6 === Result === ā The matrix is SYMMETRIC. (Matrix is equal to its transpose)
When Matrix is Not Symmetric:
Enter the size of the square matrix: 3 Enter 9 elements: matrix[0][0] = 1 matrix[0][1] = 2 matrix[0][2] = 3 matrix[1][0] = 4 matrix[1][1] = 5 matrix[1][2] = 6 matrix[2][0] = 7 matrix[2][1] = 8 matrix[2][2] = 9 === The Matrix === 1 2 3 4 5 6 7 8 9 === Result === ā The matrix is NOT symmetric. (Matrix is not equal to its transpose)
Example with Identity Matrix (Always Symmetric):
Enter the size of the square matrix: 4 Enter 16 elements: matrix[0][0] = 1 matrix[0][1] = 0 matrix[0][2] = 0 matrix[0][3] = 0 matrix[1][0] = 0 matrix[1][1] = 1 matrix[1][2] = 0 matrix[1][3] = 0 matrix[2][0] = 0 matrix[2][1] = 0 matrix[2][2] = 1 matrix[2][3] = 0 matrix[3][0] = 0 matrix[3][1] = 0 matrix[3][2] = 0 matrix[3][3] = 1 === The Matrix === 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 === Result === ā The matrix is SYMMETRIC. (Matrix is equal to its transpose)
Program Explanation
Let's break down the code step by step:
- Include Header File:
#include <stdio.h>includes the standard input/output library. - Declare Variables:
int n;ā size of the square matrixint i, j;ā loop countersint symmetric = 1;ā flag variable (1 = symmetric, 0 = not symmetric)
- Read Matrix: Reads n Ć n elements into the matrix.
- Check Symmetry:
- Only the upper triangle (i < j) needs to be checked
- Diagonal elements (i == j) are always equal to themselves
- If
matrix[i][j] != matrix[j][i], setsymmetric = 0and break
- Display Result: Prints whether the matrix is symmetric or not.
- Return:
return 0;indicates successful program execution.
š Note: We only need to check the upper triangle because symmetry is a symmetric property. If A[i][j] = A[j][i] for all i < j, then it automatically holds for all i > j as well.
Algorithm to Check Symmetric Matrix
Step-by-step algorithm:
- Start
- Read the size of the square matrix (n)
- Read n Ć n elements into the matrix
- Set
symmetric = 1 - For
i = 0ton-1:- For
j = i+1ton-1:- If
matrix[i][j] != matrix[j][i]:- Set
symmetric = 0 - Break
- Set
- If
- If
symmetric == 0, break
- For
- If
symmetric == 1:- Print "Matrix is symmetric"
- Else:
- Print "Matrix is not symmetric"
- End
Alternative Method: Using Transpose
Another way to check if a matrix is symmetric is to find its transpose and compare it with the original matrix:
#include <stdio.h>
int main() {
int n, i, j;
int symmetric = 1;
printf("Enter the size of the square matrix: ");
scanf("%d", &n);
int matrix[n][n];
int transpose[n][n];
printf("\nEnter %d elements:\n", n * n);
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}
// Find transpose
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
transpose[j][i] = matrix[i][j];
}
}
// Compare matrix with its transpose
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
if(matrix[i][j] != transpose[i][j]) {
symmetric = 0;
break;
}
}
if(symmetric == 0) {
break;
}
}
printf("\n=== Matrix ===\n");
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
printf("\n=== Transpose ===\n");
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
printf("%d\t", transpose[i][j]);
}
printf("\n");
}
printf("\n=== Result ===\n");
if(symmetric == 1) {
printf("ā
Matrix is SYMMETRIC (Matrix = Transpose)\n");
} else {
printf("ā Matrix is NOT symmetric (Matrix ā Transpose)\n");
}
return 0;
}
Properties of Symmetric Matrices
| Property | Description |
|---|---|
| Square Matrix | Must have equal rows and columns |
| A = A^T | Matrix equals its transpose |
| Diagonal Elements | Any values (not required to be equal) |
| Off-Diagonal Elements | A[i][j] = A[j][i] for all i ā j |
| Eigenvalues | All eigenvalues are real |
| Eigenvectors | Eigenvectors are mutually orthogonal |
Time and Space Complexity
| Method | Time Complexity | Space Complexity |
|---|---|---|
| Upper Triangle Check | O(n²) | O(1) |
| Transpose Method | O(n²) | O(n²) |
š» Practice Exercise
Challenge 1: Write a program to check if a matrix is skew-symmetric (A[i][j] = -A[j][i]).
Challenge 2: Create a function that returns 1 if a matrix is symmetric and 0 otherwise.
š Click to Show Solution for Challenge 1
#include <stdio.h>
int main() {
int n, i, j;
int skew_symmetric = 1;
printf("Enter the size of the square matrix: ");
scanf("%d", &n);
int matrix[n][n];
printf("\nEnter %d elements:\n", n * n);
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}
// Check if matrix is skew-symmetric
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
// Check condition: A[i][j] = -A[j][i]
if(matrix[i][j] != -matrix[j][i]) {
skew_symmetric = 0;
break;
}
}
if(skew_symmetric == 0) {
break;
}
}
printf("\n=== The Matrix ===\n");
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
printf("\n=== Result ===\n");
if(skew_symmetric == 1) {
printf("ā
The matrix is SKEW-SYMMETRIC.\n");
} else {
printf("ā The matrix is NOT skew-symmetric.\n");
}
return 0;
}
Frequently Asked Questions
1. What is a symmetric matrix?
A symmetric matrix is a square matrix that is equal to its transpose. This means A[i][j] = A[j][i] for all elements.
2. What is the condition for a matrix to be symmetric?
The matrix must be square (same number of rows and columns) and A[i][j] = A[j][i] for all i and j.
3. Can a non-square matrix be symmetric?
No, a non-square matrix cannot be symmetric because transposing a non-square matrix changes its dimensions, so it cannot equal itself.
4. What is the time complexity of checking symmetry?
The time complexity is O(n²), where n is the size of the matrix. We need to check each element once.
5. What is the difference between symmetric and skew-symmetric?
Symmetric: A[i][j] = A[j][i]
Skew-Symmetric: A[i][j] = -A[j][i] and diagonal elements are zero.
š” Tip: When checking for symmetry, you only need to check the upper triangle (i < j) to save time. The diagonal and lower triangle are automatically verified.