Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
7 changes: 5 additions & 2 deletions convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
14 changes: 13 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,16 @@ require("tap").test("ndarray-pack", function(t) {
t.equals(y.get(0, 0, 0), 1)

t.end()
})
})

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()
})