Permalink
Browse files

Gaussian deviation now required

  • Loading branch information...
wdavidw committed Apr 20, 2011
1 parent 9c01053 commit 8e21b7cb2a8eb5133924ccd43ada97634616a40d
Showing with 100 additions and 75 deletions.
  1. +14 −15 readme.md
  2. +5 −8 samples/gaussian.js
  3. +6 −6 samples/gaussian_density.js
  4. +6 −6 samples/gaussian_speed.js
  5. +6 −9 src/gaussian.cc
  6. +29 −11 src/random.cc
  7. +34 −20 test/gaussian.js
View
@@ -56,39 +56,38 @@ 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
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(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
@@ -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) );
@@ -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
@@ -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,15 +57,15 @@ 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);
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,15 +74,15 @@ 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);
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
@@ -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
@@ -18,19 +18,27 @@ 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);
}
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,13 +110,19 @@ 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));
}
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));
}
Oops, something went wrong.

0 comments on commit 8e21b7c

Please sign in to comment.