How to calculate a root square in python ?


To calculate a root square in python, there are several options:

Positive float

>>> x = 9.0
>>> x**(0.5)
3.0

with pow() function:

>>> pow(x,0.5)
3.0

with math module

>>> import math
>>> math.sqrt(x)
3.0

with numpy

>>> import numpy as np
>>> np.sqrt(x)
3.0

Complex number

>>> z = 1j
>>> z
1j

with numpy

>>> import numpy as np
>>> np.sqrt(z)
(0.7071067811865476+0.7071067811865475j)

with pow():

>>> pow(z,0.5)
(0.7071067811865476+0.7071067811865475j)

Matrix

>>> import numpy as np
>>> a = np.array((16,9,4,25))
>>> np.sqrt(a)
array([4., 3., 2., 5.])

References