Complex Numbers (complex) in Python
Complex numbers in Python are used to represent numbers that have both a real part and an imaginary part. They are expressed in the form a + bj, where a is the real part, b is the imaginary part, and j is the imaginary unit.
Characteristics of Complex Numbers
Syntax and Notation
In Python, complex numbers are represented as a + bj where:
- a is the real part.
- b is the imaginary part.
- j is the imaginary unit.
Examples
# Defining complex numbers c1 = 3 + 4j # Real part is 3, imaginary part is 4 c2 = -1 - 2j # Real part is -1, imaginary part is -2 print(c1) # Output: (3+4j) print(c2) # Output: (-1-2j)
Real and Imaginary Parts
You can access the real and imaginary parts of a complex number using the .real and .imag attributes.
Examples
# Accessing real and imaginary parts c = 5 + 6j print(c.real) # Output: 5.0 print(c.imag) # Output: 6.0
Arithmetic Operations with Complex Numbers
Complex numbers support arithmetic operations similar to those for real numbers, including addition, subtraction, multiplication, and division.
Examples
c1 = 2 + 3j c2 = 1 - 4j # Addition add_result = c1 + c2 print(add_result) # Output: (3-1j) # Subtraction sub_result = c1 - c2 print(sub_result) # Output: (1+7j) # Multiplication mul_result = c1 * c2 print(mul_result) # Output: (14-5j) # Division div_result = c1 / c2 print(div_result) # Output: (-0.4117647058823529+0.6470588235294118j)
Conjugate of a Complex Number
The conjugate of a complex number is obtained by changing the sign of the imaginary part. You can find the conjugate using the .conjugate() method.
Example
c = 4 + 5j # Conjugate conjugate_c = c.conjugate() print(conjugate_c) # Output: (4-5j)
Magnitude and Phase
The magnitude (or absolute value) and phase (or angle) of a complex number can be computed using the abs() function and the cmath module.
- Magnitude: abs(c) returns the magnitude of the complex number c.
- Phase: cmath.phase(c) returns the phase of the complex number c.
Examples
c = 3 + 4j # Magnitude magnitude = abs(c) print(magnitude) # Output: 5.0 # Phase phase = cmath.phase(c) print(phase) # Output: 0.9272952180016122 (in radians)
Conversion Between Types
Complex numbers can be converted to and from other numeric types, but be aware that converting to a real number will lose the imaginary part.
- From complex to float/int: You need to handle the real part separately, as complex numbers cannot be directly converted to float or int.
- From real numbers to complex: You can create complex numbers by specifying the imaginary part as zero.
Examples
# Complex to float/int (only real part is considered) c = 2 + 3j real_part = c.real print(float(real_part)) # Output: 2.0 print(int(real_part)) # Output: 2 # Real number to complex real_number = 7 complex_number = complex(real_number) # Equivalent to 7 + 0j print(complex_number) # Output: (7+0j)
Practical Usage
Complex numbers are used in various fields such as signal processing, electrical engineering, and quantum mechanics. They are particularly useful for solving problems involving oscillations and waves.
Example in Electrical Engineering
# Impedance calculation in electrical engineering resistance = 5 # Ohms reactance = 3j # Reactance in ohms impedance = resistance + reactance print(impedance) # Output: (5+3j) # Magnitude of impedance impedance_magnitude = abs(impedance) print(impedance_magnitude) # Output: 5.830951894845301
Checking Type
To verify if a variable is a complex number, you can use the isinstance() function.
Example
# Checking if a variable is a complex number c = 1 + 1j print(isinstance(c, complex)) # Output: True