1 | // AMD-ID "dojox/math/_base" |
---|
2 | define(["dojo", "dojox"], function(dojo, dojox) { |
---|
3 | dojo.getObject("math", true, dojox); |
---|
4 | |
---|
5 | var m = dojox.math; |
---|
6 | dojo.mixin(dojox.math, { |
---|
7 | toRadians: function(/* Number */n){ |
---|
8 | // summary: |
---|
9 | // Convert the passed number to radians. |
---|
10 | return (n*Math.PI)/180; // Number |
---|
11 | }, |
---|
12 | toDegrees: function(/* Number */n){ |
---|
13 | // summary: |
---|
14 | // Convert the passed number to degrees. |
---|
15 | return (n*180)/Math.PI; // Number |
---|
16 | }, |
---|
17 | degreesToRadians: function(/* Number */n){ |
---|
18 | // summary: |
---|
19 | // Deprecated. Use dojox.math.toRadians. |
---|
20 | return m.toRadians(n); // Number |
---|
21 | }, |
---|
22 | radiansToDegrees: function(/* Number */n){ |
---|
23 | // summary: |
---|
24 | // Deprecated. Use dojox.math.toDegrees. |
---|
25 | return m.toDegrees(n); // Number |
---|
26 | }, |
---|
27 | |
---|
28 | _gamma: function(z){ |
---|
29 | // summary: |
---|
30 | // Compute the gamma function for the passed number. |
---|
31 | // Approximately 14 dijits of precision with non-integers. |
---|
32 | var answer = 1; // 0! |
---|
33 | // gamma(n+1) = n * gamma(n) |
---|
34 | while (--z >= 1){ |
---|
35 | answer *= z; |
---|
36 | } |
---|
37 | if(z == 0){ return answer; } // normal integer quick return |
---|
38 | if(Math.floor(z) == z){ return NaN; } // undefined at nonpositive integers since sin() below will return 0 |
---|
39 | // assert: z < 1, remember this z is really z-1 |
---|
40 | if(z == -0.5){ return Math.sqrt(Math.PI); } // popular gamma(1/2) |
---|
41 | if(z < -0.5){ // remember this z is really z-1 |
---|
42 | return Math.PI / (Math.sin(Math.PI * (z + 1)) * this._gamma(-z)); // reflection |
---|
43 | } |
---|
44 | // assert: -0.5 < z < 1 |
---|
45 | // Spouge approximation algorithm |
---|
46 | var a = 13; |
---|
47 | // c[0] = sqrt(2*PI) / exp(a) |
---|
48 | // var kfact = 1 |
---|
49 | // for (var k=1; k < a; k++){ |
---|
50 | // c[k] = pow(-k + a, k - 0.5) * exp(-k) / kfact |
---|
51 | // kfact *= -k // (-1)^(k-1) * (k-1)! |
---|
52 | // } |
---|
53 | var c = [ // precomputed from the above algorithm |
---|
54 | 5.6658056015186327e-6, |
---|
55 | 1.2743717663379679, |
---|
56 | -4.9374199093155115, |
---|
57 | 7.8720267032485961, |
---|
58 | -6.6760503749436087, |
---|
59 | 3.2525298444485167, |
---|
60 | -9.1852521441026269e-1, |
---|
61 | 1.4474022977730785e-1, |
---|
62 | -1.1627561382389853e-2, |
---|
63 | 4.0117980757066622e-4, |
---|
64 | -4.2652458386405744e-6, |
---|
65 | 6.6651913290336086e-9, |
---|
66 | -1.5392547381874824e-13 |
---|
67 | ]; |
---|
68 | var sum = c[0]; |
---|
69 | for (var k=1; k < a; k++){ |
---|
70 | sum += c[k] / (z + k); |
---|
71 | } |
---|
72 | return answer * Math.pow(z + a, z + 0.5) / Math.exp(z) * sum; |
---|
73 | }, |
---|
74 | |
---|
75 | factorial: function(/* Number */n){ |
---|
76 | // summary: |
---|
77 | // Return the factorial of n |
---|
78 | return this._gamma(n+1); // Number |
---|
79 | }, |
---|
80 | |
---|
81 | permutations: function(/* Number */n, /* Number */k){ |
---|
82 | // summary: |
---|
83 | // TODO |
---|
84 | if(n==0 || k==0){ |
---|
85 | return 1; // Number |
---|
86 | } |
---|
87 | return this.factorial(n) / this.factorial(n-k); |
---|
88 | }, |
---|
89 | |
---|
90 | combinations: function(/* Number */n, /* Number */r){ |
---|
91 | // summary: |
---|
92 | // TODO |
---|
93 | if(n==0 || r==0){ |
---|
94 | return 1; // Number |
---|
95 | } |
---|
96 | return this.factorial(n) / (this.factorial(n-r) * this.factorial(r)); // Number |
---|
97 | }, |
---|
98 | |
---|
99 | bernstein: function(/* Number */t, /* Number */n, /* Number */ i){ |
---|
100 | // summary: |
---|
101 | // TODO |
---|
102 | return this.combinations(n, i) * Math.pow(t, i) * Math.pow(1-t, n-i); // Number |
---|
103 | }, |
---|
104 | |
---|
105 | gaussian: function(){ |
---|
106 | // summary: |
---|
107 | // Return a random number based on the Gaussian algo. |
---|
108 | var k=2; |
---|
109 | do{ |
---|
110 | var i=2*Math.random()-1; |
---|
111 | var j=2*Math.random()-1; |
---|
112 | k = i*i+j*j; |
---|
113 | }while(k>=1); |
---|
114 | return i * Math.sqrt((-2*Math.log(k))/k); // Number |
---|
115 | }, |
---|
116 | |
---|
117 | // create a range of numbers |
---|
118 | range: function(/* Number */a, /* Number? */b, /* Number? */step){ |
---|
119 | // summary: |
---|
120 | // Create a range of numbers based on the parameters. |
---|
121 | if(arguments.length<2){ |
---|
122 | b=a,a=0; |
---|
123 | } |
---|
124 | var range=[], s=step||1, i; |
---|
125 | if(s>0){ |
---|
126 | for(i=a; i<b; i+=s){ |
---|
127 | range.push(i); |
---|
128 | } |
---|
129 | }else{ |
---|
130 | if(s<0){ |
---|
131 | for(i=a; i>b; i+=s){ |
---|
132 | range.push(i); |
---|
133 | } |
---|
134 | }else{ |
---|
135 | throw new Error("dojox.math.range: step must not be zero."); |
---|
136 | } |
---|
137 | } |
---|
138 | return range; // Array |
---|
139 | }, |
---|
140 | |
---|
141 | distance: function(/* Array */a, /* Array */b){ |
---|
142 | // summary: |
---|
143 | // Calculate the distance between point A and point B |
---|
144 | return Math.sqrt(Math.pow(b[0]-a[0],2)+Math.pow(b[1]-a[1],2)); // Number |
---|
145 | }, |
---|
146 | |
---|
147 | midpoint: function(/* Array */a, /* Array */b){ |
---|
148 | // summary: |
---|
149 | // Calculate the midpoint between points A and B. A and B may be multidimensional. |
---|
150 | if(a.length!=b.length){ |
---|
151 | console.error("dojox.math.midpoint: Points A and B are not the same dimensionally.", a, b); |
---|
152 | } |
---|
153 | var m=[]; |
---|
154 | for(var i=0; i<a.length; i++){ |
---|
155 | m[i]=(a[i]+b[i])/2; |
---|
156 | } |
---|
157 | return m; // Array |
---|
158 | } |
---|
159 | }); |
---|
160 | |
---|
161 | return dojox.math; |
---|
162 | }); |
---|