describe('Layer: merge', function() { let arrayType = Float64Array; let input = [ndarray(new Float64Array([0.25, 0.5, 0.75]), [3]), ndarray(new Float64Array([0.65, 0.35, 0.15]), [3])]; let branches = [ [{ 'layerName': 'denseLayer', 'parameters': [{'W': [[0.3736618, 0.5745131],[1.0225855, 0.9838101],[0.7040277, -0.0223534]],'b': [0.0, 0.0]},'linear'] }], [{ 'layerName': 'denseLayer', 'parameters': [{'W': [[-1.0081574, 0.7686076],[1.0467454, -0.2916093],[0.2885694, -0.5222564]],'b': [0.0, 0.0]},'linear'] }] ]; it('should produce expected in `sum` mode', done => { let expected = [0.88707298, 0.93796146]; let y = mergeLayer(arrayType, input, branches, 'sum'); assert.deepEqual(y.shape, [2]); for (let i = 0; i < y.shape[0]; i++) { assert(almostEqual(y.get(i), expected[i], EPSILON, EPSILON)); } done(); }); it('should produce expected in `ave` mode', done => { let expected = [0.44353649, 0.46898073]; let y = mergeLayer(arrayType, input, branches, 'ave'); assert.deepEqual(y.shape, [2]); for (let i = 0; i < y.shape[0]; i++) { assert(almostEqual(y.get(i), expected[i], EPSILON, EPSILON)); } done(); }); it('should produce expected in `mul` mode', done => { let expected = [-0.27826163, 0.19750662]; let y = mergeLayer(arrayType, input, branches, 'mul'); assert.deepEqual(y.shape, [2]); for (let i = 0; i < y.shape[0]; i++) { assert(almostEqual(y.get(i), expected[i], EPSILON, EPSILON)); } done(); }); it('should produce expected in `concat` mode', done => { let expected = [1.132728975, 0.6187682750000001, -0.24565601000000006, 0.31919322499999997]; let y = mergeLayer(arrayType, input, branches, 'concat', -1); assert.deepEqual(y.shape, [4]); for (let i = 0; i < y.shape[0]; i++) { assert(almostEqual(y.get(i), expected[i], EPSILON, EPSILON)); } done(); }); });
BlockSheet.prototype.imageToBlocks = function(image, frames) { frames = frames || 1; // Temp canvas var canvas = document.createElement('canvas'); var context = canvas.getContext('2d'); context.drawImage(image, 0, 0); var imageData = context.getImageData(0, 0, image.width, image.height); var shape = [image.width, image.height, 1]; var chunk = ndarray([], shape); var palette = [null]; var colorMap = {}; var width = image.width; var row, column, frame, index; var totalRows = image.height; var frameWidth = width / frames; var chunks = []; for (var i = 0; i < imageData.data.length; i += 4) { var r = imageData.data[i] / 255.0; var g = imageData.data[i + 1] / 255.0; var b = imageData.data[i + 2] / 255.0; var a = imageData.data[i + 3] / 255.0; if (a === 0) { continue; } var color = new THREE.Color(r, g, b).getHex(); row = (-Math.floor(i / 4 / width) - 2) % totalRows; column = (i / 4) % width; frame = Math.floor(column / frameWidth); column -= frame * frameWidth; if (colorMap[color]) { index = colorMap[color]; } else { index = palette.length; palette.push(color); colorMap[color] = index; } if (chunks[frame] == null) { chunks[frame] = ndarray([], [frameWidth, image.height, 1]); } chunks[frame].set(column + 1, row, 1, index); } return { chunks: chunks, palette: palette }; };
test('ndarray-fft', function () { var fft = require('ndarray-fft'); var ndarray = require('ndarray'); var x = ndarray(real); var y = ndarray(im); test('run', function () { for (var i = 0; i < max; i++) { fft(1, x, y); } }); });
tape("ndarray-distance", function(t) { var a = ndarray([1, 2, 3, 4, 5]) var b = ndarray([0, 2, 3, 10, 6]) t.ok(almostEqual(distance(a, b), Math.sqrt(1 + 6*6 + 1), almostEqual.DBL_EPSILON, almostEqual.DBL_EPSILON)) t.ok(almostEqual(distance(a, b, 1), 1 + 6 + 1, almostEqual.DBL_EPSILON, almostEqual.DBL_EPSILON)) t.ok(almostEqual(distance(a, b, Infinity), 6, almostEqual.DBL_EPSILON, almostEqual.DBL_EPSILON)) t.ok(almostEqual(distance(a, b, 0), 3, almostEqual.DBL_EPSILON, almostEqual.DBL_EPSILON)) t.ok(almostEqual(distance(a, b,3), Math.pow(1 + 6*6*6 + 1, 1.0/3.0), almostEqual.DBL_EPSILON, almostEqual.DBL_EPSILON)) t.end() })
test("SunMaragos pyramid", function(t) { var data = ndarray(new Int32Array([11,10,6,12,3,2,15,9,4,5,14,7,13,8,1]), [3,5]) var ref = [data, ndarray(new Int32Array([11,6,3,2,7,1]), [2,3]), ndarray(new Int32Array([11,3]), [1,2]), ndarray(new Int32Array([11]), [1,1])] var p = pyramid(data, pyramid.SunMaragos) t.equal(p.length, ref.length, "number of levels") for(var i=0; i<Math.min(p.length, ref.length); i++) { t.ok(ops.equals(p[i], ref[i]), "level " + i) } t.end() })
function testLinear() { var data = JSON.parse(fs.readFileSync('test/data/full.json', 'utf8')); var weight = ndarray(data.weight, [data.outSize, data.inSize]); var bias = ndarray(data.bias, [data.outSize]); var mod = new nn.Linear(weight, bias); var gt = ndarray(data.out, [data.outSize]) var inp = ndarray(data.inp, [data.inSize]) var out = mod.forward(inp) var err = 0; for (i=0; i < data.outSize; i++) { err = Math.max(err, Math.abs(out.get(i) - gt.get(i))); } assert.equal(true, err <= eps, "Linear test failed. Error: " + err) }
test("adjunction detail pyramid reconstruction", function(t) { var data = ndarray(new Int32Array([11,10,6,12,3,2,15,9,4,5,14,7,13,8,1]), [3,5]) var ref = [data, ndarray(new Int32Array([2,4,3,7,8,1]), [2,3]), ndarray(new Int32Array([2,1]), [1,2]), ndarray(new Int32Array([1]), [1,1])] var p = pyramid.detail(data, pyramid.adjunction) pyramid.reconstruct(p, pyramid.adjunction) t.equal(p.length, ref.length, "number of levels") for(var i=0; i<Math.min(p.length, ref.length); i++) { t.ok(ops.equals(p[i], ref[i]), "level " + i) } t.end() })
it('factors a matrix into L, U, and P',function() { assert( lup(A, L, P), 'returns true on success') var Aexp = ndarray([8,7,9,5, 0,7/4,9/4,17/4, 0,0,-6/7,-2/7, 0,0,0,2/3],[4,4]) var Lexp = ndarray([1,0,0,0, 3/4,1,0,0, 1/2,-2/7,1,0, 1/4,-3/7,1/3,1],[4,4]) var Pexp = [2,3,1,0] assert( ndt.approximatelyEqual( Aexp, A, 1e-8 ), 'A is correct' ) assert( ndt.approximatelyEqual( Lexp, L, 1e-8 ), 'L is correct' ) assert( ndt.approximatelyEqual( ndarray(Pexp), ndarray(P)), 'P is correct' ) assert( ndt.matrixIsUpperTriangular( A, 1e-8 ), 'A is upper-triangular') assert( ndt.matrixIsLowerTriangular( L, 1e-8 ), 'L is lower-triangular') })
GcodeRaymarchSimulator.prototype.init = function(gl) { this._gl = gl; this.raymarchProgram = createRaymarchProgram(gl); this.raymarchProgram.bind(); this.buffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, this.buffer); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ 1, 1, 0, -1, 1, 0, -1, -1, 0, 1, 1, 0, 1, -1, 0, -1, -1, 0 ]), gl.STATIC_DRAW) this.raymarchProgram.attributes.position.pointer(); this.depthBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, this.depthBuffer); var width = this.raymarchProgram.uniforms.depth_width = 2048; var height = this.raymarchProgram.uniforms.depth_height = 2048; this.raymarchProgram.uniforms.depth_stride = width; this.depthArray = ndarray(new Float32Array(width*height), [width, height]); this.depthTexture = createTexture(gl, this.depthArray); this.depthTexture.bind(); }
co(function*(){ yield processImages(pattern, listOfImages, blockLength, db); var pixels = ndarray([], [640, 640, 3]); for(var i=0; i<pattern.length; i+=blockLength){ var block = yield pickBlock("B-"+i); for(var k=0; k<blockLength; k++){ var pixel = block[k]; if(pixel!==null){ var channels = pixel.split("-").map(function(v){ return parseInt(v, 16); }); pixels.set(pattern[i+k][0], pattern[i+k][1], 0, channels[0]); pixels.set(pattern[i+k][0], pattern[i+k][1], 1, channels[1]); pixels.set(pattern[i+k][0], pattern[i+k][1], 2, channels[2]); } } } savePixels(pixels, "jpg").pipe(fs.createWriteStream("./test.jpg")); }).then(sketchSaver).catch(function(err){
describe('Layer: embedding', function() { let input = ndarray(new Int32Array([0,0,0,1,2,3]), [6]); let E = [[0, 0, 0], [0.1, 0.2, 0.3], [0.4, 0.5, 0.6], [0.7, 0.8, 0.9]]; it('should create zero-masked embedding matrix', (done) => { let y = embeddingLayer(Float64Array, input, { E }, true); assert.deepEqual(y.shape, [3, 3]); for (let i = 0; i < y.shape[0]; i++) { for (let j = 0; j < y.shape[1]; j++) { assert(almostEqual(y.get(i, j), E[i+1][j], EPSILON, EPSILON)); } } done(); }); it('should create non-zero-masked embedding matrix', (done) => { let y = embeddingLayer(Float64Array, input, { E }, false); assert.deepEqual(y.shape, [6, 3]); for (let i = 0; i < y.shape[0]; i++) { for (let j = 0; j < y.shape[1]; j++) { assert(almostEqual(y.get(i, j), E[input.get(i)][j], EPSILON, EPSILON)); } } done(); }); });
backward(gradOutput) { this.gradInput = ndarray(gradOutput.data.map((v, k) => { return this.output.data[k] > 0 ? v : 0 }, gradOutput.shape)) return this.gradInput }
test("diveq",function(t) { var a_r = ndarray([1,1]), a_i = ndarray([2,2]), b_r = ndarray([3,3]), b_i = ndarray([4,4]); cops.diveq(a_r, a_i, b_r, b_i); t.assert( Math.abs(a_r.get(0) - 0.44 ) < 1e-8 ); t.assert( Math.abs(a_i.get(0) - 0.08 ) < 1e-8 ); t.assert( Math.abs(a_r.get(1) - 0.44 ) < 1e-8 ); t.assert( Math.abs(a_i.get(1) - 0.08 ) < 1e-8 ); t.end(); });
t('format.type', t => { var nd = ndarray(new Float32Array([0,0,0,0]), [2,2]) t.equal( format.type(new AudioBuffer(null, {length: 1024})), 'audiobuffer' ) t.equal( format.type(new Float32Array([-1, 1])), 'float32' ) t.equal( format.type(new Float32Array([-1, 1]).buffer), 'arraybuffer' ) t.equal( format.type(Array(100)), 'array' ) t.equal( format.type(Buffer.from([0, 1])), 'buffer' ) t.equal( format.type(nd), 'ndarray' ) t.end() })
function TileMap(opts) { if (!(this instanceof TileMap)) return new TileMap(opts) this.width = opts.width || window.innerWidth this.height = opts.height || window.innerHeight this.size = opts.size || 16 this.prefix = opts.prefix || 'tile-' var s = [Math.floor(this.width/this.size), Math.floor(this.height/this.size)] this.data = ndarray(new Uint8Array(s[0] * s[1]), s) this.element = document.createElement('div') var fragment = document.createDocumentFragment() for (var x = 0; x < this.data.shape[0]; x++) { for (var y = 0; y < this.data.shape[1]; y++) { var div = document.createElement('div') div.style.position = 'fixed' div.style.top = (y * this.size) + 'px' div.style.left = (x * this.size) + 'px' fragment.appendChild(div) } } this.element.appendChild(fragment) this.index = this.element.getElementsByTagName('div') this.update = false }
return cov.loadDomain().then(domain => { let x = domain.axes.get(X).values let y = domain.axes.get(Y).values let pnpolyCache = ndarray(new Uint8Array(x.length * y.length), [x.length, y.length]) for (let i = 0; i < x.length; i++) { for (let j = 0; j < y.length; j++) { let inside = pip([x[i], y[j]]) >= 0 pnpolyCache.set(i, j, inside) } } let fn = (obj, range) => { if (pnpolyCache.get(obj[X] || 0, obj[Y] || 0)) { return range.get(obj) } else { return null } } let newcov = cov for (let key of cov.parameters.keys()) { newcov = mapRange(newcov, key, fn) } return newcov })
function toRGBA (glyph) { var w = glyph.shape[0], h = glyph.shape[1] //glyph.bitmap is rotated, so we use [h, w] var alpha = ndarray(glyph.bitmap, [h, w, 1]) var buf = new Uint8ClampedArray(alpha.size * 4) var output = ndarray(buf, [h, w, 4]) //fill R,G,B as the signed distance for (var i = 0; i < 3; ++i) ops.assign(output.pick(-1, -1, i), alpha) //fill alpha as 0xff ops.assigns(output.pick(-1, -1, 3), 0xff) return output.transpose(1, 0, 2) }
//Adapted from MDN example for unicode processing: // // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt // function ndarrayFromString(str, shape, stride, offset) { var n = str.length var arr = new Uint32Array(n) var ncode_points = 0 for(var i=0; i<n; ++i) { var code = str.charCodeAt(i) var hi, low if (0xD800 <= code && code <= 0xDBFF) { hi = code low = str.charCodeAt(++i) if (isNaN(low)) { throw 'High surrogate not followed by low surrogate' } arr[ncode_points++] = ((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000 } if (0xDC00 <= code && code <= 0xDFFF) { continue } arr[ncode_points++] = code } if(!shape) { shape = [ncode_points] } return ndarray(arr, shape, stride, offset) }
// From https://github.com/substack/sillyscope/blob/master/index.js#L26 function findFrequencies (floats, opts) { var reals = ndarray(floats, [ floats.length, 1 ]); var imags = ndarray(zeroes(floats.length), [ floats.length, 1 ]); fft(1, reals, imags); mag(reals, reals, imags); var freqs = []; for (var i = 0; i < reals.data.length; i++) { var freq = i * opts.rate / floats.length; if (freq >= opts.range[0] && freq <= opts.range[1]) { freqs.push([ freq, reals.data[i] ]); } } return freqs; }
function readArray(data, meta) { // const dtype = meta.dtype; // const shape = meta.shape; const { dtype, shape } = meta; let arr = null; switch (dtype) { case 'int8': arr = new Int8Array(data); break; case 'uint8': arr = new Uint8Array(data); break; case 'int16': arr = new Int16Array(data); break; case 'uint16': arr = new Uint16Array(data); break; case 'int32': arr = new Int32Array(data); break; case 'uint32': arr = new Uint32Array(data); break; case 'float32': arr = new Float32Array(data); break; case 'float64': arr = new Float64Array(data); break; default: arr = new Float32Array(data); } return { meta, array: ndarray(arr, shape) }; }
function zeros(nshape, nsize) { var t = pool.mallocDouble(nsize) for(var i=0; i<nsize; ++i) { t[i] = 0.0 } return ndarray(t, nshape) }
// Pad coords by +1 function padField(field) { var shape = field.shape; var nshape = [shape[0]+2, shape[1]+2]; var nfield = ndarray(new Float32Array(nshape[0] * nshape[1]), nshape); // Center ops.assign(nfield.lo(1, 1).hi(shape[0], shape[1]), field); // Edges ops.assign(nfield.lo(1).hi(shape[0], 1), field.hi(shape[0], 1)); ops.assign(nfield.lo(1, nshape[1]-1).hi(shape[0], 1), field.lo(0, shape[1]-1).hi(shape[0], 1)); ops.assign(nfield.lo(0, 1).hi(1, shape[1]), field.hi(1)); ops.assign(nfield.lo(nshape[0]-1, 1).hi(1, shape[1]), field.lo(shape[0]-1)); // Corners nfield.set(0, 0, field.get(0, 0)); nfield.set(0, nshape[1]-1, field.get(0, shape[1]-1)); nfield.set(nshape[0]-1, 0, field.get(shape[0]-1, 0)); nfield.set(nshape[0]-1, nshape[1]-1, field.get(shape[0]-1, shape[1]-1)); return nfield; }
module.exports = function (N,data) { var arr = ndarray(data, [N, 1]); return function () { fft(1, arr, arr); } };
/** * Capture the node pixels. * Without parameters, it will capture the full rectangle, * otherwise you can provide (x, y) or (x, y, w, h) to provide a subset of this rectangle. */ capture(x?: number, y?: number, w?: number, h?: number): NDArray { const [width, height] = this.getGLSize(); const { gl } = this.context.glSurface; invariant(gl, "gl is no longer available"); if (x === undefined) x = 0; if (y === undefined) y = 0; if (w === undefined) w = width - x; if (h === undefined) h = height - y; invariant( x >= 0 && x + w <= width && y >= 0 && y + h <= height, "capture(%s,%s,%s,%s): requested rectangle is out of bounds (%s,%s)", x, y, w, h, width, height ); const size = w * h * 4; const pixels: Uint8Array = this._captureAlloc(size); this._bind(); gl.readPixels(x, y, w, h, gl.RGBA, gl.UNSIGNED_BYTE, pixels); return ndarray(pixels, [h, w, 4]) .step(-1, 1, 1) .transpose(1, 0, 2); }
test('can use ndarrays', function(t) { var expected = '█░\n░█\n'; var input = ndarray(new Int8Array([1, 0, 0, 1]), [2, 2]); t.equal(printArray(input), expected); t.end(); });
module.exports = function convert(arr, result) { var shape = [], c = arr, sz = 1 while(Array.isArray(c)) { shape.push(c.length) sz *= c.length c = c[0] } if(shape.length === 0) { return ndarray() } if(!result) { result = ndarray(new Float64Array(sz), shape) } do_convert(result, arr) return result }
const processPixels = function (err, pixels) { // console.log(pixels); const picX = pixels.shape[0] const picY = pixels.shape[1] const ratio = picX / picY options.sizeX = !options.sizeX && options.sizeY ? Math.floor(options.sizeY * 2 * ratio) : options.sizeX options.sizeY = !options.sizeY && options.sizeX ? Math.floor(options.sizeX / (2 * ratio)) : options.sizeY let sum = ndarray(new Uint32Array(options.sizeX * options.sizeY), [options.sizeX, options.sizeY]) const stepX = Math.floor(picX / options.sizeX) const stepY = Math.floor(picY / options.sizeY) const maxval = stepX * stepY * 255 * 3 for (let y = 0; y < Math.floor(picY / stepY); y++) { for (let x = 0; x < Math.floor(picX / stepX); x++) { let count = 0 for (let py = 0; py < Math.floor(stepY); py++) { for (let px = 0; px < Math.floor(stepX); px++) { const R = pixels.get(x * stepX + px, y * stepY + py, 0) const G = pixels.get(x * stepX + px, y * stepY + py, 1) const B = pixels.get(x * stepX + px, y * stepY + py, 2) // const A = pixels.get(x * stepX + px, y * stepY + py, 3) count += R + G + B // count += R * 0.299 + G * 0.587 + B * 0.114 } } sum.set(x, y, count) } } cb(sum, {maxval, sizeX: options.sizeX, sizeY: options.sizeY}) }
it('Two Step -- Rank 2 DFP, Analytical', function () { var x0 = ndarray(new Float64Array([-3, 1]), [2, 1]); var options = { objective: { start: x0, func: F, gradient: { func: dF, delta: 0 } }, update: { hessianInverse: true, type: 'rank2-dfp' }, solution: { tolerance: TOLERANCE, maxIterations: MAX_ITERATIONS } }; var results = quasiNewton(options); chai.assert(results.solutionValid, 'Solution is not optimal'); chai.assert.isBelow(results.objective, TOLERANCE, 'Result is not within tolerance'); chai.assert(results.iterations === 2, 'Should take only 2 iterations to reach optimum'); });
shell.on("gl-init", function() { var gl = shell.gl //Turn off depth test gl.disable(gl.DEPTH_TEST) //Initialize shaders updateShader = createUpdateShader(gl) drawShader = createDrawShader(gl) //Allocate buffers state = [ createFBO(gl, [512, 512]), createFBO(gl, [512, 512]) ] //Initialize state buffer var initial_conditions = ndarray(new Uint8Array(512*512*4), [512, 512, 4]) fill(initial_conditions, function(x,y,c) { if(c === 3) { return 255 } return Math.random() > 0.9 ? 255 : 0 }) state[0].color[0].setPixels(initial_conditions) //Set up vertex pointers drawShader.attributes.position.location = updateShader.attributes.position.location = 0 })
function createNDBitArray(shape) { var sz = 1 for(var i=0; i<shape.length; ++i) { sz *= shape[i] } return ndarray(new BitArray(sz), shape) }