Skip to content

A Python library developed as part of the Ciencias Naturales y Tecnología (CNYT) course. Implements core complex number operations (arithmetic, modulus, conjugate, phase, Cartesian ↔ Polar conversion) using tuple representations, with full unit test coverage and no reliance on Python’s built-in complex type.

License

Notifications You must be signed in to change notification settings

AnderssonProgramming/complex-number-library

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Complex Number Library

A comprehensive Python library for performing operations on complex numbers using tuple representations without using the python complex numbers data type.

Features

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

Getting Started

Prerequisites

  • Python 3.6 or higher
  • No external dependencies required (uses only Python standard library)

Installation

  1. Clone the repository:
git clone https://github.com/AnderssonProgramming/complex-number-library.git
cd complex-number-library
  1. The library is ready to use - no installation required!

Usage

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)

Running the Tests

The project includes comprehensive unit tests using Python's unittest framework.

To run all tests:

python TestComplexNumberLibrary.py

Expected output:

...........
----------------------------------------------------------------------
Ran 11 tests in 0.010s

OK

Test Coverage

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

API Reference

Core Operations

add_complex(z1, z2)

Adds two complex numbers.

  • Parameters: z1, z2 - Complex numbers as (real, imaginary) tuples
  • Returns: Sum as (real, imaginary) tuple

subtract_complex(z1, z2)

Subtracts z2 from z1.

  • Parameters: z1, z2 - Complex numbers as (real, imaginary) tuples
  • Returns: Difference as (real, imaginary) tuple

multiply_complex(z1, z2)

Multiplies two complex numbers.

  • Parameters: z1, z2 - Complex numbers as (real, imaginary) tuples
  • Returns: Product as (real, imaginary) tuple

divide_complex(z1, z2)

Divides z1 by z2.

  • Parameters: z1, z2 - Complex numbers as (real, imaginary) tuples
  • Returns: Quotient as (real, imaginary) tuple
  • Raises: ZeroDivisionError if z2 is zero

Mathematical Functions

modulus_complex(z)

Calculates the modulus (magnitude) of a complex number.

  • Parameters: z - Complex number as (real, imaginary) tuple
  • Returns: Modulus as float

conjugate_complex(z)

Finds the complex conjugate.

  • Parameters: z - Complex number as (real, imaginary) tuple
  • Returns: Conjugate as (real, -imaginary) tuple

phase_complex(z)

Calculates the phase (argument) in radians.

  • Parameters: z - Complex number as (real, imaginary) tuple
  • Returns: Phase angle as float (in radians)

Coordinate Conversion

cartesian_to_polar(z)

Converts from Cartesian to polar coordinates.

  • Parameters: z - Complex number as (real, imaginary) tuple
  • Returns: Polar form as (magnitude, phase) tuple

polar_to_cartesian(polar_z)

Converts from polar to Cartesian coordinates.

  • Parameters: polar_z - Complex number as (magnitude, phase) tuple
  • Returns: Cartesian form as (real, imaginary) tuple

Project Structure

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

Mathematical Representation

This library uses tuples to represent complex numbers:

  • Cartesian form: (real, imaginary) where the complex number is real + imaginary*i
  • Polar form: (magnitude, phase) where the complex number is magnitude * e^(i*phase)

Built With

  • Python 3 - Core programming language
  • Math module - For trigonometric and mathematical functions
  • Unittest - Testing framework

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'feat: add some amazing feature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Please use conventional commit messages (feat, fix, docs, test, chore, etc.).

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

License

This project is licensed under the GPL-3.0 license - see the LICENSE file for details.

Acknowledgments

  • Python Software Foundation for the excellent unittest framework
  • Mathematical foundations from complex analysis theory
  • Academic requirements that inspired this implementation

About

A Python library developed as part of the Ciencias Naturales y Tecnología (CNYT) course. Implements core complex number operations (arithmetic, modulus, conjugate, phase, Cartesian ↔ Polar conversion) using tuple representations, with full unit test coverage and no reliance on Python’s built-in complex type.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages