https://github.com/andrew-lei/intervals
Interval arithmetic for Python using algebraic data types
https://github.com/andrew-lei/intervals
algebraic-data-types interval-arithmetic
Last synced: about 1 month ago
JSON representation
Interval arithmetic for Python using algebraic data types
- Host: GitHub
- URL: https://github.com/andrew-lei/intervals
- Owner: andrew-lei
- License: mit
- Created: 2018-04-26T07:03:59.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-04-26T15:10:52.000Z (about 8 years ago)
- Last Synced: 2025-03-03T13:47:41.521Z (over 1 year ago)
- Topics: algebraic-data-types, interval-arithmetic
- Language: Python
- Size: 4.88 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Interval arithmetic in Python using Algebraic Data Types
More or less based on [a Haskell package](https://hackage.haskell.org/package/intervals-0.8.1/docs/Numeric-Interval.html).
Contains an [infix class](https://github.com/ActiveState/code/tree/master/recipes/Python/384122_Infix_operators) released under PSF License.
Requires uxadt
```bash
sudo pip3 install uxadt
```
Supports arithmetic (add,sub,mul,div) and comparison.
Comparison using forall by default.
Use e.g., ltSome or leSome if x < y for some values of x and y
Equality is weird for intervals.
Addition:
```python3
>>> Interval(-1,3) + Interval(4,5)
Interval(3,8)
>>> Interval(1,3) + Empty()
Empty()
>>> Empty() + Interval(1,3)
Empty()
```
Multiplication:
```python3
>>> Interval(-1,3) * Interval(4,5)
Interval(-5,15)
>>> Interval(-3,3) * Interval(-5,2)
Interval(-15,15)
>>> Interval(1,3) * Empty()
Empty()
>>> Empty() * Interval(1,3)
Empty()
```
Unary minus operator
```python3
>>> -Interval(-1,3)
Interval(-3,1)
>>> -Empty()
Empty()
```
Binary minus operator
```python3
>>> Interval(3,6) - Interval(-4,2)
Interval(1,10)
>>> Empty() - Interval(1,2)
Empty()
```
Division
```python3
>>> Interval(1,3) / Interval(3,5)
Interval(0.2,1.0))
>>> Interval(1,3) / Interval(-1,1)
Interval(-inf,inf)
>>> Interval(1,3) / Interval(0,3)
Interval(0.3333333333333333, inf)
```
Inclusion
```python3
>>> 1 in Interval(0,2)
True
>>> -1 in Interval(0,2)
False
>>> 1 in Empty()
False
```