Recipes.cpp
上传用户:szb0815
上传日期:2007-06-13
资源大小:338k
文件大小:35k
- //---------------------------------------------------------------------------
- #pragma hdrstop
- #include "Recipes.h"
- float *circx,*circy,*circw;
- int circn;
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- #include <math.h>
- #define NRANSI
#define EPS 1.0e-7
void medfit(float x[], float y[], int ndata, float *a, float *b, float *abdev)
{
float rofunc(float b);
int j;
float bb,b1,b2,del,f,f1,f2,sigb,temp;
float sx=0.0,sy=0.0,sxy=0.0,sxx=0.0,chisq=0.0;
ndatat=ndata;
xt=x;
yt=y;
for (j=1;j<=ndata;j++) {
sx += x[j];
sy += y[j];
sxy += x[j]*y[j];
sxx += x[j]*x[j];
}
del=ndata*sxx-sx*sx;
aa=(sxx*sy-sx*sxy)/del;
bb=(ndata*sxy-sx*sy)/del;
for (j=1;j<=ndata;j++)
chisq += (temp=y[j]-(aa+bb*x[j]),temp*temp);
sigb=sqrt(chisq/del);
b1=bb;
f1=rofunc(b1);
b2=bb+SIGN(3.0*sigb,f1);
f2=rofunc(b2);
if (b2 == b1) {
*a=aa;
*b=bb;
*abdev=abdevt/ndata;
return;
}
while (f1*f2 > 0.0) {
bb=b2+1.6*(b2-b1);
b1=b2;
f1=f2;
b2=bb;
f2=rofunc(b2);
}
sigb=0.01*sigb;
while (fabs(b2-b1) > sigb) {
bb=b1+0.5*(b2-b1);
if (bb == b1 || bb == b2) break;
f=rofunc(bb);
if (f*f1 >= 0.0) {
f1=f;
b1=bb;
} else {
f2=f;
b2=bb;
}
}
*a=aa;
*b=bb;
*abdev=abdevt/ndata;
}
- float rofunc(float b)
{
//float select(unsigned long k, unsigned long n, float arr[]);
int j;
float *arr,d,sum=0.0;
arr = new float[ndatat+1];
for (j=1;j<=ndatat;j++)
{
arr[j]=yt[j]-b*xt[j];
}
if (ndatat & 1) {
aa=select((ndatat+1)>>1,ndatat,arr);
}
else {
j=ndatat >> 1;
aa=0.5*(select(j,ndatat,arr)+select(j+1,ndatat,arr));
}
abdevt=0.0;
for (j=1;j<=ndatat;j++) {
d=yt[j]-(b*xt[j]+aa);
abdevt += fabs(d);
if (yt[j] != 0.0) d /= fabs(yt[j]);
if (fabs(d) > EPS) sum += (d >= 0.0 ? xt[j] : -xt[j]);
}
delete arr;
return sum;
}
float select(unsigned long k, unsigned long n, float arr[])
{
unsigned long i,ir,j,l,mid;
float a,temp;
l=1;
ir=n;
for (;;) {
if (ir <= l+1) {
if (ir == l+1 && arr[ir] < arr[l]) {
SWAP(arr[l],arr[ir])
}
return arr[k];
} else {
mid=(l+ir) >> 1;
SWAP(arr[mid],arr[l+1])
if (arr[l] > arr[ir]) {
SWAP(arr[l],arr[ir])
}
if (arr[l+1] > arr[ir]) {
SWAP(arr[l+1],arr[ir])
}
if (arr[l] > arr[l+1]) {
SWAP(arr[l],arr[l+1])
}
i=l+1;
j=ir;
a=arr[l+1];
for (;;) {
do i++; while (arr[i] < a);
do j--; while (arr[j] > a);
if (j < i) break;
SWAP(arr[i],arr[j])
}
arr[l+1]=arr[j];
arr[j]=a;
if (j >= k) ir=j-1;
if (j <= k) l=i;
}
}
}
#undef SWAP
#undef EPS
#undef NRANSI
- float fcircfit(float p[])
- {
- int i;
- float val = 0;
- float val1,val2;
- for (i=1;i<=circn;i++)
- {
- val1 = sqrt(pow(circx[i]-p[1],2)+pow(circy[i]-p[2],2));
- val2 = val1 - p[3];
- val += circw[i] * pow(val2,2);
- }
- return val;
- }
- void fcircderiv(float p[], float xi[])
- {
- int i;
- float val1,val2;
- xi[1] = 0;
- xi[2] = 0;
- xi[3] = 0;
- for (i=1;i<=circn;i++)
- {
- val1 = sqrt(pow(circx[i]-p[1],2)+pow(circy[i]-p[2],2));
- val2 = val1 - p[3];
- if (val1 > 0)
- {
- xi[1] += circw[i] * 2*p[1]*val2*(circx[i] - p[1])/val1;
- xi[2] += circw[i] * 2*p[2]*val2*(circy[i] - p[2])/val1;
- xi[3] += circw[i] * 2 * val2;
- }
- }
- }
- #define ITMAX 200
- #define EPS 1.0e-10
#define FREEALL free_vector(xi,1,n);free_vector(h,1,n);free_vector(g,1,n);
void frprmn(float p[], int n, float ftol, int *iter, float *fret,
float (*func)(float []), void (*dfunc)(float [], float []))
{
void linmin(float p[], float xi[], int n, float *fret,
float (*func)(float []));
int j,its;
float gg,gam,fp,dgg;
float *g,*h,*xi;
g=vector(1,n);
h=vector(1,n);
xi=vector(1,n);
fp=(*func)(p);
(*dfunc)(p,xi);
for (j=1;j<=n;j++) {
g[j] = -xi[j];
xi[j]=h[j]=g[j];
}
for (its=1;its<=ITMAX;its++) {
*iter=its;
linmin(p,xi,n,fret,func);
if (2.0*fabs(*fret-fp) <= ftol*(fabs(*fret)+fabs(fp)+EPS)) {
FREEALL
return;
}
fp=(*func)(p);
(*dfunc)(p,xi);
dgg=gg=0.0;
for (j=1;j<=n;j++) {
gg += g[j]*g[j];
dgg += (xi[j]+g[j])*xi[j];
}
if (gg == 0.0) {
FREEALL
return;
}
gam=dgg/gg;
for (j=1;j<=n;j++) {
g[j] = -xi[j];
xi[j]=h[j]=g[j]+gam*h[j];
}
}
nrerror("Too many iterations in frprmn");
}
#undef ITMAX
#undef EPS
#undef FREEALL
#define TOL 2.0e-4
void linmin(float p[], float xi[], int n, float *fret, float (*func)(float []))
{
float brent(float ax, float bx, float cx,
float (*f)(float), float tol, float *xmin);
float f1dim(float x);
void mnbrak(float *ax, float *bx, float *cx, float *fa, float *fb,
float *fc, float (*func)(float));
int j;
float xx,xmin,fx,fb,fa,bx,ax;
ncom=n;
pcom=vector(1,n);
xicom=vector(1,n);
nrfunc=func;
for (j=1;j<=n;j++) {
pcom[j]=p[j];
xicom[j]=xi[j];
}
ax=0.0;
xx=1.0;
mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim);
*fret=brent(ax,xx,bx,f1dim,TOL,&xmin);
for (j=1;j<=n;j++) {
xi[j] *= xmin;
p[j] += xi[j];
}
free_vector(xicom,1,n);
free_vector(pcom,1,n);
}
-
#undef TOL
- #define ITMAX 100
- #define CGOLD 0.3819660
#define ZEPS 1.0e-10
#define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d);
float brent(float ax, float bx, float cx, float (*f)(float), float tol,
float *xmin)
{
int iter;
float a,b,d,etemp,fu,fv,fw,fx,p,q,r,tol1,tol2,u,v,w,x,xm;
float e=0.0;
a=(ax < cx ? ax : cx);
b=(ax > cx ? ax : cx);
x=w=v=bx;
fw=fv=fx=(*f)(x);
for (iter=1;iter<=ITMAX;iter++) {
xm=0.5*(a+b);
tol2=2.0*(tol1=tol*fabs(x)+ZEPS);
if (fabs(x-xm) <= (tol2-0.5*(b-a))) {
*xmin=x;
return fx;
}
if (fabs(e) > tol1) {
r=(x-w)*(fx-fv);
q=(x-v)*(fx-fw);
p=(x-v)*q-(x-w)*r;
q=2.0*(q-r);
if (q > 0.0) p = -p;
q=fabs(q);
etemp=e;
e=d;
if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x))
d=CGOLD*(e=(x >= xm ? a-x : b-x));
else {
d=p/q;
u=x+d;
if (u-a < tol2 || b-u < tol2)
d=SIGN(tol1,xm-x);
}
} else {
d=CGOLD*(e=(x >= xm ? a-x : b-x));
}
u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d));
fu=(*f)(u);
if (fu <= fx) {
if (u >= x) a=x; else b=x;
SHFT(v,w,x,u)
SHFT(fv,fw,fx,fu)
} else {
if (u < x) a=u; else b=u;
if (fu <= fw || w == x) {
v=w;
w=u;
fv=fw;
fw=fu;
} else if (fu <= fv || v == x || v == w) {
v=u;
fv=fu;
}
}
}
nrerror("Too many iterations in brent");
}
- #undef ITMAX
- #undef CGOLD
- #undef ZEPS
- #undef SHFT
- #define GOLD 1.618034
- #define GLIMIT 100.0
#define TINY 1.0e-20
#define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d);
void mnbrak(float *ax, float *bx, float *cx, float *fa, float *fb, float *fc,
float (*func)(float))
{
float ulim,u,r,q,fu,dum;
*fa=(*func)(*ax);
*fb=(*func)(*bx);
if (*fb > *fa) {
SHFT(dum,*ax,*bx,dum)
SHFT(dum,*fb,*fa,dum)
}
*cx=(*bx)+GOLD*(*bx-*ax);
*fc=(*func)(*cx);
while (*fb > *fc) {
r=(*bx-*ax)*(*fb-*fc);
q=(*bx-*cx)*(*fb-*fa);
u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/
(2.0*SIGN(FMAX(fabs(q-r),TINY),q-r));
ulim=(*bx)+GLIMIT*(*cx-*bx);
if ((*bx-u)*(u-*cx) > 0.0) {
fu=(*func)(u);
if (fu < *fc) {
*ax=(*bx);
*bx=u;
*fa=(*fb);
*fb=fu;
return;
} else if (fu > *fb) {
*cx=u;
*fc=fu;
return;
}
u=(*cx)+GOLD*(*cx-*bx);
fu=(*func)(u);
} else if ((*cx-u)*(u-ulim) > 0.0) {
fu=(*func)(u);
if (fu < *fc) {
SHFT(*bx,*cx,u,*cx+GOLD*(*cx-*bx))
SHFT(*fb,*fc,fu,(*func)(u))
}
} else if ((u-ulim)*(ulim-*cx) >= 0.0) {
u=ulim;
fu=(*func)(u);
} else {
u=(*cx)+GOLD*(*cx-*bx);
fu=(*func)(u);
}
SHFT(*ax,*bx,*cx,u)
SHFT(*fa,*fb,*fc,fu)
}
}
#undef GOLD
#undef GLIMIT
#undef TINY
#undef SHFT
- float f1dim(float x)
- {
int j;
float f,*xt;
xt=vector(1,ncom);
for (j=1;j<=ncom;j++) xt[j]=pcom[j]+x*xicom[j];
f=(*nrfunc)(xt);
free_vector(xt,1,ncom);
return f;
}
- void kstwo(float data1[], unsigned long n1, float data2[], unsigned long n2,
- float *d, float *prob)
{
float probks(float alam);
void sort(unsigned long n, float arr[]);
unsigned long j1=1,j2=1;
float d1,d2,dt,en1,en2,en,fn1=0.0,fn2=0.0;
sort(n1,data1);
sort(n2,data2);
en1=n1;
en2=n2;
*d=0.0;
while (j1 <= n1 && j2 <= n2) {
if ((d1=data1[j1]) <= (d2=data2[j2])) fn1=j1++/en1;
if (d2 <= d1) fn2=j2++/en2;
if ((dt=fabs(fn2-fn1)) > *d) *d=dt;
}
en=sqrt(en1*en2/(en1+en2));
*prob=probks((en+0.12+0.11/en)*(*d));
}
- #define EPS1 0.001
- #define EPS2 1.0e-8
float probks(float alam)
{
int j;
float a2,fac=2.0,sum=0.0,term,termbf=0.0;
a2 = -2.0*alam*alam;
for (j=1;j<=100;j++) {
term=fac*exp(a2*j*j);
sum += term;
if (fabs(term) <= EPS1*termbf || fabs(term) <= EPS2*sum) return sum;
fac = -fac;
termbf=fabs(term);
}
return 1.0;
}
#undef EPS1
#undef EPS2
- #define SWAP(a,b) temp=(a);(a)=(b);(b)=temp;
- #define M 7
#define NSTACK 50
void sort(unsigned long n, float arr[])
{
unsigned long i,ir=n,j,k,l=1;
int jstack=0,*istack;
float a,temp;
istack=ivector(1,NSTACK);
for (;;) {
if (ir-l < M) {
for (j=l+1;j<=ir;j++) {
a=arr[j];
for (i=j-1;i>=l;i--) {
if (arr[i] <= a) break;
arr[i+1]=arr[i];
}
arr[i+1]=a;
}
if (jstack == 0) break;
ir=istack[jstack--];
l=istack[jstack--];
} else {
k=(l+ir) >> 1;
SWAP(arr[k],arr[l+1])
if (arr[l] > arr[ir]) {
SWAP(arr[l],arr[ir])
}
if (arr[l+1] > arr[ir]) {
SWAP(arr[l+1],arr[ir])
}
if (arr[l] > arr[l+1]) {
SWAP(arr[l],arr[l+1])
}
i=l+1;
j=ir;
a=arr[l+1];
for (;;) {
do i++; while (arr[i] < a);
do j--; while (arr[j] > a);
if (j < i) break;
SWAP(arr[i],arr[j]);
}
arr[l+1]=arr[j];
arr[j]=a;
jstack += 2;
if (jstack > NSTACK) nrerror("NSTACK too small in sort.");
if (ir-i+1 >= j-l) {
istack[jstack]=ir;
istack[jstack-1]=i;
ir=j-1;
} else {
istack[jstack]=j-1;
istack[jstack-1]=l;
l=i;
}
}
}
free_ivector(istack,1,NSTACK);
}
#undef M
#undef NSTACK
#undef SWAP
#undef NRANSI
- #define NRANSI
- #define SWAP(a,b) temp=(a);(a)=(b);(b)=temp;
#define M 7
#define NSTACK 50
- void sort2fi(unsigned long n, float arr[], int brr[])
- {
unsigned long i,ir=n,j,k,l=1;
int *istack,jstack=0;
float a,temp;
int b;
istack=ivector(1,NSTACK);
for (;;) {
if (ir-l < M) {
for (j=l+1;j<=ir;j++) {
a=arr[j];
b=brr[j];
for (i=j-1;i>=l;i--) {
if (arr[i] <= a) break;
arr[i+1]=arr[i];
brr[i+1]=brr[i];
}
arr[i+1]=a;
brr[i+1]=b;
}
if (!jstack) {
free_ivector(istack,1,NSTACK);
return;
}
ir=istack[jstack];
l=istack[jstack-1];
jstack -= 2;
} else {
k=(l+ir) >> 1;
SWAP(arr[k],arr[l+1])
SWAP(brr[k],brr[l+1])
if (arr[l] > arr[ir]) {
SWAP(arr[l],arr[ir])
SWAP(brr[l],brr[ir])
}
if (arr[l+1] > arr[ir]) {
SWAP(arr[l+1],arr[ir])
SWAP(brr[l+1],brr[ir])
}
if (arr[l] > arr[l+1]) {
SWAP(arr[l],arr[l+1])
SWAP(brr[l],brr[l+1])
}
i=l+1;
j=ir;
a=arr[l+1];
b=brr[l+1];
for (;;) {
do i++; while (arr[i] < a);
do j--; while (arr[j] > a);
if (j < i) break;
SWAP(arr[i],arr[j])
SWAP(brr[i],brr[j])
}
arr[l+1]=arr[j];
arr[j]=a;
brr[l+1]=brr[j];
brr[j]=b;
jstack += 2;
if (jstack > NSTACK) nrerror("NSTACK too small in sort2.");
if (ir-i+1 >= j-l) {
istack[jstack]=ir;
istack[jstack-1]=i;
ir=j-1;
} else {
istack[jstack]=j-1;
istack[jstack-1]=l;
l=i;
}
}
}
}
#undef M
#undef NSTACK
#undef SWAP
#undef NRANSI
- #define NRANSI
- #define SWAP(a,b) temp=(a);(a)=(b);(b)=temp;
#define M 7
#define NSTACK 50
- void sort3fff(unsigned long n, float arr[], float brr[], float crr[])
- {
unsigned long i,ir=n,j,k,l=1;
int *istack,jstack=0;
float a,temp;
float b,c;
istack=ivector(1,NSTACK);
for (;;) {
if (ir-l < M) {
for (j=l+1;j<=ir;j++) {
a=arr[j];
b=brr[j];
c=crr[j];
for (i=j-1;i>=l;i--) {
if (arr[i] <= a) break;
arr[i+1]=arr[i];
brr[i+1]=brr[i];
crr[i+1]=crr[i];
}
arr[i+1]=a;
brr[i+1]=b;
crr[i+1]=c;
}
if (!jstack) {
free_ivector(istack,1,NSTACK);
return;
}
ir=istack[jstack];
l=istack[jstack-1];
jstack -= 2;
} else {
k=(l+ir) >> 1;
SWAP(arr[k],arr[l+1])
SWAP(brr[k],brr[l+1])
SWAP(crr[k],crr[l+1])
if (arr[l] > arr[ir]) {
SWAP(arr[l],arr[ir])
SWAP(brr[l],brr[ir])
SWAP(crr[l],crr[ir])
}
if (arr[l+1] > arr[ir]) {
SWAP(arr[l+1],arr[ir])
SWAP(brr[l+1],brr[ir])
SWAP(crr[l+1],crr[ir])
}
if (arr[l] > arr[l+1]) {
SWAP(arr[l],arr[l+1])
SWAP(brr[l],brr[l+1])
SWAP(crr[l],crr[l+1])
}
i=l+1;
j=ir;
a=arr[l+1];
b=brr[l+1];
c=crr[l+1];
for (;;) {
do i++; while (arr[i] < a);
do j--; while (arr[j] > a);
if (j < i) break;
SWAP(arr[i],arr[j])
SWAP(brr[i],brr[j])
SWAP(crr[i],crr[j])
}
arr[l+1]=arr[j];
arr[j]=a;
brr[l+1]=brr[j];
brr[j]=b;
crr[l+1]=crr[j];
crr[j]=c;
jstack += 2;
if (jstack > NSTACK) nrerror("NSTACK too small in sort2.");
if (ir-i+1 >= j-l) {
istack[jstack]=ir;
istack[jstack-1]=i;
ir=j-1;
} else {
istack[jstack]=j-1;
istack[jstack-1]=l;
l=i;
}
}
}
}
#undef M
#undef NSTACK
#undef SWAP
#undef NRANSI
- #include <math.h>
- #define TINY 1.0e-20
void pearsn(float x[], float y[], unsigned long n, float *r, float *prob,
float *z)
{
float betai(float a, float b, float x);
float erfcc(float x);
unsigned long j;
float yt,xt,t,df;
float syy=0.0,sxy=0.0,sxx=0.0,ay=0.0,ax=0.0;
for (j=1;j<=n;j++) {
ax += x[j];
ay += y[j];
}
ax /= n;
ay /= n;
for (j=1;j<=n;j++) {
xt=x[j]-ax;
yt=y[j]-ay;
sxx += xt*xt;
syy += yt*yt;
sxy += xt*yt;
}
*r=sxy/(sqrt(sxx*syy)+TINY);
*z=0.5*log((1.0+(*r)+TINY)/(1.0-(*r)+TINY));
df=n-2;
t=(*r)*sqrt(df/((1.0-(*r)+TINY)*(1.0+(*r)+TINY)));
*prob=betai(0.5*df,0.5,df/(df+t*t));
}
#undef TINY
- float betai(float a, float b, float x)
- {
float betacf(float a, float b, float x);
float gammln(float xx);
void nrerror(char error_text[]);
float bt;
if (x < 0.0 || x > 1.0) nrerror("Bad x in routine betai");
if (x == 0.0 || x == 1.0) bt=0.0;
else
bt=exp(gammln(a+b)-gammln(a)-gammln(b)+a*log(x)+b*log(1.0-x));
if (x < (a+1.0)/(a+b+2.0))
return bt*betacf(a,b,x)/a;
else
return 1.0-bt*betacf(b,a,1.0-x)/b;
}
- float gammln(float xx)
{
double x,y,tmp,ser;
static double cof[6]={76.18009172947146,-86.50532032941677,
24.01409824083091,-1.231739572450155,
0.1208650973866179e-2,-0.5395239384953e-5};
int j;
y=x=xx;
tmp=x+5.5;
tmp -= (x+0.5)*log(tmp);
ser=1.000000000190015;
for (j=0;j<=5;j++) ser += cof[j]/++y;
return -tmp+log(2.5066282746310005*ser/x);
}
- #define MAXIT 100
- #define EPS 3.0e-7
#define FPMIN 1.0e-30
float betacf(float a, float b, float x)
{
void nrerror(char error_text[]);
int m,m2;
float aa,c,d,del,h,qab,qam,qap;
qab=a+b;
qap=a+1.0;
qam=a-1.0;
c=1.0;
d=1.0-qab*x/qap;
if (fabs(d) < FPMIN) d=FPMIN;
d=1.0/d;
h=d;
for (m=1;m<=MAXIT;m++) {
m2=2*m;
aa=m*(b-m)*x/((qam+m2)*(a+m2));
d=1.0+aa*d;
if (fabs(d) < FPMIN) d=FPMIN;
c=1.0+aa/c;
if (fabs(c) < FPMIN) c=FPMIN;
d=1.0/d;
h *= d*c;
aa = -(a+m)*(qab+m)*x/((a+m2)*(qap+m2));
d=1.0+aa*d;
if (fabs(d) < FPMIN) d=FPMIN;
c=1.0+aa/c;
if (fabs(c) < FPMIN) c=FPMIN;
d=1.0/d;
del=d*c;
h *= del;
if (fabs(del-1.0) < EPS) break;
}
if (m > MAXIT) nrerror("a or b too big, or MAXIT too small in betacf");
return h;
}
#undef MAXIT
#undef EPS
#undef FPMIN
- //---------------------------------------------------------------------------
- #include <math.h>
- #define NRANSI
- #include "nrutil.h"
- void spear(float data1[], float data2[], unsigned long n, float *d, float *zd,
- float *probd, float *rs, float *probrs)
- {
- float betai(float a, float b, float x);
- void crank(unsigned long n, float w[], float *s);
- float erfcc(float x);
- void sort2(unsigned long n, float arr[], float brr[]);
- unsigned long j;
- float vard,t,sg,sf,fac,en3n,en,df,aved,*wksp1,*wksp2;
- wksp1=vector(1,n);
- wksp2=vector(1,n);
- for (j=1;j<=n;j++) {
- wksp1[j]=data1[j];
- wksp2[j]=data2[j];
- }
- sort2(n,wksp1,wksp2);
- crank(n,wksp1,&sf);
- sort2(n,wksp2,wksp1);
- crank(n,wksp2,&sg);
- *d=0.0;
- for (j=1;j<=n;j++)
- *d += SQR(wksp1[j]-wksp2[j]);
- en=n;
- en3n=en*en*en-en;
- aved=en3n/6.0-(sf+sg)/12.0;
- fac=(1.0-sf/en3n)*(1.0-sg/en3n);
- vard=((en-1.0)*en*en*SQR(en+1.0)/36.0)*fac;
- *zd=(*d-aved)/sqrt(vard);
- *probd=erfcc(fabs(*zd)/1.4142136);
- *rs=(1.0-(6.0/en3n)*(*d+(sf+sg)/12.0))/sqrt(fac);
- fac=(*rs+1.0)*(1.0-(*rs));
- if (fac > 0.0) {
- t=(*rs)*sqrt((en-2.0)/fac);
- df=en-2.0;
- *probrs=betai(0.5*df,0.5,df/(df+t*t));
- } else
- *probrs=0.0;
- free_vector(wksp2,1,n);
- free_vector(wksp1,1,n);
- }
- #undef NRANSI
- #include <math.h>
- float gammln(float xx)
- {
- double x,y,tmp,ser;
- static double cof[6]={76.18009172947146,-86.50532032941677,
- 24.01409824083091,-1.231739572450155,
- 0.1208650973866179e-2,-0.5395239384953e-5};
- int j;
- y=x=xx;
- tmp=x+5.5;
- tmp -= (x+0.5)*log(tmp);
- ser=1.000000000190015;
- for (j=0;j<=5;j++) ser += cof[j]/++y;
- return -tmp+log(2.5066282746310005*ser/x);
- }
- #define NR_END 1
- #define FREE_ARG char*
- void nrerror(char error_text[])
- /* Numerical Recipes standard error handler */
- {
- fprintf(stderr,"Numerical Recipes run-time error...n");
- fprintf(stderr,"%sn",error_text);
- fprintf(stderr,"...now exiting to system...n");
- exit(1);
- }
- float *vector(long nl, long nh)
- /* allocate a float vector with subscript range v[nl..nh] */
- {
- float *v;
- v=(float *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(float)));
- if (!v) nrerror("allocation failure in vector()");
- return v-nl+NR_END;
- }
- int *ivector(long nl, long nh)
- /* allocate an int vector with subscript range v[nl..nh] */
- {
- int *v;
- v=(int *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(int)));
- if (!v) nrerror("allocation failure in ivector()");
- return v-nl+NR_END;
- }
- unsigned char *cvector(long nl, long nh)
- /* allocate an unsigned char vector with subscript range v[nl..nh] */
- {
- unsigned char *v;
- v=(unsigned char *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(unsigned char)));
- if (!v) nrerror("allocation failure in cvector()");
- return v-nl+NR_END;
- }
- unsigned long *lvector(long nl, long nh)
- /* allocate an unsigned long vector with subscript range v[nl..nh] */
- {
- unsigned long *v;
- v=(unsigned long *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(long)));
- if (!v) nrerror("allocation failure in lvector()");
- return v-nl+NR_END;
- }
- double *dvector(long nl, long nh)
- /* allocate a double vector with subscript range v[nl..nh] */
- {
- double *v;
- v=(double *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(double)));
- if (!v) nrerror("allocation failure in dvector()");
- return v-nl+NR_END;
- }
- float **matrix(long nrl, long nrh, long ncl, long nch)
- /* allocate a float matrix with subscript range m[nrl..nrh][ncl..nch] */
- {
- long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
- float **m;
- /* allocate pointers to rows */
- m=(float **) malloc((size_t)((nrow+NR_END)*sizeof(float*)));
- if (!m) nrerror("allocation failure 1 in matrix()");
- m += NR_END;
- m -= nrl;
- /* allocate rows and set pointers to them */
- m[nrl]=(float *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(float)));
- if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
- m[nrl] += NR_END;
- m[nrl] -= ncl;
- for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
- /* return pointer to array of pointers to rows */
- return m;
- }
- double **dmatrix(long nrl, long nrh, long ncl, long nch)
- /* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch] */
- {
- long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
- double **m;
- /* allocate pointers to rows */
- m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
- if (!m) nrerror("allocation failure 1 in matrix()");
- m += NR_END;
- m -= nrl;
- /* allocate rows and set pointers to them */
- m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
- if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
- m[nrl] += NR_END;
- m[nrl] -= ncl;
- for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
- /* return pointer to array of pointers to rows */
- return m;
- }
- int **imatrix(long nrl, long nrh, long ncl, long nch)
- /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
- {
- long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
- int **m;
- /* allocate pointers to rows */
- m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
- if (!m) nrerror("allocation failure 1 in matrix()");
- m += NR_END;
- m -= nrl;
- /* allocate rows and set pointers to them */
- m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
- if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
- m[nrl] += NR_END;
- m[nrl] -= ncl;
- for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
- /* return pointer to array of pointers to rows */
- return m;
- }
- float **submatrix(float **a, long oldrl, long oldrh, long oldcl, long oldch,
- long newrl, long newcl)
- /* point a submatrix [newrl..][newcl..] to a[oldrl..oldrh][oldcl..oldch] */
- {
- long i,j,nrow=oldrh-oldrl+1,ncol=oldcl-newcl;
- float **m;
- /* allocate array of pointers to rows */
- m=(float **) malloc((size_t) ((nrow+NR_END)*sizeof(float*)));
- if (!m) nrerror("allocation failure in submatrix()");
- m += NR_END;
- m -= newrl;
- /* set pointers to rows */
- for(i=oldrl,j=newrl;i<=oldrh;i++,j++) m[j]=a[i]+ncol;
- /* return pointer to array of pointers to rows */
- return m;
- }
- float **convert_matrix(float *a, long nrl, long nrh, long ncl, long nch)
- /* allocate a float matrix m[nrl..nrh][ncl..nch] that points to the matrix
- declared in the standard C manner as a[nrow][ncol], where nrow=nrh-nrl+1
- and ncol=nch-ncl+1. The routine should be called with the address
- &a[0][0] as the first argument. */
- {
- long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1;
- float **m;
- /* allocate pointers to rows */
- m=(float **) malloc((size_t) ((nrow+NR_END)*sizeof(float*)));
- if (!m) nrerror("allocation failure in convert_matrix()");
- m += NR_END;
- m -= nrl;
- /* set pointers to rows */
- m[nrl]=a-ncl;
- for(i=1,j=nrl+1;i<nrow;i++,j++) m[j]=m[j-1]+ncol;
- /* return pointer to array of pointers to rows */
- return m;
- }
- float ***f3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh)
- /* allocate a float 3tensor with range t[nrl..nrh][ncl..nch][ndl..ndh] */
- {
- long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1,ndep=ndh-ndl+1;
- float ***t;
- /* allocate pointers to pointers to rows */
- t=(float ***) malloc((size_t)((nrow+NR_END)*sizeof(float**)));
- if (!t) nrerror("allocation failure 1 in f3tensor()");
- t += NR_END;
- t -= nrl;
- /* allocate pointers to rows and set pointers to them */
- t[nrl]=(float **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(float*)));
- if (!t[nrl]) nrerror("allocation failure 2 in f3tensor()");
- t[nrl] += NR_END;
- t[nrl] -= ncl;
- /* allocate rows and set pointers to them */
- t[nrl][ncl]=(float *) malloc((size_t)((nrow*ncol*ndep+NR_END)*sizeof(float)));
- if (!t[nrl][ncl]) nrerror("allocation failure 3 in f3tensor()");
- t[nrl][ncl] += NR_END;
- t[nrl][ncl] -= ndl;
- for(j=ncl+1;j<=nch;j++) t[nrl][j]=t[nrl][j-1]+ndep;
- for(i=nrl+1;i<=nrh;i++) {
- t[i]=t[i-1]+ncol;
- t[i][ncl]=t[i-1][ncl]+ncol*ndep;
- for(j=ncl+1;j<=nch;j++) t[i][j]=t[i][j-1]+ndep;
- }
- /* return pointer to array of pointers to rows */
- return t;
- }
- void free_vector(float *v, long nl, long nh)
- /* free a float vector allocated with vector() */
- {
- free((FREE_ARG) (v+nl-NR_END));
- }
- void free_ivector(int *v, long nl, long nh)
- /* free an int vector allocated with ivector() */
- {
- free((FREE_ARG) (v+nl-NR_END));
- }
- void free_cvector(unsigned char *v, long nl, long nh)
- /* free an unsigned char vector allocated with cvector() */
- {
- free((FREE_ARG) (v+nl-NR_END));
- }
- void free_lvector(unsigned long *v, long nl, long nh)
- /* free an unsigned long vector allocated with lvector() */
- {
- free((FREE_ARG) (v+nl-NR_END));
- }
- void free_dvector(double *v, long nl, long nh)
- /* free a double vector allocated with dvector() */
- {
- free((FREE_ARG) (v+nl-NR_END));
- }
- void free_matrix(float **m, long nrl, long nrh, long ncl, long nch)
- /* free a float matrix allocated by matrix() */
- {
- free((FREE_ARG) (m[nrl]+ncl-NR_END));
- free((FREE_ARG) (m+nrl-NR_END));
- }
- void free_dmatrix(double **m, long nrl, long nrh, long ncl, long nch)
- /* free a double matrix allocated by dmatrix() */
- {
- free((FREE_ARG) (m[nrl]+ncl-NR_END));
- free((FREE_ARG) (m+nrl-NR_END));
- }
- void free_imatrix(int **m, long nrl, long nrh, long ncl, long nch)
- /* free an int matrix allocated by imatrix() */
- {
- free((FREE_ARG) (m[nrl]+ncl-NR_END));
- free((FREE_ARG) (m+nrl-NR_END));
- }
- void free_submatrix(float **b, long nrl, long nrh, long ncl, long nch)
- /* free a submatrix allocated by submatrix() */
- {
- free((FREE_ARG) (b+nrl-NR_END));
- }
- void free_convert_matrix(float **b, long nrl, long nrh, long ncl, long nch)
- /* free a matrix allocated by convert_matrix() */
- {
- free((FREE_ARG) (b+nrl-NR_END));
- }
- void free_f3tensor(float ***t, long nrl, long nrh, long ncl, long nch,
- long ndl, long ndh)
- /* free a float f3tensor allocated by f3tensor() */
- {
- free((FREE_ARG) (t[nrl][ncl]+ndl-NR_END));
- free((FREE_ARG) (t[nrl]+ncl-NR_END));
- free((FREE_ARG) (t+nrl-NR_END));
- }
- #define NRANSI
- #include "nrutil.h"
- #define SWAP(a,b) temp=(a);(a)=(b);(b)=temp;
- #define M 7
- #define NSTACK 50
- void sort2(unsigned long n, float arr[], float brr[])
- {
- unsigned long i,ir=n,j,k,l=1;
- int *istack,jstack=0;
- float a,b,temp;
- istack=ivector(1,NSTACK);
- for (;;) {
- if (ir-l < M) {
- for (j=l+1;j<=ir;j++) {
- a=arr[j];
- b=brr[j];
- for (i=j-1;i>=l;i--) {
- if (arr[i] <= a) break;
- arr[i+1]=arr[i];
- brr[i+1]=brr[i];
- }
- arr[i+1]=a;
- brr[i+1]=b;
- }
- if (!jstack) {
- free_ivector(istack,1,NSTACK);
- return;
- }
- ir=istack[jstack];
- l=istack[jstack-1];
- jstack -= 2;
- } else {
- k=(l+ir) >> 1;
- SWAP(arr[k],arr[l+1])
- SWAP(brr[k],brr[l+1])
- if (arr[l] > arr[ir]) {
- SWAP(arr[l],arr[ir])
- SWAP(brr[l],brr[ir])
- }
- if (arr[l+1] > arr[ir]) {
- SWAP(arr[l+1],arr[ir])
- SWAP(brr[l+1],brr[ir])
- }
- if (arr[l] > arr[l+1]) {
- SWAP(arr[l],arr[l+1])
- SWAP(brr[l],brr[l+1])
- }
- i=l+1;
- j=ir;
- a=arr[l+1];
- b=brr[l+1];
- for (;;) {
- do i++; while (arr[i] < a);
- do j--; while (arr[j] > a);
- if (j < i) break;
- SWAP(arr[i],arr[j])
- SWAP(brr[i],brr[j])
- }
- arr[l+1]=arr[j];
- arr[j]=a;
- brr[l+1]=brr[j];
- brr[j]=b;
- jstack += 2;
- if (jstack > NSTACK) nrerror("NSTACK too small in sort2.");
- if (ir-i+1 >= j-l) {
- istack[jstack]=ir;
- istack[jstack-1]=i;
- ir=j-1;
- } else {
- istack[jstack]=j-1;
- istack[jstack-1]=l;
- l=i;
- }
- }
- }
- }
- #undef M
- #undef NSTACK
- #undef SWAP
- #undef NRANSI
- void crank(unsigned long n, float w[], float *s)
- {
- unsigned long j=1,ji,jt;
- float t,rank;
- *s=0.0;
- while (j < n) {
- if (w[j+1] != w[j]) {
- w[j]=j;
- ++j;
- } else {
- for (jt=j+1;jt<=n && w[jt]==w[j];jt++);
- rank=0.5*(j+jt-1);
- for (ji=j;ji<=(jt-1);ji++) w[ji]=rank;
- t=jt-j;
- *s += t*t*t-t;
- j=jt;
- }
- }
- if (j == n) w[n]=n;
- }
- float erfcc(float x)
- {
- float t,z,ans;
- z=fabs(x);
- t=1.0/(1.0+0.5*z);
- ans=t*exp(-z*z-1.26551223+t*(1.00002368+t*(0.37409196+t*(0.09678418+
- t*(-0.18628806+t*(0.27886807+t*(-1.13520398+t*(1.48851587+
- t*(-0.82215223+t*0.17087277)))))))));
- return x >= 0.0 ? ans : 2.0-ans;
- }
- float betai(float a, float b, float x)
- {
- float betacf(float a, float b, float x);
- float gammln(float xx);
- void nrerror(char error_text[]);
- float bt;
- if (x < 0.0 || x > 1.0) nrerror("Bad x in routine betai");
- if (x == 0.0 || x == 1.0) bt=0.0;
- else
- bt=exp(gammln(a+b)-gammln(a)-gammln(b)+a*log(x)+b*log(1.0-x));
- if (x < (a+1.0)/(a+b+2.0))
- return bt*betacf(a,b,x)/a;
- else
- return 1.0-bt*betacf(b,a,1.0-x)/b;
- }
- #define MAXIT 100
- #define EPS 3.0e-7
- #define FPMIN 1.0e-30
- float betacf(float a, float b, float x)
- {
- void nrerror(char error_text[]);
- int m,m2;
- float aa,c,d,del,h,qab,qam,qap;
- qab=a+b;
- qap=a+1.0;
- qam=a-1.0;
- c=1.0;
- d=1.0-qab*x/qap;
- if (fabs(d) < FPMIN) d=FPMIN;
- d=1.0/d;
- h=d;
- for (m=1;m<=MAXIT;m++) {
- m2=2*m;
- aa=m*(b-m)*x/((qam+m2)*(a+m2));
- d=1.0+aa*d;
- if (fabs(d) < FPMIN) d=FPMIN;
- c=1.0+aa/c;
- if (fabs(c) < FPMIN) c=FPMIN;
- d=1.0/d;
- h *= d*c;
- aa = -(a+m)*(qab+m)*x/((a+m2)*(qap+m2));
- d=1.0+aa*d;
- if (fabs(d) < FPMIN) d=FPMIN;
- c=1.0+aa/c;
- if (fabs(c) < FPMIN) c=FPMIN;
- d=1.0/d;
- del=d*c;
- h *= del;
- if (fabs(del-1.0) < EPS) break;
- }
- if (m > MAXIT) nrerror("a or b too big, or MAXIT too small in betacf");
- return h;
- }
- #undef MAXIT
- #undef EPS
- #undef FPMIN
- //---------------------------------------------------------------------------
- #define NRANSI
- #include "nrutil.h"
- #define SWAP(a,b) temp=(a);(a)=(b);(b)=temp;
- #define M 7
- #define NSTACK 50
- void sort2i(unsigned long n, int arr[], int brr[])
- {
- unsigned long i,ir=n,j,k,l=1;
- int *istack,jstack=0;
- int a,b,temp;
- istack=ivector(1,NSTACK);
- for (;;) {
- if (ir-l < M) {
- for (j=l+1;j<=ir;j++) {
- a=arr[j];
- b=brr[j];
- for (i=j-1;i>=l;i--) {
- if (arr[i] <= a) break;
- arr[i+1]=arr[i];
- brr[i+1]=brr[i];
- }
- arr[i+1]=a;
- brr[i+1]=b;
- }
- if (!jstack) {
- free_ivector(istack,1,NSTACK);
- return;
- }
- ir=istack[jstack];
- l=istack[jstack-1];
- jstack -= 2;
- } else {
- k=(l+ir) >> 1;
- SWAP(arr[k],arr[l+1])
- SWAP(brr[k],brr[l+1])
- if (arr[l] > arr[ir]) {
- SWAP(arr[l],arr[ir])
- SWAP(brr[l],brr[ir])
- }
- if (arr[l+1] > arr[ir]) {
- SWAP(arr[l+1],arr[ir])
- SWAP(brr[l+1],brr[ir])
- }
- if (arr[l] > arr[l+1]) {
- SWAP(arr[l],arr[l+1])
- SWAP(brr[l],brr[l+1])
- }
- i=l+1;
- j=ir;
- a=arr[l+1];
- b=brr[l+1];
- for (;;) {
- do i++; while (arr[i] < a);
- do j--; while (arr[j] > a);
- if (j < i) break;
- SWAP(arr[i],arr[j])
- SWAP(brr[i],brr[j])
- }
- arr[l+1]=arr[j];
- arr[j]=a;
- brr[l+1]=brr[j];
- brr[j]=b;
- jstack += 2;
- if (jstack > NSTACK) nrerror("NSTACK too small in sort2.");
- if (ir-i+1 >= j-l) {
- istack[jstack]=ir;
- istack[jstack-1]=i;
- ir=j-1;
- } else {
- istack[jstack]=j-1;
- istack[jstack-1]=l;
- l=i;
- }
- }
- }
- }
- #undef M
- #undef NSTACK
- #undef SWAP
- #undef NRANSI
- //---------------------------------------------------------------------------
- void svdcmp(float **a, int m, int n, float w[], float **v)
- {
- float pythag(float a, float b);
- int flag,i,its,j,jj,k,l,nm;
- float anorm,c,f,g,h,s,scale,x,y,z,*rv1;
- rv1=vector(1,n);
- g=scale=anorm=0.0;
- for (i=1;i<=n;i++) {
- l=i+1;
- rv1[i]=scale*g;
- g=s=scale=0.0;
- if (i <= m) {
- for (k=i;k<=m;k++) scale += fabs(a[k][i]);
- if (scale) {
- for (k=i;k<=m;k++) {
- a[k][i] /= scale;
- s += a[k][i]*a[k][i];
- }
- f=a[i][i];
- g = -SIGN(sqrt(s),f);
- h=f*g-s;
- a[i][i]=f-g;
- for (j=l;j<=n;j++) {
- for (s=0.0,k=i;k<=m;k++) s += a[k][i]*a[k][j];
- f=s/h;
- for (k=i;k<=m;k++) a[k][j] += f*a[k][i];
- }
- for (k=i;k<=m;k++) a[k][i] *= scale;
- }
- }
- w[i]=scale *g;
- g=s=scale=0.0;
- if (i <= m && i != n) {
- for (k=l;k<=n;k++) scale += fabs(a[i][k]);
- if (scale) {
- for (k=l;k<=n;k++) {
- a[i][k] /= scale;
- s += a[i][k]*a[i][k];
- }
- f=a[i][l];
- g = -SIGN(sqrt(s),f);
- h=f*g-s;
- a[i][l]=f-g;
- for (k=l;k<=n;k++) rv1[k]=a[i][k]/h;
- for (j=l;j<=m;j++) {
- for (s=0.0,k=l;k<=n;k++) s += a[j][k]*a[i][k];
- for (k=l;k<=n;k++) a[j][k] += s*rv1[k];
- }
- for (k=l;k<=n;k++) a[i][k] *= scale;
- }
- }
- anorm=FMAX(anorm,(fabs(w[i])+fabs(rv1[i])));
- }
- for (i=n;i>=1;i--) {
- if (i < n) {
- if (g) {
- for (j=l;j<=n;j++)
- v[j][i]=(a[i][j]/a[i][l])/g;
- for (j=l;j<=n;j++) {
- for (s=0.0,k=l;k<=n;k++) s += a[i][k]*v[k][j];
- for (k=l;k<=n;k++) v[k][j] += s*v[k][i];
- }
- }
- for (j=l;j<=n;j++) v[i][j]=v[j][i]=0.0;
- }
- v[i][i]=1.0;
- g=rv1[i];
- l=i;
- }
- for (i=IMIN(m,n);i>=1;i--) {
- l=i+1;
- g=w[i];
- for (j=l;j<=n;j++) a[i][j]=0.0;
- if (g) {
- g=1.0/g;
- for (j=l;j<=n;j++) {
- for (s=0.0,k=l;k<=m;k++) s += a[k][i]*a[k][j];
- f=(s/a[i][i])*g;
- for (k=i;k<=m;k++) a[k][j] += f*a[k][i];
- }
- for (j=i;j<=m;j++) a[j][i] *= g;
- } else for (j=i;j<=m;j++) a[j][i]=0.0;
- ++a[i][i];
- }
- for (k=n;k>=1;k--) {
- for (its=1;its<=30;its++) {
- flag=1;
- for (l=k;l>=1;l--) {
- nm=l-1;
- if ((float)(fabs(rv1[l])+anorm) == anorm) {
- flag=0;
- break;
- }
- if ((float)(fabs(w[nm])+anorm) == anorm) break;
- }
- if (flag) {
- c=0.0;
- s=1.0;
- for (i=l;i<=k;i++) {
- f=s*rv1[i];
- rv1[i]=c*rv1[i];
- if ((float)(fabs(f)+anorm) == anorm) break;
- g=w[i];
- h=pythag(f,g);
- w[i]=h;
- h=1.0/h;
- c=g*h;
- s = -f*h;
- for (j=1;j<=m;j++) {
- y=a[j][nm];
- z=a[j][i];
- a[j][nm]=y*c+z*s;
- a[j][i]=z*c-y*s;
- }
- }
- }
- z=w[k];
- if (l == k) {
- if (z < 0.0) {
- w[k] = -z;
- for (j=1;j<=n;j++) v[j][k] = -v[j][k];
- }
- break;
- }
- if (its == 30) nrerror("no convergence in 30 svdcmp iterations");
- x=w[l];
- nm=k-1;
- y=w[nm];
- g=rv1[nm];
- h=rv1[k];
- f=((y-z)*(y+z)+(g-h)*(g+h))/(2.0*h*y);
- g=pythag(f,1.0);
- f=((x-z)*(x+z)+h*((y/(f+SIGN(g,f)))-h))/x;
- c=s=1.0;
- for (j=l;j<=nm;j++) {
- i=j+1;
- g=rv1[i];
- y=w[i];
- h=s*g;
- g=c*g;
- z=pythag(f,h);
- rv1[j]=z;
- c=f/z;
- s=h/z;
- f=x*c+g*s;
- g = g*c-x*s;
- h=y*s;
- y *= c;
- for (jj=1;jj<=n;jj++) {
- x=v[jj][j];
- z=v[jj][i];
- v[jj][j]=x*c+z*s;
- v[jj][i]=z*c-x*s;
- }
- z=pythag(f,h);
- w[j]=z;
- if (z) {
- z=1.0/z;
- c=f*z;
- s=h*z;
- }
- f=c*g+s*y;
- x=c*y-s*g;
- for (jj=1;jj<=m;jj++) {
- y=a[jj][j];
- z=a[jj][i];
- a[jj][j]=y*c+z*s;
- a[jj][i]=z*c-y*s;
- }
- }
- rv1[l]=0.0;
- rv1[k]=f;
- w[k]=x;
- }
- }
- free_vector(rv1,1,n);
- }
- float pythag(float a, float b)
- {
- float absa,absb;
- absa=fabs(a);
- absb=fabs(b);
- if (absa > absb) return absa*sqrt(1.0+SQR(absb/absa));
- else return (absb == 0.0 ? 0.0 : absb*sqrt(1.0+SQR(absa/absb)));
- }
- void kendl1(float data1[], float data2[], unsigned long n, float *tau,
- float *z, float *prob)
- {
- float erfcc(float x);
- unsigned long n2=0,n1=0,k,j;
- long is=0;
- float svar,aa,a2,a1;
- for (j=1;j<n;j++) {
- for (k=(j+1);k<=n;k++) {
- a1=data1[j]-data1[k];
- a2=data2[j]-data2[k];
- aa=a1*a2;
- if (aa) {
- ++n1;
- ++n2;
- aa > 0.0 ? ++is : --is;
- } else {
- if (a1) ++n1;
- if (a2) ++n2;
- }
- }
- }
- *tau=is/(sqrt((double) n1)*sqrt((double) n2));
- svar=(4.0*n+10.0)/(9.0*n*(n-1.0));
- *z=(*tau)/sqrt(svar);
- *prob=erfcc(fabs(*z)/1.4142136);
- }