Return the square of the complex-value input in Python

PythonServer Side ProgrammingProgramming

To return the element-wise square of the array input, use the numpy.square() method in Python. The method returns the element-wise x*x, of the same shape and dtype as x. This is a scalar if x is a scalar. The 1st parameter, x is the input data. The 2nd parameter, 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 3rd parameter, where, this 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.

Steps

At first, import the required libraries-

import numpy as np

Creating a numpy array using the array() method. We have added elements of complex type −

arr = np.array([[3 + 4j, 5 + 7j], [2 + 6j, -2j]])

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)

To return the element-wise square of the array input, use the numpy.square() method in Python. The method returns the element-wise x*x, of the same shape and dtype as x. This is a scalar if x is a scalar −

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

Example

import numpy as np

# Creating a numpy array using the array() method

# We have added elements of complex type
arr = np.array([[3 + 4j, 5 + 7j], [2 + 6j, -2j]])

# 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)

# To return the element-wise square of the array input, use the numpy.square() method in Python
print("\nResult...\n",np.square(arr))

Output

Our Array...
[[ 3.+4.j 5.+7.j]
[ 2.+6.j -0.-2.j]]

Dimensions of our Array...
2

Datatype of our Array object...
complex128

Result...
[[ -7.+24.j -24.+70.j]
[-32.+24.j -4. +0.j]]
raja
Updated on 25-Feb-2022 06:43:41

Advertisements