25 Years of Programming
An open source source for C, C++, OWL, BASIC, MDB, XLS, DOT, and more...
Home   Projects   Up   Sitemap   Search   Blog   Forum+Chat   About Us   Privacy   Terms of Use   Feedback   FAQ   Images   Services   Payments   Humor   Music

mylib.js - JavaScript function library

While creating JavaScript versions of some of my C and C++ programs to run on web pages, I'll need to convert utility functions from mylib.cpp. The source code is listed on this page.

You can copy the code below, or download (Save) the file (3 KB).

There's not much here yet.

mylib.js

 
/*	mylib.js		2-27-2009		JavaScript
	Copyright (C)2008-09 Steven Whitney.
	Initially published by http://25yearsofprogramming.com.

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License (GPL)
	Version 3 as published by the Free Software Foundation.
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

Library of utility functions.

*/
//----------------------------------------------------------------------------------------------
// RANDOM NUMBERS
//----------------------------------------------------------------------------------------------
// Returns a normally distributed random number from the distribution with the given mean and standard deviation. 
// That is, values close to the mean are returned often, values far from the mean are returned rarely.
// To get nearest integer: Math.round(NormalRandom(mean,stddev)).
function NormalRandom(mean, stddev)
{
	return Math.sqrt(-2 * Math.log(Math.random())) * Math.cos(2 * Math.PI * Math.random()) * stddev + mean;
}
//----------------------------------------------------------------------------------------------
// DATE AND TIME
//----------------------------------------------------------------------------------------------
// Number of calendar days between two dates.
// StartDate and EndDate must be Date objects. If StartDate > EndDate, returns negative.
function CalendarDaysBetween(StartDate, EndDate)
{
	// Explicitly set time to 0, even though a date specified with mm/dd/yyyy has 0 in all its time components.
	StartDate.setHours(0,0,0,0);
	EndDate.setHours(0,0,0,0); 
	return Math.round((EndDate - StartDate) / 86400000); //milliseconds in a day. round ensures it's an integer. 
}
////////////////////////////////////////////////////////////////////////////////////////////////
//	MODIFIED GNU SCIENTIFIC LIBRARY FUNCTIONS
//----------------------------------------------------------------------------------------------
/*
	The functions in this section are copied and greatly modified from the 
	GNU Scientific Library version 1.12, specifically from exponential.c, levy.c. 
	Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 James Theiler, Brian Gough
	Copyright (C) 2009 Steven Whitney. The following modifications made:
	2-27-2009:
	Converted my C++ conversions of these functions to JavaScript; 
	substituted JavaScript methods for calls to external GSL functions;
	used different function names, condensed, reorganized, reformatted.
*/	
//----------------------------------------------------------------------------
// RANDOM NUMBER FROM EXPONENTIAL DISTRIBUTION
//----------------------------------------------------------------------------
// The exponential distribution has the form p(x) dx = exp(-x/mu) dx/mu, for x = 0 to +infinity 
function GSLRandomExponential(mu)
{
var u;
do			// emulate gsl_rng_uniform_pos()
{
	u = Math.random()	// returns [0-1)
}
while(u == 0);			// discard 0
return -mu * Math.log(u);
}
//----------------------------------------------------------------------------
//	RANDOM NUMBER FROM LEVY ALPHA-STABLE DISTRIBUTION
//----------------------------------------------------------------------------
/*
	The stable Levy probability distributions have the form
	p(x) dx = (1/(2 pi)) \int dt exp(- it x - |c t|^alpha)
	with 0 < alpha <= 2. 

	For alpha = 1, we get the Cauchy distribution
	For alpha = 2, we get the Gaussian distribution with sigma = sqrt(2) c.

	From Chapter 5 of Bratley, Fox and Schrage "A Guide to Simulation". The original reference given there is,
	J.M. Chambers, C.L. Mallows and B. W. Stuck. "A method for simulating stable random variates". 
	Journal of the American Statistical Association, JASA 71 340-344 (1976).

	See also: 
	http://en.wikipedia.org/wiki/Stable_distributions 
	http://en.wikipedia.org/wiki/L%C3%A9vy_distribution
	Apparent properties of the values returned: 
	mean (mu) = 0; 
	c = scale (dispersion) parameter, analogous to standard deviation in a normal distribution.
	alpha = kurtosis. High alpha -> broad peak, platykurtotic. Low alpha -> pointy with fat tails, leptokurtotic.
	This function does not provide for a beta (skewness) value.
*/
function GSLRandomLevy(c, alpha)
{
// enforce limits by returning a value that will be obviously wrong (all zeroes)
if((alpha <= 0) || (alpha > 2))	
	return 0;
	
var u;
do			// emulate gsl_rng_uniform_pos()
{
	u = Math.random()	// returns [0-1)
}
while(u == 0);				// discard 0
u = Math.PI * (u - 0.5);

if(alpha == 1)				// Cauchy case 
	return c * Math.tan(u);

var v;
do			// emulate gsl_ran_exponential()
{
	v = GSLRandomExponential(1);
}
while(v == 0);

if(alpha == 2)				// Gaussian case 
	return c * (2 * Math.sin(u) * Math.sqrt(v));

// general case
var t = Math.sin(alpha * u) / Math.pow(Math.cos(u), 1 / alpha);
var s = Math.pow(Math.cos((1 - alpha) * u) / v, (1 - alpha) / alpha);
return c * t * s;
}
//----------------------------------------------------------------------------
// END OF MODIFIED GNU SCIENTIFIC LIBRARY FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////

 

Valid HTML 4.01 Transitional Valid CSS
Yahoo! Search
Search the web Search this site
View content labeling at ICRA.