Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Working with Complex Number Data Types Using Python's Built-in complex() Function

Tech 2

Complex numbers consist of a real component and an imaginary component, where the imaginary unit follows the mathematical rule i² = -1. These numeric type are foundational for use cases across signal processing, physics modeling, engineering design, and digital image processing. Python’s built-in complex() constructor generates complex number objects programmatically for these workflows.

Function Syntax

The complex() constructor supports two input patterns:

  1. Two optional numeric parameters:
complex(real_part=0, imag_part=0)
  • real_part: Numeric value (integer, float, or numeric expression) defining the real component of the output complex number, defaults to 0 if not provided
  • imag_part: Numeric value defining the coefficient of the imaginary component, defaults to 0 if not provided
  1. A single string input formatted as a valid complex number (e.g., "2+5j"), with no whtiespace between terms.

Core Usage Patterns

Generate Complex Values via Constructor

# Complex value with real=2, imaginary=3
comp_val1 = complex(2, 3)
print(f"2 + 3i equivalent: {comp_val1}")

# Complex value with only real component specified
comp_val2 = complex(7)
print(f"Pure real complex value: {comp_val2}")

# Complex value with only imaginary component specified via keyword argument
comp_val3 = complex(imag_part=-6)
print(f"Pure imaginary complex value: {comp_val3}")

# Generate complex value from string input
comp_val4 = complex("4+2j")
print(f"Complex parsed from string: {comp_val4}")

Define Complex Values with Literal Syntax

For simpler use cases, you can define complex values directly using j or J as the imaginary unit suffix, no constructor required:

# Literal complex definition
lit_val1 = 5 + 8j
lit_val2 = -12j
print(f"Literal complex value 1: {lit_val1}")
print(f"Literal complex value 2: {lit_val2}")

Common Practical Use Cases

Complex Arithmetic for Mathematical Computation

The complex() constructor simplifies setup for operaitons that require complex number inputs:

comp_x = complex(3, 4)
comp_y = complex(2, -1)

# Run arithmetic operations
multiply_output = comp_x * comp_y
addition_output = comp_x + comp_y
print(f"Multiplication result: {multiply_output}")
print(f"Addition result: {addition_output}")

Signal Processing and Control System Modeling

Complex number are used to represent amplitude and phase information for AC signals, frequency responses, and control system transfer functions:

import math

signal_frequency = 15 # Hz
amplitude = 4
time_sample = 0.2
phase_shift = math.pi / 3

# Represent time-domain AC signal as complex value
ac_signal = amplitude * complex(
    math.cos(signal_frequency * time_sample + phase_shift),
    math.sin(signal_frequency * time_sample + phase_shift)
)

Electromagnetic and Wave Physics Simulations

Complex values simplify representation of oscillating physical quantities like electric fields, sound waves, and quantum states:

wave_amplitude = 6
propagation_angle = math.pi / 4

# Represent electric field vector with amplitude and phase
electric_field = wave_amplitude * complex(
    math.cos(propagation_angle),
    math.sin(propagation_angle)
)

Image Frequency Domain Transformations

In image processing, Fourier transforms convert spatial domain image data too frequency domain representations stored as arrays of complex values:

import cv2
import numpy as np

# Load grayscale input image
input_img = cv2.imread("sample_photo.png", cv2.IMREAD_GRAYSCALE)
# Compute 2D Fast Fourier Transform, output is complex-valued array
freq_domain_data = np.fft.fft2(input_img)
Tags: Python

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.