Comments (3)
@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:
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.
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.
@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)
- [RFC]: add C implementation for `@stdlib/math/base/special/nonfibonacci` HOT 1
- [RFC]: add C implementation for `math/base/special/heaviside` HOT 1
- [RFC]: add C implementation for `@stdlib/math/base/special/factorial2` HOT 5
- [RFC]: add C implementation for `math/base/special/acscd` HOT 1
- [RFC]: add C implementation for `@stdlib/math/base/assert/is-composite` HOT 1
- [RFC]: add C implementation for `math/base/special/fast/min` HOT 1
- [RFC]: add C implementation for `number/uint32/base/to-int32` HOT 5
- [RFC]: Add C implementation for `@stdlib/math/base/special/acscd` HOT 5
- [RFC]: add C implementation for `math/base/special/modf` HOT 12
- [RFC]: add C implementation for `math/base/special/fast/*` (Tracking Issue) HOT 7
- [RFC]: Add C implementation for `@stdlib/math/base/special/atan2` HOT 6
- [RFC]: Add C implementation for `@stdlib/math/base/special/maxn` HOT 3
- [RFC]: add C implementation for `@stdlib/math/base/special/fast/uint32-log2` HOT 6
- [RFC]: add C implementation for `@stdlib/math/base/special/fast/uint32-sqrt` HOT 4
- [RFC]: add C implementation for `@stdlib/math/base/special/rempio2` HOT 2
- [RFC]: Enhancing Benchmark Visualization with TAP Parser and Plot Frontend HOT 5
- [RFC]: add C implementation for `@stdlib/math/base/special/fast/alpha-max-plus-beta-min` HOT 2
- [RFC]: add C implementation for `math/base/special/minmaxabs` HOT 1
- [RFC]: add `math/base/special/asinf` HOT 5
- [RFC]: add C implementation for `@stdlib/math/base/special/erfcx` HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from stdlib.