This module defines two classes, Zi and Qi, the Gaussian integers and Gaussian rational numbers, respectively.
Mathematically, the integers are denoted by
The Gaussian integers are denoted by
and the Gaussian rationals are denoted by
NOTE:
- Python uses
$j$ instead of$i$ to represent complex numbers - Although, both Zi and Qi are subclasses of numbers.Complex, and
$\mathbb{Z}[i] \subset \mathbb{Q}[i] \subset \mathbb{C}$ , the class Zi is not a subclass of the class Qi.
For more information, see the two Jupyter notebooks in the notebooks directory.
For a quick look, see the examples following the plot of Gaussian primes, below.
The examples below are from "The Gaussian Integers" by Keith Conrad
>>> from gaussians import Zi, Qi
>>> alpha = Zi(27, -23)
>>> beta = Zi(8, 1)
>>> quot, rem = Zi.modified_divmod(alpha, beta)
>>> print(f"{beta * quot + rem} = {beta} * {quot} + {rem}")
(27-23j) = (8+1j) * (3-3j) + -2j
>>> alpha = Zi(11, 3)
>>> beta = Zi(1, 8)
>>> gcd = Zi.gcd(alpha, beta, verbose=True) # Prints intermediate results
>>> print(f"\ngcd({alpha}, {beta}) -> {gcd}")
(11+3j) = (1+8j) * (1-1j) + (2-4j)
(1+8j) = (2-4j) * (-2+1j) + (1-2j)
(2-4j) = (1-2j) * (2+0j) + 0j
gcd((11+3j), (1+8j)) -> (1-2j)
>>> a, x, y = Zi.xgcd(alpha, beta)
>>> print(f"a = {a}")
>>> print(f"{alpha * x + beta * y} = {alpha} * {x} + {beta} * {y}")
a = (1-2j)
(1-2j) = (11+3j) * (2-1j) + (1+8j) * 3j
If
>>> a = Zi(4, 5)
>>> b = Zi(1, -2)
>>> a / b
Qi('-6/5', '13/5')
>>> print(f"{a} / {b} -> {a / b}")
(4+5j) / (1-2j) -> (-6/5+13/5j)