In a project I'm working on I'm dealing with expressions containing complex exponentials such as the one below, which I aim to simplify as much as possible:
from sympy import Rational, exp, I, pi, pretty, cos, sin
E = Rational(1,20) + (Rational(1,4) + exp(2*I*pi/5)/4)*exp(-4*I*pi/5)/5 + exp(-2*I*pi/5)/20 + (exp(4*I*pi/5)/4 + exp(2*I*pi/5)/4)*exp(2*I*pi/5)/5 + (exp(-2*I*pi/5)/4 + exp(-4*I*pi/5)/4)*exp(4*I*pi/5)/5 + (exp(-4*I*pi/5)/4 + exp(4*I*pi/5)/4)*exp(-2*I*pi/5)/5
print(pretty(E))
⎛ 2⋅ⅈ⋅π⎞ ⎛ 4⋅ⅈ⋅π 2⋅ⅈ⋅π⎞ ⎛ -2⋅ⅈ⋅π -4⋅ⅈ⋅π ⎞ ⎛ -4⋅ⅈ⋅π 4⋅ⅈ⋅π⎞
⎜ ─────⎟ -4⋅ⅈ⋅π ⎜ ───── ─────⎟ 2⋅ⅈ⋅π ⎜ ─────── ───────⎟ 4⋅ⅈ⋅π ⎜ ─────── ─────⎟ -2⋅ⅈ⋅π
⎜ 5 ⎟ ─────── -2⋅ⅈ⋅π ⎜ 5 5 ⎟ ───── ⎜ 5 5 ⎟ ───── ⎜ 5 5 ⎟ ───────
⎜1 ℯ ⎟ 5 ─────── ⎜ℯ ℯ ⎟ 5 ⎜ℯ ℯ ⎟ 5 ⎜ℯ ℯ ⎟ 5
⎜─ + ──────⎟⋅ℯ 5 ⎜────── + ──────⎟⋅ℯ ⎜──────── + ────────⎟⋅ℯ ⎜──────── + ──────⎟⋅ℯ
1 ⎝4 4 ⎠ ℯ ⎝ 4 4 ⎠ ⎝ 4 4 ⎠ ⎝ 4 4 ⎠
── + ───────────────────── + ──────── + ──────────────────────── + ──────────────────────────── + ────────────────────────────
20 5 20 5 5 5
I managed to simplify it a bit (mostly by trial-and-error using different functions described on https://docs.sympy.org/latest/modules/simplify/simplify.html):
E.rewrite(cos).expand().simplify()
-sqrt(-10 - 2*sqrt(5))/64 - sqrt(-10 + 2*sqrt(5))/64 + sqrt(-50 + 10*sqrt(5))/320 + 3*sqrt(-50 - 10*sqrt(5))/320
print(pretty(_))
____________ ____________ _____________ _____________
╲╱ -10 - 2⋅√5 ╲╱ -10 + 2⋅√5 ╲╱ -50 + 10⋅√5 3⋅╲╱ -50 - 10⋅√5
- ────────────── - ────────────── + ─────────────── + ─────────────────
64 64 320 320
However, the resulting expression can still be simplified further, and in fact vanishes altogether — the question is how to do/show this in Sympy. I've tried using sqrtdenest
to "denest" the square roots, but so far no luck.
Likewise, another expression rather similar to the first one simplifies to
print(pretty(-cos(pi/7)/7 - sin(pi/14)/7 + Rational(1,14) + sin(3*pi/14)/7))
⎛π⎞ ⎛π ⎞ ⎛3⋅π⎞
cos⎜─⎟ sin⎜──⎟ sin⎜───⎟
⎝7⎠ ⎝14⎠ 1 ⎝ 14⎠
- ────── - ─────── + ── + ────────
7 7 14 7
Again, this expression vanishes, though I'm not getting to that point in Sympy. Any directions on how to proceed would be most welcome.