algolib.core package

Submodules

algolib.core.complex module

A lightweight Complex number implementation (without using Python’s built-in complex).

This module provides a small, well-documented Complex class suitable for learning and algorithmic implementations. It supports algebraic form (a + bi) and polar form (r·e^{iθ}), with common operations.

Notes

  • This class uses plain floats and is immutable.

  • We intentionally avoid Python’s built-in complex to practice fundamentals.

Examples

>>> z1 = Complex(3, 4)
>>> z1.modulus()
5.0
>>> z2 = Complex.from_polar(2, math.pi/2)
>>> (z1 + z2).re  
3.0
>>> (z1.conjugate()).im
-4.0
class algolib.core.complex.Complex(re: float, im: float)[source]

Bases: object

Complex number in algebraic form \(a + b \mathrm{i}\).

Parameters:
  • re (float) – Real part \(a\).

  • im (float) – Imaginary part \(b\).

Raises:

InvalidTypeError – If either part is not a real number (int/float).

almost_equal(other: Complex, tol: float = 1e-12) bool[source]

Return True if each component differs by at most tol (absolute).

This is safer than exact float equality.

argument() float[source]

Compute the principal argument of the complex number.

Returns:

The argument (angle in radians) in the range \((-\pi, \pi]\).

Return type:

float

Examples

>>> z = Complex(0, 1)
>>> z.argument()
1.5707963267948966
conjugate() Complex[source]

Compute the complex conjugate of the number.

Returns:

The conjugate \(a - b \mathrm{i}\).

Return type:

Complex

Examples

>>> z = Complex(3, 4)
>>> z.conjugate()
Complex(re=3.0, im=-4.0)
static from_cartesian(re: float, im: float) Complex[source]

Construct a complex number from Cartesian coordinates.

Parameters:
  • re (float) – The real part of the complex number.

  • im (float) – The imaginary part of the complex number.

Returns:

The complex number corresponding to the Cartesian coordinates.

Return type:

Complex

Examples

>>> z = Complex.from_cartesian(3, 4)
>>> z
Complex(re=3.0, im=4.0)
static from_iterable(pair: Iterable[float]) Complex[source]

Construct a complex number from an iterable of two numbers.

Parameters:

pair (Iterable[float]) – An iterable containing exactly two elements: the real and imaginary parts.

Returns:

The complex number constructed from the iterable.

Return type:

Complex

Raises:

InvalidTypeError – If the iterable does not contain exactly two numeric elements.

Examples

>>> z = Complex.from_iterable([3, 4])
>>> z
Complex(re=3.0, im=4.0)
static from_polar(r: float, theta: float) Complex[source]

Construct a complex number from polar coordinates.

Parameters:
  • r (float) – The modulus (radius) of the complex number. Must be non-negative.

  • theta (float) – The argument (angle in radians) of the complex number.

Returns:

The complex number corresponding to the polar coordinates.

Return type:

Complex

Raises:

Examples

>>> z = Complex.from_polar(2, math.pi / 2)
>>> z
Complex(re=1.2246467991473532e-16, im=2.0)
im: float
modulus() float[source]

Compute the modulus (absolute value) of the complex number.

Returns:

The modulus \(\sqrt{a^2 + b^2}\).

Return type:

float

Examples

>>> z = Complex(3, 4)
>>> z.modulus()
5.0
normalized() Complex[source]

Normalize the complex number by \(z/|z|\) to have a modulus of 1.

Returns:

The normalized complex number.

Return type:

Complex

Raises:

InvalidValueError – If the complex number is zero.

Examples

>>> z = Complex(3, 4)
>>> z.normalized()
Complex(re=0.6, im=0.8)
re: float
to_polar() Tuple[float, float][source]

Convert the complex number to polar coordinates.

Returns:

A tuple (r, theta) where r is the modulus and theta is the argument.

Return type:

tuple of float

Examples

>>> z = Complex(3, 4)
>>> z.to_polar()
(5.0, 0.9272952180016122)
to_tuple() Tuple[float, float][source]

Convert the complex number to a tuple of its real and imaginary parts.

Returns:

A tuple (re, im) representing the real and imaginary parts.

Return type:

tuple of float

Examples

>>> z = Complex(3, 4)
>>> z.to_tuple()
(3.0, 4.0)

Module contents

class algolib.core.Complex(re: float, im: float)[source]

Bases: object

Complex number in algebraic form \(a + b \mathrm{i}\).

Parameters:
  • re (float) – Real part \(a\).

  • im (float) – Imaginary part \(b\).

Raises:

InvalidTypeError – If either part is not a real number (int/float).

almost_equal(other: Complex, tol: float = 1e-12) bool[source]

Return True if each component differs by at most tol (absolute).

This is safer than exact float equality.

argument() float[source]

Compute the principal argument of the complex number.

Returns:

The argument (angle in radians) in the range \((-\pi, \pi]\).

Return type:

float

Examples

>>> z = Complex(0, 1)
>>> z.argument()
1.5707963267948966
conjugate() Complex[source]

Compute the complex conjugate of the number.

Returns:

The conjugate \(a - b \mathrm{i}\).

Return type:

Complex

Examples

>>> z = Complex(3, 4)
>>> z.conjugate()
Complex(re=3.0, im=-4.0)
static from_cartesian(re: float, im: float) Complex[source]

Construct a complex number from Cartesian coordinates.

Parameters:
  • re (float) – The real part of the complex number.

  • im (float) – The imaginary part of the complex number.

Returns:

The complex number corresponding to the Cartesian coordinates.

Return type:

Complex

Examples

>>> z = Complex.from_cartesian(3, 4)
>>> z
Complex(re=3.0, im=4.0)
static from_iterable(pair: Iterable[float]) Complex[source]

Construct a complex number from an iterable of two numbers.

Parameters:

pair (Iterable[float]) – An iterable containing exactly two elements: the real and imaginary parts.

Returns:

The complex number constructed from the iterable.

Return type:

Complex

Raises:

InvalidTypeError – If the iterable does not contain exactly two numeric elements.

Examples

>>> z = Complex.from_iterable([3, 4])
>>> z
Complex(re=3.0, im=4.0)
static from_polar(r: float, theta: float) Complex[source]

Construct a complex number from polar coordinates.

Parameters:
  • r (float) – The modulus (radius) of the complex number. Must be non-negative.

  • theta (float) – The argument (angle in radians) of the complex number.

Returns:

The complex number corresponding to the polar coordinates.

Return type:

Complex

Raises:

Examples

>>> z = Complex.from_polar(2, math.pi / 2)
>>> z
Complex(re=1.2246467991473532e-16, im=2.0)
im: float
modulus() float[source]

Compute the modulus (absolute value) of the complex number.

Returns:

The modulus \(\sqrt{a^2 + b^2}\).

Return type:

float

Examples

>>> z = Complex(3, 4)
>>> z.modulus()
5.0
normalized() Complex[source]

Normalize the complex number by \(z/|z|\) to have a modulus of 1.

Returns:

The normalized complex number.

Return type:

Complex

Raises:

InvalidValueError – If the complex number is zero.

Examples

>>> z = Complex(3, 4)
>>> z.normalized()
Complex(re=0.6, im=0.8)
re: float
to_polar() Tuple[float, float][source]

Convert the complex number to polar coordinates.

Returns:

A tuple (r, theta) where r is the modulus and theta is the argument.

Return type:

tuple of float

Examples

>>> z = Complex(3, 4)
>>> z.to_polar()
(5.0, 0.9272952180016122)
to_tuple() Tuple[float, float][source]

Convert the complex number to a tuple of its real and imaginary parts.

Returns:

A tuple (re, im) representing the real and imaginary parts.

Return type:

tuple of float

Examples

>>> z = Complex(3, 4)
>>> z.to_tuple()
(3.0, 4.0)