SciPy - issymmetric() Function



The scipy.linalg.issymmetric() method is used to check if a matrix is symmetric. We call a matrix is symmetric if it is equal to its transpose. This means the element at position[i,j] should be same as the position[j,i]

Syntax

The syntax for the SciPy issymmetric() method is as follows −

.issymmetric(a, atol=None, rtol=None)

Parameters

This method accepts the following parameters −

  • a (ndarray) − The input matrix to check whether it is symmetric or not.

  • atol (float, optional) − Absolute tolerance. Default is 1 x 10^-8.

  • rtol (float, optional) − Relative tolerance. Default is 1 x 10^-5.

Return Value

returns (bool) − If the input matrix is symmetric it returns True, otherwise False.

Example 1

In the below code we have created two matrices a, b. Where a is symmetric because it is equal to its transpose a[1,0]=a[0,1] but it is not in the case of b

from scipy.linalg import issymmetric
import numpy as np

a = np.array([[1, 2, 3],
             [2, 5, 6],
			 [3, 6, 9]])
			 
b = np.array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]])

print(issymmetric(a))  
print(issymmetric(b)) 

When we run above program, it produces following result −

True
False

Example 2

Let us create a matrix that contains np.inf values in the diagonal. With the matrix containing inf and satisfies the condition a[i,j]=a[j,i] then the matrix is symmetric, because inf is treated as a number.

import numpy as np
from scipy.linalg import issymmetric

A = np.array([[2, np.inf],
              [np.inf, 1]])
print(issymmetric(A))  

Following is an output of the above code −

True

Example 3

Let us create a matrix with NaN values. This results false because, NaN value is not equal to itself.

import numpy as np
from scipy.linalg import issymmetric
B = np.array([[1, np.nan],
              [np.nan, 2]])
print(issymmetric(B)) 

Following is the output of above code −

False

Example 4

Let us check whether the empty square matrix is symmetric or not.

import numpy as np
from scipy.linalg import issymmetric
C = np.empty((0, 0))
print(issymmetric(C))

When we run above program, it produces following result −

True

Example 5

Let us check whether the empty square matrix is symmetric or not.

import numpy as np
from scipy.linalg import issymmetric
C = np.empty((0, 0))
print(issymmetric(C))

Output of the above code is as follows −

True

Example 6

Due to floating-point errors, a matrix might not always be exactly symmetric. Tiny deviations can be tolerated by providing tolerances atol (absolute) and rtol (relative).

If limits are not specified, the matrix would be declared as nonsymmetric because d[0,1] is not equal to d[1,0] due to the slight difference of 0.0000001. If we set atol=1e-6, we are tolerant of tiny numerical errors which this method declares as symmetric.

import numpy as np
from scipy.linalg import issymmetric
D = np.array([[1, 2.0000001],  
              [2, 3]])

print(issymmetric(D, atol=1e-6))

When we run the above code, output is as follows −

True

Example 7

Let us see the example where it raises type error because the symmetric matrix doesn't support few data types such float16, float128 or complex256. This error occurs because these types are not fully supported by SciPy linear algebra.

import numpy as np

# Define the matrix
E = np.array([[1, 2], 
              [2, 3]], dtype=np.float16)

# Check if the matrix is symmetric
is_symmetric = np.allclose(E, E.T)

print("Is the matrix symmetric?", is_symmetric)

Output of the above code is as follows −

TypeError: No matching signature found

Example 8

To avoid the error in the above example we can convert the above matrix 'E' to supported data types like float32 or float64.

import numpy as np
from scipy.linalg import issymmetric
E = np.array([[1, 2], [2, 3]], dtype=np.float16)
matrix_f_64 = E.astype(np.float64)
print(issymmetric(matrix_f_64))

Following is the output of above code −

True
scipy_linalg.htm
Advertisements