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]]
scipy_linalg.htm
Advertisements