Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow plain 'array' dtype #4 #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
22 changes: 15 additions & 7 deletions scratch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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<sz; ++i) {
buf[i] = 0
}
Expand All @@ -68,7 +76,7 @@ function ones(shape, dtype) {
stride[i] = sz
sz *= shape[i]
}
var buf = pool.malloc(sz, dtype)
var buf = poolMalloc(sz, dtype)
for(var i=0; i<sz; ++i) {
buf[i] = 1
}
Expand All @@ -88,7 +96,7 @@ function eye(shape, dtype) {
stride[i] = sz
sz *= shape[i]
}
var buf = pool.malloc(sz, dtype)
var buf = poolMalloc(sz, dtype)
for(i=0; i<sz; ++i) {
buf[i] = 0
}
Expand Down
83 changes: 44 additions & 39 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,65 @@
"use strict"

var pool = require("../scratch.js")
var tape = require('tape')

require("tape")("ndarray-scratch", function(t) {
var dtypes = ['double', 'array', 'float']

var i,j,k
tape("ndarray-scratch", function(t) {
dtypes.forEach(function(dtype) {

var x = pool.malloc([10,10])
var i,j,k

t.same(x.shape.slice(), [10,10])
t.same(x.stride.slice(), [10,1])
t.assert(x.data.length >= 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()
})