Giter Club home page Giter Club logo

Comments (3)

kgryte avatar kgryte commented on June 11, 2024 1

@Andrew-Cottrell I did a bit more investigating. I was able to update the implementation to only compute the GCD when overflow is a risk. For larger binomial coefficients, this drops performance by 50%. Not great, but also not the end of the world.

For reference:

// Use a standard algorithm for computing the binomial coefficient (e.g., see Knuth's "The Art of Computer Programming, 3rd Edition, Volume 2: Seminumerical Algorithms")...

Thanks for opening this issue and helping point in the right direction. This resolves the failing tests in fibpoly, which have subsequently been updated.

from stdlib.

Andrew-Cottrell avatar Andrew-Cottrell commented on June 11, 2024

An alternative version of the modification avoids calling gcd when unnecessary, at the cost of increased code complexity.

	var res;
	var j;
	var s; // 's' for safe
	var d; // 'd' for divisor

        ...

	s = Math.floor(Number.MAX_SAFE_INTEGER / n);
	res = n;
	for ( j = 2; j <= k && res < s; j++ ) {
		res = ( res * ( n - j + 1 ) ) / j;
	}
	for ( ; j <= k; j++ ) {
		d = gcd( res, j );
		res = ( res / d ) * ( d * (n - j + 1) / j );
	}
	return res;

The next version swaps the order of the numerator values to be increasing rather than decreasing, which helps performance by sometimes evaluating more terms using the non-gcd version of the calculation.

	s = Math.floor(Number.MAX_SAFE_INTEGER / n);
	res = n - k + 1;
	n = res + 1;
	for ( j = 2; j <= k && res < s; j++, n++ ) {
		res = (res * n) / j;
	}
	for ( ; j <= k; j++, n++ ) {
		d = gcd( res, j );
		res = ( res / d ) * ( d * n / j );
	}
	return res;

In some non-scientific testing, this version appears to be about three times faster, than the simple modification, when computing the four example binomial coefficients mentioned above. And about seven times faster when computing smaller binomial coefficients, such as binomcoef( 54, 20 ), that don't need to call the gcd function.

from stdlib.

kgryte avatar kgryte commented on June 11, 2024

@Andrew-Cottrell Thanks for opening this issue and your proposed solutions. Based on my testing, performance drops by over 10x when moving to GCD, even for the improved version.

The accuracy does improve, which is a plus, but I'd prefer, if possible, to find a different algorithm which doesn't force an order of magnitude drop in performance, especially as binomcoef is used as a dependency by other functions within stdlib. Meaning, a significant drop in binomcoef perf will likely necessarily mean a drop in dependants perf.

I've done a bit of research to try and find other algorithms which improve accuracy without sacrificing perf; however, I haven't had much luck finding one which is universally good.

from stdlib.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.