scijs / ndarray-translate

Translate/shift ndarrays
MIT License
7 stars 3 forks source link

Crashes when passed an Array-backed ndarray #1

Open timoxley opened 9 years ago

timoxley commented 9 years ago

When passing an ndarray backed by a vanilla JS Array, translate will crash with an error "TypeError: Cannot read property 'get' of null".

See example below:

var ndarray = require('ndarray')
var zeros = require('zeros')
var ops = require('ops')
var translate = require('ndarray-translate')
var show = require('ndarray-show')

// create with zeros
var points = zeros([5, 5])
points.set(2, 2, 1)

translate(points, [1, 1]) // this works as expected
console.log(show((points))) // whoo

// now define the "same" datastructure using an Array
var points2 = ndarray([
  0, 0, 0, 0, 0,
  0, 0, 0, 0, 0,
  0, 0, 1, 0, 0,
  0, 0, 0, 0, 0,
  0, 0, 0, 0, 0,
], [5, 5])

console.log("equal", ops.equals(points, points2))  // apparently the data is equivalent…

translate(points2, [1, 1]) // …yet translate bombs
console.log(show((points2))) // never gets here
TypeError: Cannot read property 'get' of null
    at assign_cwise_loop_1s0m2ga (eval at generateCWiseOp (/Users/timoxley/Projects/tests/graphing/node_modules/ndarray-ops/node_modules/cwise-compiler/lib/compile.js:351:11), <anonymous>:8:10)
    at assign_cwise_thunk (eval at createThunk (/Users/timoxley/Projects/tests/graphing/node_modules/ndarray-ops/node_modules/cwise-compiler/lib/thunk.js:82:15), <anonymous>:9:59)
    at Object.assign_ndarrayops [as assign] (eval at makeOp (/Users/timoxley/Projects/tests/graphing/node_modules/ndarray-ops/ndarray-ops.js:48:17), <anonymous>:1:43)
    at translateZeroBC (/Users/timoxley/Projects/tests/graphing/node_modules/ndarray-translate/translate.js:47:7)
    at Object.<anonymous> (/Users/timoxley/Projects/tests/graphing/index.js:24:1)
    at Module._compile (module.js:431:26)
    at Object.Module._extensions..js (module.js:449:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Function.Module.runMain (module.js:472:10)

Error comes from this line returning null:

pool.malloc(q.size, arr.dtype)

Which seems to occur because arr.dtype is 'array' rather than one of the types listed in https://github.com/mikolalysenko/typedarray-pool#poolmallocn-dtype

timoxley commented 9 years ago

Hm, the ndarray readme does say:

array.dtype Returns a string representing the undelying data type of the ndarray. Excluding generic data stores these types are compatible with typedarray-pool.

Perhaps typedarray-pool should simply accept 'array' as a valid type and return: new Array(n).

rreusser commented 9 years ago

Thanks @timoxley. I'll take a look at this when I get a chance. Was going to start factoring out dtype operations into separate scijs repos so these things can be tackled consistently. This seems like maybe a straight-up bug though. Seems like that should work… I'll take a look when I get a chance. Thanks for the bug report!