Compute Square Root of Negative Input with eMath in Python



To compute the square root of input, use the scimath.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

Creating a numpy array using the array() method −

arr = np.array([1, -4, -9, 16, -25, 36])

Display the array −

print("Our Array...\n",arr)

Check the Dimensions −

print("\nDimensions of our Array...\n",arr.ndim)

Get the Datatype −

print("\nDatatype of our Array object...\n",arr.dtype)

Get the Shape −

print("\nShape of our Array object...\n",arr.shape)

To compute the square root of input, use the scimath.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(arr))

Example

Open Compiler
import numpy as np # Creating a numpy array using the array() method arr = np.array([1, -4, -9, 16, -25, 36]) # Display the array print("Our Array...\n",arr) # Check the Dimensions print("\nDimensions of our Array...\n",arr.ndim) # Get the Datatype print("\nDatatype of our Array object...\n",arr.dtype) # Get the Shape print("\nShape of our Array object...\n",arr.shape) # To compute the square root of input, use the scimath.sqrt() method in Python Numpy print("\nResult...\n",np.emath.sqrt(arr))

Output

Our Array...
[ 1 -4 -9 16 -25 36]

Dimensions of our Array...
1

Datatype of our Array object...
int64

Shape of our Array object...
(6,)

Result...
[1.+0.j 0.+2.j 0.+3.j 4.+0.j 0.+5.j 6.+0.j]
Updated on: 2022-03-01T07:42:45+05:30

217 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements