My main goal is to identify coefficients in front of exponentials of the form
exp(1j*k*r)
in expressions such as:
(z1*exp(1j*k1*r1) + z2*exp(1j*k2*r2) + c.c.)**2
by first expanding and then using the coeff tool in sympy.
The problem is that I don't know how to expand in such a manner that we can gather the exponents additively. I would like to see this term:
exp(1j*(k1*r1+k2*r2))
but only this kind of term appears:
exp(1j*k1*r1)*exp(1j*k2*r2)
Here is my code:
from sympy import *
u = Symbol('u')
r1,r2 = symbols('r1 r2', real = True)
k1,k2 = symbols('k1 k2', real = True)
z1,z2 = symbols('z1 z2')
uu = z1*exp(1j*k1*r1)+ z2*exp(1j*k1*r2)
u = uu + uu.conjugate()
v = expand(u**2)
print(v)
I can not figure out how to do it. I tried using simplify, or considering the thing as a polynom whith symbolic coefficients, but this didn't work.
More generally, is expanding and then using coeff method the best way to extract coefficients in front of the exponentials ? Is there any automated tool that already makes it ?
Thank you for your help !
EDIT :
I also tried using powsimp with this portion of code:
w = powsimp(expand(u**2))
print(w.coeff(exp(1j*(k1*r1 + k2*r2))))
it returns 0 because we only obtain
exp(1j*k1*r1 + 1j*k2*r2)
but it is not sufficient when applying coeff method to extract the exponent 1j*(k1*r1 + k2*r2).