NumPy ndarray.conjugate()
The numpy.ndarray.conjugate()
method returns the complex conjugate of each element in an array.
If the array contains complex numbers, their imaginary parts are negated.
Syntax
ndarray.conjugate()
Parameters
This method does not take any parameters.
Return Value
Returns an array of the same shape and dtype as the original, but with the complex conjugate applied to all elements. If the array is purely real, the output remains unchanged.
Examples
1. Computing the Complex Conjugate of a NumPy Array
In this example, we create an array of complex numbers and compute their conjugates.
import numpy as np
# Create an array of complex numbers
arr = np.array([1 + 2j, 3 - 4j, 5 + 6j])
# Compute the complex conjugate
conjugate_arr = arr.conjugate()
# Print the original and conjugated arrays
print("Original array:", arr)
print("Conjugate array:", conjugate_arr)
Output:
Original array: [1.+2.j 3.-4.j 5.+6.j]
Conjugate array: [1.-2.j 3.+4.j 5.-6.j]
The complex conjugate negates the imaginary part of each complex number in the array.
2. Applying conjugate()
to a 2D Array
Here, we compute the complex conjugate of a 2D array of complex numbers.
import numpy as np
# Create a 2D array of complex numbers
arr = np.array([[2 + 3j, 4 - 5j],
[6 + 7j, 8 - 9j]])
# Compute the conjugate
conjugate_arr = arr.conjugate()
# Print the results
print("Original array:\n", arr)
print("\nConjugate array:\n", conjugate_arr)
Output:
Original array:
[[2.+3.j 4.-5.j]
[6.+7.j 8.-9.j]]
Conjugate array:
[[2.-3.j 4.+5.j]
[6.-7.j 8.+9.j]]
Each element in the 2D array has its imaginary part negated.
3. Computing the Conjugate of a Real Number Array
Applying conjugate()
to a purely real array does not change the values.
import numpy as np
# Create an array of real numbers
arr = np.array([1, 2, 3, 4])
# Compute the conjugate
conjugate_arr = arr.conjugate()
# Print the results
print("Original array:", arr)
print("Conjugate array:", conjugate_arr)
Output:
Original array: [1 2 3 4]
Conjugate array: [1 2 3 4]
Since the array contains only real numbers, applying conjugate()
does not affect the values.
4. Conjugate of a Mixed Complex and Real Number Array
If an array contains both real and complex numbers, only the complex numbers are affected.
import numpy as np
# Create an array with both real and complex numbers
arr = np.array([1, 2 + 3j, 4, 5 - 6j])
# Compute the conjugate
conjugate_arr = arr.conjugate()
# Print the results
print("Original array:", arr)
print("Conjugate array:", conjugate_arr)
Output:
Original array: [1.+0.j 2.+3.j 4.+0.j 5.-6.j]
Conjugate array: [1.-0.j 2.-3.j 4.-0.j 5.+6.j]
Real numbers remain unchanged, while the imaginary parts of complex numbers are negated.