diff --git a/.gitignore b/.gitignore index ddc9ab3..c7b283a 100644 --- a/.gitignore +++ b/.gitignore @@ -12,5 +12,6 @@ logs results npm-debug.log +package-lock.json node_modules/* *.DS_Store \ No newline at end of file diff --git a/README.md b/README.md index 259fa0d..4be7064 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,13 @@ Modular multidimensional arrays for JavaScript. ##### Coming from MATLAB or numpy? See: [scijs/ndarray for MATLAB users](https://github.com/scijs/scijs-ndarray-for-matlab-users) ##### [Big list of ndarray modules](https://github.com/scijs/ndarray/wiki/ndarray-module-list#core-module) +
+ + Maintained by Plotly + +
+ + Introduction ============ `ndarrays` provide higher dimensional views of 1D arrays. For example, here is how you can turn a length 4 typed array into an nd-array: diff --git a/ndarray.js b/ndarray.js index 43c9b5a..44bcbbc 100644 --- a/ndarray.js +++ b/ndarray.js @@ -1,4 +1,3 @@ -var iota = require("iota-array") var isBuffer = require("is-buffer") var hasTypedArrays = ((typeof Float64Array) !== "undefined") @@ -22,234 +21,883 @@ function order() { return result } -function compileConstructor(dtype, dimension) { - var className = ["View", dimension, "d", dtype].join("") - if(dimension < 0) { - className = "View_Nil" + dtype - } - var useGetters = (dtype === "generic") - - if(dimension === -1) { - //Special case for trivial arrays - var code = - "function "+className+"(a){this.data=a;};\ -var proto="+className+".prototype;\ -proto.dtype='"+dtype+"';\ -proto.index=function(){return -1};\ -proto.size=0;\ -proto.dimension=-1;\ -proto.shape=proto.stride=proto.order=[];\ -proto.lo=proto.hi=proto.transpose=proto.step=\ -function(){return new "+className+"(this.data);};\ -proto.get=proto.set=function(){};\ -proto.pick=function(){return null};\ -return function construct_"+className+"(a){return new "+className+"(a);}" - var procedure = new Function(code) - return procedure() - } else if(dimension === 0) { - //Special case for 0d arrays - var code = - "function "+className+"(a,d) {\ -this.data = a;\ -this.offset = d\ -};\ -var proto="+className+".prototype;\ -proto.dtype='"+dtype+"';\ -proto.index=function(){return this.offset};\ -proto.dimension=0;\ -proto.size=1;\ -proto.shape=\ -proto.stride=\ -proto.order=[];\ -proto.lo=\ -proto.hi=\ -proto.transpose=\ -proto.step=function "+className+"_copy() {\ -return new "+className+"(this.data,this.offset)\ -};\ -proto.pick=function "+className+"_pick(){\ -return TrivialArray(this.data);\ -};\ -proto.valueOf=proto.get=function "+className+"_get(){\ -return "+(useGetters ? "this.data.get(this.offset)" : "this.data[this.offset]")+ -"};\ -proto.set=function "+className+"_set(v){\ -return "+(useGetters ? "this.data.set(this.offset,v)" : "this.data[this.offset]=v")+"\ -};\ -return function construct_"+className+"(a,b,c,d){return new "+className+"(a,d)}" - var procedure = new Function("TrivialArray", code) - return procedure(CACHED_CONSTRUCTORS[dtype][0]) - } - - var code = ["'use strict'"] - - //Create constructor for view - var indices = iota(dimension) - var args = indices.map(function(i) { return "i"+i }) - var index_str = "this.offset+" + indices.map(function(i) { - return "this.stride[" + i + "]*i" + i - }).join("+") - var shapeArg = indices.map(function(i) { - return "b"+i - }).join(",") - var strideArg = indices.map(function(i) { - return "c"+i - }).join(",") - code.push( - "function "+className+"(a," + shapeArg + "," + strideArg + ",d){this.data=a", - "this.shape=[" + shapeArg + "]", - "this.stride=[" + strideArg + "]", - "this.offset=d|0}", - "var proto="+className+".prototype", - "proto.dtype='"+dtype+"'", - "proto.dimension="+dimension) - - //view.size: - code.push("Object.defineProperty(proto,'size',{get:function "+className+"_size(){\ -return "+indices.map(function(i) { return "this.shape["+i+"]" }).join("*"), -"}})") - - //view.order: - if(dimension === 1) { - code.push("proto.order=[0]") - } else { - code.push("Object.defineProperty(proto,'order',{get:") - if(dimension < 4) { - code.push("function "+className+"_order(){") - if(dimension === 2) { - code.push("return (Math.abs(this.stride[0])>Math.abs(this.stride[1]))?[1,0]:[0,1]}})") - } else if(dimension === 3) { - code.push( -"var s0=Math.abs(this.stride[0]),s1=Math.abs(this.stride[1]),s2=Math.abs(this.stride[2]);\ -if(s0>s1){\ -if(s1>s2){\ -return [2,1,0];\ -}else if(s0>s2){\ -return [1,2,0];\ -}else{\ -return [1,0,2];\ -}\ -}else if(s0>s2){\ -return [2,0,1];\ -}else if(s2>s1){\ -return [0,1,2];\ -}else{\ -return [0,2,1];\ -}}})") - } - } else { - code.push("ORDER})") +var allFns = { + // Special case for trivial arrays + T: function (dtype) { + function View(a) { + this.data = a } - } - - //view.set(i0, ..., v): - code.push( -"proto.set=function "+className+"_set("+args.join(",")+",v){") - if(useGetters) { - code.push("return this.data.set("+index_str+",v)}") - } else { - code.push("return this.data["+index_str+"]=v}") - } - - //view.get(i0, ...): - code.push("proto.get=function "+className+"_get("+args.join(",")+"){") - if(useGetters) { - code.push("return this.data.get("+index_str+")}") - } else { - code.push("return this.data["+index_str+"]}") - } - - //view.index: - code.push( - "proto.index=function "+className+"_index(", args.join(), "){return "+index_str+"}") + var proto = View.prototype + proto.dtype = dtype + proto.index = function () { + return -1 + } + proto.size = 0 + proto.dimension = -1 + proto.shape = proto.stride = proto.order = [] + proto.lo = + proto.hi = + proto.transpose = + proto.step = + function () { + return new View(this.data) + } + proto.get = proto.set = function () {} + proto.pick = function () { + return null + } + return function construct(a) { + return new View(a) + } + }, - //view.hi(): - code.push("proto.hi=function "+className+"_hi("+args.join(",")+"){return new "+className+"(this.data,"+ - indices.map(function(i) { - return ["(typeof i",i,"!=='number'||i",i,"<0)?this.shape[", i, "]:i", i,"|0"].join("") - }).join(",")+","+ - indices.map(function(i) { - return "this.stride["+i + "]" - }).join(",")+",this.offset)}") + // Special case for 0d arrays + 0: function (dtype, TrivialArray) { + function View(a, d) { + this.data = a + this.offset = d + } + var proto = View.prototype + proto.dtype = dtype + proto.index = function () { + return this.offset + } + proto.dimension = 0 + proto.size = 1 + proto.shape = proto.stride = proto.order = [] + proto.lo = + proto.hi = + proto.transpose = + proto.step = + function copy() { + return new View(this.data, this.offset) + } + proto.pick = function pick() { + return TrivialArray(this.data) + } + proto.valueOf = proto.get = function get() { + return dtype === "generic" ? this.data.get(this.offset) : this.data[this.offset] + } + proto.set = function set(v) { + return dtype === "generic" ? this.data.set(this.offset, v) : (this.data[this.offset] = v) + } + return function construct(a, b, c, d) { + return new View(a, d) + } + }, - //view.lo(): - var a_vars = indices.map(function(i) { return "a"+i+"=this.shape["+i+"]" }) - var c_vars = indices.map(function(i) { return "c"+i+"=this.stride["+i+"]" }) - code.push("proto.lo=function "+className+"_lo("+args.join(",")+"){var b=this.offset,d=0,"+a_vars.join(",")+","+c_vars.join(",")) - for(var i=0; i=0){\ -d=i"+i+"|0;\ -b+=c"+i+"*d;\ -a"+i+"-=d}") - } - code.push("return new "+className+"(this.data,"+ - indices.map(function(i) { - return "a"+i - }).join(",")+","+ - indices.map(function(i) { - return "c"+i - }).join(",")+",b)}") + 1: function (dtype, CTOR_LIST, ORDER) { + function View(a, b0, c0, d) { + this.data = a + this.shape = [b0] + this.stride = [c0] + this.offset = d | 0 + } + var proto = View.prototype + proto.dtype = dtype + proto.dimension = 1 + Object.defineProperty(proto, "size", { + get: function size() { + return this.shape[0] + }, + }) + proto.order = [0] + proto.set = function set(i0, v) { + return dtype === "generic" + ? this.data.set(this.offset + this.stride[0] * i0, v) + : (this.data[this.offset + this.stride[0] * i0] = v) + } + proto.get = function get(i0) { + return dtype === "generic" + ? this.data.get(this.offset + this.stride[0] * i0) + : this.data[this.offset + this.stride[0] * i0] + } + proto.index = function index(i0) { + return this.offset + this.stride[0] * i0 + } + proto.hi = function hi(i0) { + return new View(this.data, typeof i0 !== "number" || i0 < 0 ? this.shape[0] : i0 | 0, this.stride[0], this.offset) + } + proto.lo = function lo(i0) { + var b = this.offset, + d = 0, + a0 = this.shape[0], + c0 = this.stride[0] + if (typeof i0 === "number" && i0 >= 0) { + d = i0 | 0 + b += c0 * d + a0 -= d + } + return new View(this.data, a0, c0, b) + } + proto.step = function step(i0) { + var a0 = this.shape[0], + b0 = this.stride[0], + c = this.offset, + d = 0, + ceil = Math.ceil + if (typeof i0 === "number") { + d = i0 | 0 + if (d < 0) { + c += b0 * (a0 - 1) + a0 = ceil(-a0 / d) + } else { + a0 = ceil(a0 / d) + } + b0 *= d + } + return new View(this.data, a0, b0, c) + } + proto.transpose = function transpose(i0) { + i0 = i0 === undefined ? 0 : i0 | 0 + var a = this.shape, + b = this.stride + return new View(this.data, a[i0], b[i0], this.offset) + } + proto.pick = function pick(i0) { + var a = [], + b = [], + c = this.offset + if (typeof i0 === "number" && i0 >= 0) { + c = (c + this.stride[0] * i0) | 0 + } else { + a.push(this.shape[0]) + b.push(this.stride[0]) + } + var ctor = CTOR_LIST[a.length + 1] + return ctor(this.data, a, b, c) + } + return function construct(data, shape, stride, offset) { + return new View(data, shape[0], stride[0], offset) + } + }, + 2: function (dtype, CTOR_LIST, ORDER) { + function View(a, b0, b1, c0, c1, d) { + this.data = a + this.shape = [b0, b1] + this.stride = [c0, c1] + this.offset = d | 0 + } + var proto = View.prototype + proto.dtype = dtype + proto.dimension = 2 + Object.defineProperty(proto, "size", { + get: function size() { + return this.shape[0] * this.shape[1] + }, + }) + Object.defineProperty(proto, "order", { + get: function order() { + return Math.abs(this.stride[0]) > Math.abs(this.stride[1]) ? [1, 0] : [0, 1] + }, + }) + proto.set = function set(i0, i1, v) { + return dtype === "generic" + ? this.data.set(this.offset + this.stride[0] * i0 + this.stride[1] * i1, v) + : (this.data[this.offset + this.stride[0] * i0 + this.stride[1] * i1] = v) + } + proto.get = function get(i0, i1) { + return dtype === "generic" + ? this.data.get(this.offset + this.stride[0] * i0 + this.stride[1] * i1) + : this.data[this.offset + this.stride[0] * i0 + this.stride[1] * i1] + } + proto.index = function index(i0, i1) { + return this.offset + this.stride[0] * i0 + this.stride[1] * i1 + } + proto.hi = function hi(i0, i1) { + return new View( + this.data, + typeof i0 !== "number" || i0 < 0 ? this.shape[0] : i0 | 0, + typeof i1 !== "number" || i1 < 0 ? this.shape[1] : i1 | 0, + this.stride[0], + this.stride[1], + this.offset + ) + } + proto.lo = function lo(i0, i1) { + var b = this.offset, + d = 0, + a0 = this.shape[0], + a1 = this.shape[1], + c0 = this.stride[0], + c1 = this.stride[1] + if (typeof i0 === "number" && i0 >= 0) { + d = i0 | 0 + b += c0 * d + a0 -= d + } + if (typeof i1 === "number" && i1 >= 0) { + d = i1 | 0 + b += c1 * d + a1 -= d + } + return new View(this.data, a0, a1, c0, c1, b) + } + proto.step = function step(i0, i1) { + var a0 = this.shape[0], + a1 = this.shape[1], + b0 = this.stride[0], + b1 = this.stride[1], + c = this.offset, + d = 0, + ceil = Math.ceil + if (typeof i0 === "number") { + d = i0 | 0 + if (d < 0) { + c += b0 * (a0 - 1) + a0 = ceil(-a0 / d) + } else { + a0 = ceil(a0 / d) + } + b0 *= d + } + if (typeof i1 === "number") { + d = i1 | 0 + if (d < 0) { + c += b1 * (a1 - 1) + a1 = ceil(-a1 / d) + } else { + a1 = ceil(a1 / d) + } + b1 *= d + } + return new View(this.data, a0, a1, b0, b1, c) + } + proto.transpose = function transpose(i0, i1) { + i0 = i0 === undefined ? 0 : i0 | 0 + i1 = i1 === undefined ? 1 : i1 | 0 + var a = this.shape, + b = this.stride + return new View(this.data, a[i0], a[i1], b[i0], b[i1], this.offset) + } + proto.pick = function pick(i0, i1) { + var a = [], + b = [], + c = this.offset + if (typeof i0 === "number" && i0 >= 0) { + c = (c + this.stride[0] * i0) | 0 + } else { + a.push(this.shape[0]) + b.push(this.stride[0]) + } + if (typeof i1 === "number" && i1 >= 0) { + c = (c + this.stride[1] * i1) | 0 + } else { + a.push(this.shape[1]) + b.push(this.stride[1]) + } + var ctor = CTOR_LIST[a.length + 1] + return ctor(this.data, a, b, c) + } + return function construct(data, shape, stride, offset) { + return new View(data, shape[0], shape[1], stride[0], stride[1], offset) + } + }, + 3: function (dtype, CTOR_LIST, ORDER) { + function View(a, b0, b1, b2, c0, c1, c2, d) { + this.data = a + this.shape = [b0, b1, b2] + this.stride = [c0, c1, c2] + this.offset = d | 0 + } + var proto = View.prototype + proto.dtype = dtype + proto.dimension = 3 + Object.defineProperty(proto, "size", { + get: function size() { + return this.shape[0] * this.shape[1] * this.shape[2] + }, + }) + Object.defineProperty(proto, "order", { + get: function order() { + var s0 = Math.abs(this.stride[0]), + s1 = Math.abs(this.stride[1]), + s2 = Math.abs(this.stride[2]) + if (s0 > s1) { + if (s1 > s2) { + return [2, 1, 0] + } else if (s0 > s2) { + return [1, 2, 0] + } else { + return [1, 0, 2] + } + } else if (s0 > s2) { + return [2, 0, 1] + } else if (s2 > s1) { + return [0, 1, 2] + } else { + return [0, 2, 1] + } + }, + }) + proto.set = function set(i0, i1, i2, v) { + return dtype === "generic" + ? this.data.set(this.offset + this.stride[0] * i0 + this.stride[1] * i1 + this.stride[2] * i2, v) + : (this.data[this.offset + this.stride[0] * i0 + this.stride[1] * i1 + this.stride[2] * i2] = v) + } + proto.get = function get(i0, i1, i2) { + return dtype === "generic" + ? this.data.get(this.offset + this.stride[0] * i0 + this.stride[1] * i1 + this.stride[2] * i2) + : this.data[this.offset + this.stride[0] * i0 + this.stride[1] * i1 + this.stride[2] * i2] + } + proto.index = function index(i0, i1, i2) { + return this.offset + this.stride[0] * i0 + this.stride[1] * i1 + this.stride[2] * i2 + } + proto.hi = function hi(i0, i1, i2) { + return new View( + this.data, + typeof i0 !== "number" || i0 < 0 ? this.shape[0] : i0 | 0, + typeof i1 !== "number" || i1 < 0 ? this.shape[1] : i1 | 0, + typeof i2 !== "number" || i2 < 0 ? this.shape[2] : i2 | 0, + this.stride[0], + this.stride[1], + this.stride[2], + this.offset + ) + } + proto.lo = function lo(i0, i1, i2) { + var b = this.offset, + d = 0, + a0 = this.shape[0], + a1 = this.shape[1], + a2 = this.shape[2], + c0 = this.stride[0], + c1 = this.stride[1], + c2 = this.stride[2] + if (typeof i0 === "number" && i0 >= 0) { + d = i0 | 0 + b += c0 * d + a0 -= d + } + if (typeof i1 === "number" && i1 >= 0) { + d = i1 | 0 + b += c1 * d + a1 -= d + } + if (typeof i2 === "number" && i2 >= 0) { + d = i2 | 0 + b += c2 * d + a2 -= d + } + return new View(this.data, a0, a1, a2, c0, c1, c2, b) + } + proto.step = function step(i0, i1, i2) { + var a0 = this.shape[0], + a1 = this.shape[1], + a2 = this.shape[2], + b0 = this.stride[0], + b1 = this.stride[1], + b2 = this.stride[2], + c = this.offset, + d = 0, + ceil = Math.ceil + if (typeof i0 === "number") { + d = i0 | 0 + if (d < 0) { + c += b0 * (a0 - 1) + a0 = ceil(-a0 / d) + } else { + a0 = ceil(a0 / d) + } + b0 *= d + } + if (typeof i1 === "number") { + d = i1 | 0 + if (d < 0) { + c += b1 * (a1 - 1) + a1 = ceil(-a1 / d) + } else { + a1 = ceil(a1 / d) + } + b1 *= d + } + if (typeof i2 === "number") { + d = i2 | 0 + if (d < 0) { + c += b2 * (a2 - 1) + a2 = ceil(-a2 / d) + } else { + a2 = ceil(a2 / d) + } + b2 *= d + } + return new View(this.data, a0, a1, a2, b0, b1, b2, c) + } + proto.transpose = function transpose(i0, i1, i2) { + i0 = i0 === undefined ? 0 : i0 | 0 + i1 = i1 === undefined ? 1 : i1 | 0 + i2 = i2 === undefined ? 2 : i2 | 0 + var a = this.shape, + b = this.stride + return new View(this.data, a[i0], a[i1], a[i2], b[i0], b[i1], b[i2], this.offset) + } + proto.pick = function pick(i0, i1, i2) { + var a = [], + b = [], + c = this.offset + if (typeof i0 === "number" && i0 >= 0) { + c = (c + this.stride[0] * i0) | 0 + } else { + a.push(this.shape[0]) + b.push(this.stride[0]) + } + if (typeof i1 === "number" && i1 >= 0) { + c = (c + this.stride[1] * i1) | 0 + } else { + a.push(this.shape[1]) + b.push(this.stride[1]) + } + if (typeof i2 === "number" && i2 >= 0) { + c = (c + this.stride[2] * i2) | 0 + } else { + a.push(this.shape[2]) + b.push(this.stride[2]) + } + var ctor = CTOR_LIST[a.length + 1] + return ctor(this.data, a, b, c) + } + return function construct(data, shape, stride, offset) { + return new View(data, shape[0], shape[1], shape[2], stride[0], stride[1], stride[2], offset) + } + }, + 4: function (dtype, CTOR_LIST, ORDER) { + function View(a, b0, b1, b2, b3, c0, c1, c2, c3, d) { + this.data = a + this.shape = [b0, b1, b2, b3] + this.stride = [c0, c1, c2, c3] + this.offset = d | 0 + } + var proto = View.prototype + proto.dtype = dtype + proto.dimension = 4 + Object.defineProperty(proto, "size", { + get: function size() { + return this.shape[0] * this.shape[1] * this.shape[2] * this.shape[3] + }, + }) + Object.defineProperty(proto, "order", { get: ORDER }) + proto.set = function set(i0, i1, i2, i3, v) { + return dtype === "generic" + ? this.data.set(this.offset + this.stride[0] * i0 + this.stride[1] * i1 + this.stride[2] * i2 + this.stride[3] * i3, v) + : (this.data[this.offset + this.stride[0] * i0 + this.stride[1] * i1 + this.stride[2] * i2 + this.stride[3] * i3] = v) + } + proto.get = function get(i0, i1, i2, i3) { + return dtype === "generic" + ? this.data.get(this.offset + this.stride[0] * i0 + this.stride[1] * i1 + this.stride[2] * i2 + this.stride[3] * i3) + : this.data[this.offset + this.stride[0] * i0 + this.stride[1] * i1 + this.stride[2] * i2 + this.stride[3] * i3] + } + proto.index = function index(i0, i1, i2, i3) { + return this.offset + this.stride[0] * i0 + this.stride[1] * i1 + this.stride[2] * i2 + this.stride[3] * i3 + } + proto.hi = function hi(i0, i1, i2, i3) { + return new View( + this.data, + typeof i0 !== "number" || i0 < 0 ? this.shape[0] : i0 | 0, + typeof i1 !== "number" || i1 < 0 ? this.shape[1] : i1 | 0, + typeof i2 !== "number" || i2 < 0 ? this.shape[2] : i2 | 0, + typeof i3 !== "number" || i3 < 0 ? this.shape[3] : i3 | 0, + this.stride[0], + this.stride[1], + this.stride[2], + this.stride[3], + this.offset + ) + } + proto.lo = function lo(i0, i1, i2, i3) { + var b = this.offset, + d = 0, + a0 = this.shape[0], + a1 = this.shape[1], + a2 = this.shape[2], + a3 = this.shape[3], + c0 = this.stride[0], + c1 = this.stride[1], + c2 = this.stride[2], + c3 = this.stride[3] + if (typeof i0 === "number" && i0 >= 0) { + d = i0 | 0 + b += c0 * d + a0 -= d + } + if (typeof i1 === "number" && i1 >= 0) { + d = i1 | 0 + b += c1 * d + a1 -= d + } + if (typeof i2 === "number" && i2 >= 0) { + d = i2 | 0 + b += c2 * d + a2 -= d + } + if (typeof i3 === "number" && i3 >= 0) { + d = i3 | 0 + b += c3 * d + a3 -= d + } + return new View(this.data, a0, a1, a2, a3, c0, c1, c2, c3, b) + } + proto.step = function step(i0, i1, i2, i3) { + var a0 = this.shape[0], + a1 = this.shape[1], + a2 = this.shape[2], + a3 = this.shape[3], + b0 = this.stride[0], + b1 = this.stride[1], + b2 = this.stride[2], + b3 = this.stride[3], + c = this.offset, + d = 0, + ceil = Math.ceil + if (typeof i0 === "number") { + d = i0 | 0 + if (d < 0) { + c += b0 * (a0 - 1) + a0 = ceil(-a0 / d) + } else { + a0 = ceil(a0 / d) + } + b0 *= d + } + if (typeof i1 === "number") { + d = i1 | 0 + if (d < 0) { + c += b1 * (a1 - 1) + a1 = ceil(-a1 / d) + } else { + a1 = ceil(a1 / d) + } + b1 *= d + } + if (typeof i2 === "number") { + d = i2 | 0 + if (d < 0) { + c += b2 * (a2 - 1) + a2 = ceil(-a2 / d) + } else { + a2 = ceil(a2 / d) + } + b2 *= d + } + if (typeof i3 === "number") { + d = i3 | 0 + if (d < 0) { + c += b3 * (a3 - 1) + a3 = ceil(-a3 / d) + } else { + a3 = ceil(a3 / d) + } + b3 *= d + } + return new View(this.data, a0, a1, a2, a3, b0, b1, b2, b3, c) + } + proto.transpose = function transpose(i0, i1, i2, i3) { + i0 = i0 === undefined ? 0 : i0 | 0 + i1 = i1 === undefined ? 1 : i1 | 0 + i2 = i2 === undefined ? 2 : i2 | 0 + i3 = i3 === undefined ? 3 : i3 | 0 + var a = this.shape, + b = this.stride + return new View(this.data, a[i0], a[i1], a[i2], a[i3], b[i0], b[i1], b[i2], b[i3], this.offset) + } + proto.pick = function pick(i0, i1, i2, i3) { + var a = [], + b = [], + c = this.offset + if (typeof i0 === "number" && i0 >= 0) { + c = (c + this.stride[0] * i0) | 0 + } else { + a.push(this.shape[0]) + b.push(this.stride[0]) + } + if (typeof i1 === "number" && i1 >= 0) { + c = (c + this.stride[1] * i1) | 0 + } else { + a.push(this.shape[1]) + b.push(this.stride[1]) + } + if (typeof i2 === "number" && i2 >= 0) { + c = (c + this.stride[2] * i2) | 0 + } else { + a.push(this.shape[2]) + b.push(this.stride[2]) + } + if (typeof i3 === "number" && i3 >= 0) { + c = (c + this.stride[3] * i3) | 0 + } else { + a.push(this.shape[3]) + b.push(this.stride[3]) + } + var ctor = CTOR_LIST[a.length + 1] + return ctor(this.data, a, b, c) + } + return function construct(data, shape, stride, offset) { + return new View(data, shape[0], shape[1], shape[2], shape[3], stride[0], stride[1], stride[2], stride[3], offset) + } + }, + 5: function anonymous(dtype, CTOR_LIST, ORDER) { + function View(a, b0, b1, b2, b3, b4, c0, c1, c2, c3, c4, d) { + this.data = a + this.shape = [b0, b1, b2, b3, b4] + this.stride = [c0, c1, c2, c3, c4] + this.offset = d | 0 + } + var proto = View.prototype + proto.dtype = dtype + proto.dimension = 5 + Object.defineProperty(proto, "size", { + get: function size() { + return this.shape[0] * this.shape[1] * this.shape[2] * this.shape[3] * this.shape[4] + }, + }) + Object.defineProperty(proto, "order", { get: ORDER }) + proto.set = function set(i0, i1, i2, i3, i4, v) { + return dtype === "generic" + ? this.data.set( + this.offset + + this.stride[0] * i0 + + this.stride[1] * i1 + + this.stride[2] * i2 + + this.stride[3] * i3 + + this.stride[4] * i4, + v + ) + : (this.data[ + this.offset + + this.stride[0] * i0 + + this.stride[1] * i1 + + this.stride[2] * i2 + + this.stride[3] * i3 + + this.stride[4] * i4 + ] = v) + } + proto.get = function get(i0, i1, i2, i3, i4) { + return dtype === "generic" + ? this.data.get( + this.offset + + this.stride[0] * i0 + + this.stride[1] * i1 + + this.stride[2] * i2 + + this.stride[3] * i3 + + this.stride[4] * i4 + ) + : this.data[ + this.offset + + this.stride[0] * i0 + + this.stride[1] * i1 + + this.stride[2] * i2 + + this.stride[3] * i3 + + this.stride[4] * i4 + ] + } + proto.index = function index(i0, i1, i2, i3, i4) { + return ( + this.offset + + this.stride[0] * i0 + + this.stride[1] * i1 + + this.stride[2] * i2 + + this.stride[3] * i3 + + this.stride[4] * i4 + ) + } + proto.hi = function hi(i0, i1, i2, i3, i4) { + return new View( + this.data, + typeof i0 !== "number" || i0 < 0 ? this.shape[0] : i0 | 0, + typeof i1 !== "number" || i1 < 0 ? this.shape[1] : i1 | 0, + typeof i2 !== "number" || i2 < 0 ? this.shape[2] : i2 | 0, + typeof i3 !== "number" || i3 < 0 ? this.shape[3] : i3 | 0, + typeof i4 !== "number" || i4 < 0 ? this.shape[4] : i4 | 0, + this.stride[0], + this.stride[1], + this.stride[2], + this.stride[3], + this.stride[4], + this.offset + ) + } + proto.lo = function lo(i0, i1, i2, i3, i4) { + var b = this.offset, + d = 0, + a0 = this.shape[0], + a1 = this.shape[1], + a2 = this.shape[2], + a3 = this.shape[3], + a4 = this.shape[4], + c0 = this.stride[0], + c1 = this.stride[1], + c2 = this.stride[2], + c3 = this.stride[3], + c4 = this.stride[4] + if (typeof i0 === "number" && i0 >= 0) { + d = i0 | 0 + b += c0 * d + a0 -= d + } + if (typeof i1 === "number" && i1 >= 0) { + d = i1 | 0 + b += c1 * d + a1 -= d + } + if (typeof i2 === "number" && i2 >= 0) { + d = i2 | 0 + b += c2 * d + a2 -= d + } + if (typeof i3 === "number" && i3 >= 0) { + d = i3 | 0 + b += c3 * d + a3 -= d + } + if (typeof i4 === "number" && i4 >= 0) { + d = i4 | 0 + b += c4 * d + a4 -= d + } + return new View(this.data, a0, a1, a2, a3, a4, c0, c1, c2, c3, c4, b) + } + proto.step = function step(i0, i1, i2, i3, i4) { + var a0 = this.shape[0], + a1 = this.shape[1], + a2 = this.shape[2], + a3 = this.shape[3], + a4 = this.shape[4], + b0 = this.stride[0], + b1 = this.stride[1], + b2 = this.stride[2], + b3 = this.stride[3], + b4 = this.stride[4], + c = this.offset, + d = 0, + ceil = Math.ceil + if (typeof i0 === "number") { + d = i0 | 0 + if (d < 0) { + c += b0 * (a0 - 1) + a0 = ceil(-a0 / d) + } else { + a0 = ceil(a0 / d) + } + b0 *= d + } + if (typeof i1 === "number") { + d = i1 | 0 + if (d < 0) { + c += b1 * (a1 - 1) + a1 = ceil(-a1 / d) + } else { + a1 = ceil(a1 / d) + } + b1 *= d + } + if (typeof i2 === "number") { + d = i2 | 0 + if (d < 0) { + c += b2 * (a2 - 1) + a2 = ceil(-a2 / d) + } else { + a2 = ceil(a2 / d) + } + b2 *= d + } + if (typeof i3 === "number") { + d = i3 | 0 + if (d < 0) { + c += b3 * (a3 - 1) + a3 = ceil(-a3 / d) + } else { + a3 = ceil(a3 / d) + } + b3 *= d + } + if (typeof i4 === "number") { + d = i4 | 0 + if (d < 0) { + c += b4 * (a4 - 1) + a4 = ceil(-a4 / d) + } else { + a4 = ceil(a4 / d) + } + b4 *= d + } + return new View(this.data, a0, a1, a2, a3, a4, b0, b1, b2, b3, b4, c) + } + proto.transpose = function transpose(i0, i1, i2, i3, i4) { + i0 = i0 === undefined ? 0 : i0 | 0 + i1 = i1 === undefined ? 1 : i1 | 0 + i2 = i2 === undefined ? 2 : i2 | 0 + i3 = i3 === undefined ? 3 : i3 | 0 + i4 = i4 === undefined ? 4 : i4 | 0 + var a = this.shape, + b = this.stride + return new View(this.data, a[i0], a[i1], a[i2], a[i3], a[i4], b[i0], b[i1], b[i2], b[i3], b[i4], this.offset) + } + proto.pick = function pick(i0, i1, i2, i3, i4) { + var a = [], + b = [], + c = this.offset + if (typeof i0 === "number" && i0 >= 0) { + c = (c + this.stride[0] * i0) | 0 + } else { + a.push(this.shape[0]) + b.push(this.stride[0]) + } + if (typeof i1 === "number" && i1 >= 0) { + c = (c + this.stride[1] * i1) | 0 + } else { + a.push(this.shape[1]) + b.push(this.stride[1]) + } + if (typeof i2 === "number" && i2 >= 0) { + c = (c + this.stride[2] * i2) | 0 + } else { + a.push(this.shape[2]) + b.push(this.stride[2]) + } + if (typeof i3 === "number" && i3 >= 0) { + c = (c + this.stride[3] * i3) | 0 + } else { + a.push(this.shape[3]) + b.push(this.stride[3]) + } + if (typeof i4 === "number" && i4 >= 0) { + c = (c + this.stride[4] * i4) | 0 + } else { + a.push(this.shape[4]) + b.push(this.stride[4]) + } + var ctor = CTOR_LIST[a.length + 1] + return ctor(this.data, a, b, c) + } + return function construct(data, shape, stride, offset) { + return new View( + data, + shape[0], + shape[1], + shape[2], + shape[3], + shape[4], + stride[0], + stride[1], + stride[2], + stride[3], + stride[4], + offset + ) + } + }, +} - //view.step(): - code.push("proto.step=function "+className+"_step("+args.join(",")+"){var "+ - indices.map(function(i) { - return "a"+i+"=this.shape["+i+"]" - }).join(",")+","+ - indices.map(function(i) { - return "b"+i+"=this.stride["+i+"]" - }).join(",")+",c=this.offset,d=0,ceil=Math.ceil") - for(var i=0; i=0){c=(c+this.stride["+i+"]*i"+i+")|0}else{a.push(this.shape["+i+"]);b.push(this.stride["+i+"])}") + var procedure = allFns[dKey] + if(inDimension === -1) { + return procedure(inType) + } else if(inDimension === 0) { + return procedure(inType, CACHED_CONSTRUCTORS[inType][0]) } - code.push("var ctor=CTOR_LIST[a.length+1];return ctor(this.data,a,b,c)}") - - //Add return statement - code.push("return function construct_"+className+"(data,shape,stride,offset){return new "+className+"(data,"+ - indices.map(function(i) { - return "shape["+i+"]" - }).join(",")+","+ - indices.map(function(i) { - return "stride["+i+"]" - }).join(",")+",offset)}") - - //Compile procedure - var procedure = new Function("CTOR_LIST", "ORDER", code.join("\n")) - return procedure(CACHED_CONSTRUCTORS[dtype], order) + return procedure(inType, CACHED_CONSTRUCTORS[inType], order) } function arrayDType(data) { @@ -268,14 +916,14 @@ function arrayDType(data) { return "int16" case "[object Int32Array]": return "int32" + case "[object Uint8ClampedArray]": + return "uint8_clamped" case "[object Uint8Array]": return "uint8" case "[object Uint16Array]": return "uint16" case "[object Uint32Array]": return "uint32" - case "[object Uint8ClampedArray]": - return "uint8_clamped" case "[object BigInt64Array]": return "bigint64" case "[object BigUint64Array]": @@ -289,20 +937,22 @@ function arrayDType(data) { } var CACHED_CONSTRUCTORS = { + "generic":[], + "buffer":[], + "array":[], + + // typed arrays "float32":[], "float64":[], "int8":[], "int16":[], "int32":[], + "uint8_clamped":[], "uint8":[], "uint16":[], "uint32":[], - "array":[], - "uint8_clamped":[], "bigint64": [], - "biguint64": [], - "buffer":[], - "generic":[] + "biguint64": [] } ;(function() { @@ -337,10 +987,10 @@ function wrappedNDArrayCtor(data, shape, stride, offset) { } } } - var dtype = arrayDType(data) - var ctor_list = CACHED_CONSTRUCTORS[dtype] + var inType = arrayDType(data) + var ctor_list = CACHED_CONSTRUCTORS[inType] while(ctor_list.length <= d+1) { - ctor_list.push(compileConstructor(dtype, ctor_list.length-1)) + ctor_list.push(compileConstructor(inType, ctor_list.length-1)) } var ctor = ctor_list[d+1] return ctor(data, shape, stride, offset) diff --git a/package.json b/package.json index bbfb670..6b4bc2e 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,12 @@ { "name": "ndarray", - "version": "1.0.19", + "version": "1.1.0", "description": "Multidimensional Arrays", "main": "ndarray.js", "directories": { "test": "test" }, "dependencies": { - "iota-array": "^1.0.0", "is-buffer": "^1.0.2" }, "devDependencies": {