How do you write square root in python numpy?

The numpy.sqrt() function returns a non-negative square root of each element of the input array.

Syntax

numpy.sqrt(x[, out]) = <ufunc ‘sqrt’>

Parameters:

x : [array_like]

The input is array_like object.

out: [ndarray][Optional]

The output of the function can be copied to out variable. It should be of same shape as the result.

Result: [ndarray]

ndarray containing the positive square root of each element of input array. 

Examples,

>>> import numpy as np
>>> np.sqrt(4)
2.0
>>> np.sqrt([1, 2, 3, 4, 5, 6])
array([1.        , 1.41421356, 1.73205081, 2.        , 2.23606798,
       2.44948974])
>>> 

Numpy square root of a matrix

To get the square root of matrix elements we can make use of the numpy.sqrt(). 

>>> a = np.matrix([[1, 2], [3, 4]]) 
>>> a
matrix([[1, 2],
        [3, 4]])
>>> np.sqrt(a) #matrix square root of each element of a
matrix([[1.        , 1.41421356],
        [1.73205081, 2.        ]])
>>> b = np.arange(9).reshape(3,3)
>>> b
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
>>> np.sqrt(b)
array([[0.        , 1.        , 1.41421356],
       [1.73205081, 2.        , 2.23606798],
       [2.44948974, 2.64575131, 2.82842712]])
>>> 

Numpy square root of 3D array

Similar to matrices, the numpy.sqrt() function also works on multidimensional arrays. Below is an example,

>>> a = np.arange(27).reshape(3,3,3)
>>> a
array([[[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8]],

       [[ 9, 10, 11],
        [12, 13, 14],
        [15, 16, 17]],

       [[18, 19, 20],
        [21, 22, 23],
        [24, 25, 26]]])
>>> np.sqrt(a)
array([[[0.        , 1.        , 1.41421356],
        [1.73205081, 2.        , 2.23606798],
        [2.44948974, 2.64575131, 2.82842712]],

       [[3.        , 3.16227766, 3.31662479],
        [3.46410162, 3.60555128, 3.74165739],
        [3.87298335, 4.        , 4.12310563]],

       [[4.24264069, 4.35889894, 4.47213595],
        [4.58257569, 4.69041576, 4.79583152],
        [4.89897949, 5.        , 5.09901951]]])
>>> 

Numpy square root of a number

We can also obtain the square of scalar values using numpy.sqrt(). You can simply pass the number as the parameter.

>>> np.sqrt(9)
3.0
>>> np.sqrt(2)
1.4142135623730951
>>> np.sqrt(200)
14.142135623730951
>>> np.sqrt(126736)
356.0
>>> 

Numpy square root of a list

>>> a = [11, 22, 33]
>>> np.sqrt(a)
array([3.31662479, 4.69041576, 5.74456265])
>>> 

Numpy square root of tuples

>>> np.sqrt((1,2,3,4,5,6,7,89))
array([1.        , 1.41421356, 1.73205081, 2.        , 2.23606798,
       2.44948974, 2.64575131, 9.43398113])
>>> 

Numpy square root of complex numbers

The numpy.sqrt() function can also be used to find the square root of complex numbers. The square root of complex number is also a complex number.

Every complex number has a square root. So, we can assume the equation

z^2 = c

Where c is a complex number. 

To determine the square root of a complex number, we can convert the value of z in the form of a + bj to equate, where a and b are real numbers. 

Let’s determine the square root of the complex number 21 – 20j.

z^2 =  21 - 20j
z = √(21 - 20j)
a + bj = √(21 - 20j)
(a + bj)^2 = 21 - 20j
a^2 + b^2.j^2 + 2abj = 21 - 20j

In complex numbers, we know j^2 = -1. Therefore,

(a^2 - b^2) + 2abj = 21 - 20j 

If two complex numbers are equal then their real part and imaginary part are equal to each other. Hence,

a^2 - b^2 = 21 and

2abj = -20j 
abj = -10j 
b = -10/a

Substituting the value of b we can write the equation as 

a^2 - b^2 = 21
a^2 -(-10/a^2)^2 = 21
a^4 - 100/a^2 = 21
a^4 - 21a^2 - 100 = 0
(a^2 + 4)^2 . (a^2 - 25)^2 = 0

a^2 = - 4 or a^2 = 25

We know the value of a is real. Hence a = √-4 is not considered.

a = 5 or a = -5

Since, numpy.sqrt returns a non-negative the value of a = 5

b = (-10/a) = (-10/5) = -2

√(21 – 20j) = a + bj

√(21 – 20j) = 5 – 2j where a = 5 and b = -2

Let’s verify this using numpy.sqrt() function.

>>> np.sqrt(21 - 20j)
(5-2j)
>>> 

More examples,

>>> np.sqrt([9, 25, 81], dtype=np.complex)
array([3.+0.j, 5.+0.j, 9.+0.j])
>>> np.sqrt([4, -1, -3+4J])
array([2.+0.j, 0.+1.j, 1.+2.j])
>>> np.sqrt([[1+3j],[2+5j],[3-2j]])
array([[1.44261527+1.03977826j],
       [1.92160933+1.30099285j],
       [1.81735402-0.55025052j]])
>>> 

NumPy square root of a negative number

The numpy.sqrt() function returns a nan(not a number) for negative numbers. It also throws “RuntimeWarning: invalid value” warning.

>>> import numpy as np
>>> np.sqrt(-4)
<stdin>:1: RuntimeWarning: invalid value encountered in sqrt
nan
>>> 

To find the square root of negative numbers we need to consider the complex part.

Let’s say we need the square root of -9, then we can evaluate the numbers as:

√-9 = √-1 . √9

  = 3√-1

In complex number, we know j^2 = -1 i.e, j = √-1, hence

√-9 = 3j

If you are interested in obtaining the above output in Numpy, simply let the sqrt function know that:

>>> np.sqrt(-9)
nan
>>> np.sqrt(-9, dtype=np.complex) # numpy square root of negative number in complex
3j
>>> np.sqrt(-9 + 0j)
3j
>>> np.sqrt(complex(-9))
3j
>>> 

For Python use the “cmath” library.

>>> import cmath
>>> cmath.sqrt(-9)
3j
>>> 

More examples of numpy.sqrt()

Numpy square root of sum of squares

How do you write square root in python numpy?
>>> a = 12
>>> b = 24
>>> np.sqrt(a**2 + b**2)
26.832815729997478
>>> 

numpy root mean square

To calculate the root mean square we can make use of np.mean function along with np.sqrt function.

>>> a = np.arange(10)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.sqrt(np.mean(a**2))
5.338539126015656
>>> 

How do you declare a square root in Python?

sqrt() function is an inbuilt function in Python programming language that returns the square root of any number. Syntax: math.sqrt(x) Parameter: x is any number such that x>=0 Returns: It returns the square root of the number passed in the parameter.

How do I square a value in NumPy?

square(arr, out = None, ufunc 'square') : This mathematical function helps user to calculate square value of each element in the array. Parameters : arr : [array_like] Input array or object whose elements, we need to square.