简体   繁体   English

SymPy 中的指数到三角函数的转换同时简化——一个顽固的表达式

[英]Exponential to trigonometric conversion in SymPy while simplifying - a stubborn expression

I have been trying to simplify我一直在努力简化

exp(2*I*N) - 1)**2/((exp(2*I*N) - 1)**2 - 4*exp(2*I*N)*cos(N)**2)

Where the answer should be (sin N)^2, but the output is same as input.答案应该是 (sin N)^2,但输出与输入相同。

I have tried .rewrite(cos) and then simplify, trigsimp, expand and pretty much all I could discover quickly from help sources.我尝试过.rewrite(cos)然后简化、trigsimp、扩展以及几乎所有我可以从帮助来源中快速发现的内容。

Rewriting in terms of exp instead of cos is more helpful:exp而不是cos重写更有帮助:

expr.rewrite(exp).simplify()

returns -cos(2*N)/2 + 1/2 which is visibly equivalent to sin(N)**2 .返回-cos(2*N)/2 + 1/2 ,这显然等同于sin(N)**2 Clean it up with用它清理

expr.rewrite(exp).simplify().trigsimp()

getting sin(N)**2得到sin(N)**2


Old answer, might still be of value: You probably meant N to be real, so let's declare it as such.旧答案,可能仍然有价值:您可能意味着N是真实的,所以让我们这样声明。

Given a mix of complex exponentials and trigonometric functions, it will probably help to separate the real and imaginary parts with as_real_imag() .考虑到复杂的指数和三角函数的混合,使用as_real_imag()分离实部和虚部可能会有所帮助。 A direct application does not do much beyond putting re(...) and im(...), so rewriting in exponentials and expanding the squares/products is advisable first:除了放置 re(...) 和 im(...) 之外,直接应用程序并没有做太多事情,因此建议首先以指数形式重写并扩展平方/乘积:

N = symbols('N', real=True)
expr = (exp(2*I*N) - 1)**2/((exp(2*I*N) - 1)**2 - 4*exp(2*I*N)*cos(N)**2)
result = [a.trigsimp() for a in expr.rewrite(cos).expand().as_real_imag()]

Result: [sin(N)**2, 0] , meaning the real and imaginary parts of the expression.结果: [sin(N)**2, 0] ,表示表达式的实部和虚部。 It can be recombined into a single expression with result[0] + I*result[1] .它可以用result[0] + I*result[1]重新组合成单​​个表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM