Calculating Arc Hyperbolic CoSecant faster than using a standard power series

I have used the standard Power-Series to calculate Arc Hyperbolic Co-Secant trig function. But as with my other posts before, it is very slow. Here is the standard Power-Series, taken from https://en.wikipedia.org/wiki/Inverse_hyperbolic_functions#Series_expansions

$$ \operatorname{arcsch} x = \operatorname{arsinh} \frac1x = x^{-1} - \left( \frac {1} {2} \right) \frac {x^{-3}} {3} + \left( \frac {1 \cdot 3} {2 \cdot 4} \right) \frac {x^{-5}} {5} - \left( \frac {1 \cdot 3 \cdot 5} {2 \cdot 4 \cdot 6} \right) \frac {x^{-7}} {7} \pm\cdots \\ = \sum_{n=0}^\infty \left( \frac {(-1)^n(2n)!} {2^{2n}(n!)^2} \right) \frac {x^{-(2n+1)}} {2n+1} , \qquad \left| x \right| > 1 $$ I have used a different form to speed it up, which it did not. $$ \operatorname{arcsch} x = \frac {1}{x} - \left( \frac {1} {2} \right) \frac {1} {3 x^{3}}+ \left( \frac {1 \cdot 3} {2 \cdot 4} \right) \frac{1} {5x^{5}} - \left( \frac {1 \cdot 3 \cdot 5} {2 \cdot 4 \cdot 6} \right) \frac {1} {7x^{7}} \pm\cdots \\ = \sum_{n=0}^\infty \left( \frac {(-1)^n(2n)!} {2^{2n}(n!)^2} \right) \frac {x^{-(2n+1)}} {2n+1} , \qquad \left| x \right| > 1 $$ But it is easier to reproduce in my calculator program. The part that is taking so long are terms $$ \left ( \frac {1} {7x^{7}} \right ) $$ and higher. The time spent dividing $$ {7x^{7}} $$ into $$ 1 $$ is huge. At this iteration, it takes about 15 seconds. The next ones just keep growing, using more time.

And yes, this post is just my last post Calculating Arc Hyperbolic CoTangent faster than using a standard power series. Here, 2 persons showed me the Horner's method and a programming procedure. It was really fast, compared to what I was using before.

I am hoping that there is a Horner-like form that a person, you people, can show me. I have read the Wiki page that explains the Horner's method, but that does not mean I can create the method.

Thank you very much for your time.


Solution 1:

Fundamentally, Horner form involves nothing more than convenient parenthesization and factoring to avoid re-powering the variable with each term. For instance, $$\begin{align} a_0 + a_1 z + a_2 z^2 + a_3 z^3 + a_4 z^4 &\quad\to\quad a_0 + z\left(a_1 + a_2 z + a_3 z^2 + a_4 z^3 \right) \\ &\quad\to\quad a_0 + z\left(a_1 + z \left( a_2 + a_3 z + a_4 z^2 \right)\; \right) \\ &\quad\to\quad a_0 + z\left(a_1 + z \left( a_2 + z \left( a_3 + a_4 z \right)\; \right)\; \right) \\ \end{align} \tag{1}$$

That's all there is to it, really, although sometimes (especially with power series) we can exploit the specific nature of the coefficients.

So, a first pass at the hyperbolic arccosecant series would look like this (in stages):

$$\begin{align} \operatorname{arcsch}(x) &= x^{-1}\left(\;1 - \frac12\cdot\frac13 x^{-2} + \frac{1\cdot 3}{2\cdot 4}\cdot \frac15 x^{-4} - \frac{1\cdot 3\cdot 5}{2\cdot 4\cdot 6}\cdot\frac17 x^{-6} + \cdots \right. \tag{2a}\\[4pt] &= x^{-1}\left(\;1 + x^{-2}\left(- \frac12\cdot\frac13 + \frac{1\cdot 3}{2\cdot 4}\cdot \frac15 x^{-2} - \frac{1\cdot 3\cdot 5}{2\cdot 4\cdot 6}\cdot\frac17 x^{-4} + \cdots \right.\right. \tag{2b}\\[4pt] &= x^{-1}\left(\;1 + x^{-2}\left(- \frac12\cdot\frac13 + x^{-2}\left(\frac{1\cdot 3}{2\cdot 4}\cdot \frac15 - \frac{1\cdot 3\cdot 5}{2\cdot 4\cdot 6}\cdot\frac17 x^{-2} + \cdots \right.\right.\right. \tag{2c}\\[4pt] &= x^{-1}\left(\;1 + x^{-2}\left(- \frac12\cdot\frac13 + x^{-2}\left(\frac{1\cdot 3}{2\cdot 4}\cdot \frac15 + x^{-2}\left(- \frac{1\cdot 3\cdot 5}{2\cdot 4\cdot 6}\cdot\frac17 + \cdots \right.\right.\right.\right. \tag{2d} \end{align}$$

All I did was add parentheses and pull out powers of $x$ as I went along. I made no changes to coefficients.

We can clean this up a little, though. First, define $y := 1/x$ and $z:=1/x^2$ (which you can compute once and store), so that we have

$$\operatorname{arcsch}(x) = y\left(\;1 + z\left(-\frac12\cdot\frac13 + z\left(\frac{1\cdot 3}{2\cdot 4}\cdot \frac15 + z\left(-\frac{1\cdot 3\cdot 5}{2\cdot 4\cdot 6}\cdot\frac17 + \cdots\right.\right.\right.\right. \tag{3}$$

Now, notice that the coefficients are accumulating factors. We can avoid re-calculating them by factoring-out at appropriate levels. Again doing this in stages ...

$$\begin{align} \operatorname{arcsch}(x) &= y\left(\;1 + z\cdot \frac12 \left(-\frac13 + z\left(\frac{3}{4}\cdot \frac15 - z\left(\frac{3\cdot 5}{4\cdot 6}\cdot\frac17 + \cdots\right.\right.\right.\right. \tag{4a} \\[4pt] &= y\left(\;1 + z\cdot \frac12 \left(-\frac13 + z\cdot \frac34 \left(\frac15 + z\left(-\frac56\cdot\frac17 + \cdots\right.\right.\right.\right. \tag{4b} \\[4pt] &= y\left(\;1 + z\cdot \frac12 \left(-\frac13 + z\cdot \frac34 \left(\frac15 + z\cdot \frac56 \left(-\frac17 + \cdots\right.\right.\right.\right. \tag{4c} \end{align}$$

We can even factor-out negatives as we go, to get

$$\begin{align} \operatorname{arcsch}(x) &= y\left(\;1 - z\cdot \frac12 \left(\frac13 - z\cdot \frac34 \left(\frac15 - z\cdot \frac56 \left(\frac17 - \cdots\right.\right.\right.\right. \tag{5} \end{align}$$

I'll leave it to OP to implement the corresponding programmatic computational algorithm.

In any case, conversion to Horner form is entirely mechanical, at least at the first-pass level. Optimizing for factorable coefficients is relatively straightforward, as well, although it isn't always possible. (Factorials and double-factorials are well-suited to this.) For practice, OP can revisit his past fast-calculation questions, convert each series to Horner-form on his own, and then compare to answers and/or comments he's received. And, of course, he should be able to apply this procedure to new series, too.