Home > Back-end >  How do you take a square root of a matrix that contains zeros in python?
How do you take a square root of a matrix that contains zeros in python?

Time:06-03

let's say we want to compute the square root of the identity matrix, how would you do in python? I have tried numpy.sqrt() but it didn't work. Any ideas? Thanks a lot!

CodePudding user response:

You can try this:

my_matrix = np.array([[1.0, 3.0], [1.0, 4.0]])

np.sqrt(my_matrix.data)

CodePudding user response:

If you want to do this element wise you can use

import numpy as np
my_matrix = np.array([0, 4, 9])
sqrt_my_matrix = np.sqrt(my_matrix)
print(sqrt_my_matrix)

CodePudding user response:

You can do it with a package called scipy. See the code below:

from scipy.linalg import sqrtm
my_matrix = np.array([[1.0, 3.0], [1.0, 4.0]])
sqrt_my_matrix = sqrtm(my_matrix)
  • Related