2
$\begingroup$

I am facing the following problem: I want to expand the expression

exp=Tan[Sum[Subscript[a, k] x^((2 k + 1)/2), {k, 0, n}]];

in a series around $x=0$. Using

Series[exp /. n -> 2, {x, 0, 2}]

works as intended and gives a result within a moment.

The problem is expanding up to higher orders in $n$. Run times explode for $n>10$, with a wall time for $n=10$ of $88\, \mathrm{s}$ and for $n=11$ I was not able to get a result at all, having it run for more then 15 minutes, on an i5-4210U@1.7GHz machine. Of course computing higher derivatives of this expression gets harder with every order but given the rather simplistic form of the argument of $\tan$ I would expect it to work faster given the fact that there is nothing to fancy involved in terms of algebra.

Are there any tricks to speed up this expansion? I tried using assumptions like

$Assumptions = (And @@ (Element[Subscript[a, #], Reals] &) /@ 
Range[1, 15])

but without success.

In my despair I turned to my second computer algebra program - Maple - and it can expand this expression with its series() method much much faster. $n=20$ takes only around $.5\,\mathrm{s}$.

Which leave me with to possible conclusions: Either I am not aware of steps to take to enable Mathematica to use its Series[] method much faster or Mathematica's Series[] method is not very good.

I would really like to keep all computations in Mathematica since I need the expanded result for further processing.

$\endgroup$
  • $\begingroup$ I can't reproduce your problem with V11.1.1. What is the exact expression which is slow? $\endgroup$ – Shadowray Jul 6 '17 at 18:50
  • $\begingroup$ I should finally update to Version 11. I am using V10.0. $\endgroup$ – M. J. Steil Jul 6 '17 at 18:58
2
$\begingroup$

By default, Series doesn't expand the coefficients of the series, and so the leaf count of the series can grow very quickly. For example:

LeafCount /@ Series[Tan[Sum[Subscript[a,k] x^((2 k+1)/2),{k,0,17}]], {x,0,17}][[3]]

{3,1,13,1,68,1,176,1,412,1,928,1,1990,1,4143,1,12045,1,31616,1,74275,1,163011,1,343993,1,709771,1,1445670,1,2922569,1,6810467}

Notice how the leaf count more than doubles for each nonzero term of the series. On the other hand, after simplification:

LeafCount /@ Simplify /@ Series[Tan[Sum[Subscript[a,k] x^((2 k+1)/2), {k,0,17}]],{x,0,17}][[3]]

{3,1,13,1,22,1,43,1,80,1,124,1,190,1,287,1,409,1,580,1,809,1,1105,1,1505,1,2033,1,2692,1, 3573}

The leaf count grows much more slowly. So, one idea is to compose the series by hand. For example:

tanSeries[n_] := Normal @ Series[Tan[x], {x, 0, n}]
argSeries[p_, n_] := Series[Sum[Subscript[a, k] x^(k+1/2), {k, 0, n}]^p, {x, 0, n}]

Here, argSeries is the series of $\arg ^p$ where arg is the argument of Tan, and tanSeries is the normal form of the series approximation of Tan. Since the leading term of argSeries[p, n] is $x^{p/2}$, to compute the desired series to order $n$ we need to have to keep twice as many terms of the Tan series. So:

r10 = tanSeries[20] /. x^p_. :> argSeries[p, 10]; //Timing
r20 = tanSeries[40] /. x^p_. :> argSeries[p, 20]; //Timing

{0.057843, Null}

{4.73507, Null}

The leaf count of this approach is still high:

LeafCount /@ r20[[3]]

{3, 1, 13, 1, 22, 1, 57, 1, 121, 1, 262, 1, 542, 1, 1049, 1, 1921, 1, 3363, 1, 5679, 1, 9322, 1, 14869, 1, 23094, 1, 35046, 1, 52162, 1, 76285, 1, 109893, 1, 155998, 1, 218370}

but not nearly as high as the naive application of Series in M11.1.1. I expect that this approach will scale better as higher order terms are needed.

$\endgroup$
  • $\begingroup$ Doing it that way is a good way to do it. Expanding arguments and functions separately and then using rules to get them back together seems to be the most performant way. It scales much better then Series and works in the older versions. What I found use full is to use HornerForm to reduce the LeafCount of individual series coefficients. Series works reasonably well at low orders but at high orders it is necessary to break it down and solve the problem piece by piece. It would be nice if Series would be able to do it that way but that might be in all generality very hard to realize. $\endgroup$ – M. J. Steil Jul 9 '17 at 0:53
1
$\begingroup$

When I try executing the following, I get answers rapidly

exp = Tan[Sum[Subscript[a, k] x^((2 k + 1)/2), {k, 0, n}]];
Table[Timing[Series[exp, {x, 0, n}];], {n, 2, 20}]

{{0., Null}, {0., Null}, {0., Null}, {0., Null}, {0., Null}, {0., 
  Null}, {0., Null}, {0., Null}, {0.028, Null}, {0.036, Null}, {0.04, 
  Null}, {0.048, Null}, {0.064, Null}, {0.092, Null}, {0.128, 
  Null}, {0.216, Null}, {0.4, Null}, {0.764, Null}, {1.444, Null}}

I am using

$Version
(* "11.1.1 for Linux x86 (64-bit) (April 18, 2017)" *)

Perhaps I've misunderstood your question, or you are using an older version?

$\endgroup$
  • $\begingroup$ I should finally update to Version 11. I am using V10.0. I should have probably mentioned it in the question. Still kind of strange that Version 10 is inferior in that regard by multiple orders of magnitude. $\endgroup$ – M. J. Steil Jul 6 '17 at 19:00
  • 1
    $\begingroup$ I ran the code in 9, 10, 11, and 11.1.1. Only in 11.1.1 the code executes in finite time: so Wolfram evidently made massive improvements to Series[] in their latest release. Thank you for pointing out that 11.1.1 works way better for that problem. $\endgroup$ – M. J. Steil Jul 6 '17 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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