
- 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 - signm() Function
The scipy.linalg.signm() function computes the matrix sign function for a given input matrix A. It finds a matrix transformation based on the eigenvalues and defines the sign for each eigenvalue as +1 if it is positively real and -1 if it is negatively real.
Errors arise whenever the input matrix is ill-conditioned or nearly has eigenvalues along the imaginary axis, resulting in numerical inaccuracies. The algorithm expects a square matrix, but a non-square input raises a ValueError. It is crucial to validate input matrices to avoid these errors.
If signm() is applied to a diagonal matrix, the result is a diagonal matrix with entries as the signs of the original diagonal elements. For a matrix with purely imaginary eigenvalues, signm() results can be sensitive to numerical precision. Combining signm() with expm() allows reconstruction of matrices for specific stability conditions.Syntax
The syntax for the SciPy signm() method is as follows −
.signm(A, disp=True)
Parameters
This method accepts the following parameters −
A (N, N) array_like − Input square matrix (nn), real or complex.
disp (bool, optional) − If True, a warning will be displayed if the result is not properly conditioned. If False, return a tuple (signm_result, error_estimate), where error_estimate represents the Frobenius norm of the difference between A^2 and the identity matrix.
Return Value
This method returns the following −
signm(N, N) ndarray − The matrix sign of A with eigenvalues replaced by their respective signs.
errest (float) − A measure of numerical error in the computation.
Example 1
The function signm() takes a matrix of different positive and negative eigenvalues and returns the signs of each, which are useful for stability analysis.
The below code, is the basic example of using signm() method where we passed a 22 matrix as an input with values 2. The function computes the eigenvalues 2 and gives a matrix with entries that are approximations of sign(A).
import numpy as np from scipy.linalg import signm # Input: Matrix with positive and negative eigenvalues A = np.array([[2, 0], [0, -2]]) # Compute the matrix sign sign_matrix = signm(A) print("Matrix Sign Function:\n", sign_matrix)
When we run above program, it produces following result
Matrix Sign Function: [[ 1. 0.] [ 0. -1.]]
Example 2
For ill-conditioned matrices, setting disp=False in signm() produces both the result and an error estimate, so numerical precision is ensured.
In the below code, a nearly singular matrix is passed to signm() with disp=False. The method calculate the matrix sign and returns the Frobenius norm of the error.
import numpy as np from scipy.linalg import signm # Input: Ill-conditioned matrix A = np.array([[1e-5, 1], [0, 1e-5]]) # Compute the matrix sign with error estimation sign_matrix, error_estimate = signm(A, disp=False) print("Matrix Sign Function:\n", sign_matrix) print("Error Estimate:", error_estimate)
Following is an output of the above code
Matrix Sign Function: [[ 1.00000000e+00 -9.20245596e-14] [ 0.00000000e+00 1.00000000e+00]] Error Estimate: 1.3981319628517874e-13
Example 3
The method signm() enforces that the input matrix needs to be square. A ValueError is raised in case a non-square matrix is provided.
In the below example, we created a 2x3 non-square matrix and passed to signm(). An error is returned along with highlighting the wrong input.
import numpy as np from scipy.linalg import signm # Input: Non-square matrix A = np.array([[1, 2, 3], [4, 5, 6]]) try: # Attempt to compute the matrix sign sign_matrix = signm(A) except ValueError as e: print("Error:", e)
Output of the above code is as follows
Error: expected square array_like input
Example 4
Combining the signm() and logm() operations is useful in control theory. signm tests for eigenvalue stability, and logm computes the logarithm of a matrix, which can be utilized in diagnosing the long-term behavior of the system.
In the below code, matrix A describes a controlled system. Eigenvalues determine stability. The signm method identifies whether the eigenvalues are stable or unstable. The logm() will actually calculate a better understanding of system dynamics. This is useful in putting together feedback controllers and ensuring reliability.
import numpy as np from scipy.linalg import signm, logm # Define a system matrix A = np.array([[0.5, 0.1], [0.3, -0.2]]) # Compute the matrix sign sign_matrix = signm(A) # Compute the matrix logarithm log_matrix = logm(A) print("Matrix Sign Function:\n", sign_matrix) print("Matrix Logarithm:\n", log_matrix)
Output of the above code is as follows
Matrix Sign Function: [[ 0.89625816 0.25607376] [ 0.76822128 -0.89625816]] Matrix Logarithm: [[-0.6572398 +0.1629573j 0.10367732-0.40223972j] [ 0.31103195-1.20671916j -1.38298103+2.97863535j]]