-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add gamma, lngamma, invgamma, lambertw #7
Comments
Hmm, here's an approximation to factorial that'd be easier (and smaller) to implement. It's called Stirling's approximation. https://en.wikipedia.org/wiki/Stirling%27s_approximation Ramanujan's approximation improves accuracy in some cases: Inverse gamma has an implementation if you implement Lambert W function already:
Here's a C++ Lambert W function. It's messy lol. Doesn't exactly excite me to implement. But a factorial approximation should be good :> |
factorial() added |
Hi guys, the project intrigued me a lot. To have already implemented branch 0 of the lambert function (but only for x>=0), I can tell you that it can be considered to use the Halley method or the Fritsch method. Here is an implementation of which I made a long time ago in C, precise to machine epsilon. static double HalleyW(double Wn, double x)
{
double expWn = exp(Wn);
double Wn_expWn = Wn * expWn;
double un = Wn_expWn + expWn;
double tn = Wn_expWn - x;
double sn = (Wn + 2) / (2 * Wn + 2);
return Wn + tn / (tn * sn - un);
}
static double ApproxW(double x) // this function need to be fast, not accurate
{
if (x < 159.102483581697965)
return ln(sqrt(2)*x + ln(3)) - ln(ln( M_PI / M_E * x + 3)); // M_PI and M_E are pi and e
else
{
double lnx = ln(x);
double lnlnx = ln(lnx);
return lnx - lnlnx + lnlnx / lnx;
}
}
double LambertW0(double x) // (for x >= 0 only)
{
return HalleyW(HalleyW(ApproxW(x), x), x);
} |
I think most of the trigonometric operators like cos and sin and stuff are pointless, but gamma (the continuous factorial function) and its inverse deal with the domain of very large numbers, grow FASTER than exponential and, if given easy access to, might be explored by incremental games in more detail.
Notably, decimal.js does NOT have gamma/invgamma.
SpeedCrunch has implementations of gamma and lngamma:
But no invgamma.
invgamma seems to be a lot harder, discussions/papers here:
EDIT: I looked at the SpeedCrunch code for gamma and it's a ton of functions to convert, that and the fact that gamma() and x^x scale at more or less the same rate makes me less motivated to do this. But there is no technical problem stopping gamma and lngamma from being implemented in break_infinity.js besides the motivation to do it.
Also, related to invgamma, how about the Lambert-W function (inverse of x*e^x)?
The text was updated successfully, but these errors were encountered: