NumPy ndarray.conj()

The numpy.ndarray.conj() method computes the complex conjugate of each element in a NumPy array. The complex conjugate of a number flips the sign of its imaginary part.

Syntax

</>
Copy
ndarray.conj()

Return Value

Returns an array of the same shape and type as the input, where each complex number is replaced with its complex conjugate.


Examples

1. Computing the Complex Conjugate of a 1D Array

In this example, we create a 1D NumPy array with complex numbers and compute their complex conjugates.

</>
Copy
import numpy as np

# Creating a 1D array with complex numbers
arr = np.array([1 + 2j, 3 - 4j, -2 + 5j])

# Computing the complex conjugate
result = arr.conj()

# Printing the original and conjugated arrays
print("Original array:", arr)
print("Conjugate array:", result)

Output:

Original array: [ 1.+2.j  3.-4.j -2.+5.j]
Conjugate array: [ 1.-2.j  3.+4.j -2.-5.j]

Each element’s imaginary part has been negated, forming the complex conjugate of the original array.

2. Computing the Complex Conjugate of a 2D Array

This example demonstrates computing the complex conjugate of a 2D NumPy array.

</>
Copy
import numpy as np

# Creating a 2D array with complex numbers
arr = np.array([[2 + 3j, -1 - 2j],
                [4 - 5j, -3 + 6j]])

# Computing the complex conjugate of the 2D array
result = arr.conj()

# Printing the original and conjugated arrays
print("Original array:\n", arr)
print("Conjugate array:\n", result)

Output:

Original array:
 [[ 2.+3.j -1.-2.j]
 [ 4.-5.j -3.+6.j]]
Conjugate array:
 [[ 2.-3.j -1.+2.j]
 [ 4.+5.j -3.-6.j]]

The imaginary part of each element is negated, producing the complex conjugate for the entire 2D array.

3. Applying conj() on a Mixed Data Type Array

If the array contains real numbers, the complex conjugate operation does not modify them.

</>
Copy
import numpy as np

# Creating an array with both real and complex numbers
arr = np.array([5, 2 + 3j, 7, -4 - 6j])

# Computing the complex conjugate
result = arr.conj()

# Printing the original and conjugated arrays
print("Original array:", arr)
print("Conjugate array:", result)

Output:

Original array: [ 5.+0.j  2.+3.j  7.+0.j -4.-6.j]
Conjugate array: [ 5.-0.j  2.-3.j  7.-0.j -4.+6.j]

Real numbers remain unchanged since their imaginary part is zero.

4. Using conj() in Mathematical Operations

The conj() method is useful in mathematical operations such as calculating magnitudes and dot products of complex vectors.

</>
Copy
import numpy as np

# Defining two complex vectors
v1 = np.array([1 + 2j, 2 - 3j, -1 + 4j])
v2 = np.array([2 - 1j, -3 + 2j, 4 - 5j])

# Computing the dot product using the complex conjugate of v1
dot_product = np.dot(v1.conj(), v2)

# Printing the computed dot product
print("Dot product:", dot_product)

Output:

Dot product: (-36-21j)

The conj() method ensures correct computation of the dot product in complex space by negating the imaginary part before multiplication.