I am trying to simplify
cos(phi) + cos(phi - 2*pi/3)*e^(I*2*pi/3) + cos(phi - 4*pi/3)*e^(I*4*pi/3)
which I know reduces down to 1.5e^(I*phi)
I cannot get SymPy to recognize this. I have tried simplify
, trigsimp
, expand
, etc. But nothing seems to work. Any suggestions?
Here is my code:
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
import sympy as sp
from sympy import I
sp.init_printing()
phi = sp.symbols('\phi', real = True)
vec = sp.cos(phi) + sp.cos(phi - 2*sp.pi/3)*sp.exp(I*2*sp.pi/3) + sp.cos(phi - 4*sp.pi/3)*sp.exp(I*4*sp.pi/3)
vec.simplify()
vec.rewrite(sp.exp).simplify()
vec.rewrite(sp.exp).expand().simplify()
None of these produce the expected result. I can confirm my result manually, by substituting values in for phi
like this:
sp.simplify(vec.rewrite(sp.exp).simplify() - 3/2*sp.exp(I*phi)).evalf(subs={phi:3})
It's not obvious but you can get there like this:
In [40]: phi = symbols('phi', real=True)
In [41]: e = cos(phi) + cos(phi - 2*pi/3)*E**(I*2*pi/3) + cos(phi - 4*pi/3)*E**(I*4*pi/3)
In [42]: e
Out[42]:
-2⋅ⅈ⋅π 2⋅ⅈ⋅π
─────── ─────
3 ⎛ π⎞ 3 ⎛ π⎞
- ℯ ⋅sin⎜φ + ─⎟ + cos(φ) - ℯ ⋅cos⎜φ + ─⎟
⎝ 6⎠ ⎝ 3⎠
In [43]: e.rewrite(exp).expand().rewrite(sin).expand().rewrite(exp)
Out[43]:
ⅈ⋅φ
3⋅ℯ
──────
2
This is definitely an example in which doing computation by hands might be faster then exploring how to do it with SymPy.
Anyway, this is how I achieve that:
vec.rewrite(exp).simplify().subs(
root(-1, 6),
root(-1, 6).rewrite(exp)
).expand().subs(
I * exp(-5 * I * pi / 6),
(I * exp(-5 * I * pi / 6)).simplify().rewrite(exp)
).simplify().expand()
# out: 3*exp(I*\phi)/2
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.