Esempio n. 1
0
void NR::pearsn(Vec_I_DP &x, Vec_I_DP &y, DP &r, DP &prob, DP &z)
{
    const DP TINY=1.0e-20;
    int j;
    DP yt,xt,t,df;
    DP syy=0.0,sxy=0.0,sxx=0.0,ay=0.0,ax=0.0;

    int n=x.size();
    for (j=0; j<n; j++) {
        ax += x[j];
        ay += y[j];
    }
    ax /= n;
    ay /= n;
    for (j=0; 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));
    // prob=erfcc(fabs(z*sqrt(n-1.0))/1.4142136);
}
Esempio n. 2
0
void NR::qroot(Vec_I_DP &p, DP &b, DP &c, const DP eps)
{
	const int ITMAX=20;
	const DP TINY=1.0e-14;
	int iter;
	DP sc,sb,s,rc,rb,r,dv,delc,delb;
	Vec_DP d(3);

	int n=p.size()-1;
	Vec_DP q(n+1),qq(n+1),rem(n+1);
	d[2]=1.0;
	for (iter=0;iter<ITMAX;iter++) {
		d[1]=b;
		d[0]=c;
		poldiv(p,d,q,rem);
		s=rem[0];
		r=rem[1];
		poldiv(q,d,qq,rem);
		sb = -c*(rc = -rem[1]);
		rb = -b*rc+(sc = -rem[0]);
		dv=1.0/(sb*rc-sc*rb);
		delb=(r*sc-s*rc)*dv;
		delc=(-r*sb+s*rb)*dv;
		b += (delb=(r*sc-s*rc)*dv);
		c += (delc=(-r*sb+s*rb)*dv);
		if ((fabs(delb) <= eps*fabs(b) || fabs(b) < TINY)
			&& (fabs(delc) <= eps*fabs(c) || fabs(c) < TINY)) {
			return;
		}
	}
	nrerror("Too many iterations in routine qroot");
}
Esempio n. 3
0
void NR::polint(Vec_I_DP &xa, Vec_I_DP &ya, const DP x, DP &y, DP &dy)
{
	int i,m,ns=0;
	DP den,dif,dift,ho,hp,w;

	int n=xa.size();
	Vec_DP c(n),d(n);
	dif=fabs(x-xa[0]);
	for (i=0;i<n;i++) {
		if ((dift=fabs(x-xa[i])) < dif) {
			ns=i;
			dif=dift;
		}
		c[i]=ya[i];
		d[i]=ya[i];
	}
	y=ya[ns--];
	for (m=1;m<n;m++) {
		for (i=0;i<n-m;i++) {
			ho=xa[i]-x;
			hp=xa[i+m]-x;
			w=c[i+1]-d[i];
			if ((den=ho-hp) == 0.0) nrerror("Error in routine polint");
			den=w/den;
			d[i]=hp*den;
			c[i]=ho*den;
		}
		y += (dy=(2*(ns+1) < (n-m) ? c[ns+1] : d[ns--]));
	}
}
Esempio n. 4
0
void NR::asolve(Vec_I_DP &b, Vec_O_DP &x, const int itrnsp)
{
	int i;

	int n=b.size();
	for(i=0;i<n;i++) x[i]=((*sa_p)[i] != 0.0 ? b[i]/(*sa_p)[i] : b[i]);
}
Esempio n. 5
0
void NR::svdfit(Vec_I_DP &x, Vec_I_DP &y, Vec_I_DP &sig, Vec_O_DP &a,
	Mat_O_DP &u, Mat_O_DP &v, Vec_O_DP &w, DP &chisq,
	void funcs(const DP, Vec_O_DP &))
{
	int i,j;
	const DP TOL=1.0e-13;
	DP wmax,tmp,thresh,sum;

	int ndata=x.size();
	int ma=a.size();
	Vec_DP b(ndata),afunc(ma);
	for (i=0;i<ndata;i++) {
		funcs(x[i],afunc);
		tmp=1.0/sig[i];
		for (j=0;j<ma;j++) u[i][j]=afunc[j]*tmp;
		b[i]=y[i]*tmp;
	}
	svdcmp(u,w,v);
	wmax=0.0;
	for (j=0;j<ma;j++)
		if (w[j] > wmax) wmax=w[j];
	thresh=TOL*wmax;
	for (j=0;j<ma;j++)
		if (w[j] < thresh) w[j]=0.0;
	svbksb(u,w,v,b,a);
	chisq=0.0;
	for (i=0;i<ndata;i++) {
		funcs(x[i],afunc);
		sum=0.0;
		for (j=0;j<ma;j++) sum += a[j]*afunc[j];
		chisq += (tmp=(y[i]-sum)/sig[i],tmp*tmp);
	}
}
Esempio n. 6
0
void NR::vander(Vec_I_DP &x, Vec_O_DP &w, Vec_I_DP &q)
{
	int i,j,k;
	DP b,s,t,xx;

	int n=q.size();
	Vec_DP c(n);
	if (n == 1) w[0]=q[0];
	else {
		for (i=0;i<n;i++) c[i]=0.0;
		c[n-1] = -x[0];
		for (i=1;i<n;i++) {
			xx = -x[i];
			for (j=(n-1-i);j<(n-1);j++) c[j] += xx*c[j+1];
			c[n-1] += xx;
		}
		for (i=0;i<n;i++) {
			xx=x[i];
			t=b=1.0;
			s=q[n-1];
			for (k=n-1;k>0;k--) {
				b=c[k]+xx*b;
				s += q[k-1]*b;
				t=xx*t+b;
			}
			w[i]=s/t;
		}
	}
}
Esempio n. 7
0
void NR::predic(Vec_I_DP &data, Vec_I_DP &d, Vec_O_DP &future)
{
	int k,j;
	DP sum,discrp;

	int ndata=data.size();
	int m=d.size();
	int nfut=future.size();
	Vec_DP reg(m);
	for (j=0;j<m;j++) reg[j]=data[ndata-1-j];
	for (j=0;j<nfut;j++) {
		discrp=0.0;
		sum=discrp;
		for (k=0;k<m;k++) sum += d[k]*reg[k];
		for (k=m-1;k>=1;k--) reg[k]=reg[k-1];
		future[j]=reg[0]=sum;
	}
}
Esempio n. 8
0
void NR::convlv(Vec_I_DP &data, Vec_I_DP &respns, const int isign,
	Vec_O_DP &ans)
{
	int i,no2;
	DP mag2,tmp;

	int n=data.size();
	int m=respns.size();
	Vec_DP temp(n);
	temp[0]=respns[0];
	for (i=1;i<(m+1)/2;i++) {
		temp[i]=respns[i];
		temp[n-i]=respns[m-i];
	}
	for (i=(m+1)/2;i<n-(m-1)/2;i++)
		temp[i]=0.0;
	for (i=0;i<n;i++)
		ans[i]=data[i];
	realft(ans,1);
	realft(temp,1);
	no2=n>>1;
	if (isign == 1) {
		for (i=2;i<n;i+=2) {
			tmp=ans[i];
			ans[i]=(ans[i]*temp[i]-ans[i+1]*temp[i+1])/no2;
			ans[i+1]=(ans[i+1]*temp[i]+tmp*temp[i+1])/no2;
		}
		ans[0]=ans[0]*temp[0]/no2;
		ans[1]=ans[1]*temp[1]/no2;
	} else if (isign == -1) {
		for (i=2;i<n;i+=2) {
			if ((mag2=SQR(temp[i])+SQR(temp[i+1])) == 0.0)
				nrerror("Deconvolving at response zero in convlv");
			tmp=ans[i];
			ans[i]=(ans[i]*temp[i]+ans[i+1]*temp[i+1])/mag2/no2;
			ans[i+1]=(ans[i+1]*temp[i]-tmp*temp[i+1])/mag2/no2;
		}
		if (temp[0] == 0.0 || temp[1] == 0.0)
			nrerror("Deconvolving at response zero in convlv");
		ans[0]=ans[0]/temp[0]/no2;
		ans[1]=ans[1]/temp[1]/no2;
	} else nrerror("No meaning for isign in convlv");
	realft(ans,-1);
}
Esempio n. 9
0
DP NR::fredin(const DP x, const DP a, const DP b, Vec_I_DP &t, Vec_I_DP &f,
	Vec_I_DP &w, DP g(const DP), DP ak(const DP, const DP))
{
	int i;
	DP sum=0.0;

	int n=t.size();
	for (i=0;i<n;i++) sum += ak(x,t[i])*w[i]*f[i];
	return g(x)+sum;
}
Esempio n. 10
0
void NR::ftest(Vec_I_DP &data1, Vec_I_DP &data2, DP &f, DP &prob)
{
	DP var1,var2,ave1,ave2,df1,df2;

	int n1=data1.size();
	int n2=data2.size();
	avevar(data1,ave1,var1);
	avevar(data2,ave2,var2);
	if (var1 > var2) {
		f=var1/var2;
		df1=n1-1;
		df2=n2-1;
	} else {
		f=var2/var1;
		df1=n2-1;
		df2=n1-1;
	}
	prob = 2.0*betai(0.5*df2,0.5*df1,df2/(df2+df1*f));
	if (prob > 1.0) prob=2.0-prob;
}
Esempio n. 11
0
void NR::simpr(Vec_I_DP &y, Vec_I_DP &dydx, Vec_I_DP &dfdx, Mat_I_DP &dfdy,
	const DP xs, const DP htot, const int nstep, Vec_O_DP &yout,
	void derivs(const DP, Vec_IO_DP &, Vec_O_DP &))
{
        const int NMAX=61000;
        Vec_INT ija(NMAX);
        Vec_DP sa(NMAX);
	double sa_a[NMAX];
	int ija_a[NMAX];
	int i,nn,RAZMER;
	DP d,h,x;
        const int ITOL=2,ITMAX=75;
        const DP TOL=1.0e-9;
        int ii,iter;
        DP err;

	int n=y.size();
	Vec_INT indx(n);
	Vec_DP del(n),ytemp(n),xinit(n);
	h=htot/nstep;
        NR::sprsin(dfdy,1.e-9,sa,ija);
		RAZMER = (ija[n]-1);
		for(i=0;i<RAZMER;i++) {ija_a[i]=ija[i]; sa_a[i] = -h*sa[i];}
		for (i=0;i<n;i++) ++sa_a[i];// a[i][j] = -h*dfdy[i][j];

//	ludcmp(a,indx,d);
	for (i=0;i<n;i++){ xinit[i] = 0.;
		yout[i]=h*(dydx[i]+h*dfdx[i]);}
	ija_p=new Vec_INT(ija_a,NMAX);
        sa_p=new Vec_DP(sa_a,NMAX);
	NR::linbcg(yout,xinit,ITOL,TOL,ITMAX,iter,err);
//	lubksb(a,indx,yout);
	for (i=0;i<n;i++)
		ytemp[i]=y[i]+(del[i]=yout[i]=xinit[i]);
	x=xs+h;
	derivs(x,ytemp,yout);
	for (nn=2;nn<=nstep;nn++) {
		for (i=0;i<n;i++)
			yout[i]=h*yout[i]-del[i];
	NR::linbcg(yout,xinit,ITOL,TOL,ITMAX,iter,err);
//		lubksb(a,indx,yout);
		for (i=0;i<n;i++) ytemp[i] += (del[i] += 2.0*(yout[i]=xinit[i]));
		x += h;
		derivs(x,ytemp,yout);
	}
	for (i=0;i<n;i++)
		yout[i]=h*yout[i]-del[i];
	NR::linbcg(yout,xinit,ITOL,TOL,ITMAX,iter,err);
//	lubksb(a,indx,yout);
	for (i=0;i<n;i++)
		yout[i] = xinit[i] + ytemp[i];
        delete sa_p;
        delete ija_p;
}
Esempio n. 12
0
void NR::hunt(Vec_I_DP &xx, const DP x, int &jlo)
{
	int jm,jhi,inc;
	bool ascnd;

	int n=xx.size();
	ascnd=(xx[n-1] >= xx[0]);
	if (jlo < 0 || jlo > n-1) {
		jlo=-1;
		jhi=n;
	} else {
		inc=1;
		if (x >= xx[jlo] == ascnd) {
			if (jlo == n-1) return;
			jhi=jlo+1;
			while (x >= xx[jhi] == ascnd) {
				jlo=jhi;
				inc += inc;
				jhi=jlo+inc;
				if (jhi > n-1) {
					jhi=n;
					break;
				}
			}
		} else {
			if (jlo == 0) {
				jlo=-1;
				return;
			}
			jhi=jlo--;
			while (x < xx[jlo] == ascnd) {
				jhi=jlo;
				inc <<= 1;
				if (inc >= jhi) {
					jlo=-1;
					break;
				}
				else jlo=jhi-inc;
			}
		}
	}
	while (jhi-jlo != 1) {
		jm=(jhi+jlo) >> 1;
		if (x >= xx[jm] == ascnd)
			jlo=jm;
		else
			jhi=jm;
	}
	if (x == xx[n-1]) jlo=n-2;
	if (x == xx[0]) jlo=0;
}
Esempio n. 13
0
void NR::sprsax(Vec_I_DP &sa, Vec_I_INT &ija, Vec_I_DP &x, Vec_O_DP &b)
{
	int i,k;

	int n=x.size();
	if (ija[0] != n+1)
		nrerror("sprsax: mismatched vector and matrix");
	for (i=0;i<n;i++) {
		b[i]=sa[i]*x[i];
		for (k=ija[i];k<ija[i+1];k++) {
			b[i] += sa[k]*x[ija[k]];
		}
	}
}
Esempio n. 14
0
void NR::jacobn_s(const DP x, Vec_I_DP &y, Vec_O_DP &dfdx, Mat_O_DP &dfdy)
{
	int i;

	int n=y.size();
	for (i=0;i<n;i++) dfdx[i]=0.0;
	dfdy[0][0] = -0.013-1000.0*y[2];
	dfdy[0][1] = 0.0;
	dfdy[0][2] = -1000.0*y[0];
	dfdy[1][0] = 0.0;
	dfdy[1][1] = -2500.0*y[2];
	dfdy[1][2] = -2500.0*y[1];
	dfdy[2][0] = -0.013-1000.0*y[2];
	dfdy[2][1] = -2500.0*y[2];
	dfdy[2][2] = -1000.0*y[0]-2500.0*y[1];
}
Esempio n. 15
0
void NR::fgauss(const DP x, Vec_I_DP &a, DP &y, Vec_O_DP &dyda)
{
	int i;
	DP fac,ex,arg;

	int na=a.size();
	y=0.0;
	for (i=0;i<na-1;i+=3) {
		arg=(x-a[i+1])/a[i+2];
		ex=exp(-arg*arg);
		fac=a[i]*ex*2.0*arg;
		y += a[i]*ex;
		dyda[i]=ex;
		dyda[i+1]=fac/a[i+2];
		dyda[i+2]=fac*arg/a[i+2];
	}
}
Esempio n. 16
0
void NR::avevar(Vec_I_DP &data, DP &ave, DP &var)
{
	DP s,ep;
	int j;

	int n=data.size();
	ave=0.0;
	for (j=0;j<n;j++) ave += data[j];
	ave /= n;
	var=ep=0.0;
	for (j=0;j<n;j++) {
		s=data[j]-ave;
		ep += s;
		var += s*s;
	}
	var=(var-ep*ep/n)/(n-1);
}
Esempio n. 17
0
void NR::tridag(Vec_I_DP &a, Vec_I_DP &b, Vec_I_DP &c, Vec_I_DP &r, Vec_O_DP &u)
{
    int j;
    DP bet;

    int n=a.size();
    Vec_DP gam(n);
    if (b[0] == 0.0) nrerror("Error 1 in tridag");
    u[0]=r[0]/(bet=b[0]);
    for (j=1; j<n; j++) {
        gam[j]=c[j-1]/bet;
        bet=b[j]-a[j]*gam[j];
        if (bet == 0.0) nrerror("Error 2 in tridag");
        u[j]=(r[j]-a[j]*u[j-1])/bet;
    }
    for (j=(n-2); j>=0; j--)
        u[j] -= gam[j+1]*u[j+1];
}
Esempio n. 18
0
DP NR::snrm(Vec_I_DP &sx, const int itol)
{
	int i,isamax;
	DP ans;

	int n=sx.size();
	if (itol <= 3) {
		ans = 0.0;
		for (i=0;i<n;i++) ans += sx[i]*sx[i];
		return sqrt(ans);
	} else {
		isamax=0;
		for (i=0;i<n;i++) {
			if (fabs(sx[i]) > fabs(sx[isamax])) isamax=i;
		}
		return fabs(sx[isamax]);
	}
}
Esempio n. 19
0
void NR::chstwo(Vec_I_DP &bins1, Vec_I_DP &bins2, const int knstrn, DP &df,
	DP &chsq, DP &prob)
{
	int j;
	DP temp;

	int nbins=bins1.size();
	df=nbins-knstrn;
	chsq=0.0;
	for (j=0;j<nbins;j++)
		if (bins1[j] == 0.0 && bins2[j] == 0.0)
			--df;
		else {
			temp=bins1[j]-bins2[j];
			chsq += temp*temp/(bins1[j]+bins2[j]);
		}
	prob=gammq(0.5*df,0.5*chsq);
}
Esempio n. 20
0
void NR::shootf(Vec_I_DP &v, Vec_O_DP &f)
{
	const DP EPS=1.0e-14;
	int i,nbad,nok;
	DP h1,hmin=0.0;

	int nvar=v.size();
	Vec_DP f1(nvar),f2(nvar),y(nvar);
	Vec_DP v2(&v[n2],nvar-n2);
	kmax=0;
	h1=(x2-x1)/100.0;
	load1(x1,v,y);
	odeint(y,x1,xf,EPS,h1,hmin,nok,nbad,derivs,rkqs);
	score(xf,y,f1);
	load2(x2,v2,y);
	odeint(y,x2,xf,EPS,h1,hmin,nok,nbad,derivs,rkqs);
	score(xf,y,f2);
	for (i=0;i<nvar;i++) f[i]=f1[i]-f2[i];
}
Esempio n. 21
0
void NR::splint(Vec_I_DP &xa, Vec_I_DP &ya, Vec_I_DP &y2a, const DP x, DP &y)
{
    int k;
    DP h,b,a;

    int n=xa.size();
    int klo=0;
    int khi=n-1;
    while (khi-klo > 1) {
        k=(khi+klo) >> 1;
        if (xa[k] > x) khi=k;
        else klo=k;
    }
    h=xa[khi]-xa[klo];
    if (h == 0.0) nrerror("Bad xa input to routine splint");
    a=(xa[khi]-x)/h;
    b=(x-xa[klo])/h;
    y=a*ya[klo]+b*ya[khi]+((a*a*a-a)*y2a[klo]
                           +(b*b*b-b)*y2a[khi])*(h*h)/6.0;
}
Esempio n. 22
0
void NR::locate(Vec_I_DP &xx, const DP x, int &j)
{
	int ju,jm,jl;
	bool ascnd;

	int n=xx.size();
	jl=-1;
	ju=n;
	ascnd=(xx[n-1] >= xx[0]);
	while (ju-jl > 1) {
		jm=(ju+jl) >> 1;
		if (x >= xx[jm] == ascnd)
			jl=jm;
		else
			ju=jm;
	}
	if (x == xx[0]) j=0;
	else if (x == xx[n-1]) j=n-2;
	else j=jl;
}
Esempio n. 23
0
void sigmoid(const DP x, Vec_I_DP &a, DP &y, Vec_O_DP &dyda)
{
	int i;
	DP fac,ex,arg;
	
	if(a.size()!=5)
		cerr << "sigmoid: wrong # of parameters\n";
	ex = pow(1+exp((-x+a[3])/a[1]),(-a[2]));
	y=a[4]+a[0]*ex;
	dyda[0] = ex;
	dyda[2] = -a[0]*log(1+exp((-x+a[3])/a[1]))*ex;
	ex /= 1+exp((-x+a[3])/a[1]);
	dyda[1] = a[0]*a[2]*(-x+a[3])*exp((-x+a[3])/a[1])/a[1]/a[1]*ex;
	dyda[3] = -a[0]*a[2]*exp((-x+a[3])/a[1])/a[1]*ex;
	dyda[4] = 1;
	//	dyda[0] = 1/(pow(1+exp(-(x-a[3])/a[1]),a[2]));
	//	dyda[1] = -a[0]/(pow(1+exp(-(x-a[3])/a[1]),a[2]))*a[2]*(x-a[3])/(a[1]*a[1])
	//		*exp(-(x-a[3])/a[1])/(1+exp(-(x-a[3])/a[1]));
	//	dyda[2] = -a[0]/(pow(1+exp(-(x-a[3])/a[1]),a[2]))*log(1+exp(-(x-a[3])/a[1]));
	//	dyda[3] = -a[0]/(pow(1+exp(-(x-a[3])/a[1]),a[2]))*a[2]/a[1]*exp(-(x-a[3])/a[1])/(1+exp(-(x-a[3])/a[1]));
}
Esempio n. 24
0
void NR::cyclic(Vec_I_DP &a, Vec_I_DP &b, Vec_I_DP &c, const DP alpha,
	const DP beta, Vec_I_DP &r, Vec_O_DP &x)
{
	int i;
	DP fact,gamma;

	int n=a.size();
	if (n <= 2) nrerror("n too small in cyclic");
	Vec_DP bb(n),u(n),z(n);
	gamma = -b[0];
	bb[0]=b[0]-gamma;
	bb[n-1]=b[n-1]-alpha*beta/gamma;
	for (i=1;i<n-1;i++) bb[i]=b[i];
	tridag(a,bb,c,r,x);
	u[0]=gamma;
	u[n-1]=alpha;
	for (i=1;i<n-1;i++) u[i]=0.0;
	tridag(a,bb,c,u,z);
	fact=(x[0]+beta*x[n-1]/gamma)/
		(1.0+z[0]+beta*z[n-1]/gamma);
	for (i=0;i<n;i++) x[i] -= fact*z[i];
}
Esempio n. 25
0
void NR::simpr(Vec_I_DP &y, Vec_I_DP &dydx, Vec_I_DP &dfdx, Mat_I_DP &dfdy,
	const DP xs, const DP htot, const int nstep, Vec_O_DP &yout,
	void derivs(const DP, Vec_I_DP &, Vec_O_DP &))
{
	int i,j,nn;
	DP d,h,x;

	int n=y.size();
	Mat_DP a(n,n);
	Vec_INT indx(n);
	Vec_DP del(n),ytemp(n);
	h=htot/nstep;
	for (i=0;i<n;i++) {
		for (j=0;j<n;j++) a[i][j] = -h*dfdy[i][j];
		++a[i][i];
	}
	ludcmp(a,indx,d);
	for (i=0;i<n;i++)
		yout[i]=h*(dydx[i]+h*dfdx[i]);
	lubksb(a,indx,yout);
	for (i=0;i<n;i++)
		ytemp[i]=y[i]+(del[i]=yout[i]);
	x=xs+h;
	derivs(x,ytemp,yout);
	for (nn=2;nn<=nstep;nn++) {
		for (i=0;i<n;i++)
			yout[i]=h*yout[i]-del[i];
		lubksb(a,indx,yout);
		for (i=0;i<n;i++) ytemp[i] += (del[i] += 2.0*yout[i]);
		x += h;
		derivs(x,ytemp,yout);
	}
	for (i=0;i<n;i++)
		yout[i]=h*yout[i]-del[i];
	lubksb(a,indx,yout);
	for (i=0;i<n;i++)
		yout[i] += ytemp[i];
}
Esempio n. 26
0
void NR::chebpc(Vec_I_DP &c, Vec_O_DP &d)
{
	int k,j;
	DP sv;

	int n=c.size();
	Vec_DP dd(n);
	for (j=0;j<n;j++) d[j]=dd[j]=0.0;
	d[0]=c[n-1];
	for (j=n-2;j>0;j--) {
		for (k=n-j;k>0;k--) {
			sv=d[k];
			d[k]=2.0*d[k-1]-dd[k];
			dd[k]=sv;
		}
		sv=d[0];
		d[0] = -dd[0]+c[j];
		dd[0]=sv;
	}
	for (j=n-1;j>0;j--)
		d[j]=d[j-1]-dd[j];
	d[0] = -dd[0]+0.5*c[0];
}
Esempio n. 27
0
DP NR::trncst(Vec_I_DP &x, Vec_I_DP &y, Vec_I_INT &iorder, Vec_IO_INT &n)
{
	int j,ii;
	DP de;
	Vec_DP xx(6),yy(6);

	int ncity=x.size();
	n[3]=(n[2]+1) % ncity;
	n[4]=(n[0]+ncity-1) % ncity;
	n[5]=(n[1]+1) % ncity;
	for (j=0;j<6;j++) {
		ii=iorder[n[j]];
		xx[j]=x[ii];
		yy[j]=y[ii];
	}
	de = -alen(xx[1],xx[5],yy[1],yy[5]);
	de -= alen(xx[0],xx[4],yy[0],yy[4]);
	de -= alen(xx[2],xx[3],yy[2],yy[3]);
	de += alen(xx[0],xx[2],yy[0],yy[2]);
	de += alen(xx[1],xx[3],yy[1],yy[3]);
	de += alen(xx[4],xx[5],yy[4],yy[5]);
	return de;
}
Esempio n. 28
0
void NR::ratint(Vec_I_DP &xa, Vec_I_DP &ya, const DP x, DP &y, DP &dy)
{
	const DP TINY=1.0e-25;
	int m,i,ns=0;
	DP w,t,hh,h,dd;

	int n=xa.size();
	Vec_DP c(n),d(n);
	hh=fabs(x-xa[0]);
	for (i=0;i<n;i++) {
		h=fabs(x-xa[i]);
		if (h == 0.0) {
			y=ya[i];
			dy=0.0;
			return;
		} else if (h < hh) {
			ns=i;
			hh=h;
		}
		c[i]=ya[i];
		d[i]=ya[i]+TINY;
	}
	y=ya[ns--];
	for (m=1;m<n;m++) {
		for (i=0;i<n-m;i++) {
			w=c[i+1]-d[i];
			h=xa[i+m]-x;
			t=(xa[i]-x)*d[i]/h;
			dd=t-c[i+1];
			if (dd == 0.0) nrerror("Error in routine ratint");
			dd=w/dd;
			d[i]=c[i+1]*dd;
			c[i]=t*dd;
		}
		y += (dy=(2*(ns+1) < (n-m) ? c[ns+1] : d[ns--]));
	}
}
Esempio n. 29
0
void NR::memcof(Vec_I_DP &data, DP &xms, Vec_O_DP &d)
{
	int k,j,i;
	DP p=0.0;

	int n=data.size();
	int m=d.size();
	Vec_DP wk1(n),wk2(n),wkm(m);
	for (j=0;j<n;j++) p += SQR(data[j]);
	xms=p/n;
	wk1[0]=data[0];
	wk2[n-2]=data[n-1];
	for (j=1;j<n-1;j++) {
		wk1[j]=data[j];
		wk2[j-1]=data[j];
	}
	for (k=0;k<m;k++) {
		DP num=0.0,denom=0.0;
		for (j=0;j<(n-k-1);j++) {
			num += (wk1[j]*wk2[j]);
			denom += (SQR(wk1[j])+SQR(wk2[j]));
		}
		d[k]=2.0*num/denom;
		xms *= (1.0-SQR(d[k]));
		for (i=0;i<k;i++)
			d[i]=wkm[i]-d[k]*wkm[k-1-i];
		if (k == m-1)
			return;
		for (i=0;i<=k;i++) wkm[i]=d[i];
		for (j=0;j<(n-k-2);j++) {
			wk1[j] -= (wkm[k]*wk2[j]);
			wk2[j]=wk2[j+1]-wkm[k]*wk1[j+1];
		}
	}
	nrerror("never get here in memcof.");
}
Esempio n. 30
0
DP NR::selip(const int k, Vec_I_DP &arr)
{
	const int M=64;
	const DP BIG=1.0e30;
	int i,j,jl,jm,ju,kk,mm,nlo,nxtmm;
	DP ahi,alo,sum;
	Vec_INT isel(M+2);
	Vec_DP sel(M+2);

	int n=arr.size();
	if (k < 0 || k > n-1) nrerror("bad input to selip");
	kk=k;
	ahi=BIG;
	alo = -BIG;
	for (;;) {
		mm=nlo=0;
		sum=0.0;
		nxtmm=M+1;
		for (i=0;i<n;i++) {
			if (arr[i] >= alo && arr[i] <= ahi) {
				mm++;
				if (arr[i] == alo) nlo++;
				if (mm <= M) sel[mm-1]=arr[i];
				else if (mm == nxtmm) {
					nxtmm=mm+mm/M;
					sel[(i+2+mm+kk) % M]=arr[i];
				}
				sum += arr[i];
			}
		}
		if (kk < nlo) {
			return alo;
		}
		else if (mm < M+1) {
			shell(mm,sel);
			ahi = sel[kk];
			return ahi;
		}
		sel[M]=sum/mm;
		shell(M+1,sel);
		sel[M+1]=ahi;
		for (j=0;j<M+2;j++) isel[j]=0;
		for (i=0;i<n;i++) {
			if (arr[i] >= alo && arr[i] <= ahi) {
				jl=0;
				ju=M+2;
				while (ju-jl > 1) {
					jm=(ju+jl)/2;
					if (arr[i] >= sel[jm-1]) jl=jm;
					else ju=jm;
				}
				isel[ju-1]++;
			}
		}
		j=0;
		while (kk >= isel[j]) {
			alo=sel[j];
			kk -= isel[j++];
		}
		ahi=sel[j];
	}
}