Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 35d2598
Showing
5 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
lib-cov | ||
*.seed | ||
*.log | ||
*.csv | ||
*.dat | ||
*.out | ||
*.pid | ||
*.gz | ||
|
||
pids | ||
logs | ||
results | ||
|
||
npm-debug.log | ||
node_modules/* | ||
*.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
The MIT License (MIT) | ||
|
||
Copyright (c) 2013 Mikola Lysenko | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
ndarray-lazy | ||
============ | ||
Creates lazily initialized [ndarrays](https://github.com/mikolalysenko/ndarray). These give a logical view of a function as an ndarray. | ||
|
||
Introduction | ||
============ | ||
Lazy arrays work just like ndarrays, and can be used in all the same ways. However, unlike dense arrays they do not keep any storage and instead use a function to determine the contents of the array. Here is a simple example: | ||
|
||
```javascript | ||
var lazy = require("ndarray-lazy") | ||
|
||
//Create a 10x10 lazily initialized ndarray | ||
var x = lazy([10, 10], function(i, j) { | ||
console.log("Called:", i, j) | ||
return 10*i + j | ||
}) | ||
|
||
x.get(1,2) //Prints out: Called: 1 2 | ||
x.get(7,8) //Prints out: Called: 7 8 | ||
``` | ||
|
||
It is possible to slice the view of a lazy ndarray, just like a regular ndarray: | ||
|
||
```javascript | ||
x.lo(2,3).get(1,1) //Prints out: Called: 3 4 | ||
x.transpose(1,0).get(3,7) //Prints out: Called: 7 3 | ||
``` | ||
|
||
You can also use lazy ndarrays with cwise or any other library that works with ndarrays. | ||
|
||
*HOWEVER:* You are not allowed to assing to ndarrays: | ||
|
||
```javascript | ||
x.set(0,0,1) //Error! | ||
``` | ||
|
||
# Install | ||
|
||
You can install the library using [npm](http://npmjs.org): | ||
|
||
```sh | ||
npm install ndarray-lazy | ||
``` | ||
|
||
And like all ndarray modules it should work in a browser that supports typed arrays using [browserify](https://github.com/substack/node-browserify). | ||
|
||
# API | ||
|
||
### `require("ndarray-lazy")(shape, func)` | ||
The main func | ||
|
||
# Credits | ||
(c) 2013 Mikola Lysenko. MIT License |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"use strict" | ||
|
||
var ndarray = require("ndarray") | ||
var bits = require("bit-twiddle") | ||
|
||
function createLazyArray(shape, func) { | ||
var d = shape.length | ||
var shape_bits = new Array(d) | ||
var total_bits = 0 | ||
var className = "Lazy1DStore_" + func.name | ||
for(var i=0; i<d; ++i) { | ||
shape_bits[i] = bits.log2(bits.nextPow2(shape[i])) | ||
total_bits += shape_bits[i] | ||
} | ||
|
||
if(total_bits > 32) { | ||
throw new Error("ndarray-lazy: Not enough addressable bits. Can't create lazy array: " + shape) | ||
} | ||
|
||
var stride = new Array(d) | ||
var args = new Array(d) | ||
var sz = 0 | ||
for(var i=d-1; i>=0; --i) { | ||
stride[i] = 1<<sz | ||
if(sz === 0) { | ||
args[i] = "i&"+((1<<shape_bits[i])-1) | ||
} else { | ||
args[i] = ["(i>>>", sz, ")&", ((1<<shape_bits[i])-1)].join("") | ||
} | ||
sz += shape_bits[i] | ||
} | ||
|
||
var code = ["'use strict'"] | ||
code.push(["function ", className, "(){};var proto=", className].join("")) | ||
code.push("proto.set=function(i,v){throw new Error('ndarray-lazy: Can\\'t write to lazy ndarray')}") | ||
code.push("proto.length=" + (1<<total_bits)) | ||
code.push(["proto.get=function(i){return func(", args.join(","), ");}"].join("")) | ||
code.push("return " + className) | ||
|
||
var store = new Function("func", code.join("\n")) | ||
|
||
return ndarray(store(func), shape, stride, 0) | ||
} | ||
module.exports = createLazyArray |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
var lazy = require("../ndlazy") | ||
|
||
require("tape")("ndarray-lazy", function(t) { | ||
//Create a 10x10 lazily initialized ndarray | ||
var last_accessed = [] | ||
var x = lazy([10, 10], function(i, j) { | ||
last_accessed = [i,j] | ||
return 10*i + j | ||
}) | ||
|
||
t.equals(x.get(1,2), 12) | ||
t.same(last_accessed, [1,2]) | ||
|
||
t.equals(x.get(7,8), 78) | ||
t.same(last_accessed, [7,8]) | ||
|
||
t.equals(x.lo(2,3).get(1,1), 34) | ||
t.same(last_accessed, [3,4]) | ||
|
||
x.transpose(1,0).get(3,7) | ||
x.set(0,0,1) | ||
|
||
t.end() | ||
}) |