Calculate the absolute value of complex numbers in Numpy

NumpyServer Side ProgrammingProgramming

To return the absolute value of complex values, use the numpy.absolute() method in Python Numpy. 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.

The condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value. Note that if an uninitialized out array is created via the default out=None, locations within it where the condition is False will remain uninitialized.

NumPy offers comprehensive mathematical functions, random number generators, linear algebra routines, Fourier transforms, and more. It supports a wide range of hardware and computing platforms, and plays well with distributed, GPU, and sparse array libraries.

Steps

At first, import the required library −

import numpy as np

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

arr = np.array([56.+0.j, 27.+0.j, 68.+0.j, 49.+0.j, 120.+0.j,3 + 4.j])

Display the array −

print("Array...
", arr)

Get the type of the array −

print("
Our Array type...
", arr.dtype)

Get the dimensions of the Array −

print("
Our Array Dimension...
",arr.ndim)

Get the shape of the Array −

print("
Our Array Shape...
",arr.shape)

To return the absolute value of complex values, use the numpy.absolute() method in Python Numpy −

print("
Result...
",np.absolute(arr))

Example

import numpy as np

# Create an array with complex type using the array() method
arr = np.array([56.+0.j, 27.+0.j, 68.+0.j, 49.+0.j, 120.+0.j,3 + 4.j])

# Display the array
print("Array...
", arr) # Get the type of the array print("
Our Array type...
", arr.dtype) # Get the dimensions of the Array print("
Our Array Dimension...
",arr.ndim) # Get the shape of the Array print("
Our Array Shape...
",arr.shape) # To return the absolute value of complex values, use the numpy.absolute() method in Python Numpy print("
Result...
",np.absolute(arr))

Output

Array...
[ 56.+0.j 27.+0.j 68.+0.j 49.+0.j 120.+0.j 3.+4.j]

Our Array type...
complex128

Our Array Dimension...
1

Our Array Shape...
(6,)

Result...
[ 56. 27. 68. 49. 120. 5.]
raja
Updated on 08-Feb-2022 09:50:38

Advertisements