How to Work with Complex Numbers in NumPy

Updated: January 23, 2024 By: Guest Contributor Post a comment

Introduction

In scientific computing, complex numbers are essential for various computations and analyses. NumPy, a fundamental package for numerical computation in Python, provides excellent support for dealing with complex numbers. In this tutorial, we’ll learn how to work with complex numbers in NumPy, and we’ll cover everything from the basics of creating complex arrays to more advanced operations.

Getting Started with Complex Numbers

First, let’s discuss how to create complex number arrays in NumPy:

import numpy as np
# Create a complex number
a = np.array([1+2j, 3+4j, 5+6j])
print(a)

Output:

[1.+2.j 3.+4.j 5.+6.j]

The ‘j’ indicates the imaginary part of the complex number.

Complex Number Attributes

import numpy as np
a = np.array([1+2j, 3+4j, 5+6j])
# Real part
print(a.real)
# Imaginary part
print(a.imag)

Output:

[1. 3. 5.]
[2. 4. 6.]

Arithmetic with Complex Numbers

Now let’s perform some arithmetic operations:

import numpy as np
a = np.array([1+2j, 3+4j])
b = np.array([5+6j, 7+8j])
# Addition
print(a + b)
# Subtraction
print(a - b)
# Multiplication
print(a * b)
# Division
print(a / b)

Output:

[ 6.+8.j 10.+12.j]
[-4.-4.j -4.-4.j]
[-7.+22.j -11.+52.j]
[0.26...+0.07...j 0.28...+0.11...j]

Absolute Value and Angle

import numpy as np
a = np.array([1+2j, 3+4j])
# Absolute value
print(np.abs(a))
# Phase angle
print(np.angle(a))

Output:

[2.23606798 5.]
[1.10714872 0.92729522]

Complex Functions and Operations

NumPy also provides functions for other complex number operations:

import numpy as np
a = np.array([1+2j, 3+4j])
# Complex conjugate
print(np.conj(a))
# Exponential
print(np.exp(a))
# Logarithm
print(np.log(a))
# Power
print(np.power(a, 2))
# Square root
print(np.sqrt(a))

Output:

[1.-2.j 3.-4.j]
[-1.13120438+2.47172667j -13.12878308+15.20078446j]
[0.80471896+1.10714872j 1.60943791+0.92729522j]
[-3.+4.j -7.+24.j]
[1.27201965+0.78615138j 2.+1.j]

Working with Matrices

Complex numbers can be organized into matrices for linear algebra operations:

import numpy as np
a = np.array([[1+2j, 3+4j], [5+6j, 7+8j]])
# Matrix multiplication
b = np.array([[9+10j, 11+12j], [13+14j, 15+16j]])
print(np.dot(a, b))
# Eigenvalues and eigenvectors
values, vectors = np.linalg.eig(a)
print(values)
print(vectors)

Output:

[[-37.+100.j -47.+120.j]
[-83.+220.j -107.+264.j]]
[-0.71984631+0.j 8.71984631+0.j]
[[ 0.41380294+0.j -0.57402182+0.j]
[ 0.91036648+0.j -0.8186735 +0.j]]

Handling Complex-Valued Functions

In addition to numerical computations, NumPy enables the evaluation of functions that return complex numbers:

import numpy as np
# Function that returns a complex number
def complex_func(x):
    return np.sin(x) + 1j*np.cos(x)
x = np.linspace(0, 2*np.pi, 5)
print(complex_func(x))

Output:

[1.+0.j 0.+1.j -1.+0.j -0.-1.j 1.-0.j]

Conclusion

Working with complex numbers in NumPy is straightforward and immensely useful for many scientific and engineering applications. By mastering the array creation, arithmetic operations, and utilizing complex functions, you can handle complex computations with ease using NumPy.