numpy.conj() function is alias for numpy.conjugate() function.

NumPy conj()

The numpy.conj() function computes the complex conjugate of each element in an input array. The complex conjugate is obtained by negating the imaginary part of the complex number.

Syntax

</>
Copy
numpy.conj(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)

Parameters

ParameterTypeDescription
xarray_likeInput array of complex numbers.
outndarray, None, or tuple of ndarray and None, optionalOptional output array where the result is stored. If None, a new array is created.
wherearray_like, optionalBoolean mask specifying which elements to compute. Elements where where=False retain their original value.
castingstr, optionalDefines the casting behavior when computing the complex conjugate.
orderstr, optionalMemory layout order of the output array.
dtypedata-type, optionalDefines the data type of the output array.
subokbool, optionalDetermines if subclasses of ndarray are preserved in the output.

Return Value

Returns an array with the complex conjugates of the input elements. If the input is a scalar, a scalar is returned.


Examples

1. Computing the Complex Conjugate of a Single Value

Here, we compute the complex conjugate of a single complex number.

</>
Copy
import numpy as np

# Define a complex number
complex_number = 3 + 4j  

# Compute the complex conjugate
result = np.conj(complex_number)

# Print the result
print("Complex conjugate of", complex_number, "is", result)

Output:

Complex conjugate of (3+4j) is (3-4j)

2. Computing the Complex Conjugate for an Array

We compute the complex conjugate for multiple complex numbers in an array.

</>
Copy
import numpy as np

# Define an array of complex numbers
complex_array = np.array([1 + 2j, 3 - 4j, -2 + 5j])

# Compute the complex conjugate of each element
conjugates = np.conj(complex_array)

# Print the results
print("Original complex array:", complex_array)
print("Complex conjugate array:", conjugates)

Output:

Original complex array: [ 1.+2.j  3.-4.j -2.+5.j]
Complex conjugate array: [ 1.-2.j  3.+4.j -2.-5.j]

3. Using the out Parameter

Using an output array to store results instead of creating a new array.

</>
Copy
import numpy as np

# Define an array of complex numbers
complex_array = np.array([2 + 3j, -1 - 4j, 5 + 6j])

# Create an output array with the same shape
output_array = np.empty_like(complex_array)

# Compute the complex conjugates and store in output_array
np.conj(complex_array, out=output_array)

# Print the results
print("Computed complex conjugates:", output_array)

Output:

Computed complex conjugates: [ 2.-3.j -1.+4.j  5.-6.j]

4. Using the where Parameter

Using a condition to compute the complex conjugate only for selected elements.

</>
Copy
import numpy as np

# Define an array of complex numbers
complex_array = np.array([2 + 3j, -1 - 4j, 5 + 6j])

# Define a mask (compute conjugate only where mask is True)
mask = np.array([True, False, True])

# Compute complex conjugates where mask is True
result = np.conj(complex_array, where=mask)

# Print the results
print("Computed complex conjugates with mask:", result)

Output:

Computed complex conjugates with mask: [ 2.-3.j 0.+0.j  5.-6.j]

The complex conjugate is computed only for elements where mask=True. The other values remain unchanged.