How to calculate the Square Root of Negative Input with emath in Python?

Let us see how to use NumPy to compute the square root of negative inputs with emath in Python.

Python numPy.emath.sqrt() Function:

The numpy.emath.sqrt() function of the numpy module is used to compute the square root of complex inputs. In contrast to numpy.sqrt, which returns NaN for negative input elements, complex values are returned.

Syntax:

numpy.emath.sqrt()

Parameters

x: It is an array-like object given as input.

Return Value:
A scalar value or an ndarray is returned by the numpy.emath.sqrt() function.

If the array contains negative input values, complex numbers are returned in the output, and the .shape, dtype, and .ndim attributes can be used to determine the array’s shape, datatype, and dimensions respectively.

Python Program to Calculate the Square Root of Negative Input with emath

For Negative Array Values:

Approach:

  • Import numpy module using the import keyword
  • Pass some random list of negative numbers as an argument to the array() function of the numpy module to create an array.
  • Store it in a variable.
  • Print the given array.
  • Apply the shape attribute on the given array to get the shape of the given array and print the result.
  • Apply ndim attribute on the given array to get the dimension of the given array and print the result.
  • Apply dtype attribute on the given array to get the datatype of all the elements of the given array and print the result.
  • Pass the given array as an argument to the sqrt() function of emath of the numpy module to calculate the square root of the given negative input array and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np

# Pass some random list of negative numbers as an argument to the array() function
# of the numpy module to create an array.
# Store it in a variable
gvn_arry = np.array([-2, -5, -3])

# Print the given array
print("The given array is:")
print(gvn_arry)

# Apply shape attribute on the given array to get the shape of the given array and
# print the result
print("The Shape of the given array ",gvn_arry," = ", gvn_arry.shape)

# Apply ndim attribute on the given array to get the dimension of the given array and
# print the result
print("The dimension of the given array ",gvn_arry," = ",gvn_arry.ndim)

# Apply dtype attribute on the given array to get the datatype of all the elements of the
# given array and print the result
print("The datatype of the given array ",gvn_arry," = ",gvn_arry.dtype)

# Pass the given array as an argument to the sqrt() function of emath of the numpy module
# to calculate the square root of the given negative input array and print it.
print(np.emath.sqrt(gvn_arry))

Output:

The given array is:
[-2 -5 -3]
The Shape of the given array [-2 -5 -3] = (3,)
The dimension of the given array [-2 -5 -3] = 1
The datatype of the given array [-2 -5 -3] = int64
[0.+1.41421356j 0.+2.23606798j 0.+1.73205081j]

For Complex numbers:

Approach:

  • Import numpy module using the import keyword
  • Pass some random list of complex numbers using the complex() function as an argument to the array() function of the numpy module to create an array.
  • Store it in a variable.
  • Print the given array.
  • Apply the shape attribute on the given array to get the shape of the given array and print the result
  • Apply ndim attribute on the given array to get the dimension of the given array and print the result
  • Apply dtype attribute on the given array to get the datatype of all the elements of the given array and print the result
  • Pass the given array as an argument to the sqrt() function of emath of the numpy module to calculate the square root of the given array of complex values and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np

# Pass some random list of complex numbers using the complex() function as an argument
# to the array() function of the numpy module to create an array.
# Store it in a variable
gvn_arry = np.array([complex(-3, -1),complex(-2, -4)])

# Print the given array
print("The given array is:")
print(gvn_arry)

# Apply shape attribute on the given array to get the shape of the given array and
# print the result
print("The Shape of the given array ",gvn_arry," = ", gvn_arry.shape)

# Apply ndim attribute on the given array to get the dimension of the given array and
# print the result
print("The dimension of the given array ",gvn_arry," = ",gvn_arry.ndim)

# Apply dtype attribute on the given array to get the datatype of all the elements of the
# given array and print the result
print("The datatype of the given array ",gvn_arry," = ",gvn_arry.dtype)

# Pass the given array as an argument to the sqrt() function of emath of the numpy module
# to calculate the square root of the given array of complex values and print it.
print(np.emath.sqrt(gvn_arry))

Output:

The given array is:
[-3.-1.j -2.-4.j]
The Shape of the given array [-3.-1.j -2.-4.j] = (2,)
The dimension of the given array [-3.-1.j -2.-4.j] = 1
The datatype of the given array [-3.-1.j -2.-4.j] = complex128
[0.28484878-1.7553173j 1.11178594-1.79890744j]