Python 中有_rational_ 区间算术的实现吗?

发布于 2024-10-10 04:04:23 字数 178 浏览 0 评论 0原文

Python 中是否有有理区间算术的实现? 使用浮点数,而不是有理数。

如果没有,Python 中是否有包含 ±∞ 的有理数实现?

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 ±∞ ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

公布 2024-10-17 04:04:23

Sympy 具有 间隔、有理数和无穷大。 Interval 类是 Set 类的子类。

# 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

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
吃兔兔 2024-10-17 04:04:23

PyInterval 现在在 Python 中具有有理区间算术功能。

来自 PyInterval 文档

可以通过在当前命名空间中注入间隔类、表示数学无穷大的常量以及提供间隔超越函数的模块的语句将间隔包加载到 Python 解释器中。

from interval import interval, inf, imath

interval[0, 2] * interval[4, inf]
interval([-inf, inf])

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])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文