Subversion Repositories Applications.papyrus

Rev

Rev 1318 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
1422 alexandre_ 11
 
12
 
1318 alexandre_ 13
dojo.provide("dojo.math");
14
dojo.math.degToRad = function (x) {
15
	return (x * Math.PI) / 180;
16
};
17
dojo.math.radToDeg = function (x) {
18
	return (x * 180) / Math.PI;
19
};
20
dojo.math.factorial = function (n) {
21
	if (n < 1) {
22
		return 0;
23
	}
24
	var retVal = 1;
25
	for (var i = 1; i <= n; i++) {
26
		retVal *= i;
27
	}
28
	return retVal;
29
};
30
dojo.math.permutations = function (n, k) {
31
	if (n == 0 || k == 0) {
32
		return 1;
33
	}
34
	return (dojo.math.factorial(n) / dojo.math.factorial(n - k));
35
};
36
dojo.math.combinations = function (n, r) {
37
	if (n == 0 || r == 0) {
38
		return 1;
39
	}
40
	return (dojo.math.factorial(n) / (dojo.math.factorial(n - r) * dojo.math.factorial(r)));
41
};
42
dojo.math.bernstein = function (t, n, i) {
43
	return (dojo.math.combinations(n, i) * Math.pow(t, i) * Math.pow(1 - t, n - i));
44
};
45
dojo.math.gaussianRandom = function () {
46
	var k = 2;
47
	do {
48
		var i = 2 * Math.random() - 1;
49
		var j = 2 * Math.random() - 1;
50
		k = i * i + j * j;
51
	} while (k >= 1);
52
	k = Math.sqrt((-2 * Math.log(k)) / k);
53
	return i * k;
54
};
55
dojo.math.mean = function () {
56
	var array = dojo.lang.isArray(arguments[0]) ? arguments[0] : arguments;
57
	var mean = 0;
58
	for (var i = 0; i < array.length; i++) {
59
		mean += array[i];
60
	}
61
	return mean / array.length;
62
};
63
dojo.math.round = function (number, places) {
64
	if (!places) {
65
		var shift = 1;
66
	} else {
67
		var shift = Math.pow(10, places);
68
	}
69
	return Math.round(number * shift) / shift;
70
};
71
dojo.math.sd = dojo.math.standardDeviation = function () {
72
	var array = dojo.lang.isArray(arguments[0]) ? arguments[0] : arguments;
73
	return Math.sqrt(dojo.math.variance(array));
74
};
75
dojo.math.variance = function () {
76
	var array = dojo.lang.isArray(arguments[0]) ? arguments[0] : arguments;
77
	var mean = 0, squares = 0;
78
	for (var i = 0; i < array.length; i++) {
79
		mean += array[i];
80
		squares += Math.pow(array[i], 2);
81
	}
82
	return (squares / array.length) - Math.pow(mean / array.length, 2);
83
};
84
dojo.math.range = function (a, b, step) {
85
	if (arguments.length < 2) {
86
		b = a;
87
		a = 0;
88
	}
89
	if (arguments.length < 3) {
90
		step = 1;
91
	}
92
	var range = [];
93
	if (step > 0) {
94
		for (var i = a; i < b; i += step) {
95
			range.push(i);
96
		}
97
	} else {
98
		if (step < 0) {
99
			for (var i = a; i > b; i += step) {
100
				range.push(i);
101
			}
102
		} else {
103
			throw new Error("dojo.math.range: step must be non-zero");
104
		}
105
	}
106
	return range;
107
};
108