将Sympy表达式与变量指数比较为零

时间:2017-11-16 19:38:18

标签: python sympy

我想知道是否有办法让SymPy认识到Pow(positive, variable)等表达式总是大于零(假设实数)。

from sympy import *
init_session()

StictLessThan(0, 1)      # returns desired output
>>> False

StrictLessThan(0, x)     # returns desired output
>>> 0 < x

StrictLessThan(0, 2**x)  # returns undesired output, but not surprising
>>> 0 < 2**x

(2**x).is_comparable     # From this I assume that I would have to define my own function
>>> False

some_function(0, 2**x)   # does this exist built into the SymPy package?
>>> True

我知道我可以自己创建这个功能,但我很好奇SymPy的内置功能和限制。

def is_positive_exponential(exponential):
    base, exponent = exponential.as_base_exp()
    return True if base > 0 else False

1 个答案:

答案 0 :(得分:1)

考虑到当x = pi*I/log(2)(一个复数)时,表达式2**x的计算结果为-1,这不是正数。

如果告诉SymPy x是实数,它会知道2**x是正数。

x = Symbol('x', real=True)
StrictLessThan(0, 2**x)    # returns True

请参阅list of possible assumptions。默认情况下,假定Symbol('x')&#34;可交换&#34;但没有别的假设。