diff --git a/README.md b/README.md index 3418f98..e5c40b6 100644 --- a/README.md +++ b/README.md @@ -19,11 +19,12 @@ Install npm install ndarray-pack -### `require("ndarray-pack")(nested_array[,out])` +### `require("ndarray-pack")(nested_array[,out, [Type]])` Converts the nested array into a packed ndarray. * `nested_array` is an array-of-arrays (ie a numeric.js array) * `out` is an optional ndarray that gets the result of unpacking `array` +* `Type` is an optional type. This is the underlying type of the generated ndarray, default to Float64Array **Returns** A packed ndarray representation of the nested arrays. diff --git a/convert.js b/convert.js index fcd9e62..595f77a 100644 --- a/convert.js +++ b/convert.js @@ -3,7 +3,10 @@ var ndarray = require("ndarray") var do_convert = require("./doConvert.js") -module.exports = function convert(arr, result) { +module.exports = function convert(arr, result, Type) { + if (typeof Type === 'undefined') { + Type = Float64Array; + } var shape = [], c = arr, sz = 1 while(Array.isArray(c)) { shape.push(c.length) @@ -14,7 +17,7 @@ module.exports = function convert(arr, result) { return ndarray() } if(!result) { - result = ndarray(new Float64Array(sz), shape) + result = ndarray(new Type(sz), shape) } do_convert(result, arr) return result diff --git a/test/test.js b/test/test.js index 2e62b68..13e91ea 100644 --- a/test/test.js +++ b/test/test.js @@ -22,4 +22,16 @@ require("tap").test("ndarray-pack", function(t) { t.equals(y.get(0, 0, 0), 1) t.end() -}) \ No newline at end of file +}) + +require("tap").test("ndarray-pack", function(t) { + + var x = [[1, 0, 1], + [0, 1, 1], + [0, 0, 1], + [1, 0, 0]] + + var y = require("../convert.js")(x, undefined, Float32Array) + t.type(y.data, Float32Array); + t.end() +})