如何强制符号化求值

2024-10-06 11:24:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使变量test等于变量test2

我从test2得到的打印输出只是(1-g1 g1*)所需要的

但是test吐出(g1-1)(g1*-1)(x)(x*),其中xsqrt{(1- g1 g1*)/[(g1-1)(g1*-1)]}

从视觉上看问题,我知道表达式应该取消,但我无法用test来简化共轭表达式

  • Sympy版本1.4
  • Python 3.7版

试过把sp.simplify,sp.together

import sympy as sp

def Lambdaj(gammaj):
    step1 = sp.together(1 - sp.conjugate(gammaj)) * sp.sqrt((1 - gammaj * sp.conjugate(gammaj)) / ((1-gammaj) * (1 - sp.conjugate(gammaj))))
    return step1

g1 = sp.symbols('g1')

test = sp.together(Lambdaj(g1) * sp.conjugate(Lambdaj(g1)))
print(test)

test2 = 1 - g1 * sp.conjugate(g1)
print(test2)

print(sp.together((1-g1)*(1-sp.conjugate(g1))))

Tags: test表达式视觉sqrtspprinttogethertest2
1条回答
网友
1楼 · 发布于 2024-10-06 11:24:42

假设g1**2 < 1公平吗?如果是这样,我们可以用只接受这些值的表达式替换g1,简化结果,并重新替换原始的g1

>>> p = var('p',nonnegative=True)
>>> u=Eq(g1, p/(1+p)) # p in [0, 1)
>>> test.subs(*u.args).simplify().subs(p,solve(u,p)[0]).simplify()
1 - g1**2 

相关问题 更多 >