2
$\begingroup$

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])) 
$\endgroup$
5
  • $\begingroup$ When I run your two sets of code, I get the same output for both, so I'm not sure what your question is! $\endgroup$
    – march
    Commented May 19, 2016 at 23:17
  • $\begingroup$ 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. $\endgroup$ Commented May 19, 2016 at 23:58
  • 3
    $\begingroup$ @march test1 was given as argument to CoefficientList in both cases. I have edited the question to fix this. $\endgroup$
    – sorenba
    Commented May 20, 2016 at 1:02
  • $\begingroup$ @Marius Ladegård Meyer: That is a beautiful piece of Mathematica coding. $\endgroup$ Commented May 20, 2016 at 16:04
  • $\begingroup$ @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! $\endgroup$ Commented May 20, 2016 at 20:14

2 Answers 2

2
$\begingroup$

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] :=

 Block[{num = Length[varList], x, xs, expohead, headform, exprx, 
   expos},
  xs = Array[x, num];
  exprx = Expand[expr /. Thread[varList -> Log[xs]] /. E^x_ :> E^Expand[x]];
  expohead /: expohead[a_List]*expohead[b_List] := expohead[a + b];
  headform = exprx /. x[i_]^m_. :> expohead[UnitVector[num, i]*m];
  expos = 
   DeleteDuplicates@Cases[headform, expohead[v_] :> v, Infinity];
  ({#1, headform /. {expohead[#1] -> 1, expohead[_] -> 0}}) & /@ 
   expos
  ]

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

{exponents, coefficient}

This way it's easy to recunstruct the expression we start with:

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

1
$\endgroup$
7
  • $\begingroup$ 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? $\endgroup$ Commented May 23, 2016 at 3:58
  • $\begingroup$ 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]]; $\endgroup$ Commented May 23, 2016 at 4:38
  • $\begingroup$ 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]. $\endgroup$ Commented May 23, 2016 at 6:15
  • $\begingroup$ 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. $\endgroup$ Commented May 23, 2016 at 13:46
  • $\begingroup$ 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])}} $\endgroup$ Commented May 23, 2016 at 14:13
2
$\begingroup$

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!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.