# How to Calculate Square Root using Numpy in Python?

There are many measures in mathematics that intrigue one. One such measure is the square root! While we can readily cry the number out loud when the question is to find the square root for the numbers which are the product of themselves, things take an ugly turn when they no longer comply with the aforementioned condition.

Also read: RMSE – Root Mean Square Error in Python

In this article, we would be exploring the different methods of putting into use the in-built function within the numpy library for calculating the square root of the given entities – the sqrt( ) function!

First things first! Import the numpy library before getting on with the deployment of the sqrt( ) function using the below code.

```import numpy as np
```

We shall cover the aspects of the sqrt( ) function in the following sections.

• Syntax of sqrt( ) function
• Calculating Square Root for One-Dimensional Array
• Calculating Square Root for N-Dimensional Array
• Calculating Square Root for Complex Numbers
• Limitations of sqrt( ) function

## Syntax of the sqrt( ) function

Before diving into the details of how to use the sqrt( ) function, let us get started by understanding the basic components that it needs for proper functioning.

```numpy.sqrt(x, out=None, where=True, dtype=None)
```

where,

• x – input array or scalar entity for which the square root is to be calculated
• out – an optional construct set to none by default, but could be used to store the results in the desired array which is of the same length as the output
• where – an optional construct which is used to calculate the universal function (ufunc) at the given position when set to True (default setting) or not calculate when set to False
• dtype – an optional construct used to specify the data type which is being used

## Calculating Square Root for One-Dimensional Array

After importing the numpy library let us construct a one-dimensional array with the elements for which the square root is to be calculated as shown below.

```ar1 = [[12, 36, 71, 99]]
```

Now let us use the sqrt( ) function to deduce the results for the above array.

```np.sqrt(ar1)
```

## Calculating Square Root for N-Dimensional Array:

Similar to one-dimensional arrays, one can also calculate the square root of the given entities grouped together in N-dimensional arrays. Here is a two-dimensional array for which the square root is calculated using the sqrt( ) function as shown below.

```ar2 = np.array([[12.5, 33.3, 25],
[26, 79, 14.5]], dtype = float)
np.sqrt(ar2)
```

## Calculating Square Root for Complex Numbers

While a straightforward deduction of a square root can give us a run for the money, things get even trickier when we venture into the territory of complex numbers. To put things into perspective, the following is the formula used to calculate the square root of a complex number!

To save us from misery, the sqrt( ) function also has the capability to deduce the square root of complex numbers too. It has the ability to deduce a complex number directly from the given array, but the flip side to this characteristic is that the function goes on to consider every other element within that array also as a complex number.

```ar3 = [[12+5j, 33-3j, 25],
[26, 7-9j, 14+5j]]
np.sqrt(ar3)
```

## Limitations of sqrt( ) function

While it extends its abilities to complex numbers, it is not that the sqrt( ) function is all mighty, rather it also has its limitations too. One such is the disability to calculate the square root for negative numbers.

Let us now attempt to find the square root of ‘-6’ and bear witness to its outcome.

## Conclusion

Now that we have reached the end of this article, hope it has elaborated on how to find the square root of array elements using the sqrt( ) function from the numpy library. Here’s another article that explains how to find the maximum amongst a set of array elements using numpy in Python. There are numerous other enjoyable & equally informative articles in AskPython that might be of great help to those who are looking to level up in Python. Whilst you enjoy those, adios!