Friday, October 3, 2025
HomeLanguagesCompute the square root of complex inputs with scimath in Python

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.

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32331 POSTS0 COMMENTS
Milvus
85 POSTS0 COMMENTS
Nango Kala
6703 POSTS0 COMMENTS
Nicole Veronica
11867 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11929 POSTS0 COMMENTS
Shaida Kate Naidoo
6818 POSTS0 COMMENTS
Ted Musemwa
7080 POSTS0 COMMENTS
Thapelo Manthata
6775 POSTS0 COMMENTS
Umr Jansen
6776 POSTS0 COMMENTS