5
$\begingroup$

Suppose I want to expand a multivariable function to an order n.

f1[x_, y_] = Sin[x + y];
Series[f1[x + y], {x, 0, 3}, {y, 0, 3}] // Normal // Expand

$x - x^3/6 + y - (x^2 y)/2 - (x y^2)/2 + (x^3 y^2)/12 - y^3/6 + ( x^2 y^3)/12$

So I have to eliminate terms like $x^3 y^2$. One way could be using Exponent.

series[f_Symbol, x_, y_, n_] := Module[{expn},
       expn = Series[f[x, y], {x, 0, n}, {y, 0, n}] // Normal // Expand;
       Total@Select[Level[expn, 1], (Exponent[#, x] + Exponent[#, y]) <= n &] 
      ]

series[f1, x, y, 3]

$x - x^3/6 + y - (x^2 y)/2 - (x y^2)/2 - y^3/6$

  • Is there any better way to do it?
  • Can it be generalised for any number of variables?

By generalised I mean given a function, can the code detect the variables and make the expansion? I tried to use Variables but it does not work with trigonometric functions.

A related question might be Multivariable Taylor expansion does not work as expected

$\endgroup$
2
  • $\begingroup$ +1 It would be great if there were a built in function for this, or otherwise if it were possible to do Series[f1[x + y], {x, 0, 3}, {y, 0, 3 - x}]. $\endgroup$
    – Feyre
    Commented Jan 16, 2017 at 12:21
  • $\begingroup$ Thanks @DanielLichtblau for the comment (and eyeopener :|). I can assure you there was no hidden intention behind the question. Sometimes I stuck with simple questions just because I search the answer in wrong place :p $\endgroup$
    – Sumit
    Commented Jan 25, 2017 at 7:03

2 Answers 2

3
$\begingroup$

Another possibility is to rescale x,y,... by sx, sy,... and expand around s:

series[f_, var : {_Symbol ..}, n_Integer?Positive] := Module[{s},
  Expand@Normal[Series[ f /. Thread[ var -> (s var)], {s, 0, n}]] /. 
   s -> 1]
$\endgroup$
1
$\begingroup$
series[f_, var : {_Symbol ..}, n_Integer?Positive] :=
 Module[{expr =
    Series[f, Sequence @@ ({#, 0, n} & /@ var)] //
      Normal // Expand},
  Select[expr, Total[Exponent[#, var]] <= n &]]

f1[x_, y_] = Sin[x + y];

series[f1[x, y], {x, y}, 3]

(*  x - x^3/6 + y - (x^2 y)/2 - (x y^2)/2 - y^3/6  *)

f2[x_, y_, z_] = z*Sin[x + y];

series[f2[x, y, z], {x, y, z}, 4]

(*  x z - (x^3 z)/6 + y z - 1/2 x^2 y z - 1/2 x y^2 z - (y^3 z)/6  *)
$\endgroup$

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