What is a symmetric matrix?
Let’s say A is a square matrix and the element in the ith row and jth column of A is Ai,j. The square matrix A is a symmetric matrix if and only if the following condition holds true:
Now, we know that, if AT is the transpose of the matrix A, then:
So, if A is a symmetric matrix and the element in the ith row and jth column of A and AT are Ai,j and ATi,j, respectively, then we can say:
How to check whether a matrix is a symmetric matrix using Python NumPy?
We can use the following Python code to check whether a matrix is a symmetric matrix using NumPy.
import numpy n = 3 A = numpy.random.randint(low=1, high=10, size=(n, n)) print("A: \n", A) if numpy.allclose(A, A.transpose()): print("A is symmetric") else: print("A is not symmetric")
Firstly, we are creating a square matrix with random integers. We are using the numpy.random.randint() function for that purpose. The generated random integers will be within the range [low, high). And the size argument specifies the size of the created matrix. Here, size=(n,n) specifies that the created matrix will have n rows and n columns.
Now, we are calculating the transpose of the created matrix using the transpose() function. And then, we are checking whether A and the transpose of A are equal element-wise. We are using the numpy.allclose() function for that purpose.
The output of the mentioned program will be:
A: [[7 9 7] [1 6 6] [7 4 8]] A is not symmetric
0 Comments