From a8b3bd9406f0cb0ddafa8fe880f17fed6b78331d Mon Sep 17 00:00:00 2001 From: Ricky Reusser Date: Sun, 3 Jan 2016 07:06:32 -0500 Subject: [PATCH 1/2] Allow plain 'array' dtype #4 --- scratch.js | 21 ++++++++----- test/test.js | 83 ++++++++++++++++++++++++++++------------------------ 2 files changed, 57 insertions(+), 47 deletions(-) diff --git a/scratch.js b/scratch.js index bb638d9..2cb666a 100644 --- a/scratch.js +++ b/scratch.js @@ -4,12 +4,17 @@ 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" - } - 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 +31,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 +54,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() }) From 0e387d54a5016cd13d3feb12287088c1049f18bc Mon Sep 17 00:00:00 2001 From: Ricky Reusser Date: Sun, 3 Jan 2016 07:13:05 -0500 Subject: [PATCH 2/2] Restore generic -> double fallback #5 --- scratch.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/scratch.js b/scratch.js index 2cb666a..ba432d8 100644 --- a/scratch.js +++ b/scratch.js @@ -14,6 +14,9 @@ function poolMalloc(sz, dtype) { function clone(array) { var dtype = array.dtype + if(array.dtype === "generic") { + dtype = 'double' + } var data = poolMalloc(array.size, dtype) var result = ndarray(data, array.shape) ops.assign(result, array)