HOWTO · NumPy

Imaginary Numbers in NumPy Arrays

This tutorial demonstrates how to store and manipulate imaginary numbers in NumPy arrays. Learn to create complex number arrays, perform arithmetic operations, and access real and imaginary parts easily. Enhance your numerical computing skills with practical examples and explanations. Ideal for Python enthusiasts and engineers alike, this guide will help you leverage NumPy for complex number applications.

On this page

Python writes the imaginary part of a complex-number literal with j, as in 2j. NumPy arrays can store these complex values, apply arithmetic element by element, and expose each value’s real and imaginary components. Use complex literals when the values are already known; choose a complex dtype when creating an array from real-valued input that will later need to hold complex results.

Create NumPy arrays with complex numbers

An imaginary number has no real component, while a complex number combines a real component with an imaginary one. Python’s j suffix marks the imaginary part, so 2j is purely imaginary and 1 + 2j has both components. NumPy infers a complex element type when an array is built from complex literals. This is useful when your starting data already includes both parts.

Run this example:

import numpy as np

values = np.array([1 + 2j, 3 + 4j, 5 + 6j])
print(values)
[1.+2.j 3.+4.j 5.+6.j]

The array contains three values. In each printed element, the number before the sign is the real component and the number attached to j is the imaginary component. NumPy’s compact display is a representation of the values; it does not change Python’s j notation. Keep the j suffix on the imaginary term when writing a literal. Writing 1 + 2 instead would create an ordinary real number because neither term is marked as imaginary.

Choose a complex dtype when initializing an array

When an array starts with real numbers but must retain complex-valued results later, choose a complex dtype at initialization. For example, np.ones(3, dtype=complex) creates three complex-valued elements initialized from real ones. If you let NumPy infer the type from real-only input, the resulting array has a real dtype; assigning complex values into a real array cannot preserve their imaginary components. Setting the type up front avoids that loss and makes the array’s intended use clear.

This choice concerns the array’s element type, not a separate operation for adding an imaginary part. A complex value can have an imaginary component of zero and still belong to a complex-typed array. Conversely, a real-looking value such as 1.0 does not by itself make an array complex. Use dtype=complex when the data source is real-valued but later calculations or assignments need complex numbers. Use complex literals directly when the initial values already carry imaginary components; NumPy can infer the complex type from those values.

Perform arithmetic on complex arrays

NumPy applies the standard arithmetic operators element by element when two arrays have compatible shapes. The following complete example creates two arrays and computes their sum, difference, product, and quotient. Each result is a new array, and complex division follows the usual rules for complex numbers. The inputs are deliberately chosen so the denominator elements are nonzero.

import numpy as np

left = np.array([1 + 2j, 3 + 4j])
right = np.array([2 + 3j, 4 + 5j])

added = left + right
subtracted = left - right
multiplied = left * right
divided = left / right

print("Addition:", added)
print("Subtraction:", subtracted)
print("Multiplication:", multiplied)
print("Division:", divided)
Addition: [3.+5.j 7.+9.j]
Subtraction: [-1.-1.j -1.-1.j]
Multiplication: [-4. +7.j -8.+31.j]
Division: [0.61538462+0.07692308j 0.7804878 +0.02439024j]

The arrays have the same shape, so corresponding elements are paired: the first element of left is combined with the first element of right, and likewise for the second. This is elementwise arithmetic, not matrix multiplication. For matrix products, use an appropriate matrix operation instead. Division requires care with denominator values: a denominator of zero does not have a finite quotient, so choose inputs suited to the calculation you intend to perform.

Access the real and imaginary parts

For a complex NumPy array, .real and .imag provide array views of the real and imaginary components, respectively. They are useful when a later calculation needs just one component, or when you want to inspect the two parts separately. The component arrays keep the same element positions as the source, so index k in each corresponds to index k in the original complex array.

import numpy as np

values = np.array([1 + 2j, 3 + 4j, 5 + 6j])
real_parts = values.real
imaginary_parts = values.imag

print("Real parts:", real_parts)
print("Imaginary parts:", imaginary_parts)

For the values in this example, the real components are 1, 3, and 5; the imaginary components are 2, 4, and 6. These attributes separate existing values without changing the original complex array. If you need to construct a complex array from separate real and imaginary arrays, combine those arrays with real_parts + 1j * imaginary_parts; make sure corresponding inputs have compatible shapes. Together, literal construction, an explicit complex dtype, elementwise operators, and component access cover the common steps for working with imaginary values in NumPy arrays.