The ComplexNumber
class represents a complex number with real and imaginary parts,
offering a range of operations for complex number arithmetic.
- Representation of complex numbers in Cartesian format.
- Arithmetic operations: addition, subtraction, multiplication, division, and exponentiation.
- Utility methods: square root, conjugation, modulus (absolute value), argument (phase angle), and conversions between polar and cartesian forms.
To use the ComplexNumber
class, include the complex_number.py
file in your project and import it as needed.
from complex_number import ComplexNumber
# Initialize a complex number with real and imaginary parts
z = ComplexNumber(3, 4)
Adds two complex numbers, or a complex number and a real number.
z1 = ComplexNumber(1, 2)
z2 = ComplexNumber(3, 4)
result = z1 + z2
Mathematically:
Subtracts two complex numbers, or a complex number and a real number.
result = z1 - z2
Mathematically:
Multiplies two complex numbers, or a complex number and a real number.
result = z1 * z2
Mathematically:
Divides two complex numbers, or a complex number by a real number.
result = z1 / z2
Mathematically:
Raises a complex number to the power of a real number. Complex exponents are not implemented.
result = z1 ** 2
Mathematically:
Calculates the square root of a complex number.
result = z1.sqrt()
Returns the conjugate of a complex number.
result = z1.conjugate()
Mathematically: If
Calculates the modulus (absolute value) of a complex number.
result = z1.modulus()
Mathematically:
Returns the argument (phase angle) of a complex number in radians.
result = z1.argument()
Mathematically:
Converts a complex number to its polar form (magnitude and phase).
result = z1.to_polar()
Creates a complex number instance from polar coordinates.
z = ComplexNumber.from_polar(magnitude, angle)
Comprehensive tests covering arithmetic operations, utility methods, and special cases are included in the tests
directory. Ensure to run these tests to validate functionality.