Is there an implementation of _rational_ interval arithmetic in Python?
Asked Answered
K

2

8

Is there an implementation of rational interval arithmetic in Python? This uses floats, not rationals.

If not, is there any implementation of rationals in Python that includes ±∞ ?

Katerine answered 2/1, 2011 at 8:41 Comment(0)
U
5

Sympy has intervals, rational numbers, and infinity. The Interval class is a subclass of the Set class.

# oo is the symbol for infinity
from sympy import Interval, oo, Rational

i1 = Interval(10, 15)
i2 = Interval(0, oo)
i3 = Interval(-5, -1)
# adding intervals
i4 = i1 + i3
i5 = i1 + i2
# interval with open end
i6 = Interval(Rational(1, 2), Rational(45, 3), left_open=True)

print 11 in i1 # True
print -1 in i1 # False
print 0 in i4 # False
print Rational(3, 2) in i6 # True
print oo in i2 # False
print i2.sup # oo (infinity)
print Rational(1, 2) in i6 # False
print i6.inf # 1/2
Uzia answered 2/1, 2011 at 14:9 Comment(0)
S
0

PyInterval now has rational interval arithmetic in Python feature.

From PyInterval Docs:

The interval package can be loaded into the Python interpreter with the statement which injects in the current namespace the interval class, a constant representing the mathematical infinity, and a module providing interval transcendental functions.

from interval import interval, inf, imath

interval[0, 2] * interval[4, inf]
interval([-inf, inf])
Serranid answered 19/3, 2018 at 12:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.