Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Gaussian deviation now required

  • Loading branch information...
commit 8e21b7cb2a8eb5133924ccd43ada97634616a40d 1 parent 9c01053
@wdavidw authored
View
29 readme.md
@@ -56,23 +56,25 @@ Seeds are always optional and must be provided as unsigned integers. Deviations,
*Random.uniform([])*
Returns a double precision floating point number uniformly distributed in the range [0,1). The range includes 0.0 but excludes 1.0.
-### [Gaussian](http://www.gnu.org/software/gsl/manual/html_node/The-Gaussian-Distribution.html)
+### [The Gaussian Distribution](http://www.gnu.org/software/gsl/manual/html_node/The-Gaussian-Distribution.html)
-- *random.gaussian([seed], [deviation])*
+- *random.gaussian([seed], deviation)*
*Random.gaussian([deviation])*
- Returns a Gaussian random float with mean zero. Standart deviation is a float and default to 3.
+ Returns a Gaussian random float with mean zero given a standart deviation as a float.
-- *random.gaussianZiggurat([seed], [deviation])*
- *Random.gaussianZiggurat([deviation])*
+- *random.gaussianZiggurat([seed], deviation)*
+ *Random.gaussianZiggurat(deviation)*
Same as `random.gaussian` but using the alternative Marsaglia-Tsang ziggurat method.
-- *random.gaussianRatioMethod([seed], [deviation])*
- *Random.gaussianRatioMethod([deviation])*
+- *random.gaussianRatioMethod([seed], deviation)*
+ *Random.gaussianRatioMethod(deviation)*
Same as `random.gaussian` but using the alternative Kinderman-Monahan-Leva ratio method.
+### [The Poisson Distribution](http://www.gnu.org/software/gsl/manual/html_node/The-Poisson-Distribution.html)
+
- *random.poisson([seed], mean)*
*Random.poisson(mean)*
- Returns a random integer from the Poisson distribution given a provided mean.
+ Returns a random integer from the Poisson distribution given a provided mean as a float.
Exemple
@@ -80,15 +82,12 @@ Exemple
seed = 50,
deviation = 0.5;
- console.log( gsl.random.gaussian() );
- console.log( gsl.random.gaussian(seed) );
+ console.log( gsl.random.gaussian(deviation) );
console.log( gsl.random.gaussian(seed, deviation) );
- var it1 = new gsl.Random();
- console.log( it1.gaussian() );
-
- var it1 = new gsl.Random(seed);
- console.log( it1.gaussian(deviation) );
+ var iterator = new gsl.Random(seed);
+ console.log( iterator.gaussian(deviation) );
+ console.log( iterator.gaussian(deviation) );
Running the tests
-----------------
View
13 samples/gaussian.js
@@ -2,13 +2,10 @@
var gsl = require('gsl'),
seed = 50,
deviation = 0.5;
-
- console.log( gsl.random.gaussian() );
- console.log( gsl.random.gaussian(seed) );
- console.log( gsl.random.gaussian(seed, deviation) );
- var it1 = new gsl.Random();
- console.log( it1.gaussian() );
+ console.log( gsl.random.gaussian(deviation) );
+ console.log( gsl.random.gaussian(seed, deviation) );
- var it1 = new gsl.Random(seed);
- console.log( it1.gaussian(deviation) );
+ var iterator = new gsl.Random(seed);
+ console.log( iterator.gaussian(deviation) );
+ console.log( iterator.gaussian(deviation) );
View
12 samples/gaussian_density.js
@@ -3,10 +3,10 @@ var gsl = require('gsl'),
fs = require('fs'),
spawn = require('child_process').spawn;
- var stream = fs.createWriteStream(__dirname+'/gaussian_density/function_dev_no.data');
+ var stream = fs.createWriteStream(__dirname+'/gaussian_density/function_dev_1.data');
var buf = '';
for(var i=0;i<10000;i++){
- buf += gsl.random.gaussian();
+ buf += gsl.random.gaussian(1);
buf += '\n';
}
stream.write(buf);
@@ -15,17 +15,17 @@ var gsl = require('gsl'),
var stream = fs.createWriteStream(__dirname+'/gaussian_density/function_dev_10.data');
var buf = '';
for(var i=0;i<10000;i++){
- buf += gsl.random.gaussian(i,10);
+ buf += gsl.random.gaussian(10);
buf += '\n';
}
stream.write(buf);
stream.end();
- var stream = fs.createWriteStream(__dirname+'/gaussian_density/object_dev_no.data');
+ var stream = fs.createWriteStream(__dirname+'/gaussian_density/object_dev_1.data');
var ran = new gsl.Random();
var buf = '';
for(var i=0;i<10000;i++){
- buf += ran.gaussian();
+ buf += ran.gaussian(1);
buf += '\n';
}
stream.write(buf);
@@ -42,7 +42,7 @@ var gsl = require('gsl'),
stream.end();
var cmd = [''];
- ['function_dev_no','function_dev_10','object_dev_no','object_dev_10'].forEach(function(type){
+ ['function_dev_1','function_dev_10','object_dev_1','object_dev_10'].forEach(function(type){
cmd.push('png(filename="'+__dirname+'/gaussian_density/'+type+'.png", height=295, width=500, bg="white")');
cmd.push('data <- read.csv("'+__dirname+'/gaussian_density/'+type+'.data", header=F, sep="\\t")');
cmd.push('d <- density(data[,1])');
View
12 samples/gaussian_speed.js
@@ -48,7 +48,7 @@ var assert = require('assert'),
var time = (new Date).getTime();
for(var i=0;i<1000000;i++){
for(var j=0;j<2;j++){
- gsl.random.gaussian(1978);
+ gsl.random.gaussian(1978,1);
}
}
console.log('C++ fn',(new Date).getTime() - time);
@@ -57,7 +57,7 @@ var assert = require('assert'),
var random = new gsl.Random(1978);
for(var i=0;i<1000000;i++){
for(var j=0;j<2;j++){
- random.gaussian();
+ random.gaussian(1);
}
}
console.log('C++ it',(new Date).getTime() - time);
@@ -65,7 +65,7 @@ var assert = require('assert'),
var time = (new Date).getTime();
for(var i=0;i<1000000;i++){
for(var j=0;j<2;j++){
- gsl.random.gaussianZiggurat(1978);
+ gsl.random.gaussianZiggurat(1978,1);
}
}
console.log('C++ fn Ziggurat',(new Date).getTime() - time);
@@ -74,7 +74,7 @@ var assert = require('assert'),
var random = new gsl.Random(1978);
for(var i=0;i<1000000;i++){
for(var j=0;j<2;j++){
- random.gaussianZiggurat();
+ random.gaussianZiggurat(1);
}
}
console.log('C++ it Ziggurat',(new Date).getTime() - time);
@@ -82,7 +82,7 @@ var assert = require('assert'),
var time = (new Date).getTime();
for(var i=0;i<1000000;i++){
for(var j=0;j<2;j++){
- gsl.random.gaussianRatioMethod(1978);
+ gsl.random.gaussianRatioMethod(1978,1);
}
}
console.log('C++ fn RatioMethod',(new Date).getTime() - time);
@@ -91,7 +91,7 @@ var assert = require('assert'),
var random = new gsl.Random(1978);
for(var i=0;i<1000000;i++){
for(var j=0;j<2;j++){
- random.gaussianRatioMethod();
+ random.gaussianRatioMethod(1);
}
}
console.log('C++ it RatioMethod',(new Date).getTime() - time);
View
15 src/gaussian.cc
@@ -21,10 +21,9 @@ Handle<Value> ranGaussian(const Arguments& args)
gsl_rng_set (r_global, args[0]->Uint32Value());
deviation = args[1]->NumberValue();
}else if(args.Length() == 1){
- gsl_rng_set (r_global, args[0]->Uint32Value());
- deviation = 3;
+ deviation = args[0]->NumberValue();
}else{
- deviation = 3;
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
}
double val = gsl_ran_gaussian(r_global, deviation);
return Number::New(val);
@@ -37,10 +36,9 @@ Handle<Value> ranGaussianZiggurat(const Arguments& args)
gsl_rng_set (r_global, args[0]->Uint32Value());
deviation = args[1]->NumberValue();
}else if(args.Length() == 1){
- gsl_rng_set (r_global, args[0]->Uint32Value());
- deviation = 3;
+ deviation = args[0]->NumberValue();
}else{
- deviation = 3;
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
}
double val = gsl_ran_gaussian_ziggurat(r_global, deviation);
return Number::New(val);
@@ -53,10 +51,9 @@ Handle<Value> ranGaussianRatioMethod(const Arguments& args)
gsl_rng_set (r_global, args[0]->Uint32Value());
deviation = args[1]->NumberValue();
}else if(args.Length() == 1){
- gsl_rng_set (r_global, args[0]->Uint32Value());
- deviation = 3;
+ deviation = args[0]->NumberValue();
}else{
- deviation = 3;
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
}
double val = gsl_ran_gaussian_ratio_method(r_global, deviation);
return Number::New(val);
View
40 src/random.cc
@@ -18,6 +18,8 @@ Handle<Value> rngGet(const Arguments& args)
{
if(args.Length() == 1){
gsl_rng_set (r_global, args[0]->Uint32Value());
+ }else if(args.Length() != 0){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
}
unsigned int val = gsl_rng_get(r_global);
return Int32::NewFromUnsigned(val);
@@ -25,12 +27,18 @@ Handle<Value> rngGet(const Arguments& args)
Handle<Value> rngMin(const Arguments& args)
{
+ if(args.Length() != 0){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
double val = gsl_rng_min(r_global);
return Int32::NewFromUnsigned(val);
}
Handle<Value> rngMax(const Arguments& args)
{
+ if(args.Length() != 0){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
double val = gsl_rng_max(r_global);
return Int32::NewFromUnsigned(val);
}
@@ -39,6 +47,8 @@ Handle<Value> rngUniform(const Arguments& args)
{
if(args.Length() == 1){
gsl_rng_set (r_global, args[0]->Uint32Value());
+ }else if(args.Length() != 0){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
}
double val = gsl_rng_uniform(r_global);
return Number::New(val);
@@ -100,6 +110,9 @@ class Random: public ObjectWrap
static Handle<Value> Get(const Arguments& args)
{
HandleScope scope;
+ if(args.Length() != 0){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
Random* g = ObjectWrap::Unwrap<Random>(args.Holder());
unsigned int val = gsl_rng_get(g->_gsl_rng);
return scope.Close(Int32::NewFromUnsigned(val));
@@ -107,6 +120,9 @@ class Random: public ObjectWrap
static Handle<Value> Uniform(const Arguments& args)
{
HandleScope scope;
+ if(args.Length() != 0){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
Random* g = ObjectWrap::Unwrap<Random>(args.Holder());
double val = gsl_rng_uniform(g->_gsl_rng);
return scope.Close(Number::New(val));
@@ -116,10 +132,10 @@ class Random: public ObjectWrap
HandleScope scope;
Random* g = ObjectWrap::Unwrap<Random>(args.Holder());
double deviation;
- if(args.Length() == 0){
- deviation = 3;
- }else{
+ if(args.Length() == 1){
deviation = args[0]->NumberValue();
+ }else{
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
}
double val = gsl_ran_gaussian(g->_gsl_rng, deviation);
return scope.Close(Number::New(val));
@@ -129,10 +145,10 @@ class Random: public ObjectWrap
HandleScope scope;
Random* g = ObjectWrap::Unwrap<Random>(args.Holder());
double deviation;
- if(args.Length() == 0){
- deviation = 3;
- }else{
+ if(args.Length() == 1){
deviation = args[0]->NumberValue();
+ }else{
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
}
double val = gsl_ran_gaussian_ziggurat(g->_gsl_rng, deviation);
return scope.Close(Number::New(val));
@@ -142,10 +158,10 @@ class Random: public ObjectWrap
HandleScope scope;
Random* g = ObjectWrap::Unwrap<Random>(args.Holder());
double deviation;
- if(args.Length() == 0){
- deviation = 3;
- }else{
+ if(args.Length() == 1){
deviation = args[0]->NumberValue();
+ }else{
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
}
double val = gsl_ran_gaussian_ratio_method(g->_gsl_rng, deviation);
return scope.Close(Number::New(val));
@@ -154,10 +170,12 @@ class Random: public ObjectWrap
{
HandleScope scope;
Random* g = ObjectWrap::Unwrap<Random>(args.Holder());
- if(args.Length() != 1){
+ double mean;
+ if(args.Length() == 1){
+ mean = args[0]->NumberValue();
+ }else{
return ThrowException(Exception::TypeError(String::New("Invalid argument")));
}
- double mean = args[0]->NumberValue();
double val = gsl_ran_poisson(g->_gsl_rng, mean);
return scope.Close(Number::New(val));
}
View
54 test/gaussian.js
@@ -3,10 +3,24 @@ var assert = require('assert'),
gsl = require('gsl');
module.exports = {
+ 'Test gaussian - arguments': function(){
+ try{
+ gsl.random.gaussian();
+ assert.ok(false);
+ }catch(e){
+ assert.eql('Invalid argument',e.message);
+ }
+ try{
+ (new gsl.Random()).gaussian();
+ assert.ok(false);
+ }catch(e){
+ assert.eql('Invalid argument',e.message);
+ }
+ },
'Test gaussian - function': function(){
var results = {};
for(var i=0;i<100;i++){
- var random = gsl.random.gaussian();
+ var random = gsl.random.gaussian(1);
assert.ok( 'number', typeof random );
assert.ok( !isNaN(random) );
results[ random ] = true;
@@ -16,10 +30,10 @@ module.exports = {
'Test gaussian - function with seed': function(){
var results1 = {}, results2 = {};
for(var i=0;i<100;i++){
- results1[ gsl.random.gaussian(1978) ] = true;
+ results1[ gsl.random.gaussian(1978,1) ] = true;
}
for(var i=0;i<100;i++){
- results2[ gsl.random.gaussian(1978) ] = true;
+ results2[ gsl.random.gaussian(1978,1) ] = true;
}
assert.eql(1,Object.keys(results1).length);
assert.eql(results1,results2);
@@ -28,7 +42,7 @@ module.exports = {
var results = {};
var obj = new gsl.Random();
for(var i=0;i<100;i++){
- var random = obj.gaussian();
+ var random = obj.gaussian(1);
assert.ok( 'number', typeof random );
assert.ok( !isNaN(random) );
results[ random ] = true;
@@ -37,13 +51,13 @@ module.exports = {
},
'Test gaussian - object with seed': function(){
var results1 = {}, results2 = {};
- var obj1 = new gsl.Random(1978);
+ var obj1 = new gsl.Random(1978,1);
for(var i=0;i<100;i++){
- results1[ obj1.gaussian() ] = true;
+ results1[ obj1.gaussian(1) ] = true;
}
- var obj2 = new gsl.Random(1978);
+ var obj2 = new gsl.Random(1978,1);
for(var i=0;i<100;i++){
- results2[ obj2.gaussian() ] = true;
+ results2[ obj2.gaussian(1) ] = true;
}
assert.eql(100,Object.keys(results1).length);
assert.eql(results1,results2);
@@ -51,7 +65,7 @@ module.exports = {
'Test gaussian ziggurat - function': function(){
var results = {};
for(var i=0;i<100;i++){
- var random = gsl.random.gaussianZiggurat();
+ var random = gsl.random.gaussianZiggurat(1);
assert.ok( 'number', typeof random );
assert.ok( !isNaN(random) );
results[ random ] = true;
@@ -61,10 +75,10 @@ module.exports = {
'Test gaussian ziggurat - function with seed': function(){
var results1 = {}, results2 = {};
for(var i=0;i<100;i++){
- results1[ gsl.random.gaussianZiggurat(1978) ] = true;
+ results1[ gsl.random.gaussianZiggurat(1978,1) ] = true;
}
for(var i=0;i<100;i++){
- results2[ gsl.random.gaussianZiggurat(1978) ] = true;
+ results2[ gsl.random.gaussianZiggurat(1978,1) ] = true;
}
assert.eql(1,Object.keys(results1).length);
assert.eql(results1,results2);
@@ -73,7 +87,7 @@ module.exports = {
var results = {};
var obj = new gsl.Random();
for(var i=0;i<100;i++){
- var random = obj.gaussianZiggurat();
+ var random = obj.gaussianZiggurat(1);
assert.ok( 'number', typeof random );
assert.ok( !isNaN(random) );
results[ random ] = true;
@@ -84,11 +98,11 @@ module.exports = {
var results1 = {}, results2 = {};
var obj1 = new gsl.Random(1978);
for(var i=0;i<100;i++){
- results1[ obj1.gaussianZiggurat() ] = true;
+ results1[ obj1.gaussianZiggurat(1) ] = true;
}
var obj2 = new gsl.Random(1978);
for(var i=0;i<100;i++){
- results2[ obj2.gaussianZiggurat() ] = true;
+ results2[ obj2.gaussianZiggurat(1) ] = true;
}
assert.eql(100,Object.keys(results1).length);
assert.eql(results1,results2);
@@ -96,7 +110,7 @@ module.exports = {
'Test gaussian ratio method - function': function(){
var results = {};
for(var i=0;i<100;i++){
- var random = gsl.random.gaussianRatioMethod();
+ var random = gsl.random.gaussianRatioMethod(1);
assert.ok( 'number', typeof random );
assert.ok( !isNaN(random) );
results[ random ] = true;
@@ -106,10 +120,10 @@ module.exports = {
'Test gaussian ratio method - function with seed': function(){
var results1 = {}, results2 = {};
for(var i=0;i<100;i++){
- results1[ gsl.random.gaussianRatioMethod(1978) ] = true;
+ results1[ gsl.random.gaussianRatioMethod(1978,1) ] = true;
}
for(var i=0;i<100;i++){
- results2[ gsl.random.gaussianRatioMethod(1978) ] = true;
+ results2[ gsl.random.gaussianRatioMethod(1978,1) ] = true;
}
assert.eql(1,Object.keys(results1).length);
assert.eql(results1,results2);
@@ -118,7 +132,7 @@ module.exports = {
var results = {};
var obj = new gsl.Random();
for(var i=0;i<100;i++){
- var random = obj.gaussianRatioMethod();
+ var random = obj.gaussianRatioMethod(1);
assert.ok( 'number', typeof random );
assert.ok( !isNaN(random) );
results[ random ] = true;
@@ -129,11 +143,11 @@ module.exports = {
var results1 = {}, results2 = {};
var obj1 = new gsl.Random(1978);
for(var i=0;i<100;i++){
- results1[ obj1.gaussianRatioMethod() ] = true;
+ results1[ obj1.gaussianRatioMethod(1) ] = true;
}
var obj2 = new gsl.Random(1978);
for(var i=0;i<100;i++){
- results2[ obj2.gaussianRatioMethod() ] = true;
+ results2[ obj2.gaussianRatioMethod(1) ] = true;
}
assert.eql(100,Object.keys(results1).length);
assert.eql(results1,results2);
Please sign in to comment.
Something went wrong with that request. Please try again.