4
$\begingroup$

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.

$\endgroup$

2 Answers 2

1
$\begingroup$

[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, 
       Prepend[MapThread[Prepend[#1, #2] &, {{xo}, args}], 
        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.

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

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] &

Mathematica graphics

[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]
$\endgroup$
10
  • $\begingroup$ Because it fails on something like FSeries[Exp[#1 + #2] &, {a, 1}, {b, 1}]. $\endgroup$
    – user541686
    Commented Mar 6, 2016 at 23:27
  • 1
    $\begingroup$ @Mehrdad "something like"... see my edit... I miss the point of Inactive for example... $\endgroup$
    – unlikely
    Commented Mar 6, 2016 at 23:41
  • $\begingroup$ 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. $\endgroup$
    – user541686
    Commented Mar 6, 2016 at 23:51
  • $\begingroup$ @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. $\endgroup$
    – unlikely
    Commented Mar 7, 2016 at 0:10
  • $\begingroup$ 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? $\endgroup$
    – user541686
    Commented Mar 7, 2016 at 0: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.