最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Why does SymPy incorrectly evaluate inequalities for infinite symbols? - Stack Overflow

programmeradmin3浏览0评论

I'm using SymPy to handle symbolic mathematics involving possibly infinite values. I need to define a symbol y>=0 and y<=sympy.inf and tried the following but the behaviour is not as I would expect it:

import sympy

y = sympy.symbols("y", finite=False, extended_nonnegative=True)

print(y >= 0)          # True (as expected)
print(y < sympy.oo)    # False (should be uncertain)
print(y > 19)          # True (should be uncertain)

How to ensure the last two comparisons ain't evaluated?

I'm using SymPy to handle symbolic mathematics involving possibly infinite values. I need to define a symbol y>=0 and y<=sympy.inf and tried the following but the behaviour is not as I would expect it:

import sympy

y = sympy.symbols("y", finite=False, extended_nonnegative=True)

print(y >= 0)          # True (as expected)
print(y < sympy.oo)    # False (should be uncertain)
print(y > 19)          # True (should be uncertain)

How to ensure the last two comparisons ain't evaluated?

Share Improve this question asked Mar 15 at 13:31 CorramCorram 3331 gold badge3 silver badges17 bronze badges 6
  • 1 In SymPy's mind your symbol is the same as oo. You declared it to be extended_nonnegative so it is a nonnegative element of the extended reals. You also said it is not finite so it must be oo. If you have some other idea of what y is supposed to represent then that is not what the stated assumptions mean: docs.sympy./latest/guides/assumptions.html#predicates – Oscar Benjamin Commented Mar 15 at 14:12
  • Ok I get why sympy thinks y = inf, but it doesn't entirely answer the question unfortunately as I don't find a way of letting sympy know that y<= inf should hold. Also let me clarify i tried other combinations of nonnegative parameters etc. but couldn't find a way to represent the case that i need. – Corram Commented Mar 15 at 14:36
  • Why would you need to do anything to tell sympy that a value is less than or equal to infinity? What possibilities do you have in mind that would not be less than or equal to infinity? – user2357112 Commented Mar 15 at 14:43
  • 1 (Unfortunately, at least on the SymPy 1.9 I just tested, y = sympy.symbols("y", extended_nonnegative=True) doesn't work. For some reason, SymPy thinks that y < sympy.oo when y is defined this way.) – user2357112 Commented Mar 15 at 15:00
  • 1 The case of symbols('y', extended_nonnegative=True) < oo is a bug which should be reported to github. For implementing the Archimedean copula I think that the main question here is probably not relevant. It would be better to ask about what you are actually trying to do rather than this question about the assumptions system and inequalities. – Oscar Benjamin Commented Mar 15 at 16:08
 |  Show 1 more comment

1 Answer 1

Reset to default -1

From the docs

when an inequality is indeterminate: we get an instance of StrictGreaterThan which represents the inequality as a symbolic expression.

which refers to

x = Symbol('x')

type(x > 0)
<class 'sympy.core.relational.StrictGreaterThan'>

In this case it returns

type(y > 19)
<class 'sympy.logic.boolalg.BooleanTrue'>

which is not Python's True but a sympy's symbolic boolean

>>> (y > 19) is True
False

>>> type(y < sympy.oo)
<class 'sympy.logic.boolalg.BooleanFalse'>

>>> y.is_infinite is True
True

>>> sympy.core.relational.StrictLessThan(y, sympy.oo)
False
>>> type(sympy.core.relational.StrictLessThan(y, sympy.oo))
<class 'sympy.logic.boolalg.BooleanFalse'>
>>> sympy.Eq(y, sympy.oo)
True

>>> type(y)
<class 'sympy.core.symbol.Symbol'>
>>> type(sympy.oo)
<class 'sympy.core.numbers.Infinity'>
发布评论

评论列表(0)

  1. 暂无评论