I am trying to simplify
JavaScript
x
2
1
cos(phi) + cos(phi - 2*pi/3)*e^(I*2*pi/3) + cos(phi - 4*pi/3)*e^(I*4*pi/3)
2
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:
JavaScript
1
15
15
1
import numpy as np
2
%matplotlib inline
3
import matplotlib.pyplot as plt
4
5
import sympy as sp
6
from sympy import I
7
sp.init_printing()
8
phi = sp.symbols('phi', real = True)
9
10
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)
11
12
vec.simplify()
13
vec.rewrite(sp.exp).simplify()
14
vec.rewrite(sp.exp).expand().simplify()
15
None of these produce the expected result.
I can confirm my result manually, by substituting values in for phi
like this:
JavaScript
1
2
1
sp.simplify(vec.rewrite(sp.exp).simplify() - 3/2*sp.exp(I*phi)).evalf(subs={phi:3})
2
Advertisement
Answer
It’s not obvious but you can get there like this:
JavaScript
1
19
19
1
In [40]: phi = symbols('phi', real=True)
2
3
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)
4
5
In [42]: e
6
Out[42]:
7
-2⋅ⅈ⋅π 2⋅ⅈ⋅π
8
─────── ─────
9
3 ⎛ π⎞ 3 ⎛ π⎞
10
- ℯ ⋅sin⎜φ + ─⎟ + cos(φ) - ℯ ⋅cos⎜φ + ─⎟
11
⎝ 6⎠ ⎝ 3⎠
12
13
In [43]: e.rewrite(exp).expand().rewrite(sin).expand().rewrite(exp)
14
Out[43]:
15
ⅈ⋅φ
16
3⋅ℯ
17
──────
18
2
19