# How do I get Series[] as a functional, rather than as an expression (i.e. to avoid the dummy variable)?

How can I write an equivalent to Series that doesn't require a dummy variable?

Note that the series should be constructed before the evaluation point is supplied, not after.

Also, I'd rather avoid using Unique[] if possible (so that multiple return the same thing), and I want to avoid scope leaks as well.

[Updated] It does not quite seem to be possible to avoid generating new variable names, since there is always a need to avoid potential conflicts with the body of each function.

However, it is possible to avoid calling Unique:

FSeries[f_, xo__] := Module[{t},
With[{args =
Symbol /@
Array[SymbolName[t] <> "\$" <> ToString[#1] &, Length[{xo}]]},
Function[Evaluate[args],
Evaluate[
Apply[Series,
Apply[f, args]]]]]]];


Now if you say:

FSeries[Exp, {1, 3}]


you get back the 3rd-degree Taylor series of Exp around 1, which you can then call Normal on to turn it into a regular series without the O[] portion.

• This seems to fail: FSeries[Exp[#1 + #2] &, {a, 1}, {b, 1}][1, 2] Mar 6, 2016 at 23:45
• @MichaelE2: Fail in what way? Did you call Normal on the output of FSeries? Mar 7, 2016 at 1:36
• Actually Unique get called by Module... Mar 7, 2016 at 13:51
• @unlikely: Whoops. Well this is the best answer I've been able to find, let me know if you find something better. Mar 7, 2016 at 19:17
• @Mehrdad Maybe you like my edit. Mar 8, 2016 at 13:11

Why something like this is not enough?

FSeries[f_, xo__] :=
With[{fn = Series[f[#], {#, Sequence @@ xo}]}, fn &]
FSeries[Exp, {1, 3}]


SeriesData[#, 1, {E, E, Rational[1, 2] E, Rational[1, 6] E}, 0, 4, 1] &

[EDIT] Or this?

FSeries[f_, xo__] :=
With[{args = Slot /@ Range@Length@{xo}},
With[{fn = Normal@Series[f @@ args, Sequence @@ MapThread[Prepend, {{xo}, args}]]},
fn &]
]

FSeries[Exp, {1, 3}]
FSeries[Exp[#1 + #2] &, {a, 1}, {b, 1}]


As pointed by @MichealE2, the Normal is necessary to evaluate the result for numerical values in the multivariate case, but you loose the O also for symbolic arguments. So a possible problem is if you want to handle (how?) numerical and non-numerical arguments in multivariate.

[EDIT 2] Maybe you like this. If you use formal symbols in the intended way use and you don't normally define functions with 53 arguments or more, this version satisfy your requirements.

FSeries[f_, xo__] :=
With[{args = Symbol /@ Take[CharacterRange["\[FormalA]", "\[FormalCapitalZ]"], Length@{xo}]},
With[{fn = Series[f @@ args, Sequence @@ MapThread[Prepend, {{xo}, args}]]},
Function @@ {args, fn}
]
]

FSeries[Exp, {1, 3}]
FSeries[Exp[#1 + #2] &, {a, 1}, {b, 1}]
(Normal@FSeries[Exp[#1 + #2] &, {a, 1}, {b, 1}])[1, 2]
FSeries[Function[t, Exp[#1]], {0, 2}]

Function[{\[FormalA]}, SeriesData[\[FormalA], 1, {E, E, Rational[1, 2] E, Rational[1, 6] E}, 0, 4, 1]]
Function[{\[FormalA], \[FormalB]}, SeriesData[\[FormalA], a, {
SeriesData[\[FormalB], b, {E^(a + b), E^(a + b)}, 0, 2, 1],
SeriesData[\[FormalB], b, {E^(a + b), E^(a + b)}, 0, 2, 1]}, 0, 2, 1]]
E^(a + b) + (2 - b) E^(a + b) + (1 - a) (E^(a + b) + (2 - b) E^(a + b))
Function[{\[FormalA]}, E^#1]

• Because it fails on something like FSeries[Exp[#1 + #2] &, {a, 1}, {b, 1}]. Mar 6, 2016 at 23:27
• @Mehrdad "something like"... see my edit... I miss the point of Inactive for example... Mar 6, 2016 at 23:41
• It was meant to prevent some bugs that (I just now realized) it apparently didn't prevent... and which yours apparently also suffers from: try FSeries[Function[t, Exp[#1]], {0, 2}] and notice that #1 gets substituted when it shouldn't. Mar 6, 2016 at 23:51
• @Mehrdad I miss the use-case of this syntax, but, if you want, I think changing Slot with Inactive[Slot] and appending an //Activate at the end of my version can work. I'm not sure because I don't know what you want to get. Mar 7, 2016 at 0:10
• Oh, I want it to give the same exact result as Series[Function[t, Exp[#1]][t], {t, 0, 2}], i.e. Series[Exp[#1], {t, 0, 2}]. (I realize it looks useless, but I still want it to be correct since I don't want to track down a bug because of it later.) I'm fairly sure the solution can't be so simple -- otherwise you might Activate things that the user didn't intend to. Are you sure that would work? Mar 7, 2016 at 0:26