# Compute the square root of complex inputs with scimath in Python

In this article, we will cover how to compute the square root of complex inputs with scimath in Python using NumPy.

### Example

```Input: [-1 -2]
Output: [0.+1.j  0.+1.41421356j]
Explanation: Square root of complex input.```

## NumPy.emath.sqrt method

The np.emath.sqrt() method from the NumPy library calculates the square root of complex inputs. A complex value is returned for negative input elements unlike numpy.sqrt. which returns NaN.

Syntax: np.emath.sqrt()

Parameters:

• x: array like object. input array.

Return: out: scalar or ndarray.

### Example 1:

If the array contains negative input values, complex numbers are returned in the output, and the shape, datatype, and dimensions of the array can be found by .shape , .dtype, and .ndim attributes.

## Python3

 `import` `numpy as np`   `# Creating an array` `array ``=` `np.array([``-``1``, ``-``2``])` `print``(array)`   `# shape of the array is` `print``(``"Shape of the array is : "``,array.shape)`   `# dimension of the array` `print``(``"The dimension of the array is : "``,array.ndim)`   `# Datatype of the array` `print``(``"Datatype of our Array is : "``,array.dtype)`   `# computing the square root of negative inputs` `print``(np.emath.sqrt(array))`

Output:

```[-1 -2]
Shape of the array is :  (2,)
The dimension of the array is :  1
Datatype of our Array is :  int64
[0.+1.j  0.+1.41421356j]```

Time complexity: O(n), where n is the number of elements in the array.
Auxiliary space: O(n), as we are creating a new array of the same size as the input array to store the square roots of the negative inputs.

### Example 2:

In this example, the NumPy package is imported. A 2-d complex array is created using NumPy.array() method and np.emath.sqrt()  is used to compute the square root of complex inputs. The shape, datatype, and dimensions of the array can be found by .shape , .dtype, and .ndim attributes.

## Python3

 `import` `numpy as np`   `# Creating an array` `array ``=` `np.array([``complex``(``1``,``2``),``complex``(``3``,``5``)])` `print``(array)`   `# shape of the array is` `print``(``"Shape of the array is : "``,array.shape)`   `# dimension of the array` `print``(``"The dimension of the array is : "``,array.ndim)`   `# Datatype of the array` `print``(``"Datatype of our Array is : "``,array.dtype)`   `# computing the square root of complex inputs` `print``(np.emath.sqrt(array))`

Output:

```[1.+2.j 3.+5.j]
Shape of the array is :  (2,)
The dimension of the array is :  1
Datatype of our Array is :  complex128
[1.27201965+0.78615138j 2.10130339+1.18973776j]```

Time complexity: O(n), where n is the number of elements in the array.
Auxiliary space: O(n), where n is the number of elements in the array.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Previous
Next