/* Copyright (C) 2008 Jiri Pittner or This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "lbinom.h" #include //gnu scientific library, presently used for zeta only, later also gamma and incomplete gamma can be taken from it #ifndef NO_GSL #include #else //needs libexps extern "C" double myzeta(double); #endif #define MAXFACT 70 unsigned long lfact(const unsigned int n) { register int j; static unsigned int ntop=12; static unsigned long a[MAXFACT+1]={1L,1L,2L,6L,24L,120L,720L,5040L, 40320L, 362880L, 3628800L, 39916800L, 479001600L}; if (n < 0) laerror("negative argument of factorial"); if (n > MAXFACT) laerror("overflow in factorial"); while (ntopn||k<0) return(0); if(k>n/2) k=n-k; if(k==0) return(1); if(k==1) return(n); int ind=0; if(n<=MAXBINOM) { ind=ibidx(n,k); if (ibitab[ind]) return ibitab[ind]; } /* nonrecurent method used anyway */ d=n-k; p=1; for(;n>d;n--) p *= n; value=p/lfact(k); if(n<=MAXBINOM) ibitab[ind]=value; return value; } #undef ibidx #undef ibidxmaxeven #undef MAXBINOM #define MAXFACT 170 REAL ifact(int n) { register int j; static int ntop=12; static REAL a[MAXFACT+1]={1L,1L,2L,6L,24L,120L,720L,5040L, 40320L, 362880L, 3628800L, 39916800L, 479001600L}; if (n < 0) laerror("negative argument of factorial"); if (n > MAXFACT) laerror("overflow in factorial"); while (ntopn||k<0) return(0); if(k>n/2) k=n-k; if(k==0) return(1); if(k==1) return(n); int ind=0; if(n<=MAXBINOM) { ind=ibidx(n,k); if (ibitab[ind]) return ibitab[ind]; } /* nonrecurent method used anyway */ d=n-k; p=1; for(;n>d;n--) p *= n; value=p/lfact(k); if(n<=MAXBINOM) ibitab[ind]=value; return value; } #undef ibidx #undef ibidxmaxeven #undef MAXBINOM #define MAXFACT2 300 REAL ifact2(int n) { register int j; static int ntop=12; static REAL a[MAXFACT2+1]={1.,1.,2.,3.,8.,15.,48.,105.,384.,945.,3840.,10395.,46080. }; if (n < -1) laerror("illegal argument of double factorial"); /* in some cases advantageous to define it to have value 1 for -1 */ if (n<2) return 1.0; if (n > MAXFACT2) laerror("overflow in double factorial"); while (ntop0) return(0.0); if(i==0) return(1.0); /*ieee inf.*/ return(FP_INFINITE); } if(i) { if(i<0) {i= -i; x= 1.0/x;} do { if(i&1) y *= x; x *= x; } while((i >>= 1)/*!=0*/); } return(y); } #define eps 1e-13 REAL smartpow(REAL x,REAL n) { REAL a; if(fabs((a=floor(n+0.5))-n)0.0) return(0.0); /*ieee*/ if(n<0.) return(FP_INFINITE); return FP_NAN; } /*n==0.0 can be excluded because it will go to dipow*/ if(x<0.0) { REAL b,p; /*try to calculate odd root - like (-1/27)^(-1/3)==-3 is OK*/ a= 1.0/n; if(fabs((b=floor(a+0.5))-a)0.0) return sqrt(x); else return sqrt(1.0/x); } return(pow(x,n)); } #define MAXBERN 50 REAL ibernum(int n) { static REAL bern[MAXBERN]={1.0,0.166666666666666666,-0.03333333333333333333,0.0238095238095238,-0.033333333333333333,0.07575757575757575757}; static int top=5; register int i; if(n<0) laerror("negative index of Bernoulli number"); if(n==1) return(-0.5); if(n&1) return(0.0); n /= 2; if( n >= MAXBERN) /* use zeta function for it */ {int m; m=2*n; return( #ifndef NO_GSL gsl_sf_zeta #else myzeta #endif ((double)m)*(n&1?2.0:-2.0)*ifact(m)/dipow(2.0*pi,m)); } if(n<=top) return(bern[n]); /* precalculate and save for next use, because the calc. is expensive */ /* could be calculated just one at need, but let's do all of them */ for(i=top+1;i<=n;i++) {int m; m=2*i; bern[i]= #ifndef NO_GSL gsl_sf_zeta #else myzeta #endif ((double)m)*(i&1?2.0:-2.0)*ifact(m)/dipow(2.0*pi,m); } top=n; return(bern[n]); } #undef MAXBERN REAL stirfact(REAL n) /* n should not be less than 9 or 7 at worst */ { REAL logz,cor,nn,z; cor=0.0; z=n+1.0; logz=log(z); if(z>=2.0) /*does not have sense for smaller anyway and could make troubles*/ { register int i,ii,m; m=(int)ceil(0.5-log(eps/10000.)/logz/2.0); if(m<2)m=2; if(m>16)m=16; nn=z*z; for(i=m;i>0;i--) {ii=2*i;cor = cor/nn + ibernum(ii)/((double)ii*(ii-1));} cor /=z; } /*0.5*ln(2*pi)=0.91893853320467275836*/ return((z-0.5)*logz-z+0.918938533204672741780329736406+cor); /*cor=log((1.0/(24.0*n)+1.0)/(12.0*n)+1.0)*/ } #define MAXFACT 100 #define LIMITFACT 150 REAL factln(int n) { static int top=MAXFACT; static REAL a[LIMITFACT+1]={ 0.0, 0.0, 0.69314718055994530843, 1.7917594692280549573, 3.1780538303479457518, 4.7874917427820458116, 6.579251212010101213, 8.5251613610654146669, 10.604602902745250859, 12.801827480081469091, 15.10441257307551588, 17.502307845873886549, 19.987214495661884683, 22.552163853123424531, 25.191221182738679829, 27.899271383840890337, 30.671860106080671926, 33.505073450136890756, 36.395445208033052609, 39.339884187199494647, 42.335616460753485057, 45.380138898476907627, 48.471181351835227247, 51.606675567764376922, 54.784729398112318677, 58.003605222980517908, 61.261701761002001376, 64.557538627006337606, 67.889743137181540078, 71.257038967168014665, 74.658236348830158136}; if(n < 0) laerror("negative argument of factln"); if(n < 2) return(0.0); if(n <= LIMITFACT) /*supposed LIMITFACT > MAXFACT*/ if(a[n]) return(a[n]); else { if(n<=MAXFACT) return(a[n]= log(ifact(n))); { register int j; if(!a[top]) a[top]= log(ifact(top)); while (topMAXFACT) laerror("too big argument in gamma"); f= (fabs(x-floor(x+0.5))