SciPy linalg.pinvh() Function



The scipy.linalg.pinvh() function calculates the Moore-Penrose pseudo-inverse of a Hermitian matrix. This matrix is a complex matrix that is identical to its own conjugate transpose. The SciPy.linalg package contains the function linalg.pinh() function.

The Moore-Penrose pseudo inverse generalizes the concept of a matrix inverse for cases where the matrix inverse when the matrix A is invertible. Even if A is not invertible, the Moore-Penrose pseudo inverse is still defined. In this context, we calculate the generalized inverse of a complex Hermitian symmetric matrix by employing its eigenvalue decomposition and considering all eigenvalues with large absolute value. This function also raises an LinAlgError error if the eigenvalue algorithm fails to converge.

Syntax

Following is the syntax for the SciPy linalg.pinvh() function.

scipy.linalg.pinvh(a, atol=None, rtol=None, lower=True, return_rank=False, check_finite=True, cond=None, rcond=None)

Parameters

The parameters for the scipy.linalg.pinv() function are listed below −

  • a: This parameter accepts an array that represents a real symmetric or complex Hermitian matrix requiring pseudo-inversion.

  • atol: This parameter is of the float data type, representing the absolute threshold term. It is optional, with a default value of 0.

  • rtol: This parameter is of the float data type, representing the relative threshold term. It is optional, with a default value of N x eps, where eps denotes the machine precision value corresponding to the data type of the input matrix a.

  • lower: This parameter is of the Boolean data type and determines whether to extract the relevant array data from the lower or upper triangle of the input matrix a.

  • return_rank: This parameter is of the Boolean data type. When set to True, it returns the effective rank of the matrix. It is an optional parameter.

  • check_finite: This parameter is of the Boolean data type and is used to verify whether the input matrix contains only finite numbers. Disabling this parameter enhances performances; however, if the input matrix contains infinities or NaN values, it may lead to issues such as crashes or non-termination. The default value is True.

  • cond, rcond: In earlier versions, these values were intended for use as atol with rtol=0. However, if both rcond and cond were specified, rcond would overwrite cond, resulting in incorrect code. Therefore, it is strongly recommended to use the tolerance mentioned above instead of these.

Return Value

The linalg.pinvh() function accepts the parameters mentioned above and computes the pseudo-inverse of the input Hermitian matrix a. Additionally, it returns the effective rank of the matrix when the return_rank parameter is specified and set to True.

Example 1

The linalg.pinvh() function is used to compute the pseudo-inverse of a Hermitian matrix efficiently. It handles complex data types and ensures numerical stability during the computation.

In the following code, necessary libraries like NumPy and SciPy's linalg.pinvh are imported. A complex Hermitian matrix is defined as the input, and the pinvh() function is applied to calculate its pseudo-inverse. Finally, the computed result is displayed to user by printing the pseudo=-inverse matrix.

import numpy as np
from scipy.linalg import pinvh

# Define the input matrix
a = np.array([[0, 2-1j, 1j, -2-1j],
              [8, 5, 9, 6],
              [1, 1+1j, 1-2j, 0],
              [5, 9, 1-2j, 0]])

# Compute the pseudo-inverse of the matrix
b = pinvh(a)

# Print the result
print('The pseudo-inverse matrix of the input matrix a is:', b)

Output

The result is generated as follows −

The pseudo-inverse matrix of the input matrix a is: [[-0.08360129+0.00000000e+00j  0.09807074-2.09003215e-02j
  -0.01768489+2.23472669e-01j  0.04662379-1.12540193e-02j]
 [ 0.09807074+2.09003215e-02j -0.02411576+1.62630326e-19j
  -0.09646302+8.84244373e-02j  0.05787781-1.76848875e-02j]
 [-0.01768489-2.23472669e-01j -0.09646302-8.84244373e-02j
   0.95659164-1.38235777e-17j -0.03697749+1.41479100e-01j]
 [ 0.04662379+1.12540193e-02j  0.05787781+1.76848875e-02j
  -0.03697749-1.41479100e-01j -0.085209  +0.00000000e+00j]]

Example 2

This function is specifically designed to compute the pseudo-inverse of symmetric or Hermitian matrices. This function is highly efficient and ensures numerical stability during the computation, making it suitable for dealing with matrices that may not be invertible in the conventional sense.

In the following code, the NumPy library is used to generate a random matrix using a random number generator. The matrix is then converted into a symmetric matrix by multiplying it with its transpose. The pinvh() function from the SciPy library is applied to compute the pseudo-inverse of the symmetric matrix. Finally, assertions are performed to verify that computed pseudo-inverse satisfies the mathematical properties expected of a pseudo-inverse, ensuring correctness of the computation.

import numpy as np
from scipy.linalg import pinvh

# Create a random number generator
rng = np.random.default_rng()

# Generate a random matrix and create its symmetric version
a = rng.standard_normal((5, 6))
a = np.dot(a, a.T)

# Compute the pseudo-inverse of the symmetric matrix
B = pinvh(a)

# Print the pseudo-inverse matrix
print('The pseudo-inverse matrix of the input matrix a is:', B)

# Verify the pseudo-inverse properties
print(np.allclose(a, a @ B @ a))  # Should return True if the pseudo-inverse is correct
print(np.allclose(B, B @ a @ B))  # Should return True if the pseudo-inverse is correct

Output

The code is generated as follows −

The pseudo-inverse matrix of the input matrix a is: [[1.06980307 0.2540103  0.47398341 0.2942561  0.47902096]
 [0.2540103  0.26467483 0.19749685 0.13091301 0.09716396]
 [0.47398341 0.19749685 0.46996561 0.03549391 0.25846023]
 [0.2942561  0.13091301 0.03549391 0.34441315 0.04172379]
 [0.47902096 0.09716396 0.25846023 0.04172379 0.52240443]]
True
True

Example 3

A ValueError occurs when an operation receives arguments of the correct type but invalid value, violating expectations.

Here, pinvh() expects a square Hermitian matrix. However, the provided matrix is non-squared, leading to a ValueError.

import numpy as np
from scipy.linalg import pinvh

# Define the input matrix
a = np.array([[0, 2-1j, 1j, -2-1j],
              [8, 5, 9, 6],
              [1, 1+1j, 1-2j, 0]])

# Compute the pseudo-inverse of the Hermitian matrix
b = pinvh(a)

# Print the result
print('The pseudo-inverse matrix of the input matrix a is:', b)

Output

The output is obtained as follows −

Traceback (most recent call last):
  File "/home/main.py", line 10, in <module>
    b = pinvh(a)
        ^^^^^^^^
  File "/usr/lib/python3/dist-packages/scipy/linalg/_basic.py", line 1536, in pinvh
    s, u = _decomp.eigh(a, lower=lower, check_finite=False)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/scipy/linalg/_decomp.py", line 462, in eigh
    raise ValueError('expected square "a" matrix')
ValueError: expected square "a" matrix
parent_file.htm
Advertisements