Python – scipy.linalg.sqrtm

ScipyScientific ComputingProgramming

The sqrtm() function of scipy.linalg package can be used to find the square root of an input matrix.

Syntax

scipy.linalg.sqrtm(x)

Example 1

Let us consider the following example −

# Importing the required libraries from scipy
from scipy import linalg
import numpy as np

# Define the input array
x = np.array([[14 , 2] , [89 , 33]])
print("Input array:\n", x)

# Calculate the square root
r = linalg.sqrtm(x)

# Display the square root
print("Square Root of x: \n", r)

Output

It will generate the following output −

Input array:
[[14 2]
[89 33]]
Square Root of x:
[[3.43430132 0.22262855]
[9.90697038 5.54927253]]

Example 2

Let us take another example −

# Importing the required libraries from scipy
from scipy import linalg
import numpy as np

# Define the input array
x = np.array([[25 , 8] , [66 , 54]])
print("Input array:\n", x)

# Calculate the square root
m = linalg.sqrtm(x, disp=False)

# Display the square root
print("Square Root of x: \n", m)

Output

The above program will generate the following output −

Input array:
[[25 8]
[66 54]]
Square Root of x:
(array([[4.59645076, 0.68513573],
[5.65236974, 7.08006776]]), 4.668211715240082e-30)
raja
Updated on 22-Dec-2021 10:07:13

Advertisements