Comparing version 1.0.5 to 1.0.6
{ | ||
"name": "cwise", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "Component-wise operations on ndarrays", | ||
@@ -12,3 +12,3 @@ "main": "cwise.js", | ||
"cwise-parser": "^1.0.0", | ||
"cwise-compiler": "^1.0.0", | ||
"cwise-compiler": "^1.1.1", | ||
"static-module": "^1.0.0", | ||
@@ -19,14 +19,16 @@ "uglify-js": "2.4.13" | ||
"ndarray": "^1.0.13", | ||
"browserify": "^3.44.2", | ||
"tape": "^2.12.3" | ||
"browserify": "^10.2.4", | ||
"tape": "^4.0.0", | ||
"tap": "^1.3.1" | ||
}, | ||
"scripts": { | ||
"test": "tape test/*.js", | ||
"pretest": "rm -rf node_modules/cwise && ln -s .. node_modules/cwise" | ||
"test": "tap test/*.js", | ||
"pretest": "rm -f node_modules/cwise && ln -s .. node_modules/cwise" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/mikolalysenko/cwise.git" | ||
"url": "git://github.com/scijs/cwise.git" | ||
}, | ||
"keywords": [ | ||
"scijs", | ||
"ndarray", | ||
@@ -54,5 +56,11 @@ "component", | ||
"author": "Mikola Lysenko", | ||
"contributors": [ | ||
"Jasper van de Gronde <th.v.d.gronde@hccnet.nl>" | ||
], | ||
"license": "MIT", | ||
"gitHead": "1dd03e72f1b0fbec4186e497b3956d87aec6fc15", | ||
"readmeFilename": "README.md" | ||
"readmeFilename": "README.md", | ||
"bugs": { | ||
"url": "https://github.com/scijs/cwise/issues" | ||
} | ||
} |
216
README.md
@@ -5,12 +5,7 @@ cwise | ||
[![stable](https://rawgithub.com/hughsk/stability-badges/master/dist/stable.svg)](http://github.com/hughsk/stability-badges) | ||
[![build status](https://secure.travis-ci.org/scijs/cwise.png)](http://travis-ci | ||
.org/scijs/cwise) | ||
Usage | ||
===== | ||
First, install using npm: | ||
npm install cwise | ||
Then you can create an ndarray operation as follows: | ||
# Examples | ||
For brevity, we will assume the following precedes each example: | ||
```javascript | ||
@@ -20,3 +15,7 @@ //Import libraries | ||
, ndarray = require("ndarray") | ||
``` | ||
## Adding two arrays | ||
The array equivalent of `+=`: | ||
```javascript | ||
//Create operation | ||
@@ -48,70 +47,4 @@ var addeq = cwise({ | ||
`require("cwise")(user_args)` | ||
----------------------------- | ||
To use the library, you pass it an object with the following fields: | ||
* `args`: (Required) An array describing the type of the arguments passed to the body. These may be one of the following: | ||
+ `"array"`: An `ndarray`-type argument | ||
+ `"scalar"`: A globally broadcasted scalar argument | ||
+ `"index"`: (Hidden) An array representing the current index of the element being processed. Initially [0,0,...] in the pre block and set to some undefined value in the post block. | ||
+ `"shape"`: (Hidden) An array representing the shape of the arrays being processed | ||
+ An object representing a "blocked" array (for example a colour image, an array of matrices, etc.): | ||
+ `blockIndices` The number of indices (from the front of the array shape) to expose in the body (rather than iterating over them). Negative integers take indices from the back of the array shape. | ||
+ (Hidden) An object containing two properties representing an offset pointer from an array argument. | ||
+ `offset` An array representing the relative offset of the object | ||
+ `array` The index of an array parameter | ||
* `pre`: A function to be executed before starting the loop | ||
* `body`: (Required) A function that gets applied to each element of the input arrays | ||
* `post`: Executed when loop completes | ||
* `printCode`: If this flag is set, then log all generated code | ||
* `blockSize`: The size of a block (default 32) | ||
* `funcName`: The name to give to the generated procedure for debugging/profiling purposes. (Default is `body.name||"cwise"`) | ||
The result is a procedure that you can call which executes these methods along the following lines: | ||
## Multiply an array with a scalar | ||
```javascript | ||
function(a0, a1, ...) { | ||
pre() | ||
for(var i=0; i<a0.shape[0]; ++i) { | ||
for(var j=0; j<a0.shape[1]; ++j) { | ||
... | ||
body(a0[i,j,...], a1[i,j,...], ... ) | ||
} | ||
} | ||
post() | ||
} | ||
``` | ||
### Notes | ||
* To pass variables between the pre/body/post, use `this.*` | ||
* The order in which variables get visited depends on the stride ordering if the input arrays. In general it is not safe to assume that elements get visited (co)lexicographically. | ||
* If no return statement is specified, the first ndarray argument is returned | ||
* All input arrays must have the same shape. If not, then the library will throw an error | ||
### As a browserify transform | ||
If bundle size is an issue for you, it is possible to use cwise as a [browserify transform](http://browserify.org/), thus avoiding the potentially large parser dependencies. To do this, add the following lines to your package.json: | ||
```javascript | ||
//Contents of package.json | ||
{ | ||
// ... | ||
"browserify": { | ||
"transform": [ "cwise" ] | ||
} | ||
// ... | ||
} | ||
``` | ||
Then when you use the module with browserify, only the cwise-compile submodule will get loaded into your script instead of all of esprima. Note that this step is optional and the library will still work in the browser even if you don't use a transform. | ||
Examples | ||
======== | ||
Here are a few recipes showing how to use cwise to implement some common operations to get you started: | ||
### Multiply an array with a scalar | ||
```javascript | ||
var muls = cwise({ | ||
@@ -128,3 +61,3 @@ args: ["array", "scalar"], | ||
### Initialize an array with a grid with the first index | ||
## Initialize an array with a grid with the first index | ||
```javascript | ||
@@ -142,3 +75,3 @@ var mgrid = cwise({ | ||
### Compute 2D vector norms | ||
## Compute 2D vector norms using blocks | ||
```javascript | ||
@@ -157,25 +90,6 @@ var norm2D = cwise({ | ||
``` | ||
Note that in the above, `i` is not an actual `Array`, the indexing notation is just syntactic sugar. | ||
### Check if any element is set | ||
## Apply a stencil to an array | ||
```javascript | ||
var any = cwise({ | ||
args: ["array"], | ||
body: function(a) { | ||
if(a) { | ||
return true | ||
} | ||
}, | ||
post: function() { | ||
return false | ||
} | ||
}) | ||
//Usage | ||
if(any(array)) { | ||
// ... | ||
} | ||
``` | ||
### Apply a stencil to an array | ||
```javascript | ||
var laplacian = cwise({ | ||
@@ -191,3 +105,3 @@ args:["array", "array", {offset:[0,1], array:1}, {offset:[0,-1], array:1}, {offset:[1,0], array:1}, {offset:[-1,0], array:1}], | ||
### Compute the sum of all the elements in an array | ||
## Compute the sum of all the elements in an array | ||
```javascript | ||
@@ -210,6 +124,25 @@ var sum = cwise({ | ||
``` | ||
Note that variables stored in `this` are common to all the blocks | ||
Note that variables stored in `this` are common to all three code blocks. Also note that one should not treat `this` as an actual object (for example, one should not attempt to return `this`). | ||
## Check if any element is set | ||
```javascript | ||
var any = cwise({ | ||
args: ["array"], | ||
body: function(a) { | ||
if(a) { | ||
return true | ||
} | ||
}, | ||
post: function() { | ||
return false | ||
} | ||
}) | ||
### Compute the index of the maximum element of an array: | ||
//Usage | ||
if(any(array)) { | ||
// ... | ||
} | ||
``` | ||
## Compute the index of the maximum element of an array: | ||
```javascript | ||
@@ -239,15 +172,78 @@ var argmin = cwise({ | ||
FAQ | ||
=== | ||
# Install | ||
Install using [npm](https://www.npmjs.com/): | ||
Is it fast? | ||
----------- | ||
npm install cwise | ||
# API | ||
#### `require("cwise")(user_args)` | ||
To use the library, you pass it an object with the following fields: | ||
* `args`: (Required) An array describing the type of the arguments passed to the body. These may be one of the following: | ||
+ `"array"`: An `ndarray`-type argument | ||
+ `"scalar"`: A globally broadcasted scalar argument | ||
+ `"index"`: (Hidden) An array representing the current index of the element being processed. Initially [0,0,...] in the pre block and set to some undefined value in the post block. | ||
+ `"shape"`: (Hidden) An array representing the shape of the arrays being processed | ||
+ An object representing a "blocked" array (for example a colour image, an array of matrices, etc.): | ||
+ `blockIndices` The number of indices (from the front of the array shape) to expose in the body (rather than iterating over them). Negative integers take indices from the back of the array shape. | ||
+ (Hidden) An object containing two properties representing an offset pointer from an array argument. Note that cwise does not implement any boundary conditions. | ||
+ `offset` An array representing the relative offset of the object | ||
+ `array` The index of an array parameter | ||
* `pre`: A function to be executed before starting the loop | ||
* `body`: (Required) A function that gets applied to each element of the input arrays | ||
* `post`: Executed when loop completes | ||
* `printCode`: If this flag is set, then log all generated code | ||
* `blockSize`: The size of a block (default 32) | ||
* `funcName`: The name to give to the generated procedure for debugging/profiling purposes. (Default is `body.name||"cwise"`) | ||
The result is a procedure that you can call which executes these methods along the following lines: | ||
```javascript | ||
function(a0, a1, ...) { | ||
pre() | ||
for(var i=0; i<a0.shape[0]; ++i) { | ||
for(var j=0; j<a0.shape[1]; ++j) { | ||
... | ||
body(a0[i,j,...], a1[i,j,...], ... ) | ||
} | ||
} | ||
post() | ||
} | ||
``` | ||
### Notes | ||
* To pass variables between the pre/body/post, use `this.*` | ||
* The order in which variables get visited depends on the stride ordering if the input arrays. In general it is not safe to assume that elements get visited (co)lexicographically. | ||
* If no return statement is specified, the first ndarray argument is returned | ||
* All input arrays must have the same shape. If not, then the library will throw an error | ||
### As a browserify transform | ||
If bundle size is an issue for you, it is possible to use cwise as a [browserify transform](http://browserify.org/), thus avoiding the potentially large parser dependencies. To do this, add the following lines to your package.json: | ||
```javascript | ||
//Contents of package.json | ||
{ | ||
// ... | ||
"browserify": { | ||
"transform": [ "cwise" ] | ||
} | ||
// ... | ||
} | ||
``` | ||
Then when you use the module with browserify, only the cwise-compile submodule will get loaded into your script instead of all of esprima. Note that this step is optional and the library will still work in the browser even if you don't use a transform. | ||
# FAQ | ||
## Is it fast? | ||
[Yes](https://github.com/mikolalysenko/ndarray-experiments) | ||
How does it work? | ||
----------------- | ||
## How does it work? | ||
You can think of cwise as a type of macro language on top of JavaScript. Internally, cwise uses node-falafel to parse the functions you give it and sanitize their arguments. At run time, code for each array operation is generated lazily depending on the ordering and stride of the input arrays so that you get optimal cache performance. These compiled functions are then memoized for future calls to the same function. As a result, you should reuse array operations as much as possible to avoid wasting time and memory regenerating common functions. | ||
Credits | ||
======= | ||
# License | ||
(c) 2013 Mikola Lysenko. MIT License |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
13202
9
1
0
4
241
Updatedcwise-compiler@^1.1.1