├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── package.json └── test ├── browserify.js └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | 14 | npm-debug.log 15 | node_modules/* -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "6" 4 | - "5" 5 | - "5.1" 6 | - "4" 7 | - "4.2" 8 | - "4.1" 9 | - "4.0" 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2013 Mikola Lysenko 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ndarray-fill 2 | ============ 3 | Initialize an [ndarray](https://github.com/mikolalysenko/ndarray) with a function. 4 | 5 | [![Build Status](https://travis-ci.org/scijs/ndarray-fill.svg?branch=master)](https://travis-ci.org/scijs/ndarray-fill) 6 | 7 | ## Example 8 | 9 | ```javascript 10 | var zeros = require("zeros") 11 | var fill = require("ndarray-fill") 12 | 13 | var x = zeros([5, 5]) 14 | 15 | fill(x, function(i,j) { 16 | return 10*i + j 17 | }) 18 | 19 | //Now x = 20 | // 21 | // 0 1 2 3 4 22 | // 10 11 12 13 14 23 | // 20 21 22 23 24 24 | // 30 31 32 33 34 25 | // 26 | ``` 27 | 28 | ## Install 29 | 30 | ``` 31 | npm install ndarray-fill 32 | ``` 33 | 34 | ### `require("ndarray-fill")(array, func)` 35 | Fills an ndarray with a pattern 36 | 37 | * `array` is an ndarray which will be initialized 38 | * `func` is a function that will be used to initialize the array 39 | 40 | **Returns** An initialized ndarray 41 | 42 | ## Credits 43 | (c) 2013 Mikola Lysenko. MIT License 44 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | "use strict" 2 | 3 | var cwise = require("cwise") 4 | 5 | var fill = cwise({ 6 | args: ["index", "array", "scalar"], 7 | body: function(idx, out, f) { 8 | out = f.apply(undefined, idx) 9 | } 10 | }) 11 | 12 | module.exports = function(array, f) { 13 | fill(array, f) 14 | return array 15 | } 16 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ndarray-fill", 3 | "version": "1.0.2", 4 | "description": "Fills an ndarray with function", 5 | "main": "index.js", 6 | "directories": { 7 | "test": "test" 8 | }, 9 | "dependencies": { 10 | "cwise": "^1.0.10" 11 | }, 12 | "devDependencies": { 13 | "browserify": "^14.1.0", 14 | "tape": "^2.12.3", 15 | "zeros": "~0.0.0" 16 | }, 17 | "scripts": { 18 | "test": "tape test/*.js" 19 | }, 20 | "repository": { 21 | "type": "git", 22 | "url": "git://github.com/mikolalysenko/ndarray-fill.git" 23 | }, 24 | "keywords": [ 25 | "ndarray", 26 | "fill", 27 | "eval", 28 | "map" 29 | ], 30 | "author": "Mikola Lysenko", 31 | "license": "MIT", 32 | "readmeFilename": "README.md", 33 | "gitHead": "8831bd16c9e816764d66abbfb7054f27c40940d5", 34 | "bugs": { 35 | "url": "https://github.com/mikolalysenko/ndarray-fill/issues" 36 | }, 37 | "browserify": { 38 | "transform": ["cwise"] 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /test/browserify.js: -------------------------------------------------------------------------------- 1 | "use strict" 2 | 3 | var browserify = require("browserify") 4 | var tape = require("tape") 5 | var vm = require("vm") 6 | 7 | var cases = [ "test" ] 8 | 9 | bundleCasesFrom(0) 10 | 11 | function bundleCasesFrom(i) { 12 | if (i>=cases.length) return 13 | var b = browserify() 14 | b.ignore("tape") 15 | b.add(__dirname + "/" + cases[i] + ".js") 16 | tape(cases[i], function(t) { // Without nested tests, the asynchronous nature of bundle causes issues with tape... 17 | b.bundle(function(err, src) { 18 | if(err) { 19 | throw new Error("failed to bundle!") 20 | } 21 | vm.runInNewContext(src, { 22 | test: t.test.bind(t), 23 | Buffer: Buffer, 24 | Int8Array: Int8Array, 25 | Int16Array: Int16Array, 26 | Int32Array: Int32Array, 27 | Float32Array: Float32Array, 28 | Float64Array: Float64Array, 29 | Uint8Array: Uint8Array, 30 | Uint16Array: Uint16Array, 31 | Uint32Array: Uint32Array, 32 | Uint8ClampedArray: Uint8ClampedArray, 33 | console: { log: console.log.bind(console) } 34 | }) 35 | t.end() 36 | }) 37 | }) 38 | bundleCasesFrom(i+1) 39 | } 40 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | var zeros = require("zeros") 2 | var fill = require("../index.js") 3 | 4 | if(typeof test === "undefined") { 5 | test = require("tape") 6 | } 7 | 8 | test('fill zeros', function(t) { 9 | 10 | var x = zeros([10,10]) 11 | 12 | fill(x, function(a, b) { 13 | return a * 10 + b 14 | }) 15 | 16 | for(var i=0; i<10; ++i) { 17 | for(var j=0; j<10; ++j) { 18 | t.equals(x.get(i,j), 10*i+j) 19 | } 20 | } 21 | 22 | t.end() 23 | }) 24 | --------------------------------------------------------------------------------