| 1 |
|
| 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(n){ |
| 8 |
|
| 9 |
|
| 10 |
return (n*Math.PI)/180; |
| 11 |
}, |
| 12 |
toDegrees: function(n){ |
| 13 |
|
| 14 |
|
| 15 |
return (n*180)/Math.PI; |
| 16 |
}, |
| 17 |
degreesToRadians: function(n){ |
| 18 |
|
| 19 |
|
| 20 |
return m.toRadians(n); |
| 21 |
}, |
| 22 |
radiansToDegrees: function(n){ |
| 23 |
|
| 24 |
|
| 25 |
return m.toDegrees(n); |
| 26 |
}, |
| 27 |
|
| 28 |
_gamma: function(z){ |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
var answer = 1; |
| 33 |
|
| 34 |
while (--z >= 1){ |
| 35 |
answer *= z; |
| 36 |
} |
| 37 |
if(z == 0){ return answer; } |
| 38 |
if(Math.floor(z) == z){ return NaN; } |
| 39 |
|
| 40 |
if(z == -0.5){ return Math.sqrt(Math.PI); } |
| 41 |
if(z < -0.5){ |
| 42 |
return Math.PI / (Math.sin(Math.PI * (z + 1)) * this._gamma(-z)); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
var a = 13; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
var c = [ |
| 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(n){ |
| 76 |
|
| 77 |
|
| 78 |
return this._gamma(n+1); |
| 79 |
}, |
| 80 |
|
| 81 |
permutations: function(n, k){ |
| 82 |
|
| 83 |
|
| 84 |
if(n==0 || k==0){ |
| 85 |
return 1; |
| 86 |
} |
| 87 |
return this.factorial(n) / this.factorial(n-k); |
| 88 |
}, |
| 89 |
|
| 90 |
combinations: function(n, r){ |
| 91 |
|
| 92 |
|
| 93 |
if(n==0 || r==0){ |
| 94 |
return 1; |
| 95 |
} |
| 96 |
return this.factorial(n) / (this.factorial(n-r) * this.factorial(r)); |
| 97 |
}, |
| 98 |
|
| 99 |
bernstein: function(t, n, i){ |
| 100 |
|
| 101 |
|
| 102 |
return this.combinations(n, i) * Math.pow(t, i) * Math.pow(1-t, n-i); |
| 103 |
}, |
| 104 |
|
| 105 |
gaussian: function(){ |
| 106 |
|
| 107 |
|
| 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); |
| 115 |
}, |
| 116 |
|
| 117 |
|
| 118 |
range: function(a, b, step){ |
| 119 |
|
| 120 |
|
| 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; |
| 139 |
}, |
| 140 |
|
| 141 |
distance: function(a, b){ |
| 142 |
|
| 143 |
|
| 144 |
return Math.sqrt(Math.pow(b[0]-a[0],2)+Math.pow(b[1]-a[1],2)); |
| 145 |
}, |
| 146 |
|
| 147 |
midpoint: function(a, b){ |
| 148 |
|
| 149 |
|
| 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; |
| 158 |
} |
| 159 |
}); |
| 160 |
|
| 161 |
return dojox.math; |
| 162 |
}); |