# CoefficientList for exponentials terms

I have a long expression with exponentials of variable t1. I need to create a list of the coefficients for each exponential. The problem seems to be that several equivalent forms are present and get ignored. As a small example;

test1 = 1/16 E^(2 I t1) a[1] - (E^(-I (-2 + Sqrt[2]) t1) a[2])/(16 (-2 + Sqrt[2])) + (E^(I (2 + Sqrt[2]) t1) a[3])/(16 (2 + Sqrt[2])) - (E^(2 I t1 - I Sqrt[2] t1) a[4])/(16 Sqrt[2]) + (E^(-I Sqrt[2] t1 + 2 I (1 + Sqrt[2]) t1) a[5])/(16 Sqrt[2]);
CoefficientList[test1, { E^(2 I t1), E^(I (2 + Sqrt[2]) t1), E^(I (2 - Sqrt[2]) t1)}]

{{{(E^(-I Sqrt[2] t1 + 2 I (1 + Sqrt[2]) t1) a[5])/(16 Sqrt[2]), -(a[2]/(16 (-2 + Sqrt[2])))}, {a[3]/(16 (2 + Sqrt[2])),0}}, {{a[1]/16 - (E^(-I Sqrt[2] t1) a[4])/(16 Sqrt[2]), 0}, {0, 0}}}


Whereas if entered in the equivalent form

test2 = 1/16 E^(2 I t1) a[1] - (E^(-I (-2 + Sqrt[2]) t1) a[2])/(16 (-2 + Sqrt[2])) + (E^(I (2 + Sqrt[2]) t1) a[3])/(16 (2 + Sqrt[2])) - (E^(-I (-2 + Sqrt[2]) t1) a[4])/(16 Sqrt[2]) + (E^(I (2 + Sqrt[2]) t1) a[5])/(16 Sqrt[2]);
CoefficientList[test2, { E^(2 I t1), E^(I (2 + Sqrt[2]) t1), E^(I (2 - Sqrt[2]) t1)}]

{{{0, -(a[2]/(16 (-2 + Sqrt[2]))) - a[4]/(16 Sqrt[2])}, {a[3]/(
16 (2 + Sqrt[2])) + a[5]/(16 Sqrt[2]), 0}}, {{a[1]/16, 0}, {0, 0}}}


Is there a way to make Mathematica recognize test1 the same as test2? Or should I try some other method?

More specifically, I need to extract the a[i] coefficients for each different exponential from an expression like the following:

1/16 E^(-2 I (-1 + Sqrt[2]) t1) (2 E^(
2 I (Sqrt[2] t1 + 2 t2)) (a[3] - a[4] - a[7] + a[8] - a[9] +
a[10] + a[13] - a[14]) + E^(3 I Sqrt[2] t1 +
2 I t2) (Sqrt[2] a[2] - 2 a[3] - (-2 + Sqrt[2]) a[4] -
Sqrt[2] a[5] + Sqrt[2] a[7] + 2 a[9] -
Sqrt[2] a[10] + (-2 + Sqrt[2]) (a[12] + a[13] - a[15])) + E^(I (Sqrt[2] t1 + 2 t2)) (-Sqrt[2] a[2] -
2 a[3] + (2 + Sqrt[2]) a[4] + Sqrt[2] a[5] - Sqrt[2] a[7] +
2 a[9] +
Sqrt[2] a[10] - (2 + Sqrt[2]) (a[12] + a[13] - a[15])) - 2 E^(2 I ((-1 + Sqrt[2]) t1 + 2 t2)) (a[2] + a[4] - a[5] - a[7] +
a[10] + a[12] - a[13] - a[15]) + 2 E^(2 I (Sqrt[2] t1 + t2)) (a[2] - a[4] - a[5] + a[7] - a[10] +
a[12] + a[13] - a[15]) + E^(3 I Sqrt[2]
t1) (a[2] - (-1 + Sqrt[2]) a[3] - (-2 + Sqrt[2]) a[4] - a[5] -
Sqrt[2] a[7] - (-1 + Sqrt[2]) a[8] + (-1 + Sqrt[2]) a[9] +
Sqrt[2] a[10] +
a[12] + (-2 + Sqrt[2]) a[13] + (-1 + Sqrt[2]) a[14] - a[15]) + E^(I Sqrt[2]
t1) (a[2] + (1 + Sqrt[2]) a[3] + (2 + Sqrt[2]) a[4] - a[5] +
Sqrt[2] a[7] + (1 + Sqrt[2]) a[8] - (1 + Sqrt[2]) a[9] -
Sqrt[2] a[10] +
a[12] - (2 + Sqrt[2]) a[13] - (1 + Sqrt[2]) a[14] - a[15]) - 2 E^(2 I ((-1 + Sqrt[2]) t1 + t2)) (a[2] + 2 a[3] + a[4] - a[5] +
a[7] - 2 a[9] - a[10] - a[12] - a[13] + a[15]) + (-1 + Sqrt[
2]) E^(3 I Sqrt[2] t1 +
4 I t2) (a[2] - a[3] - a[5] + a[8] + a[9] - a[12] - a[14] +
a[15]) - (1 + Sqrt[2]) E^(
I (Sqrt[2] t1 + 4 t2)) (a[2] - a[3] - a[5] + a[8] + a[9] - a[12] -
a[14] + a[15]) + 2 E^(2 I Sqrt[2]
t1) (a[2] + a[3] - a[5] - a[8] - a[9] - a[12] + a[14] + a[15]))

• When I run your two sets of code, I get the same output for both, so I'm not sure what your question is! Commented May 19, 2016 at 23:17
• I get the output given. I'm using Mathematica 10.3 on windows. What I need is a consistent way to obtain the coefficients regardless of how mathematica expresses the exponent. Commented May 19, 2016 at 23:58
• @march test1 was given as argument to CoefficientList in both cases. I have edited the question to fix this. Commented May 20, 2016 at 1:02
• @Marius Ladegård Meyer: That is a beautiful piece of Mathematica coding. Commented May 20, 2016 at 16:04
• @PhillipDukes, thanks for the accept! But I would not be surprised if someone came along and solved this with a one-liner, or something not so round-about as my (though hopefully transparent) solution. So next time I'd wait a day or two before awarding the accept ;) Glad you liked it though! Commented May 20, 2016 at 20:14

My solution does not use Coefficient(List) because I couldn't get Mathematica to avoid interpreting

c1*E^a + c2*E^(a+b)


as

E^a (c1 + c2*E^b )


when finding the coefficient of E^a. It is probably possible, and would probably look prettier than what I'm about to present.

Let me first address test1 and test2. I will assume that t1 ONLY appears in exponents.

Introduce x1 = Log[t1]:

test1x1 = Expand[test1 /. t1 -> Log[x1] /. E^x_ :> E^Expand[x]]

1/16 x1^(2 I) a[1] - (x1^(2 I - I Sqrt[2]) a[2])/(
16 (-2 + Sqrt[2])) + (x1^(2 I + I Sqrt[2]) a[3])/(
16 (2 + Sqrt[2])) - (x1^(2 I - I Sqrt[2]) a[4])/(16 Sqrt[2]) + (
x1^(2 I + I Sqrt[2]) a[5])/(16 Sqrt[2])


Find the list of distinct exponents:

expos = DeleteDuplicates@Cases[test1x1, x1^m_ :> m, Infinity]

{2 I, 2 I - I Sqrt[2], 2 I + I Sqrt[2]}


Find coefficients:

coeffs = (test1x1 /. {x1^#1 -> 1, x1^_ -> 0}) & /@ expos

{a[1]/16, -(a[2]/(16 (-2 + Sqrt[2]))) - a[4]/(16 Sqrt[2]),
a[3]/(16 (2 + Sqrt[2])) + a[5]/(16 Sqrt[2])}


Of course you can wrap all this up in a function:

expoCoeffs[expr_, var_] :=
Block[{x1, exprx1, expos},
exprx1 = Expand[expr /. var -> Log[x1] /. E^x_ :> E^Expand[x]];
expos = DeleteDuplicates@Cases[exprx1, x1^m_ :> m, Infinity];
(exprx1 /. {x1^#1 -> 1, x1^_ -> -0}) & /@ expos
]


We find

expoCoeffs[test1, t1] == expoCoeffs[test2, t1]

True


as we wanted.

For more than one t variable, as in your last example (lets call it big), we can do something similar. I will use a wrapper expohead for the exponents to get all sets of exponents.

expoCoeffs[expr_, varList_List] :=

expos},
xs = Array[x, num];
exprx = Expand[expr /. Thread[varList -> Log[xs]] /. E^x_ :> E^Expand[x]];
expos =
expos
]


In this case, I return a list where each element is of the form

{exponents, coefficient}


reconst = Plus @@ (#2*E^(#1.{t1, t2}) & @@@ expoCoeffs[big, {t1, t2}]);
FullSimplify[reconst/big]

1

• I have a problem which needs your attention. There is an example in which I get the error Infinity::indet: Indeterminate expression E^I [Infinity] encountered. >> Can you please help? Commented May 23, 2016 at 3:58
• The example is test = Expand[(1/ 64 E^(-I Sqrt[ 2] (t1 + t2)) (-Sqrt[2] E^(2 I t1) - 2 E^(I Sqrt[2] t1) + Sqrt[2] E^(2 I (1 + Sqrt[2]) t1) + 2 E^(I (2 + Sqrt[2]) t1)) ) a[15]]; Commented May 23, 2016 at 4:38
• Please see my edited post. We just need to make sure the exponents are not factored, so I added a rule E^x_ :> E^Expand[x]. Commented May 23, 2016 at 6:15
• I'm sorry, but I still get the same error. I looks as if the correct substitution takes place even without the new E^x_ :> E^Expand[x] edit. Commented May 23, 2016 at 13:46
• Have you tried clearing all definitions? It works fine here. I get {{{0, -I Sqrt[2]}, -(a[15]/32)}, {{2 I, -I Sqrt[2]}, a[15]/ 32}, {{2 I - I Sqrt[2], -I Sqrt[2]}, -(a[15]/( 32 Sqrt[2]))}, {{2 I + I Sqrt[2], -I Sqrt[2]}, a[15]/(32 Sqrt[2])}} Commented May 23, 2016 at 14:13

These are two test lists with exponents:

test1 = 1/16 E^(2 I t1) a[
1] - (E^(-I (-2 + Sqrt[2]) t1) a[
2])/(16 (-2 + Sqrt[2])) + (E^(I (2 + Sqrt[2]) t1) a[
3])/(16 (2 + Sqrt[2])) - (E^(2 I t1 - I Sqrt[2] t1) a[
4])/(16 Sqrt[2]) + (E^(-I Sqrt[2] t1 + 2 I (1 + Sqrt[2]) t1) a[
5])/(16 Sqrt[2]);
test2 = c1*E^a + c2*E^(a + b);


Assuming that all terms contain an exponent try this:

    List @@ test1 /. E^_ -> 1

(*    {a[1]/16, -(a[2]/(16 (-2 + Sqrt[2]))), a[3]/(
16 (2 + Sqrt[2])), -(a[4]/(16 Sqrt[2])), a[5]/(16 Sqrt[2])}   *)

List @@ test2 /. E^_ -> 1

(*    {c1, c2}    *)


Have fun!