// $Id: numRec.cpp 5990 2009-03-19 10:21:20Z privmane $ #include "numRec.h" #include "matrixUtils.h" #include #include #ifndef VERBOS #define VERBOS #endif void validateSym(VVdouble & v) { const MDOUBLE epsilon = 0.00000001; for (int i=0; i < v.size(); ++i) { for (int j=i+1; j < v.size(); ++j) { if (fabs(v[i][j] - v[j][i])> epsilon) { LOG(5,<<"v["<3 && (fabs(d[ip]+g) == fabs(d[ip])) && (fabs(d[iq]+g)==fabs(d[iq])))==false) { LOG(5,<<"g is small: "<3 && (fabs(d[ip]+g) == fabs(d[ip])) && (fabs(d[iq]+g)==fabs(d[iq])) ) { a[ip][iq] = 0.0; } else if (fabs(a[ip][iq]) > tresh) { MDOUBLE h; MDOUBLE t; MDOUBLE theta; h = d[iq]-d[ip]; // assert(h!=0); if (fabs(h) + g == fabs(h)) { assert(h!=0); t = a[ip][iq] / h; } else { theta = 0.5*h/(a[ip][iq]); t = 1.0 / (fabs(theta)+sqrt(1.0+theta*theta)); if (theta<0.0) t = -t; } MDOUBLE c,s; c = 1.0 / sqrt(1.0+t*t); s = t*c; MDOUBLE tau; tau = s/ (1.0 + c); h = t * a[ip][iq]; d[ip] = d[ip] - t * a[ip][iq]; d[iq] = d[iq] + t * a[ip][iq]; a[ip][iq]=0.0; MDOUBLE tmp1, tmp2; for (j = 0; j < ip; ++j) { tmp1 = a[j][ip] - s*(a[j][iq]+a[j][ip]*tau); // updating the above element of a... tmp2 = a[j][iq] + s*(a[j][ip]-a[j][iq]*tau); a[j][ip] = tmp1; a[j][iq] = tmp2; } for (j = ip+1;j tresh)" } // end of for (iq = ... } // end of for (ip = ... } // end of for (i = 0; i< MaxNumberOfSweeps ; ++i) { vector err; err.push_back("problems in function MyJacobi. more than MaxNumberOfSweeps were necesary."); errorMsg::reportError(err); return -1; } //end of function /////////////////////////////////////////// //Adi cahnges ////////////////////////// ///////////////////////////////////////// MDOUBLE sign(MDOUBLE a,MDOUBLE b){ return (b>0?fabs(a):-fabs(a)); } MDOUBLE pythag(const MDOUBLE a, const MDOUBLE b){ return sqrt(pow(a,2)+pow(b,2)); } void houseHolder(VVdouble &mat,VVdouble &Q){ MDOUBLE sigma=0,H,sqrtSigma,K=0,tmp; int c,r,j,i,n = mat.size(); Q.resize(n); for(i=0;i1;i--){ sigma=0; //init sigma K=0; //init K for(j=0;j=0.0 ? sqrt(sigma) : -sqrt(sigma); //compute sqrt of sigma +/- H=sigma+mat[i][i-1]*sqrtSigma; //comute H = 0.5*|u|^2. until here O(n) /***createing U*******/ for(r=0;r=i. u[r]=0.0; /***********************/ for(r=0;rAU/H } for(r=0;r0;i--) { l=i-1; h=scale=0.0; if (l > 0) { for (k=0;k= 0.0 ? -sqrt(h) : sqrt(h)); e[i]=scale*g; h -= f*g; a[i][l]=f-g; f=0.0; for (j=0;j=l;i--) { f=s*e[i]; b=c*e[i]; e[i+1]=(r=pythag(f,g)); if (r == 0.0) { d[i+1] -= p; e[m]=0.0; break; } s=f/r; c=g/r; g=d[i+1]-p; r=(d[i]-g)*s+2.0*c*b; d[i+1]=g+(p=s*r); g=c*r-b; // Next loop can be omitted if eigenvectors not wanted for (k=0;k= l) continue; d[l] -= p; e[l]=g; e[m]=0.0; } } while (m != l); } } */ //called if tred2 was used - the original QL implementation from numerical recepies void QL(Vdouble &d, Vdouble &e, VVdouble &z){ int m,l,iter,i,k; MDOUBLE s,r,p,g,f,dd,c,b; int n=d.size(); for(i=1;i=l;i--){ f=s*e[i]; b=c*e[i]; e[i+1]=(r=pythag(f,g)); if(r==0.0){ d[i+1]-=p; e[m]=0.0; break; } s=f/r; c=g/r; g=d[i+1]-p; r=(d[i]-g)*s+2.0*c*b; d[i+1]=g+(p=s*r); g=c*r-b; for(k=0;k=l) continue; d[l]-=p; e[l]=g; e[m]=0.0; } } while(m!=l); } } /************************************************************************/ //diaganol will be eigen values and fill matrix of eigen vectors. */ /************************************************************************/ //A modified implementation for eigen analysis, using the house holder function. /* void computeEigenSystem(VVdouble &symmetricMatrix,VVdouble &eigenVectros,Vdouble &diagonal){ houseHolder(symmetricMatrix,eigenVectros); Vdouble offdiagonal; offdiagonal.resize(symmetricMatrix.size()); for (int i=0; i distance) distance = dt; fo = fn; } en = sqrt(en); pVal = computeProbForKS((en+0.12+0.11/en)*distance); return pVal; } // function called only by performKSTest MDOUBLE computeProbForKS (const MDOUBLE QsParam) { const MDOUBLE EPS1 = 1.0e-6,EPS2 = 1.0e-16; int j; MDOUBLE a2,fac = 2.0, sum = 0.0, term, termbf = 0.0; a2 = -2.0*QsParam*QsParam; 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; //get here only by failing to converge }