Say I start with an expression with potentially an arbitrary number of variables (input-dependent), for example Exp[x]Sin[y]z^(-1)w, and I want to expand in all variables to a certain power. I could do this


But is there a more compact way to achieve this? Not to mention that this method fails when the number of variables depend on the inputs. I tried something like


This gives

{{x, 0, 5}, {y, 0, 5}, {z, 0, 5}, {w, 0, 5}}

as an output. This is as far as I got. I tried to extract the elements using Row and StringRiffle but it doesn't work. Any help is appreciated, thanks!

Edit: Changed my example from all exponentials to a more general one.


1 Answer 1


There are two answers to your question. The simple answer is as follows. Since you only have the exponents, the expression Exp[x]Exp[y]Exp[z]Exp[w] is equal to Exp[a], where a=x+y+z+w. Therefore, one can operate as follows:

(Series[Exp[a], {a, 0, 5}] // Normal) /. 
  a -> x + y + z + w // Expand

I do not post the result since it is a bit too large, but you easily get it by evaluating this code.

However, I guess that your question is about a more general situation, in which the functions are not necessarily all exponents and may differ from one another. To be specific, let us expand exprA = Exp[x] Sin[y] up to the 5th order both in x and y.

To do this, let us first replace x and y by t x and t y:

exprB = exprA /. {x -> t*x, y -> t*y}

(*  E^(t x) Sin[t y]  *)

Now let us expand the result in terms of t up to t^5 and then put t=1:

 exprC = (Series[exprB, {t, 0, 5}] // Normal) /. t -> 1

(* y + x y + (x^2 y)/2 - y^3/6 + 1/6 (x^3 y - x y^3) + 
 1/120 (5 x^4 y - 10 x^2 y^3 + y^5)  *)

Now to bring this to a better visible form one can use Collect or Expand:


(*  y + x y + (x^2 y)/2 + (x^3 y)/6 + (x^4 y)/24 - y^3/6 - (x y^3)/6 - (
 x^2 y^3)/12 + y^5/120   *)

Done. Have fun!

  • $\begingroup$ Yes I used a very bad example, thank you! Your second method seems to be a very smart way to get around things although I'm a bit concerned what if I have polynomials like x^n/y^m, then some powers of t will cancel. $\endgroup$
    – Lepnak
    Commented Jun 16, 2021 at 9:49
  • $\begingroup$ @Lepnak It is no problem, since in the end you get rid of t and get a pure polynomial in terms of x and y. It becomes independent of t and starting from this point it "forgets" to which powers of t belonged this or that term. Another story that you may then want to regroup the result somehow. But this is a different task. $\endgroup$ Commented Jun 16, 2021 at 9:55
  • $\begingroup$ Ah yes you're right. Although I notice I might need to expand to a higher order. What I mean is for example let exprA=1/(1-x)(1-y^2), then if I expand to {t,0,5} I do not see x^2y^4, x^3y^4 etc. I need to expand to {t,0,10} to see all the y^5 terms (although now I also see redundant x^10 terms). Whereas if I do {x,0,5},{y,0,5} then I see all terms up to x^5, y^5. I think if x and y are both small numbers and I want to expand in powers of xy, then yours is definitely the way to go! Although I'm working on more of a character formula where I want to expand x and y individually. $\endgroup$
    – Lepnak
    Commented Jun 16, 2021 at 10:26

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.