Return the square of the complex-value input in Python

To return the element-wise square of complex-valued arrays, use the numpy.square() method in Python. This method returns the element-wise x*x of the same shape and dtype as the input array.

Syntax

numpy.square(x, out=None, where=True)

Parameters

The numpy.square() method accepts the following parameters:

  • x − Input array or scalar
  • out − Optional output array where results are stored
  • where − Condition to broadcast over input (optional)

Example

Let's create a 2D array with complex numbers and compute their squares ?

import numpy as np

# Creating a numpy array with complex elements
arr = np.array([[3 + 4j, 5 + 7j], [2 + 6j, -2j]])

# Display the array
print("Our Array...")
print(arr)

# Check the dimensions
print("\nDimensions of our Array...")
print(arr.ndim)

# Get the datatype
print("\nDatatype of our Array object...")
print(arr.dtype)

# Calculate element-wise square
print("\nResult...")
print(np.square(arr))
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]]

How It Works

For complex numbers, the square is calculated as (a + bj)² = a² - b² + 2abj. For example:

import numpy as np

# Single complex number example
z = 3 + 4j
result = np.square(z)

print(f"Original: {z}")
print(f"Square: {result}")

# Manual calculation: (3 + 4j)² = 9 - 16 + 24j = -7 + 24j
print(f"Manual: {3**2 - 4**2} + {2*3*4}j = -7 + 24j")
Original: (3+4j)
Square: (-7+24j)
Manual: -7 + 24j = -7 + 24j

Conclusion

The numpy.square() method efficiently computes element-wise squares of complex arrays. It preserves the original array shape and dtype, making it ideal for mathematical operations on complex data structures.

Updated on: 2026-03-26T19:23:36+05:30

251 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements