For given an expression like
$-\frac{e^{-\frac{\text{d1} t}{\text{c1}}} \left(-\text{a1} \text{a2} \text{d1}^2+\text{a1} \text{b2} \text{c1} \text{d1}+\text{a2} \text{b1} \text{c1} \text{d1}-\text{b1} \text{b2} \text{c1}^2\right)}{\text{c1} \text{d1} (\text{c2} \text{d1}-\text{c1} \text{d2})}-\frac{e^{-\frac{\text{d2} t}{\text{c2}}} \left(-\text{a1} \text{a2} \text{d2}^2+\text{a1} \text{b2} \text{c2} \text{d2}+\text{a2} \text{b1} \text{c2} \text{d2}-\text{b1} \text{b2} \text{c2}^2\right)}{\text{c2} \text{d2} (\text{c1} \text{d2}-\text{c2} \text{d1})}+\frac{\text{b1} \text{b2}}{\text{d1} \text{d2}}$ The Mathematica code of this expression is

(b1 b2)/(d1 d2) - ((-b1 b2 c1^2 + a2 b1 c1 d1 + a1 b2 c1 d1 - 
a1 a2 d1^2) E^(-((d1 t)/c1)))/(
c1 d1 (c2 d1 - c1 d2)) - ((-b1 b2 c2^2 + a2 b1 c2 d2 + a1 b2 c2 d2 - 
a1 a2 d2^2) E^(-((d2 t)/c2)))/(c2 d2 (-c2 d1 + c1 d2))

How to expand it into $\sum _{i=0}^{\infty } a_i e^{b_i}$ within Mathematica?

I know $a_i$ and $b_i$ for the given expression without writing any code, but how to write a personal general function to do this work?

Thank you very much!

  • 1
    $\begingroup$ I suggest you that you post something in Mathematica format. Otherwise it is rather impossible that you will get a reply. $\endgroup$
    – Dimitris
    Feb 24, 2017 at 13:05
  • $\begingroup$ @dimitris Thanks for your advice. I have modified my question. $\endgroup$
    – tanghe2014
    Feb 24, 2017 at 14:22
  • 1
    $\begingroup$ Surely you need to put some constraints on a_i and b_i. Otherwise you could trivially write every function f[t]=f[t]*E^0. Are you expecting this expansion to be unique? $\endgroup$
    – jjc385
    Feb 24, 2017 at 15:02
  • $\begingroup$ perhaps something along the lines of $b_i\to\log\beta_i$, and Taylor expand in $\beta_i$? $\endgroup$ Feb 25, 2017 at 13:43

2 Answers 2


I would try something like this:

f = a E^x +b + c E^(7y) + d;
Replace[f, b_ Exp[a_] -> g[b, a], 1];
Replace[%, a_Plus :> List @@ a];
% /. g -> List;
Replace[%, b_ /; ! ListQ[b] -> {b, 0}, 1];
% //. {a___, {x_, e_}, b___, {y_, e_}, c___} -> {a, {x + y, e}, b, c};
{as, bs} = Transpose[%];

I find some code (What is the correct way to use specific rule for specific pattern match when parsing an expression?) to do some similar work.
This code is

Clear[coeff, z]

(*Applies to plus expressions*)
coeff[expr_Plus] := 
 Cases[expr,HoldPattern[(c : _?(FreeQ[#, z] &) : 1) (zVar : z^e_. : z^0)] :> {c,e}]

(*Applies to standalone expressions:nest them in a list first before using Cases*)
coeff[expr_] := 
 Cases[{expr},HoldPattern[(c : _?(FreeQ[#, z] &) : 1) (zVar : z^e_. : z^0)] :> {c,e}]

{#, coeff@#} & /@ {1, z, -z, 1/z, 1 + z^-1, 1/z^3 + 3/z + 3 z + z^3, 
 6 + 1/z^4 + 4/z^2 + 4 z^2 + z^4} // 
 Grid[Prepend[#, {"expr", "coeff[expr]"}], Frame -> All] &

The return of it is Out of this code. To get the coefficient and exponent I think one can modify this code to get the correct answer of my question, but I don't know how to finish it.


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.