
- SciPy - Home
- SciPy - Introduction
- SciPy - Environment Setup
- SciPy - Basic Functionality
- SciPy - Relationship with NumPy
- SciPy Clusters
- SciPy - Clusters
- SciPy - Hierarchical Clustering
- SciPy - K-means Clustering
- SciPy - Distance Metrics
- SciPy Constants
- SciPy - Constants
- SciPy - Mathematical Constants
- SciPy - Physical Constants
- SciPy - Unit Conversion
- SciPy - Astronomical Constants
- SciPy - Fourier Transforms
- SciPy - FFTpack
- SciPy - Discrete Fourier Transform (DFT)
- SciPy - Fast Fourier Transform (FFT)
- SciPy Integration Equations
- SciPy - Integrate Module
- SciPy - Single Integration
- SciPy - Double Integration
- SciPy - Triple Integration
- SciPy - Multiple Integration
- SciPy Differential Equations
- SciPy - Differential Equations
- SciPy - Integration of Stochastic Differential Equations
- SciPy - Integration of Ordinary Differential Equations
- SciPy - Discontinuous Functions
- SciPy - Oscillatory Functions
- SciPy - Partial Differential Equations
- SciPy Interpolation
- SciPy - Interpolate
- SciPy - Linear 1-D Interpolation
- SciPy - Polynomial 1-D Interpolation
- SciPy - Spline 1-D Interpolation
- SciPy - Grid Data Multi-Dimensional Interpolation
- SciPy - RBF Multi-Dimensional Interpolation
- SciPy - Polynomial & Spline Interpolation
- SciPy Curve Fitting
- SciPy - Curve Fitting
- SciPy - Linear Curve Fitting
- SciPy - Non-Linear Curve Fitting
- SciPy - Input & Output
- SciPy - Input & Output
- SciPy - Reading & Writing Files
- SciPy - Working with Different File Formats
- SciPy - Efficient Data Storage with HDF5
- SciPy - Data Serialization
- SciPy Linear Algebra
- SciPy - Linalg
- SciPy - Matrix Creation & Basic Operations
- SciPy - Matrix LU Decomposition
- SciPy - Matrix QU Decomposition
- SciPy - Singular Value Decomposition
- SciPy - Cholesky Decomposition
- SciPy - Solving Linear Systems
- SciPy - Eigenvalues & Eigenvectors
- SciPy Image Processing
- SciPy - Ndimage
- SciPy - Reading & Writing Images
- SciPy - Image Transformation
- SciPy - Filtering & Edge Detection
- SciPy - Top Hat Filters
- SciPy - Morphological Filters
- SciPy - Low Pass Filters
- SciPy - High Pass Filters
- SciPy - Bilateral Filter
- SciPy - Median Filter
- SciPy - Non - Linear Filters in Image Processing
- SciPy - High Boost Filter
- SciPy - Laplacian Filter
- SciPy - Morphological Operations
- SciPy - Image Segmentation
- SciPy - Thresholding in Image Segmentation
- SciPy - Region-Based Segmentation
- SciPy - Connected Component Labeling
- SciPy Optimize
- SciPy - Optimize
- SciPy - Special Matrices & Functions
- SciPy - Unconstrained Optimization
- SciPy - Constrained Optimization
- SciPy - Matrix Norms
- SciPy - Sparse Matrix
- SciPy - Frobenius Norm
- SciPy - Spectral Norm
- SciPy Condition Numbers
- SciPy - Condition Numbers
- SciPy - Linear Least Squares
- SciPy - Non-Linear Least Squares
- SciPy - Finding Roots of Scalar Functions
- SciPy - Finding Roots of Multivariate Functions
- SciPy - Signal Processing
- SciPy - Signal Filtering & Smoothing
- SciPy - Short-Time Fourier Transform
- SciPy - Wavelet Transform
- SciPy - Continuous Wavelet Transform
- SciPy - Discrete Wavelet Transform
- SciPy - Wavelet Packet Transform
- SciPy - Multi-Resolution Analysis
- SciPy - Stationary Wavelet Transform
- SciPy - Statistical Functions
- SciPy - Stats
- SciPy - Descriptive Statistics
- SciPy - Continuous Probability Distributions
- SciPy - Discrete Probability Distributions
- SciPy - Statistical Tests & Inference
- SciPy - Generating Random Samples
- SciPy - Kaplan-Meier Estimator Survival Analysis
- SciPy - Cox Proportional Hazards Model Survival Analysis
- SciPy Spatial Data
- SciPy - Spatial
- SciPy - Special Functions
- SciPy - Special Package
- SciPy Advanced Topics
- SciPy - CSGraph
- SciPy - ODR
- SciPy Useful Resources
- SciPy - Reference
- SciPy - Quick Guide
- SciPy - Cheatsheet
- SciPy - Useful Resources
- SciPy - Discussion
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