4 ms·
Ask HN: Is there a JavaScript library for arbitrary precision complex mathematics?
I am looking for a JavaScript library to work with very small (very large) complex numbers. I need things like pow(), sqrt(), etc. Best I could find is Math.js [1], but it doesn't have arbitrary precision functions for complex numbers. To illustrate my point, please see this JSFiddle [2].
If there is no such library - what is the best alternative?
a.) create such a library from scratch
b.) convert some Java, Python, or C/C++ library to JavaScript using pyjs or Emscripten
[1] http://mathjs.org/
[2] https://jsfiddle.net/95mxb0dL/12/
- niftich 10y agoThere are not many JS libraries in this space that have sane handling for complex numbers and arbitrary precision. I'm not entirely sure, but perhaps NumericJS [1] would have what you're looking for? [1] http://www.numericjs.com/documentation.html http://www.numericjs.com/documentation.html
- valera_rozuvan 10y agoThanks for your reply. But after trying this library - it doesn't offer precision beyond that which is in native JavaScript. Upon closer inspection [1] you can see that it uses standard Number.toPrecision() function. ECMA-262 only requires a precision of up to 21 significant digits [2]. This is no good for me. [1] https://github.com/sloisel/numeric/blob/656fa1254be540f428710738ca9c1539625777f1/src/numeric.js#L46 https://github.com/sloisel/numeric/blob/656fa1254be540f42871... [2] https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Number/toPrecision#Exceptions https://developer.mozilla.org/en/docs/Web/JavaScript/Referen...
- tucaz 10y agoI'm going to ask a really stupid question, but here it goes: why the need for such precision? (just curiosity)
- valera_rozuvan 10y ago=) A valid question! I am developing (yet another) fractal viewer for the browser. It's called FractalViewer, and is available from GitHub [1]. I am working on allowing it to zoom past the limit of about 10^(-13). The specific FractalViewer development issue is [2]. Why bother at all? I am very frustrated that there currently is no fractal viewer for the browser that can do a truly deep zoom. See a list I compiled on web-based fractal viewers [3]. Maybe someone can correct me, and point to a JavaScript based fractal viewer that can do really deep zooms... [1] https://github.com/valera-rozuvan/FractalViewer https://github.com/valera-rozuvan/FractalViewer [2] https://github.com/valera-rozuvan/FractalViewer/issues/10 https://github.com/valera-rozuvan/FractalViewer/issues/10 [3] https://github.com/valera-rozuvan/bookmarks-md/blob/master/fractals.md#html--js-interactive-fractals https://github.com/valera-rozuvan/bookmarks-md/blob/master/f...
- GregBuchholz 10y agoHave you heard of Fracint, the mandelbrot generator that uses integer math? http://www.fractint.org/ http://www.fractint.org/
- valera_rozuvan 10y ago@GregBuchholz Of course. I know many programs that allow deep zooms of fractals. However, I am after an implementation in the browser.
- GregBuchholz 10y agoIt wasn't clear that you've looked at its implementation of arbitrary precision floating point numbers without using floating point numbers and found it wanting.
- valera_rozuvan 10y agoThe author of Fractint, Wes Loewer, created his own arbitrary precision math library [1]. I am aware of how such pieces of software work. I am also aware that to create such a thing from scratch - will take much effort. Especially testing it for correctness. Why reinvent the wheel? That's why I asked the question in the first place. Best case scenario - someone has already implemented such functionality in JavaScript (with complex numbers). But I think that this is not the case. So I guess this leads to the case of porting from some other language. [1] http://www.nahee.com/spanky/www/fractint/arb-prec.html http://www.nahee.com/spanky/www/fractint/arb-prec.html
- niftich 10y agoThough I'm sure you'd rather avoid it, you can inspect other bignum implementations and perform a port. Coming from a Java library, for example, the port would be relatively straightforward, as syntax and semantics will largely match. The most unpleasant thing is that JS doesn't have native ints, so you'll have to either use some other type as backing storage, and/or clever type coercion trickery, like what you see in asm.js. [1] apfloat: http://grepcode.com/snapshot/repo1.maven.org/maven2/org.apfloat/apfloat/1.8.2/ http://grepcode.com/snapshot/repo1.maven.org/maven2/org.apfl... [2] JDK's BigDecimal: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/math/BigDecimal.java#BigDecimal http://grepcode.com/file/repository.grepcode.com/java/root/j...
- valera_rozuvan 10y agoI am also considering Python-based "mpmath" [1], and C-based "Gnu Mpc" [2] as candidates for porting to JavaScript. [1] http://mpmath.org/ http://mpmath.org/ [2] http://www.multiprecision.org/ http://www.multiprecision.org/
- S4M 10y agoI had a look at your jsfiddle, and it looks to me that it's a bug in mathjs. I have the impression that mathjs doesn't parse well complex numbers (I can be wrong there), so why not spend 1 hour to try to fix that bug before considering alternatives?
- valera_rozuvan 10y agoAs it turns out, Math.js doesn't support arbitrary precision calculations with complex numbers. This is because it is using libraries complex-js [1] and bignumber.js [2], which in turn also don't support arbitrary precision calculations with complex numbers. This situation will improve in the future. See the Math.js issue [3]. [1] https://github.com/patrickroberts/complex-js https://github.com/patrickroberts/complex-js [2] https://github.com/MikeMcl/bignumber.js/ https://github.com/MikeMcl/bignumber.js/ [3] https://github.com/josdejong/mathjs/issues/694 https://github.com/josdejong/mathjs/issues/694
- antoineMoPa 10y agoWe should write a library for that with webgl + GLSL backend for faster computation (It would be quite a hack: textures would be used to store data and communicate with JS, but everything would be much faster). We could also use WebCL but this thing does not seem to work with many browsers.
- valera_rozuvan 10y agoAn interesting idea. Do you want to spear-head this project? I don't have much expertise in GLSL...
- antoineMoPa 10y agoI might (or might not) start some quick proof of concept in the next few days.
- valera_rozuvan 10y agoAwesome! Will be watching https://github.com/antoineMoPa https://github.com/antoineMoPa for the appearance of your poc =) Will contribute to it too.