Understanding numpy.complex64 and numpy.complex128 types (5 examples)

Updated: February 25, 2024 By: Guest Contributor Post a comment

Overview

NumPy is a fundamental package for scientific computing in Python. It offers powerful data structures, implementing multi-dimensional arrays and matrices. These data structures are essential for various scientific computations. NumPy supports a range of data types, including complex numbers. In this tutorial, we’ll delve into the world of numpy.complex64 and numpy.complex128 types, offering a detailed explanation and showcasing their applicability through five illustrative examples.

Complex Numbers in NumPy

Complex numbers are numbers with a real and an imaginary part. In NumPy, complex64 and complex128 types are used to represent these numbers. The 64 and 128 in the names refer to the bit width. numpy.complex64 is a complex number represented by two 32-bit floats (real and imaginary parts), while numpy.complex128 is represented by two 64-bit floats. Understanding these types is crucial for scientific computing tasks that involve complex numbers, such as signal processing or solving differential equations.

Example 1: Basic Usage of Complex Numbers

import numpy as np

# Creating a complex number using numpy.complex64
c_num_64 = np.array([1.5 + 2.5j], dtype=np.complex64)
print('numpy.complex64:', c_num_64)

# Creating a complex number using numpy.complex128
c_num_128 = np.array([1.5 + 2.5j], dtype=np.complex128)
print('numpy.complex128:', c_num_128)

This example demonstrates the basic creation and printing of complex numbers using both complex64 and complex128. Although the input complex numbers are identical, they are stored differently due to their type, which affects both the precision and the maximum ranges of real and imaginary values they can represent.

Example 2: Arithmetic Operations with complex64 and complex128

import numpy as np

# Arithmetic operations
c1 = np.array([2+3j], dtype=np.complex64)
c2 = np.array([4-5j], dtype=np.complex64)
result = c1 + c2
print("Addition result:", result)

result = c1 * c2
print("Multiplication result:", result)

This example showcases how to perform basic arithmetic operations such as addition and multiplication with complex numbers. The operations are straightforward and similar to those performed on real numbers, illustrating NumPy’s ease of use when handling complex data types.

Example 3: Applying Mathematical Functions

import numpy as np

# Apply np.sqrt on a complex number
c_num = np.array([-1+0j], dtype=np.complex64)
sqrt_c_num = np.sqrt(c_num)
print("Square root:", sqrt_c_num)

Many mathematical functions in NumPy, such as np.sqrt, can be applied directly to complex numbers. This capability is particularly useful in fields like quantum mechanics and signal processing, where complex numbers are frequent.

Example 4: Complex Number Arrays and Vectorized Operations

import numpy as np

# Vectorized operations on complex number arrays
c_array = np.array([1+2j, 3+4j, 5+6j], dtype=np.complex64)
result = np.abs(c_array)
print("Absolute values:", result)

NumPy excels in performing vectorized operations on arrays. The same is true for arrays of complex numbers. This example demonstrates the calculation of the absolute values of an array of complex numbers, highlighting the performance benefits of NumPy’s vectorized operations.

Example 5: Real and Imaginary Parts

import numpy as np

# Extracting real and imaginary parts
complex_array = np.array([1+2j, 3+4j], dtype=np.complex64)
real_parts = complex_array.real
imaginary_parts = complex_array.imag
print("Real parts:", real_parts)
print("Imaginary parts:", imaginary_parts)

NumPy provides convenient attributes to access the real and imaginary parts of complex numbers. This functionality is useful for a variety of tasks, including data analysis and scientific research, where separating these components may be necessary.

Conclusion

Throughout this tutorial, we’ve explored the functionalities of numpy.complex64 and numpy.complex128 types through practical examples. Whether you’re performing arithmetic operations, applying mathematical functions, or manipulating arrays of complex numbers, NumPy offers robust solutions. Understanding these complex data types enriches your toolbox for tackling scientific computing challenges.