This is a WebAssembly packaging of the cephes library. The cephes library contains C implementations of most special functions, distributions, and other hard-to-implement mathematical functions.
Note that there are a few cephes functions that are not exposed here, as some of them are quite hard to make consumable in JavaScript using WebAssembly. Feel free to send a pull request if you need one of them.
npm install cephes
If you are looking on GitHub, you will notice some files are missing. These are statically built from the cephes library. See the CONTRIBUTING.md file, for how to build them.
Cephes is a WebAssembly module but is very small and fast to compile, as it doesn't depend on any runtime libraries. In Node.js it is therefore compiled synchronously and all you need to do is require the module.
const cephes = require('cephes'); // Node.js
In the browser, it is, for good practice, compiled asynchronously. You must
therefore wait for the .compiled
promise to be resolved.
const cephes = require('cephes'); // Browser
await cephes.compiled;
Note that the .compiled
promise is also available in Node.js, but it is
simply a dummy promise that resolves immediately.
There are three variations of functions to be aware of:
These don't require anything special.
const value = cephes.zeta(2, 1);
In C, these functions return a primary value and then return extra value using pointer arguments. In JavaScript this is implemented as a function that returns an array of length 2. The first element is the primary returned value, the second is an object of the extra returned values.
const [value, {ai, aip, bi, bip}] = cephes.airy(-1);
Some functions consumes an array of values, these must be TypedArrays
of
the appropriate type. These functions will typically also require a variation
of .length
value as a parameter, like you would do in C. Be aware, that in
some cases it may not be exactly the .length
of the TypedArray
, but may be
one less or one more. Check the specific function documentation to be sure.
const arrayInput = new Float64Array([2.2, 3.3, 4.4]);
const value = ephes.polevl(1.1, arrayInput, arrayInput.length - 1);