void NR::kendl2(Mat_I_DP &tab, DP &tau, DP &z, DP &prob) { int k,l,nn,mm,m2,m1,lj,li,kj,ki; DP svar,s=0.0,points,pairs,en2=0.0,en1=0.0; int i=tab.nrows(); int j=tab.ncols(); nn=i*j; points=tab[i-1][j-1]; for (k=0;k<=nn-2;k++) { ki=(k/j); kj=k-j*ki; points += tab[ki][kj]; for (l=k+1;l<=nn-1;l++) { li=l/j; lj=l-j*li; mm=(m1=li-ki)*(m2=lj-kj); pairs=tab[ki][kj]*tab[li][lj]; if (mm != 0) { en1 += pairs; en2 += pairs; s += (mm > 0 ? pairs : -pairs); } else { if (m1 != 0) en1 += pairs; if (m2 != 0) en2 += pairs; } } } tau=s/sqrt(en1*en2); svar=(4.0*points+10.0)/(9.0*points*(points-1.0)); z=tau/sqrt(svar); prob=erfcc(fabs(z)/1.4142136); }
void NR::copy(Mat_O_DP &aout, Mat_I_DP &ain) { int i,j; int n=ain.nrows(); for (i=0;i<n;i++) for (j=0;j<n;j++) aout[j][i]=ain[j][i]; }
DP NR::anorm2(Mat_I_DP &a) { int i,j; DP sum=0.0; int n=a.nrows(); for (j=0;j<n;j++) for (i=0;i<n;i++) sum += a[i][j]*a[i][j]; return sqrt(sum)/n; }
void NR::banmul(Mat_I_DP &a, const int m1, const int m2, Vec_I_DP &x, Vec_O_DP &b) { int i,j,k,tmploop; int n=a.nrows(); for (i=0;i<n;i++) { k=i-m1; tmploop=MIN(m1+m2+1,int(n-k)); b[i]=0.0; for (j=MAX(0,-k);j<tmploop;j++) b[i] += a[i][j]*x[j+k]; } }
void NR::cholsl(Mat_I_DP &a, Vec_I_DP &p, Vec_I_DP &b, Vec_O_DP &x) { int i,k; DP sum; int n=a.nrows(); for (i=0;i<n;i++) { for (sum=b[i],k=i-1;k>=0;k--) sum -= a[i][k]*x[k]; x[i]=sum/p[i]; } for (i=n-1;i>=0;i--) { for (sum=x[i],k=i+1;k<n;k++) sum -= a[k][i]*x[k]; x[i]=sum/p[i]; } }
void NR::resid(Mat_O_DP &res, Mat_I_DP &u, Mat_I_DP &rhs) { int i,j; DP h,h2i; int n=u.nrows(); h=1.0/(n-1); h2i=1.0/(h*h); for (j=1;j<n-1;j++) for (i=1;i<n-1;i++) res[i][j] = -h2i*(u[i+1][j]+u[i-1][j]+u[i][j+1] +u[i][j-1]-4.0*u[i][j])+rhs[i][j]; for (i=0;i<n;i++) res[i][0]=res[i][n-1]=res[0][i]=res[n-1][i]=0.0; }
void NR::sprsin(Mat_I_DP &a, const DP thresh, Vec_O_DP &sa, Vec_O_INT &ija) { int i,j,k; int n=a.nrows(); int nmax=sa.size(); for (j=0;j<n;j++) sa[j]=a[j][j]; ija[0]=n+1; k=n; for (i=0;i<n;i++) { for (j=0;j<n;j++) { if (fabs(a[i][j]) >= thresh && i != j) { if (++k > nmax) nrerror("sprsin: sa and ija too small"); sa[k]=a[i][j]; ija[k]=j; } } ija[i+1]=k+1; } }
void NR::banbks(Mat_I_DP &a, const int m1, const int m2, Mat_I_DP &al, Vec_I_INT &indx, Vec_IO_DP &b) { int i,j,k,l,mm; DP dum; int n=a.nrows(); mm=m1+m2+1; l=m1; for (k=0;k<n;k++) { j=indx[k]-1; if (j!=k) SWAP(b[k],b[j]); if (l<n) l++; for (j=k+1;j<l;j++) b[j] -= al[k][j-k-1]*b[k]; } l=1; for (i=n-1;i>=0;i--) { dum=b[i]; for (k=1;k<l;k++) dum -= a[i][k]*b[k+i]; b[i]=dum/a[i][0]; if (l<mm) l++; } }