Constructing a rootfinding function with factorial

computational mathematicsfactorialroots

I'm working on a root-finding problem and dealing with a function with factorial. $\epsilon$ is a very small machine error, approximately $2 \times 10^{-16}$, which is a tolerance to prevent infinite iterations. And $x$ is a known value, which are $[0.1,1,10,100,1000]$. Therefore, there are five functions I need to find the root of $n$.
$$
r = \frac{x^{n+1}}{(n + 1)!} \approx \epsilon
$$

My idea is that

\begin{align}
r &= \frac{x^{n+1}}{(n+1)!} \\
\log r &= \log \frac{x^{n+1}}{(n+1)!} \\
\log r &= (n+1)\log x – \log (n+1)!
\end{align}

$\log r$ should be a number around $-16$ and we can find the root. But the problem requires me not to use Stirling's approximation; moreover, I cannot use cumulative sum for $\log$ because $n$ can be a decimal number. Could someone provide some ideas how to continue this problem? Thank you very much. No need to worry about root finding algorithm. I just need to construct a doable function that I can deal with on Python.

Best Answer

If you're extending factorials to real values, you'll probably want to look into the Gamma Function.

$\Gamma(n+1) = n!$ when $n$ is a natural number, but $\Gamma$ can be defined for all complex numbers except for the negative integers. In Python you can use the function lgamma from the math module in the standard library. lgamma computes the log of the absolute value of the Gamma function.

Something like this could work to compute the function you're interested in

import math

def log_r(n: float, x: float) -> float:
    return (n + 1) * math.log(x) - math.lgamma(n)

though whether or not the Gamma function is actually what you want may be use case dependent.

Edit: I neglected to mention. If you plan to use Newton's method, you'll also need to be able to calculate the derivative of lgamma. In Python you can use scipy.special.digamma from the 3rd party scipy package.

Related Question