Compute the square root of complex inputs with scimath in Python

PythonNumpyServer Side ProgrammingProgramming

To compute the square root of input, use the emath.sqrt() method in Python Numpy. The method returns the square root of x. If x was a scalar, so is out, otherwise an array is returned. The parameter x is the input value. For negative input elements, a complex value is returned

Steps

At first, import the required libraries −

import numpy as np

The complex input explicity using complex() method −

a = complex(-9.0, 0.0)

Display the array −

print("Display the complex value...\n",a)

To compute the square root of input, use the emath.sqrt() method in Python Numpy −

print("\nResult...\n",np.emath.sqrt(a))

Example

import numpy as np

# Explicity using complex() method
a = complex(-9.0, 0.0)

# Display the array
print("Display the complex value...\n",a)

# To compute the square root of input, use the emath.sqrt() method in Python Numpy
# The method returns the square root of x. If x was a scalar, so is out, otherwise an array is returned.
print("\nResult...\n",np.emath.sqrt(a))

Output

Display the complex value...
(-9+0j)

Result...
3j
raja
Updated on 01-Mar-2022 07:45:08

Advertisements