An open API service indexing awesome lists of open source software.

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

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
```