Exact, rational-valued hypercomplex numbers -- reals, complex
numbers, quaternions, octonions, and beyond -- built via the
Cayley-Dickson construction,
implemented in the hyprat package.
>>> from hyprat import Hy
>>> from IPython.display import display, Math
>>> z = Hy('5/2', '-16/5')
>>> print(f"{z = }\n")
>>> print(f"{str(z) = }\n")
>>> display(Math(z.latex()))z = Hy('5/2', '-16/5')
str(z) = '(5/2-16/5j)'
>>> quat = Hy(Hy(1.5, '2/3'), Hy('3/7', 4))
>>> print(f"{quat = }\n")
>>> print(f"{str(quat) = }\n")
>>> display(Math(quat.latex()))quat = Hy(Hy('3/2', '2/3'), Hy('3/7', '4'))
str(quat) = '(3/2+2/3i+3/7j+4k)'
>>> Hy.from_array([1.5, '2/3', '3/7', 4]) == quatTrue
>>> Hy.parse('(3/2+2/3i+3/7j+4k)') == quatTrue
>>> oct = Hy.from_array(['2/3', 0, 3, -5, '-2/3', '2/5', '-4/5', 2])
>>> print(f"{oct = }\n")
>>> print(f"{str(oct) = }\n")
>>> display(Math(oct.latex()))
>>> print(f"\n{Hy.parse(str(oct)) == oct = }")oct = Hy(Hy(Hy('2/3', '0'), Hy('3', '-5')), Hy(Hy('-2/3', '2/5'), Hy('-4/5', '2')))
str(oct) = '(2/3+3j-5k-2/3L+2/5iL-4/5jL+2kL)'
Hy.parse(str(oct)) == oct = True
The single immutable Hy class represents every rank:
rank 0 -> a plain fractions.Fraction (a "real")
rank 1 -> Hy(real, imag) (a "complex")
rank 2 -> Hy(h1, h2), where h1 & h2 are rank 1 (a "quaternion")
rank 3 -> Hy(h3, h4), where h3 & h4 are rank 2 (an "octonion")
rank n -> Hy(x, y), where x & y are rank (n-1) ("sedenion", "pathion", ...)
+ - * /, conjugation, norms, and inverses all follow the standard
recursive Cayley-Dickson formulas, using exact fractions.Fraction
arithmetic throughout -- no floating-point rounding.
Full documentation, including the API reference, is on Read the Docs.
pip install git+https://github.com/alreich/hyper_rationals.gitOr, for local development:
git clone https://github.com/alreich/hyper_rationals.git
cd hyper_rationals
pip install -e .[dev]pytest(or python -m unittest discover -s tests)
pip install -e .[docs]
sphinx-build -b html docs/source docs/_build/htmlhyper_rationals/
+-- src/hyprat/ the hyprat package (import as `from hyprat import Hy`)
+-- tests/ unit tests (unittest, run via pytest or unittest)
+-- docs/source/ Sphinx documentation source
+-- notebooks/ Jupyter notebooks (examples, Claude dialog, ...)
+-- papers/ Papers on hypercomplex numbers (quaternions, octonions, ...)
+-- .github/workflows/ CI (tests + docs build)
+-- pyproject.toml packaging / metadata
+-- .readthedocs.yaml Read the Docs build config
MIT -- see LICENSE.