SkillAgentSearch skills...

LibRmath.js

This R statistical nmath module re-created in typescript/javascript and handwritten webassembly.

Install / Use

/learn @R-js/LibRmath.js

README

libRmath.js

This R statistical nmath re-created in typescript/javascript and handwritten webassembly.

Distributions:

beta, binomial, binomial-negative, cauchy, ch-2, exponential, fisher, gamma, geometric, hypergeometric (web assembly), logistic, log normal, multinomial, normal/gaussian, poisson distribution, singnrank (web assembly), student-t, tukey, uniform, weibull, wilcoxon.

Special functions:

bessel, beta, gamma, choose (the binomial coefficient $\binom{n}{k}$).

Pseudo Random genererators:

knuth-taocp, lecuyer-cmrg, marsalglia-multicarry, mersenne-twister, super-duper, wichman-hill, ahrens-dieter, box-muller, buggy-kinderman-ramage, inversion, kinderman-ramage.

This library has zero external dependencies.

Synopsys

This documentation assumes you knowledgable about the R build-in libaries.

npm i lib-r-math.js@latest # most recent stable version

Older documentation:

  • for the lts branch of version 2 see here
  • for the lts branch of version 1 see here.

If you were not using a previous version to 2.0.0, you can skip breaking changes and go to:

BREAKING CHANGES For version 2.0

Removed

RNG (normal and uniform) are only selectable via RNGkind function.

The normal and uniform implementation of the various RNG's are not exported publicly anymore Select normal and uniform RNG's via the function RNGkind.

// this is NOT possible anymore
import { AhrensDieter } from 'lib-r-math.js';
const ad = new AhrensDieter();
ad.random();

// NEW way of doing things
import { RNGkind, rnorm } from 'lib-r-math.js';
RNGkind({ normal: 'AHRENS_DIETER' }); // R analog to "RNGkind"
rnorm(8); // get 8 samples, if you only want one sample consider rnormOne()

helper functions for data mangling

Functions removed from 2.0.0 onwards: any, arrayrify, multiplex, each, flatten, c, map, selector, seq, summary.

It is recommended you either use well established js tools like Rxjs or Ramdajs to mangle arrays and data.

Removed helper functions for limiting numeric precision

Functions removed from 2.0.0 onwards: numberPrecision

This function mimicked the R's options(digits=N).

Changed

helper functions

Functions changed from 2.0.0 onwards: timeseed.

timeseed is now replaced by a cryptographic safe seed seed.

Sample distributions return a result of type Float64Array.

Functions changed from 2.0.0 onwards:

All these functions will return type of Float64Array: rbeta, rbinom, rcauchy, rchisq, rexp, rf, rgamma, rgeom, rhyper, rlogis, rlnorm, rmultinom, rnorm, rpois, rsignrank, rt,runif, rweibull, rwilcox.

For single scalar (number) return values, use the analogs: rbetaOne, rbinomOne, rcauchyOne, rchisqOne, rexpOne, rfOne, rgammaOne, rgeomOne, rhyperOne, rlogisOne, rlnormOne, rnormOne, rpoisOne, rsignrankOne, rtOne,runifOne, rweibullOne, rwilcoxOne.

Example:

import { rbinom, rbinomOne, setSeed } from 'lib-r-math.js';

rbinom(0); //
// -> FloatArray(0)

setSeed(123); // set.seed(123) in R
rbinom(2, 8, 0.5);
// -> Float64Array(2) [ 3, 5 ]  //same result as in R

setSeed(456); // set.seed(456) in R
rbinomOne(350, 0.5);
// -> 174  ( a single scalar )

UMD module removed

There is no UMD module from 2.0.0 onward.

Installation and usage

Minimal version of node required is 22.12.0.

npm i lib-r-math.js

lib-r-math.js supports the following module types:

ESM for use in observablehq

ObservableHQ works nicely with lib-r-math.js visualizing data generated by lib-r-math.js functions

Example: observableHQ-bessel-demo

<div style="font-size:12px">Output As Graphic: <img style="display:block" src="./bessel.jpg" width="480px" height="320px"> </div>

ESM for use as Browser client

<script type="module">
    import { BesselJ } from 'https://unpkg.dev/lib-r-math.js@latest/dist/web.esm.mjs';

    console.log(BesselJ(3, 0.4));
    //-> -0.30192051329163955
</script>

IIFE (immediately-invoked Function Expression) for use in Browser client

<script src="https://unpkg.dev/lib-r-math.js@latest/dist/web.iife.js"></script>
<script>
    const answ = window.R.BesselJ(3, 0.4);
    console.log(answ);
    //-> -0.30192051329163955
</script>

ESM for Node

import { BesselJ } from 'lib-r-math.js';

const answ = BesselJ(3, 0.4);
//-> -0.30192051329163955

COMMONJS for node

const { BesselJ } = require('lib-r-math.js');

const answ = BesselJ(3, 0.4);
//-> -0.30192051329163955

Table of Contents

Auxiliary functions

RNGkind

RNGkind is the analog to R's "RNGkind". This is how you select what RNG (normal and uniform) you use and the samplingKind (Rounding or Rejection) property

Follows closely the R implementation here

R console:

> RNGkind()
[1] "Mersenne-Twister" "Ahrens-Dieter"
[3] "Rejection"

Just like in R, calling RNGkind with no argument returns the currently active RNG's (uniform and normal) and sample kind (Rounding or Rejection)

Like in R, RNGkind optionally takes an argument of type RandomGenSet, after processing it will return the (adjusted) RandomGenSet indicating what RNG's and "kind of sampling" is being used.

Rjs typescript decl:

function RNGkind(options?: RandomGenSet): RandomGenSet;

Arguments:

  • options: an object of type RandomGenSet
    • options.uniform: string, specify name of uniform RNG to use.
    • options.normal: string, specify nam of normal RNG (shaper) to use
    • options.sampleKind: string, specify sample strategy to use

Typescript definition:

type RandomGenSet = {
    uniform?:
        | 'KNUTH_TAOCP'
        | 'KNUTH_TAOCP2002'
        | 'LECUYER_CMRG'
        | 'MARSAGLIA_MULTICARRY'
        | 'MERSENNE_TWISTER'
        | 'SUPER_DUPER'
        | 'WICHMANN_HILL';
    n

Related Skills

View on GitHub
GitHub Stars471
CategoryDevelopment
Updated11d ago
Forks31

Languages

TypeScript

Security Score

100/100

Audited on Mar 25, 2026

No findings