Skip to content
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

Open
Patashu opened this issue Nov 20, 2017 · 3 comments
Open

Add gamma, lngamma, invgamma, lambertw #7

Patashu opened this issue Nov 20, 2017 · 3 comments
Labels
only if you're bored very difficult or of dubious value

Comments

@Patashu
Copy link
Owner

Patashu commented Nov 20, 2017

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)?

@Patashu Patashu changed the title Add gamma/invgamma Add gamma, lngamma, invgamma, lambertw Nov 21, 2017
@Patashu Patashu added only if you're bored very difficult or of dubious value and removed enhancement labels Nov 21, 2017
@Patashu
Copy link
Owner Author

Patashu commented Dec 14, 2017

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:

https://math.stackexchange.com/questions/676952/is-ramanujans-approximation-for-the-factorial-optimal-or-can-it-be-tweaked-a

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 :>

@Patashu
Copy link
Owner Author

Patashu commented Dec 22, 2017

factorial() added

@lsarrazi
Copy link

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.
HalleyW function have cubic convergence. In this example I use 2 call to HalleyW, and i have ~16 rights decimals.
So in theory, for 3, 4, 5 calls to HalleyW you get 48, 144, 432 decimals.
Fritsch have quartic convergence, but Halley could be a better choice because it involve less operations per call.
The function ApproxW need to be accurate to at least 10^-2 or a little more, something like that.

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); 
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
only if you're bored very difficult or of dubious value
Projects
None yet
Development

No branches or pull requests

2 participants