SciPy - sqrtm() Function



The scipy.linalg.sqrtm method returns the matrix square root of a square matrix A. The square root of a matrix is defined as another matrix B such that B^2 = A. This function is used to calculate the principal square root. For matrices with positive definite eigenvalues, the principal square root is unique, but for matrices with mixed or negative eigenvalues, it involves the computation of complex eigenvalues.

The approach is widely used in scientific computations, for example, in quantum physics, numerical simulations, and control theory. It also contains a utility for matrix decomposition, stability analysis, and solution of matrix equations.

Errors can be caused by input matrix A that has eigenvalues with negative real portions or an ill-conditioned matrix. The conditions may provoke warnings or misleading results. Also, non-square matrices will produce a ValueError because the squareroot is only defined for square matrices.

Syntax

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

.sqrtm(A, disp=True, blocksize=64)

Parameters

This method accepts the following parameters −

  • A (N, N) array_like − Input square matrix (nn) to compute the square root.

  • disp (bool, optional) − If True, warnings for ill-conditioned matrices are displayed. If False, the function also returns an error estimate.

  • blocksize (integer, optional) − Optional parameter to specify block size for computational optimization.

Return Value

  • sqrtm (N, N) ndarray − The computed matrix square root B such that B^2 = A

  • errest (float) − A measure of the Frobenius norm of the error in the computation.

Example 1

The square root of a positive definite matrix A is another matrix B such that B^2 = A. This is useful when doing calculations that require covariance matrices and stability analysis.

In the below code, a 22 symmetric positive definite matrix is given. The sqrtm() function computes the square root with high accuracy.

import numpy as np
from scipy.linalg import sqrtm

# Input: Positive definite matrix
A = np.array([[4, 2], 
              [2, 3]])

# Compute the matrix square root
sqrt_matrix = sqrtm(A)
print("Matrix Square Root:\n", sqrt_matrix)

When we run above program, it produces following result

Matrix Square Root:
 [[1.91936596 0.56216928]
 [0.56216928 1.63828133]]

Example 2

The scipy.linalg.sqrtm() function computes the square root of matrices whose elements can be complex numbers. The outcome is a matrix that is also complex valued. B^2 = A.

In the below example, we create a 2x2 complex matrix. The function sqrtm() computes the square root with the output containing complex numbers.

import numpy as np
from scipy.linalg import sqrtm

# Input: Complex matrix
A = np.array([[1 + 1j, 0], 
              [0, 2 + 2j]])
sqrt_matrix = sqrtm(A)

print("Matrix Square Root:\n", sqrt_matrix)

Following is an output of the above code

Matrix Square Root:
 [[1.09868411+0.45508986j 0.        +0.j        ]
 [0.        +0.j         1.55377397+0.64359425j]]

Example 3

For ill-conditioned matrices, if disp = False then it returns the error estimate in addition to the result to be sure that the calculation is done with enough precision.

In the below code, a ill-conditioned matrix is used as an input. The function sqrtm() calculates the square root and returns an error estimate in order to compute its accuracy.

import numpy as np
from scipy.linalg import sqrtm

# Input: Ill-conditioned matrix
A = np.array([[1e-5, 1], 
              [0, 1e-5]])

sqrt_matrix, error_estimate = sqrtm(A, disp=False)

print("Matrix Square Root:\n", sqrt_matrix)
print("Error Estimate:", error_estimate)

Output of the above code is as follows

Matrix Square Root:
 [[3.16227766e-03 1.58113883e+02]
 [0.00000000e+00 3.16227766e-03]]
Error Estimate: 1.2325951642845714e-32

Example 4

Sqrtm simplifies the computation of huge matrices by breaking them into smaller pieces through its blocksize parameter. This improves performance and assures effective memory usage without compromising accuracy.

In the below example, a 44 symmetric matrix is presented to demonstrate how the blocksize parameter affects. The work would be split up into smaller pieces by setting blocksize=2, thus being more efficient in computation speed for bigger matrices. Thus, the outcome would be square root matrix and B^2 = A so that the output retains the same symmetry and form as the original input matrix.

import numpy as np
from scipy.linalg import sqrtm

# Input: Large symmetric matrix
A = np.array([[4, 1, 0, 0], 
              [1, 4, 1, 0], 
              [0, 1, 4, 1], 
              [0, 0, 1, 4]])

# Compute the matrix square root with optimized block size
sqrt_matrix = sqrtm(A, blocksize=2)
print("Matrix Square Root:\n", sqrt_matrix)

Output of the above code is as follows

Matrix Square Root:
 [[ 1.9837087   0.25420207 -0.01663014  0.00213107]
 [ 0.25420207  1.96707856  0.25633314 -0.01663014]
 [-0.01663014  0.25633314  1.96707856  0.25420207]
 [ 0.00213107 -0.01663014  0.25420207  1.9837087 ]]
scipy_linalg.htm
Advertisements