libRmath.js: rnorm crashes when generating a single sample

This is using version 1.0.76 installed from npm today. When getting a single sample from the normal distribution, an error is thrown:

const libR = require('lib-r-math.js');
libR.Normal().rnorm();
TypeError: r_func_1.seq(...)(...)(...).map is not a function
    at Object.rnorm (./node_modules/lib-r-math.js/dist/lib/libR.js:3905:41)
    at Object.rnorm (./node_modules/lib-r-math.js/dist/lib/libR.js:7503:54)
    at repl:1:15
    at ContextifyScript.Script.runInThisContext (vm.js:44:33)
    at REPLServer.defaultEval (repl.js:239:29)
    at bound (domain.js:301:14)
    at REPLServer.runBound [as eval] (domain.js:314:12)
    at REPLServer.onLine (repl.js:440:10)
    at emitOne (events.js:120:20)
    at REPLServer.emit (events.js:210:7)

In contrast, libR.Normal().rnorm(2) works.

Would you consider changing the functions to always return number[] instead of number | number[]? The fact that the return type isn’t known ahead of time will require a lot of extra coding and surely lead to a lot more run-time errors. More convenient yet would be to have two functions: one that returns number and one that returns number[].

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 20

Most upvoted comments

Regarding your comment:

To facilitate porting code to JS, the function works exactly like in R, If any of the functions return a single it will not be wrapped in an array. This is to make is a lot easier to port R code.

Aren’t single numbers and strings always 1-arrays in R?:

> 1
[1] 1
> class(1)
[1] "numeric"
> length(1)
[1] 1
> 1[0]
numeric(0)
> 1[1]
[1] 1
> 1[2]
[1] NA
> sapply(1, function(n) 2*n)
[1] 2

So shouldn’t returning vectors actually make porting easier?

Hi, Ellis excuse me, I actually missed this email up till now, libnmath only partially implements stuff from base and stats , and I have now made a giant todolist for all of base and stats package (hence my need for blas and lapack (partial, need only 400 routines out of 1963) , so duable, I can make a functional wrapper for you that always returns an array, meanwhile I will change the code, assume all functions will return arrays

Will now concentrate on the issues you raised for libRmath, sorry for the delay…

Hi Ellis, BLASJS (foundational library used by R, numpy, for matrix operations) ported to JS https://github.com/jacobbogers/blasjs

thumbs up for @ellis here {but then I’m an R corer, not a JS programmer at all}.

Thanks a ton, Jason. I’m quite excited about the possibilities that your library opens up. I do most of my coding in R and javascript, and this could definitely simplify some things a lot.

Fixed, pushed a new version 1.0.77 (use npm upgrade or git pull)

const libR = require('lib-r-math.js');
libR.Normal().rnorm();
//1.2629542848807933

Always force return array regardless

const libR = require('lib-r-math.js');
const { R: { asArray }, Normal } = libR;
const { rnorm } = Normal();

const rn = asArray( rnorm );
rn()
//[ 1.2629542848807933 ]
rn()
//[ -0.3262333607056494 ]
rn(5)
/*
[ 
  1.3297992629225006,
  1.2724293214294047,
  0.4146414344564082,
  -1.5399500419037095,
  -0.9285670347135381
]*/