NumPy conjugate()
The numpy.conjugate()
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.conjugate(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
Parameter | Type | Description |
---|---|---|
x | array_like | Input array of complex numbers. |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array where the result is stored. If None, a new array is created. |
where | array_like, optional | Boolean mask specifying which elements to compute. Elements where where=False retain their original value. |
casting | str, optional | Defines the casting behavior when computing the complex conjugate. |
order | str, optional | Memory layout order of the output array. |
dtype | data-type, optional | Defines the data type of the output array. |
subok | bool, optional | Determines 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.conjugate(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.conjugate(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.conjugate(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.conjugate(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.