Skip to content

Commit

Permalink
test pass
Browse files Browse the repository at this point in the history
  • Loading branch information
lsbardel committed Feb 12, 2016
1 parent dc9ce75 commit ab114ce
Show file tree
Hide file tree
Showing 12 changed files with 246 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
ecmaFeatures:
modules: true

env:
es6: true
browser: true
node: true

extends:
"eslint:recommended"

rules:
no-cond-assign: 0
no-floating-decimal: 2
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# javascript
node_modules
dist
build

# docs
giottoweb/docs/_build/

.idea
.project
.pydevproject
.grunt
.coveralls.yml
clean.py
*.log
*.pyc
__pycache__
.DS_Store
coverage
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build/*.zip
test/
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: node_js

install:
- npm install

before_script: grunt build

notifications:
email: false
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export {default as sobol} from "./src/sobol";
export {default as version} from "./src/version";
38 changes: 38 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"version": "0.1.0",
"name": "d3-quant",
"title": "d3-quant",
"description": "D3 library fron quantitative data analisys",
"homepage": "https://github.com/quantmind/d3-quant",
"repository": {
"type": "git",
"url": "https://github.com/quantmind/d3-quant.git"
},
"keywords": [
"d3",
"math"
],
"license": "BSD-3-Clause",
"author": {
"name": "quantmind.com",
"email": "message@quantmind.com"
},
"main": "build/d3-quant.js",
"jsnext:main": "index",
"devDependencies": {
"faucet": "0.0",
"rollup": "0.25",
"rollup-plugin-babel": "",
"rollup-plugin-json": "",
"babel-preset-es2015-rollup": "",
"tape": "4",
"uglify-js": "2",
"eslint": ""
},
"scripts": {
"pretest": "mkdir -p build && node -e 'process.stdout.write(\"var version = \\\"\" + require(\"./package.json\").version + \"\\\"; export default version;\");' > src/version.js && rollup -c",
"test": "eslint index.js src test && faucet `find test -name '*-test.js'`",
"prepublish": "npm run test && uglifyjs build/d3-quant.js -c -m -o build/d3-quant.min.js && rm -f build/d3-quant.zip && zip -j build/d3-quant.zip -- LICENSE README.md build/d3-quant.js build/d3-quant.min.js",
"postpublish": "VERSION=`node -e 'console.log(require(\"./package.json\").version)'`; git push && git tag -am \"Release $VERSION.\" v${VERSION} && git push --tags && cp build/d3-quant.js"
}
}
10 changes: 10 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import json from 'rollup-plugin-json';
import babel from 'rollup-plugin-babel';

export default {
entry: 'index.js',
"format": "umd",
"moduleName": "d3_quant",
plugins: [ json(), babel({"presets": ["es2015-rollup"]}) ],
dest: 'build/d3-quant.js'
};
10 changes: 10 additions & 0 deletions src/banner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// <%= pkg.title || pkg.name %> - v<%= pkg.version %>
//
// Compiled <%= grunt.template.today("yyyy-mm-dd") %>.
//
// Copyright (c) <%= grunt.template.today("yyyy") %> - <%= pkg.author.name %>
// All rights reserved.
// License: <%= pkg.license %>
// For all details and documentation:
// <%= pkg.homepage %>
//
113 changes: 113 additions & 0 deletions src/sobol.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
'use strict';
const
BITS = 52,
SCALE = 2 << 51,
COEFFICIENTS = [
'd s a m_i',
'2 1 0 1',
'3 2 1 1 3',
'4 3 1 1 3 1',
'5 3 2 1 1 1',
'6 4 1 1 1 3 3',
'7 4 4 1 3 5 13',
'8 5 2 1 1 5 5 17',
'9 5 4 1 1 5 5 5',
'10 5 7 1 1 7 11 1'
];

class Sobol {

constructor (dimension) {
_sobol.set(this, new SobolImpl(dimension));
}

get dimension () {
return _sobol.get(this).dimension;
}

get count () {
return _sobol.get(this).count;
}

next () {
return _sobol.get(this).next();
}
}

let i;

// Simulate a WeekMap for now
var _sobol = {
get: function (obj) {
return obj._self;
},
set: function (obj, value) {
obj._self = value;
}
};

class SobolImpl {

constructor(dimension) {
if (dimension < 1 || dimension > COEFFICIENTS.length) throw new Error("Out of range dimension");
this.dimension = dimension
this.count = 0;
this.direction = [];
this.x = [];
this.zero = [];

let tmp = [],
direction = this.direction,
zero = this.zero,
x = this.x,
lines = COEFFICIENTS;

for (i = 0; i <= BITS; i++) tmp.push(0);
for (i = 0; i < this.dimension; i++) {
direction[i] = tmp.slice();
x[i] = 0;
zero[i] = 0;
}

for (i = 1; i <= BITS; i++) direction[0][i] = 1 << (BITS - i);
for (var d = 1; d < this.dimension; d++) {
var cells = lines[d].split(/\s+/);
var s = +cells[1];
var a = +cells[2];
var m = [0];
for (i = 0; i < s; i++) m.push(+cells[3 + i]);
for (i = 1; i <= s; i++) direction[d][i] = m[i] << (BITS - i);
for (i = s + 1; i <= BITS; i++) {
direction[d][i] = direction[d][i - s] ^ (direction[d][i - s] >> s);
for (var k = 1; k <= s - 1; k++)
direction[d][i] ^= ((a >> (s - 1 - k)) & 1) * direction[d][i - k];
}
}
}

next() {
if (this.count === 0) {
this.count++;
return this.zero.slice();
}
let v = [];
let c = 1;
let value = this.count - 1;
while ((value & 1) == 1) {
value >>= 1;
c++;
}
for (i = 0; i < this.dimension; i++) {
this.x[i] ^= this.direction[i][c];
v[i] = this.x[i] / SCALE;
}
this.count++;
return v;
}
}

function sobol(dim) {
return new Sobol(dim);
}

export default sobol;
1 change: 1 addition & 0 deletions src/version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var version = "0.1.0"; export default version;
8 changes: 8 additions & 0 deletions test/quant-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var tape = require("tape"),
quant = require('../');


tape('quant version', function(test) {
test.ok(quant.version);
test.end();
});
20 changes: 20 additions & 0 deletions test/sobol-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var tape = require("tape"),
quant = require('../');


tape('test sobol constructor', function(test) {
var sobol = quant.sobol(1);
test.equal(sobol.dimension, 1);
test.equal(sobol.count, 0);
test.end();
});

tape('test sobol next', function(test) {
var sobol = quant.sobol(1);
var v = sobol.next();
test.ok(v);
test.equal(sobol.count, 1);
test.notEqual(v, sobol.next());
test.end();
});

0 comments on commit ab114ce

Please sign in to comment.