From ab114ce0cf8ab8aaee8ca793ed1870b96ab15a69 Mon Sep 17 00:00:00 2001 From: Luca Sbardella Date: Fri, 12 Feb 2016 13:03:43 +0000 Subject: [PATCH] test pass --- .eslintrc | 14 ++++++ .gitignore | 19 ++++++++ .npmignore | 2 + .travis.yml | 9 ++++ index.js | 2 + package.json | 38 +++++++++++++++ rollup.config.js | 10 ++++ src/banner.js | 10 ++++ src/sobol.js | 113 +++++++++++++++++++++++++++++++++++++++++++++ src/version.js | 1 + test/quant-test.js | 8 ++++ test/sobol-test.js | 20 ++++++++ 12 files changed, 246 insertions(+) create mode 100644 .eslintrc create mode 100644 .gitignore create mode 100644 .npmignore create mode 100644 .travis.yml create mode 100644 index.js create mode 100644 package.json create mode 100644 rollup.config.js create mode 100644 src/banner.js create mode 100644 src/sobol.js create mode 100644 src/version.js create mode 100644 test/quant-test.js create mode 100644 test/sobol-test.js diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..bd022aa --- /dev/null +++ b/.eslintrc @@ -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 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d4f31f9 --- /dev/null +++ b/.gitignore @@ -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 diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..6e6b220 --- /dev/null +++ b/.npmignore @@ -0,0 +1,2 @@ +build/*.zip +test/ diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..5eddcb9 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,9 @@ +language: node_js + +install: + - npm install + +before_script: grunt build + +notifications: + email: false diff --git a/index.js b/index.js new file mode 100644 index 0000000..c41efbf --- /dev/null +++ b/index.js @@ -0,0 +1,2 @@ +export {default as sobol} from "./src/sobol"; +export {default as version} from "./src/version"; diff --git a/package.json b/package.json new file mode 100644 index 0000000..538ed05 --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 0000000..327fc9a --- /dev/null +++ b/rollup.config.js @@ -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' +}; diff --git a/src/banner.js b/src/banner.js new file mode 100644 index 0000000..978ea6d --- /dev/null +++ b/src/banner.js @@ -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 %> +// diff --git a/src/sobol.js b/src/sobol.js new file mode 100644 index 0000000..c820fbe --- /dev/null +++ b/src/sobol.js @@ -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; diff --git a/src/version.js b/src/version.js new file mode 100644 index 0000000..a5b6d10 --- /dev/null +++ b/src/version.js @@ -0,0 +1 @@ +var version = "0.1.0"; export default version; \ No newline at end of file diff --git a/test/quant-test.js b/test/quant-test.js new file mode 100644 index 0000000..abf15b0 --- /dev/null +++ b/test/quant-test.js @@ -0,0 +1,8 @@ +var tape = require("tape"), + quant = require('../'); + + +tape('quant version', function(test) { + test.ok(quant.version); + test.end(); +}); diff --git a/test/sobol-test.js b/test/sobol-test.js new file mode 100644 index 0000000..cd2bc45 --- /dev/null +++ b/test/sobol-test.js @@ -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(); +}); +