A comprehensive Python library for performing operations on complex numbers using tuple representations without using the python complex numbers data type.
The library provides 8 essential complex number operations:
- Addition - Add two complex numbers
- Subtraction - Subtract two complex numbers
- Multiplication - Multiply two complex numbers
- Division - Divide two complex numbers
- Modulus - Calculate the magnitude of a complex number
- Conjugate - Find the complex conjugate
- Phase - Calculate the argument (angle) of a complex number
- Coordinate Conversion - Convert between Cartesian and polar representations
- Python 3.6 or higher
- No external dependencies required (uses only Python standard library)
- Clone the repository:
git clone https://github.com/AnderssonProgramming/complex-number-library.git
cd complex-number-library- The library is ready to use - no installation required!
Import the library and start using complex number operations:
from ComplexNumberLibrary import *
# Define complex numbers as (real, imaginary) tuples
z1 = (3, 4) # Represents 3 + 4i
z2 = (1, 2) # Represents 1 + 2i
# Basic operations
sum_result = add_complex(z1, z2) # (4, 6)
diff_result = subtract_complex(z1, z2) # (2, 2)
product = multiply_complex(z1, z2) # (-5, 10)
quotient = divide_complex(z1, z2) # (2.2, -0.4)
# Advanced operations
magnitude = modulus_complex(z1) # 5.0
conjugate = conjugate_complex(z1) # (3, -4)
angle = phase_complex(z1) # 0.9272952180016122
# Coordinate conversion
polar_form = cartesian_to_polar(z1) # (5.0, 0.9272952180016122)
cartesian_form = polar_to_cartesian(polar_form) # (3.0, 4.0)The project includes comprehensive unit tests using Python's unittest framework.
To run all tests:
python TestComplexNumberLibrary.pyExpected output:
...........
----------------------------------------------------------------------
Ran 11 tests in 0.010s
OK
The test suite includes:
- 11 test methods covering all 8 functions
- Multiple test cases per function (minimum 2 per function as required)
- Edge cases: zero division, zero values, negative numbers
- Mathematical properties: modulus multiplication property, conjugate properties
- Round-trip conversions: Cartesian ↔ Polar conversions
Adds two complex numbers.
- Parameters:
z1,z2- Complex numbers as(real, imaginary)tuples - Returns: Sum as
(real, imaginary)tuple
Subtracts z2 from z1.
- Parameters:
z1,z2- Complex numbers as(real, imaginary)tuples - Returns: Difference as
(real, imaginary)tuple
Multiplies two complex numbers.
- Parameters:
z1,z2- Complex numbers as(real, imaginary)tuples - Returns: Product as
(real, imaginary)tuple
Divides z1 by z2.
- Parameters:
z1,z2- Complex numbers as(real, imaginary)tuples - Returns: Quotient as
(real, imaginary)tuple - Raises:
ZeroDivisionErrorif z2 is zero
Calculates the modulus (magnitude) of a complex number.
- Parameters:
z- Complex number as(real, imaginary)tuple - Returns: Modulus as
float
Finds the complex conjugate.
- Parameters:
z- Complex number as(real, imaginary)tuple - Returns: Conjugate as
(real, -imaginary)tuple
Calculates the phase (argument) in radians.
- Parameters:
z- Complex number as(real, imaginary)tuple - Returns: Phase angle as
float(in radians)
Converts from Cartesian to polar coordinates.
- Parameters:
z- Complex number as(real, imaginary)tuple - Returns: Polar form as
(magnitude, phase)tuple
Converts from polar to Cartesian coordinates.
- Parameters:
polar_z- Complex number as(magnitude, phase)tuple - Returns: Cartesian form as
(real, imaginary)tuple
complex-number-library/
├── ComplexNumberLibrary.py # Main library implementation
├── TestComplexNumberLibrary.py # Comprehensive unit tests
├── README.md # Project documentation
├── LICENSE # GPL-3.0 license
└── .gitignore # Git ignore rules
This library uses tuples to represent complex numbers:
- Cartesian form:
(real, imaginary)where the complex number isreal + imaginary*i - Polar form:
(magnitude, phase)where the complex number ismagnitude * e^(i*phase)
- Python 3 - Core programming language
- Math module - For trigonometric and mathematical functions
- Unittest - Testing framework
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'feat: add some amazing feature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Please use conventional commit messages (feat, fix, docs, test, chore, etc.).
We use SemVer for versioning. For the versions available, see the tags on this repository.
- Andersson Programming - Initial work - AnderssonProgramming
This project is licensed under the GPL-3.0 license - see the LICENSE file for details.
- Python Software Foundation for the excellent unittest framework
- Mathematical foundations from complex analysis theory
- Academic requirements that inspired this implementation