Skip to Content

Numpy – Get the Complex Conjugate

In this tutorial, we will look at how to get the complex conjugate of a complex number in a Numpy array. The complex conjugate of a complex number is obtained by changing the sign of the imaginary part. For example for the complex number a + bj, the complex conjugate would be a - bj.

How to get the complex conjugate of complex values in a Numpy array?

get the complex conjugate of a complex number in numpy array

You can use the numpy.conj() function to get (element-wise) the complex conjugate of complex numbers in a Numpy array. The following is the syntax –

# get the complex conjugate - pass scaler value or numpy array
np.conj(a)

Note that the numpy.conj() function is an alias for the numpy.conjugate() function (they both return the element-wise complex conjugate of a Numpy array).

If you pass a scalar value, it returns the complex conjugate of that value. And, if you pass a Numpy array, it returns the element-wise array of the complex conjugates.

Examples

Let’s now look at some examples of using the above function on a scalar value and a Numpy array.

Example 1 – Get the complex conjugate for a scalar value using numpy.conj()

First, let’s pass scaler values to the numpy.conj() function.

Let’s apply the numpy.conj() function on three scalar values – a real number, a complex number with a non-zero real part, and a complex number with the real part as 0.

import numpy as np

# get the complex conjugate
print(np.conj(14))
print(np.conj(2 + 4j))
print(np.conj(3j))

Output:

14
(2-4j)
-3j

We get the complex conjugate for all the scalar values irrespective of whether we passed a real or a complex number as an argument.

Example 2 – Element-wise get the complex conjugate in a Numpy array using numpy.conj()

If you apply the numpy.conj() function on an array, it will return a Numpy array of the complex conjugates for the elements inside the array.

Let’s create a 1-D array and apply the numpy.conj() function to it.

# create a numpy array
ar = np.array([1, 2+3j, 2+0j, 0+4j, 5.7, np.nan, np.inf, 1j, 0+0j])
# element-wise get the complex conjugate in ar
np.conj(ar)

Output:

array([1. -0.j, 2. -3.j, 2. -0.j, 0. -4.j, 5.7-0.j, nan-0.j, inf-0.j,
       0. -1.j, 0. -0.j])

We get a Numpy array containing the complex conjugate of each value in the array ar.

Summary

In this tutorial, we looked at how we can use the numpy.conj() function to get the complex conjugate of a complex number in a Numpy array. Keep in mind that if you apply this function on a scalar value, it returns the resulting complex conjugate whereas if you apply it on an array, it returns the array of complex conjugates.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush

    Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.