Skip to content

Latest commit

 

History

History
134 lines (111 loc) · 4.51 KB

README.md

File metadata and controls

134 lines (111 loc) · 4.51 KB

sqrtfractions

Python module to handle linear combinations of square roots (field extension of rational numbers over integer square roots) exactly. A single immutable class, SqrtFraction, is provided, to create object of the form

$$ \frac{\sum_in_i\sqrt{r_i}}{d} \qquad n_i\in\mathbb{Z}, \ r_i, d\in\mathbb{N}^+ $$

where the values are represented by Pythons built-in arbitrary size integers, meaning there is no theoretical limit in magnitude nor precision. It's meant to be the next step from Pythons fraction/Fraction towards the reals.

Installation

pip install git+https://github.com/goessl/sqrtfractions.git

Usage

A SqrtFraction can be initialised in two ways:

  • with the constructor SqrtFraction(n={}, d=1). The numerator n can be given as an integer or as a dictionary of keys:values that correspond to the radicand:factor terms.
  • by the random factory SqrtFraction.random(N=10, precision=20).
>>> from sqrtfractions import SqrtFraction
>>> SqrtFraction(5)
+51/1
>>> SqrtFraction(5, 2)
+51/2
>>> SqrtFraction({2:3, 5:-7}, 2)
(+32-75)/2

SqrtFractions can be printed

  • in Unicode by __repr__
  • in Latex by _repr_latex_.

SqrtFractions can be casted to

  • floats,
  • ints (might check first with is_integer()),
  • Fractions (might check first with is_fraction()) &
  • bools.
>>> float(SqrtFraction(5, 2))
2.5
>>> int(SqrtFraction(5))
5
>>> SqrtFraction(5, 2).as_fraction()
Fraction(5, 2)
>>> int(SqrtFraction(5, 2))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "sqrtfractions.py", line 114, in __int__
    raise ValueError('doesn\'t represent an integer')
ValueError: doesn't represent an integer

SqrtFractions are totally ordered

>>> s, t = SqrtFraction({2:3}, 4), SqrtFraction({5:-6}, 7)
>>> s
+32/4
>>> t
-65/7
>>> s == t
False
>>> s > t
True
>>> abs(t)
+65/7

Basic arithmetic operations are implemented:

  • unary negation - and inversion (reciprocal value) ~,
  • addition + and subtraction -, multiplication * and division / with other SqrtFractions and with ints &
  • exponentiation ** to positive and negative integers.
>>> -s
-32/4
>>> ~s
+22/3
>>> 
>>> s+t
(+212-245)/28
>>> s-t
(+212+245)/28
>>> 
>>> s*t
-910/14
>>> s/t
-710/40
>>> s**-2
+81/9

For more precise descriptions of the methods please refer to the docstrings.

Design choices

  • Reducing a new SqrtFraction directly after initialization: Initially SqrtFractions didn't simplify themselves. It had been thought that the intermediate simplification during multiple consecutive operations would hinder performance. Eager simplification actually improved speed of an application. Actual testing hasn't been done.

Relevant links

TODO

License (MIT)

Copyright (c) 2024 Sebastian Gössl

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.