Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | 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
 
11
dojo.provide("dojo.math");
12
dojo.math.degToRad = function (x) {
13
	return (x * Math.PI) / 180;
14
};
15
dojo.math.radToDeg = function (x) {
16
	return (x * 180) / Math.PI;
17
};
18
dojo.math.factorial = function (n) {
19
	if (n < 1) {
20
		return 0;
21
	}
22
	var retVal = 1;
23
	for (var i = 1; i <= n; i++) {
24
		retVal *= i;
25
	}
26
	return retVal;
27
};
28
dojo.math.permutations = function (n, k) {
29
	if (n == 0 || k == 0) {
30
		return 1;
31
	}
32
	return (dojo.math.factorial(n) / dojo.math.factorial(n - k));
33
};
34
dojo.math.combinations = function (n, r) {
35
	if (n == 0 || r == 0) {
36
		return 1;
37
	}
38
	return (dojo.math.factorial(n) / (dojo.math.factorial(n - r) * dojo.math.factorial(r)));
39
};
40
dojo.math.bernstein = function (t, n, i) {
41
	return (dojo.math.combinations(n, i) * Math.pow(t, i) * Math.pow(1 - t, n - i));
42
};
43
dojo.math.gaussianRandom = function () {
44
	var k = 2;
45
	do {
46
		var i = 2 * Math.random() - 1;
47
		var j = 2 * Math.random() - 1;
48
		k = i * i + j * j;
49
	} while (k >= 1);
50
	k = Math.sqrt((-2 * Math.log(k)) / k);
51
	return i * k;
52
};
53
dojo.math.mean = function () {
54
	var array = dojo.lang.isArray(arguments[0]) ? arguments[0] : arguments;
55
	var mean = 0;
56
	for (var i = 0; i < array.length; i++) {
57
		mean += array[i];
58
	}
59
	return mean / array.length;
60
};
61
dojo.math.round = function (number, places) {
62
	if (!places) {
63
		var shift = 1;
64
	} else {
65
		var shift = Math.pow(10, places);
66
	}
67
	return Math.round(number * shift) / shift;
68
};
69
dojo.math.sd = dojo.math.standardDeviation = function () {
70
	var array = dojo.lang.isArray(arguments[0]) ? arguments[0] : arguments;
71
	return Math.sqrt(dojo.math.variance(array));
72
};
73
dojo.math.variance = function () {
74
	var array = dojo.lang.isArray(arguments[0]) ? arguments[0] : arguments;
75
	var mean = 0, squares = 0;
76
	for (var i = 0; i < array.length; i++) {
77
		mean += array[i];
78
		squares += Math.pow(array[i], 2);
79
	}
80
	return (squares / array.length) - Math.pow(mean / array.length, 2);
81
};
82
dojo.math.range = function (a, b, step) {
83
	if (arguments.length < 2) {
84
		b = a;
85
		a = 0;
86
	}
87
	if (arguments.length < 3) {
88
		step = 1;
89
	}
90
	var range = [];
91
	if (step > 0) {
92
		for (var i = a; i < b; i += step) {
93
			range.push(i);
94
		}
95
	} else {
96
		if (step < 0) {
97
			for (var i = a; i > b; i += step) {
98
				range.push(i);
99
			}
100
		} else {
101
			throw new Error("dojo.math.range: step must be non-zero");
102
		}
103
	}
104
	return range;
105
};
106