Return the element-wise square-root of a complex type array in Numpy

NumpyServer Side ProgrammingProgramming

To return the non-negative square-root of an array, element-wise, use the numpy.sqrt() method in Python Numpy. An array of the same shape as x, containing the positive square-root of each element in x. If any element in x is complex, a complex array is returned (and the square-roots of negative reals are calculated). If all of the elements in x are real, so is y, with negative elements returning nan. If out was provided, y is a reference to it. This is a scalar if x is a scalar.

The out is a location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple (possible only as a keyword argument) must have length equal to the number of outputs.

Steps

At first, import the required library −

import numpy as np

Create an array with complex type using the array() method −

arr = np.array([-1j, 1e-15, 27.+0.j, 68.-2.j, 49.+0.j])

Display the array −

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

Get the type of the array −

print("\nOur Array type...\n", arr.dtype)

Get the dimensions of the Array −

print("\nOur Array Dimension...\n",arr.ndim)

Get the shape of the Array −

print("\nOur Array Shape...\n",arr.shape)

To return the non-negative square-root of an array, element-wise, use the numpy.sqrt() method in Python Numpy −

print("\nResult...\n",np.sqrt(arr))

Example

import numpy as np

# Create an array with complex type using the array() method
arr = np.array([-1j, 1e-15, 27.+0.j, 68.-2.j, 49.+0.j])

# Display the array
print("Array...\n", arr)

# Get the type of the array
print("\nOur Array type...\n", arr.dtype)

# Get the dimensions of the Array
print("\nOur Array Dimension...\n",arr.ndim)

# Get the shape of the Array
print("\nOur Array Shape...\n",arr.shape)

# To return the non-negative square-root of an array, element-wise, use the numpy.sqrt() method in Python Numpy
print("\nResult...\n",np.sqrt(arr))

Output

Array...
[-0.0e+00-1.j 1.0e-15+0.j 2.7e+01+0.j 6.8e+01-2.j 4.9e+01+0.j]

Our Array type...
complex128

Our Array Dimension...
1

Our Array Shape...
(5,)

Result...
[7.07106781e-01-0.70710678j 3.16227766e-08+0.j
5.19615242e+00+0.j 8.24710269e+00-0.1212547j
7.00000000e+00+0.j ]
raja
Updated on 07-Feb-2022 11:12:11

Advertisements