math_wev8.js
2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
Copyright (c) 2004-2005, The Dojo Foundation
All Rights Reserved.
Licensed under the Academic Free License version 2.1 or above OR the
modified BSD license. For more information on Dojo licensing, see:
http://dojotoolkit.org/community/licensing.shtml
*/
dojo.provide("dojo.math");
dojo.math.degToRad = function (x) { return (x*Math.PI) / 180; }
dojo.math.radToDeg = function (x) { return (x*180) / Math.PI; }
dojo.math.factorial = function (n) {
if(n<1){ return 0; }
var retVal = 1;
for(var i=1;i<=n;i++){ retVal *= i; }
return retVal;
}
//The number of ways of obtaining an ordered subset of k elements from a set of n elements
dojo.math.permutations = function (n,k) {
if(n==0 || k==0) return 1;
return (dojo.math.factorial(n) / dojo.math.factorial(n-k));
}
//The number of ways of picking n unordered outcomes from r possibilities
dojo.math.combinations = function (n,r) {
if(n==0 || r==0) return 1;
return (dojo.math.factorial(n) / (dojo.math.factorial(n-r) * dojo.math.factorial(r)));
}
dojo.math.bernstein = function (t,n,i) {
return (dojo.math.combinations(n,i) * Math.pow(t,i) * Math.pow(1-t,n-i));
}
/**
* Returns random numbers with a Gaussian distribution, with the mean set at
* 0 and the variance set at 1.
*
* @return A random number from a Gaussian distribution
*/
dojo.math.gaussianRandom = function () {
var k = 2;
do {
var i = 2 * Math.random() - 1;
var j = 2 * Math.random() - 1;
k = i * i + j * j;
} while (k >= 1);
k = Math.sqrt((-2 * Math.log(k)) / k);
return i * k;
}
/**
* Calculates the mean of an Array of numbers.
*
* @return The mean of the numbers in the Array
*/
dojo.math.mean = function () {
var array = dojo.lang.isArray(arguments[0]) ? arguments[0] : arguments;
var mean = 0;
for (var i = 0; i < array.length; i++) { mean += array[i]; }
return mean / array.length;
}
/**
* Extends Math.round by adding a second argument specifying the number of
* decimal places to round to.
*
* @param number The number to round
* @param places The number of decimal places to round to
* @return The rounded number
*/
// TODO: add support for significant figures
dojo.math.round = function (number, places) {
if (!places) { var shift = 1; }
else { var shift = Math.pow(10, places); }
return Math.round(number * shift) / shift;
}
/**
* Calculates the standard deviation of an Array of numbers
*
* @return The standard deviation of the numbers
*/
dojo.math.sd = function () {
var array = dojo.lang.isArray(arguments[0]) ? arguments[0] : arguments;
return Math.sqrt(dojo.math.variance(array));
}
/**
* Calculates the variance of an Array of numbers
*
* @return The variance of the numbers
*/
dojo.math.variance = function () {
var array = dojo.lang.isArray(arguments[0]) ? arguments[0] : arguments;
var mean = 0, squares = 0;
for (var i = 0; i < array.length; i++) {
mean += array[i];
squares += Math.pow(array[i], 2);
}
return (squares / array.length)
- Math.pow(mean / array.length, 2);
}