简体   繁体   中英

How to expand one exponential complex equation to two trigonometric ones in sympy?

I have one exponential equation with two unknowns, say:

y*exp(ix) = sqrt(2) + i * sqrt(2)

Manually, I can transform it to system of trigonometric equations:

y * cos x = sqrt(2)
y * sin x = sqrt(2)

How can I do it automatically in sympy?

I tried this:

from sympy import *
x = Symbol('x', real=True)
y = Symbol('y', real=True)
eq = Eq(y * cos(I * x), sqrt(2) + I * sqrt(2))
print([e.trigsimp() for e in eq.as_real_imag()])

but only got two identical equations except one had "re" before it and another one "im".

You can call the method .rewrite(sin) or .rewrite(cos) to obtain the desired form of your equation. Unfortunately, as_real_imag cannot be called on an Equation directly but you could do something like this:

from sympy import *


def eq_as_real_imag(eq):
    lhs_ri = eq.lhs.as_real_imag()
    rhs_ri = eq.rhs.as_real_imag()    
    return Eq(lhs_ri[0], rhs_ri[0]), Eq(lhs_ri[1], rhs_ri[1])

x = Symbol('x', real=True)
y = Symbol('y', real=True)

original_eq = Eq(y*exp(I*x), sqrt(2) + I*sqrt(2))
trig_eq = original_eq.rewrite(sin)  # Eq(y*(I*sin(x) + cos(x)), sqrt(2) + sqrt(2)*I)

eq_real, eq_imag = eq_as_real_imag(trig_eq) 
print(eq_real)  # Eq(y*cos(x), sqrt(2))
print(eq_imag)  # Eq(y*sin(x), sqrt(2)) 

(You might also have more luck just working with expressions (implicitly understood to be 0) instead of an Equation eg eq.lhs - eq.rhs in order to call the method as_real_imag directly)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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