Sympy无法识别表达式为实数。

huangapple go评论82阅读模式
英文:

Sympy not recognizing expression as real

问题

I've specified two variables, gamma and t, as real and positive, and they are used in an expression as follows:

gamma, t = symbols('gamma t', real=True, positive=True)
expr = sqrt((exp(gamma*t))-1)

result = expr*conjugate(expr)

The problem I'm having is that when I multiply the expression by its complex conjugate (using sp.conjugate), I don't get the answer I'm expecting.

Expected Result

Actual Result

In case those don't show up or something, what I'm expecting looks like exp(gamma*t))-1 while the result I get is sqrt((exp(gamma*t))-1)*sqrt((exp(gamma*t))-1) where the second sqrt term has a bar over it, indicating it is complex.

I'm expecting the simplified term since gamma and t are both specified to be positive and real, meaning the expression under the root can't be negative, so the whole expression should remain real no matter what values of gamma and t I use.

英文:

I've specified two variables, gamma and t, as real and positive, and they are used in an expression as follows:

gamma, t = symbols('gamma t', real=True, positive=True)
expr = sqrt((exp(gamma*t))-1)

result = expr*conjugate(expr)

The problem I'm having is that when I multiply the expression by its complex conjugate (using sp.conjugate), I don't get the answer I'm expecting.

Expected Result

Actual Result

In case those don't show up or something, what I'm expecting looks like exp(gamma*t))-1 while the result I get is sqrt((exp(gamma*t))-1)*sqrt((exp(gamma*t))-1) where the second sqrt term has a bar over it, indicating it is complex.

I'm expecting the simplified term since gamma and t are both specified to be positive and real, meaning the expression under the root can't be negative, so the whole expression should remain real no matter what values of gamma and t I use.

This is my first question, so apologies if the format is wrong.

I've tried using simplify, which doesn't work. I've tried specifying the whole expression as real, but I'm new to Sympy and don't think/know if that is possible.

There's another question posted that is similar, but the solution there did not work for this situation.

答案1

得分: 1

SymPy尚不足够聪明,无法理解exp(positive) - 1).is_nonegative -> None应该为True。因此,让我们通过将指数替换为1 + nonnegative来帮助它,然后撤消替换:

from sympy import Dummy
nn = Dummy(nonnegative=True)
x = exp(gamma*t)
result.subs(x, nn + 1).subs(nn, x - 1)
exp(gamma*t) - 1
英文:

SymPy is not (yet) insightful enough to know that exp(positive) - 1).is_nonegative -> None should be True. So let's help it by replacing the exponential with 1 + nonnegative and then undoing the replacement:

>>> from sympy import Dummy
>>> nn= Dummy(nonnegative=True)
>>> x = exp(gamma*t)
>>> result.subs(x, nn + 1).subs(nn, x- 1)
exp(gamma*t) - 1

huangapple
  • 本文由 发表于 2023年4月6日 20:59:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949821.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定