root / trunk / web / dojo / dojox / math / _base.js @ 10
History | View | Annotate | Download (2.24 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
4 | see: http://dojotoolkit.org/license for details
|
||
5 | */
|
||
6 | |||
7 | |||
8 | if(!dojo._hasResource["dojox.math._base"]){ |
||
9 | dojo._hasResource["dojox.math._base"]=true; |
||
10 | dojo.provide("dojox.math._base");
|
||
11 | (function(){
|
||
12 | var m=dojox.math;
|
||
13 | dojo.mixin(dojox.math,{toRadians:function(n){ |
||
14 | return (n*Math.PI)/180; |
||
15 | },toDegrees:function(n){ |
||
16 | return (n*180)/Math.PI; |
||
17 | },degreesToRadians:function(n){ |
||
18 | return m.toRadians(n);
|
||
19 | },radiansToDegrees:function(n){ |
||
20 | return m.toDegrees(n);
|
||
21 | },_gamma:function(z){ |
||
22 | var _1=1; |
||
23 | while(--z>=1){ |
||
24 | _1*=z; |
||
25 | } |
||
26 | if(z==0){ |
||
27 | return _1;
|
||
28 | } |
||
29 | if(Math.floor(z)==z){
|
||
30 | return NaN; |
||
31 | } |
||
32 | if(z==-0.5){ |
||
33 | return Math.sqrt(Math.PI);
|
||
34 | } |
||
35 | if(z<-0.5){ |
||
36 | return Math.PI/(Math.sin(Math.PI*(z+1))*this._gamma(-z)); |
||
37 | } |
||
38 | var a=13; |
||
39 | var c=[0.000005665805601518633,1.274371766337968,-4.937419909315511,7.872026703248596,-6.676050374943609,3.252529844448517,-0.9185252144102627,0.14474022977730785,-0.011627561382389852,0.0004011798075706662,-0.0000042652458386405745,6.665191329033609e-9,-1.5392547381874824e-13]; |
||
40 | var _2=c[0]; |
||
41 | for(var k=1;k<a;k++){ |
||
42 | _2+=c[k]/(z+k); |
||
43 | } |
||
44 | return _1*Math.pow(z+a,z+0.5)/Math.exp(z)*_2; |
||
45 | },factorial:function(n){ |
||
46 | return this._gamma(n+1); |
||
47 | },permutations:function(n,k){ |
||
48 | if(n==0||k==0){ |
||
49 | return 1; |
||
50 | } |
||
51 | return this.factorial(n)/this.factorial(n-k); |
||
52 | },combinations:function(n,r){ |
||
53 | if(n==0||r==0){ |
||
54 | return 1; |
||
55 | } |
||
56 | return this.factorial(n)/(this.factorial(n-r)*this.factorial(r)); |
||
57 | },bernstein:function(t,n,i){ |
||
58 | return this.combinations(n,i)*Math.pow(t,i)*Math.pow(1-t,n-i); |
||
59 | },gaussian:function(){ |
||
60 | var k=2; |
||
61 | do{
|
||
62 | var i=2*Math.random()-1; |
||
63 | var j=2*Math.random()-1; |
||
64 | k=i*i+j*j; |
||
65 | }while(k>=1); |
||
66 | return i*Math.sqrt((-2*Math.log(k))/k); |
||
67 | },range:function(a,b,_3){ |
||
68 | if(arguments.length<2){ |
||
69 | b=a,a=0;
|
||
70 | } |
||
71 | var _4=[],s=_3||1,i; |
||
72 | if(s>0){ |
||
73 | for(i=a;i<b;i+=s){
|
||
74 | _4.push(i); |
||
75 | } |
||
76 | }else{
|
||
77 | if(s<0){ |
||
78 | for(i=a;i>b;i+=s){
|
||
79 | _4.push(i); |
||
80 | } |
||
81 | }else{
|
||
82 | throw new Error("dojox.math.range: step must not be zero."); |
||
83 | } |
||
84 | } |
||
85 | return _4;
|
||
86 | },distance:function(a,b){ |
||
87 | return Math.sqrt(Math.pow(b[0]-a[0],2)+Math.pow(b[1]-a[1],2)); |
||
88 | },midpoint:function(a,b){ |
||
89 | if(a.length!=b.length){
|
||
90 | console.error("dojox.math.midpoint: Points A and B are not the same dimensionally.",a,b);
|
||
91 | } |
||
92 | var m=[];
|
||
93 | for(var i=0;i<a.length;i++){ |
||
94 | m[i]=(a[i]+b[i])/2;
|
||
95 | } |
||
96 | return m;
|
||
97 | }}); |
||
98 | })(); |
||
99 | } |