~t7-vla7-lz/psiphon/psiphon

« back to all changes in this revision

Viewing changes to trunk/testing/selenium_scripts/selenium/tests/html/dojo-0.4.0-mini/src/math.js

  • Committer: Eugene Fryntov
  • Date: 2016-11-15 22:13:59 UTC
  • mfrom: (373.1.1 psiphon)
  • Revision ID: e.fryntov@psiphon.ca-20161115221359-f6s56ue1a54n4ijj
merged lp:~t7-vla7-lz/psiphon/psiphon @ 374

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
13
 
dojo.math.degToRad = function(/* float */x) {
14
 
        //      summary
15
 
        //      Converts degrees to radians.
16
 
        return (x*Math.PI) / 180;       //      float
17
 
}
18
 
dojo.math.radToDeg = function(/* float */x) { 
19
 
        //      summary
20
 
        //      Converts radians to degrees.
21
 
        return (x*180) / Math.PI;       //      float
22
 
}
23
 
 
24
 
dojo.math.factorial = function(/* integer */n){
25
 
        //      summary
26
 
        //      Returns n!
27
 
        if(n<1){ return 0; }
28
 
        var retVal = 1;
29
 
        for(var i=1;i<=n;i++){ retVal *= i; }
30
 
        return retVal;  //      integer
31
 
}
32
 
 
33
 
dojo.math.permutations = function(/* integer */n, /* integer */k) {
34
 
        //      summary
35
 
        //      The number of ways of obtaining an ordered subset of k elements from a set of n elements
36
 
        if(n==0 || k==0) return 1;
37
 
        return (dojo.math.factorial(n) / dojo.math.factorial(n-k));     //      float
38
 
}
39
 
 
40
 
dojo.math.combinations = function (/* integer */n, /* integer */r) {
41
 
        //      summary
42
 
        //      The number of ways of picking n unordered outcomes from r possibilities
43
 
        if(n==0 || r==0) return 1;
44
 
        return (dojo.math.factorial(n) / (dojo.math.factorial(n-r) * dojo.math.factorial(r)));  //      float
45
 
}
46
 
 
47
 
dojo.math.bernstein = function(/* float */t, /* float */n, /* float */i) {
48
 
        //      summary
49
 
        //      Calculates a weighted average based on the Bernstein theorem.
50
 
        return (dojo.math.combinations(n,i) * Math.pow(t,i) * Math.pow(1-t,n-i));       //      float
51
 
}
52
 
 
53
 
dojo.math.gaussianRandom = function(){
54
 
        //      summary
55
 
        //      Returns random numbers with a Gaussian distribution, with the mean set at 0 and the variance set at 1.
56
 
        var k = 2;
57
 
        do {
58
 
                var i = 2 * Math.random() - 1;
59
 
                var j = 2 * Math.random() - 1;
60
 
                k = i * i + j * j;
61
 
        } while (k >= 1);
62
 
        k = Math.sqrt((-2 * Math.log(k)) / k);
63
 
        return i * k;   //      float
64
 
}
65
 
 
66
 
dojo.math.mean = function() {
67
 
        //      summary
68
 
        //      Calculates the mean of an Array of numbers.
69
 
        var array = dojo.lang.isArray(arguments[0]) ? arguments[0] : arguments;
70
 
        var mean = 0;
71
 
        for (var i = 0; i < array.length; i++) { mean += array[i]; }
72
 
        return mean / array.length;     //      float
73
 
}
74
 
 
75
 
dojo.math.round = function(/* float */number, /* integer */places) {
76
 
        //      summary
77
 
        //      Extends Math.round by adding a second argument specifying the number of decimal places to round to.
78
 
        // TODO: add support for significant figures
79
 
        if (!places) { var shift = 1; }
80
 
        else { var shift = Math.pow(10, places); }
81
 
        return Math.round(number * shift) / shift;      //      float
82
 
}
83
 
 
84
 
dojo.math.sd = dojo.math.standardDeviation = function(/* array */){
85
 
        //      summary
86
 
        //      Calculates the standard deviation of an Array of numbers
87
 
        var array = dojo.lang.isArray(arguments[0]) ? arguments[0] : arguments;
88
 
        return Math.sqrt(dojo.math.variance(array));    //      float
89
 
}
90
 
 
91
 
dojo.math.variance = function(/* array */) {
92
 
        //      summary
93
 
        //      Calculates the variance of an Array of numbers
94
 
        var array = dojo.lang.isArray(arguments[0]) ? arguments[0] : arguments;
95
 
        var mean = 0, squares = 0;
96
 
        for (var i = 0; i < array.length; i++) {
97
 
                mean += array[i];
98
 
                squares += Math.pow(array[i], 2);
99
 
        }
100
 
        return (squares / array.length) - Math.pow(mean / array.length, 2);     //      float
101
 
}
102
 
 
103
 
dojo.math.range = function(/* integer */a, /* integer */b, /* integer */step) {
104
 
        //      summary
105
 
        //      implementation of Python's range()
106
 
    if(arguments.length < 2) {
107
 
        b = a;
108
 
        a = 0;
109
 
    }
110
 
    if(arguments.length < 3) {
111
 
        step = 1;
112
 
    }
113
 
 
114
 
    var range = [];
115
 
    if(step > 0) {
116
 
        for(var i = a; i < b; i += step) {
117
 
            range.push(i);
118
 
        }
119
 
    } else if(step < 0) {
120
 
        for(var i = a; i > b; i += step) {
121
 
            range.push(i);
122
 
        }
123
 
    } else {
124
 
        throw new Error("dojo.math.range: step must be non-zero");
125
 
    }
126
 
    return range;       //      array
127
 
}