diff --git a/scratch.js b/scratch.js index bb638d9..ba432d8 100644 --- a/scratch.js +++ b/scratch.js @@ -4,12 +4,20 @@ var ndarray = require("ndarray") var ops = require("ndarray-ops") var pool = require("typedarray-pool") +function poolMalloc(sz, dtype) { + if (dtype==='array') { + return new Array(sz) + } else { + return pool.malloc(sz,dtype) + } +} + function clone(array) { var dtype = array.dtype - if(dtype === "generic" || dtype === "array") { - dtype = "double" + if(array.dtype === "generic") { + dtype = 'double' } - var data = pool.malloc(array.size, dtype) + var data = poolMalloc(array.size, dtype) var result = ndarray(data, array.shape) ops.assign(result, array) return result @@ -26,7 +34,7 @@ function malloc(shape, dtype) { stride[i] = sz sz *= shape[i] } - return ndarray(pool.malloc(sz, dtype), shape, stride, 0) + return ndarray(poolMalloc(sz, dtype), shape, stride, 0) } exports.malloc = malloc @@ -49,7 +57,7 @@ function zeros(shape, dtype) { stride[i] = sz sz *= shape[i] } - var buf = pool.malloc(sz, dtype) + var buf = poolMalloc(sz, dtype) for(var i=0; i= 10*10) + var x = pool.malloc([10,10], dtype) - var y = pool.clone(x) - t.same(x.shape, y.shape) - t.same(x.stride, y.stride) - t.same(x.data.length, y.data.length) - t.same(x.dtype, y.dtype) + t.same(x.shape.slice(), [10,10]) + t.same(x.stride.slice(), [10,1]) + t.assert(x.data.length >= 10*10) - pool.free(x) - pool.free(y) + var y = pool.clone(x) + t.same(x.shape, y.shape) + t.same(x.stride, y.stride) + t.same(x.data.length, y.data.length) + t.same(x.dtype, y.dtype) - var zeros = pool.zeros([10,10]) - t.same(zeros.shape, x.shape) + pool.free(x) + pool.free(y) - for(i=0; i<10; i++) { - for(j=0; j<10; j++) { - t.equal(zeros.get(i,j), 0) + var zeros = pool.zeros([10,10]) + t.same(zeros.shape, x.shape) + + for(i=0; i<10; i++) { + for(j=0; j<10; j++) { + t.equal(zeros.get(i,j), 0) + } } - } - var ones = pool.ones([10,10]) - t.same(ones.shape, x.shape) - for(i=0; i<10; i++) { - for(j=0; j<10; j++) { - t.equal(ones.get(i,j), 1) + var ones = pool.ones([10,10]) + t.same(ones.shape, x.shape) + for(i=0; i<10; i++) { + for(j=0; j<10; j++) { + t.equal(ones.get(i,j), 1) + } } - } - var eye = pool.eye([10,10]) - t.same(eye.shape, x.shape) - for(i=0; i<10; i++) { - for(j=0; j<10; j++) { - t.equal(eye.get(i,j), i===j ? 1 : 0) + var eye = pool.eye([10,10]) + t.same(eye.shape, x.shape) + for(i=0; i<10; i++) { + for(j=0; j<10; j++) { + t.equal(eye.get(i,j), i===j ? 1 : 0) + } } - } - - var eye3 = pool.eye([3,4,5]) - t.same(eye3.shape, [3,4,5]) - for(i=0; i<3; i++) { - for(j=0; j<4; j++) { - for(k=0; k<5; k++) { - t.equal(eye3.get(i,j,k), (i===j && j===k) ? 1 : 0) + + var eye3 = pool.eye([3,4,5]) + t.same(eye3.shape, [3,4,5]) + for(i=0; i<3; i++) { + for(j=0; j<4; j++) { + for(k=0; k<5; k++) { + t.equal(eye3.get(i,j,k), (i===j && j===k) ? 1 : 0) + } } } - } + }) t.end() })