Ejemplo n.º 1
0
void chldec1(real_t a[], int n, real_t aux[])
{
	real_t vecvec(int, int, int, real_t [], real_t []);
	int j,k,kk,kj,low,up;
	real_t r,epsnorm;

	r=0.0;
	kk=0;
	for (k=1; k<=n; k++) {
		kk += k;
		if (a[kk] > r) r=a[kk];
	}
	epsnorm=aux[2]*r;
	kk=0;
	for (k=1; k<=n; k++) {
		kk += k;
		low=kk-k+1;
		up=kk-1;
		r=a[kk]-vecvec(low,up,0,a,a);
		if (r <= epsnorm) {
			aux[3]=k-1;
			return;
		}
		a[kk]=r=sqrt(r);
		kj=kk+k;
		for (j=k+1; j<=n; j++) {
			a[kj]=(a[kj]-vecvec(low,up,kj-kk,a,a))/r;
			kj +=j;
		}
	}
	aux[3]=n;
}
Ejemplo n.º 2
0
int CGSolver(SparseMatrix A,    // CSR format matrix
             const std::vector<double> &b,
             std::vector<double> &u,
             double tol,
             std::map<int, std::vector<double>> &soln_iter) {
    unsigned int n_iter = 0;
    long unsigned int max_iter = A.GetRows();

    soln_iter[n_iter] = u;    // store initial state
    auto A_u = A.MulVec(u);   // A*u
    std::vector<double> r = subvec(b, A_u);   // b - A*u
    auto L2normr0 = L2norm(r);
    std::vector<double> p = r;    // p =r

    while (n_iter < max_iter) {
        n_iter++;
        auto A_p = A.MulVec(p);                         // A * p
        double alpha = vecvec(r, r) / vecvec(p, A_p);   // (r * r)/(p * A * p)

        auto alpha_p = scalvec(alpha, p);   // alpha * p
        u = addvec(u, alpha_p);             // u = u + alpha * p

        /**
         * alpha * A * p
         * r_n+1 = r_n - alpha * A * p
         */
        std::vector<double> alpha_A_p = scalvec(alpha, A_p);
        std::vector<double> r_next = subvec(r, alpha_A_p);

        auto L2normr = L2norm(r_next);  // L2normr = L2norm(r_n+1)

        if (L2normr / L2normr0 < tol) {
            soln_iter[n_iter] = u;
            std::cout << "SUCCESS: CG solver converged in "
                      << n_iter << " iterations" << std::endl;
            break;
        } else {
            double beta = vecvec(r_next, r_next) / vecvec(r, r);
            auto beta_p = scalvec(beta, p);

            p = addvec(r_next, beta_p);
            r = r_next;

            if ((n_iter % 10 == 0) || (n_iter == 0)) {
                soln_iter[n_iter] = u;
            }
            if (n_iter == max_iter) {
                std::cout << "FAILURE: CG solver failed to converge" << std::endl;
                return 1;
            }
        }
    }
    return 0;
}
Ejemplo n.º 3
0
void main ()
{
	void impex(int, real_t, real_t, real_t [],
				void (*)(real_t, real_t [], real_t [], int),
				int (*)(real_t, real_t [], real_t **, int),
				real_t, real_t, int, real_t, real_t [],
				void (*)(real_t [], real_t [], int), int *fail,
				void (*)(real_t *, real_t, real_t, real_t, real_t **,
							real_t [], int, real_t));
	void dupvec(int, int, int, real_t [], real_t []);
	real_t vecvec(int, int, int, real_t [], real_t []);
	void elmvec(int, int, int, real_t [], real_t [], real_t);
	int n,fail,i,it;
	real_t t,tend,eps,hmax,l,h2,y[4],sw[4],f1[4],f2[4],z[4],x[4],
			n1,n2;

	printf("The results with IMPEX are:\n\n");
	n=3;
	nje=nfe=0;
	t=0.0;
	tend=400.0;
	eps=1.0e-5;
	hmax=400.0;
	y[1]=y[2]=y[3]=0.0;
	sw[1]=sw[2]=sw[3]=1.0;
	print[1]=0.1;  print[2]=1.0;  print[3]=10.0;  print[4]=100.0;
	print[5]=400.0;
	dupvec(1,n,0,z,y);
	for (i=1; i<=n; i++)
		x[i] = (y[i] == 0.0) ? eps : (1.0+eps)*y[i];
	n1=sqrt(vecvec(1,n,0,x,x))*eps;
	f(t,x,f1,n);
	for (it=1; it<=5; it++) {
		f(t,z,f2,n);
		elmvec(1,n,0,f2,f1,-1.0);
		n2=n1/sqrt(vecvec(1,n,0,f2,f2));
		dupvec(1,n,0,z,x);
		elmvec(1,n,0,z,f2,n2);
	}
	f(t,z,f2,n);
	elmvec(1,n,0,f2,f1,-1.0);
	l=sqrt(vecvec(1,n,0,f2,f2))/n1;
	h2=pow(eps*320.0,1.0/5.0)/(4.0*l);
	printf("EPS = %e\nInterval of integration = (%1.0f,%3.0f)\n"
		"Maximally allowed stepsize = %e\n\nLipschconst = %e\n"
		"Starting stepsize = %e\nFunctional eval = %2d\n\n"
		"    X    ERROR       Y[1]          Y[2]        NFE  NJE\n",
		eps,t,tend,hmax,l,h2,nfe);
	impex(n,t,tend,y,f,available,h2,hmax,0,eps,sw,update,&fail,
			control);
	printf("\nNumber of functional evaluations =%4d\n"
		"Number of Jacobian evaluations   = %3d\n",nfe,nje);
}
Ejemplo n.º 4
0
void chldecbnd(real_t a[], int n, int w, real_t aux[])
{
	real_t vecvec(int, int, int, real_t [], real_t []);
	int j,k,jmax,kk,kj,w1,start;
	real_t r,eps,max;

	max=0.0;
	kk = -w;
	w1=w+1;
	for (j=1; j<=n; j++) {
		kk += w1;
		if (a[kk] > max) max=a[kk];
	}
	jmax=w;
	w1=w+1;
	kk = -w;
	eps=aux[2]*max;
	for (k=1; k<=n; k++) {
		if (k+w > n) jmax--;
		kk += w1;
		start=kk-k+1;
		r=a[kk]-vecvec(((k <= w1) ? start : kk-w),kk-1,0,a,a);
		if (r <= eps) {
			aux[3]=k-1;
			return;
		}
		a[kk]=r=sqrt(r);
		kj=kk;
		for (j=1; j<=jmax; j++) {
			kj += w;
			a[kj]=(a[kj]-vecvec(((k+j <= w1) ? start : kk-w+j),
							kk-1,kj-kk,a,a))/r;
		}
	}
	aux[3]=n;
}
Ejemplo n.º 5
0
void lsqdglinv(real_t **a, int m, real_t aid[], int ci[], real_t diag[])
{
	real_t vecvec(int, int, int, real_t [], real_t []);
	real_t tamvec(int, int, int, real_t **, real_t []);
	int j,k,cik;
	real_t w;

	for (k=1; k<=m; k++) {
		diag[k]=1.0/aid[k];
		for (j=k+1; j<=m; j++) diag[j] = -tamvec(k,j-1,j,a,diag)/aid[j];
		diag[k]=vecvec(k,m,0,diag,diag);
	}
	for (k=m; k>=1; k--) {
		cik=ci[k];
		if (cik != k) {
			w=diag[k];
			diag[k]=diag[cik];
			diag[cik]=w;
		}
	}
}
Ejemplo n.º 6
0
void gssnewton(int m, int n, real_t par[], real_t rv[], real_t **jjinv,
					int (*funct)(int, int, real_t[], real_t[]),
					void (*jacobian)(int, int, real_t[], real_t[], real_t **),
					real_t in[], real_t out[])
{
	int *allocate_integer_vector(int, int);
	real_t *allocate_real_vector(int, int);
	real_t **allocate_real_matrix(int, int, int, int);
	void free_integer_vector(int *, int);
	void free_real_vector(real_t *, int);
	void free_real_matrix(real_t **, int, int, int);
	real_t vecvec(int, int, int, real_t [], real_t []);
	void dupvec(int, int, int, real_t [], real_t []);
	void elmvec(int, int, int, real_t [], real_t [], real_t);
	void lsqortdec(real_t **, int, int, real_t [], real_t [], int []);
	void lsqsol(real_t **, int, int, real_t [], int [], real_t []);
	void lsqinv(real_t **, int, real_t [], int []);
	int i,j,inr,mit,text,it,itmax,inrmax,tim,feval,fevalmax,conv,
			testthf,dampingon,*ci,fail;
	real_t rho,res1,res2,rn,reltolpar,abstolpar,abstolres,stap,normx,
			**jac,*pr,*aid,*sol,*fu2,aux[6];

	ci=allocate_integer_vector(1,n);
	pr=allocate_real_vector(1,n);
	aid=allocate_real_vector(1,n);
	sol=allocate_real_vector(1,n);
	fu2=allocate_real_vector(1,m);
	jac=allocate_real_matrix(1,m+1,1,n);

	itmax=fevalmax=in[5];
	aux[2]=n*in[0];
	tim=in[7];
	reltolpar=in[1]*in[1];
	abstolpar=in[2]*in[2];
	abstolres=in[4]*in[4];
	inrmax=in[6];
	dupvec(1,n,0,pr,par);
	if (m < n)
		for (i=1; i<=n; i++) jac[m+1][i]=0.0;
	text=4;
	mit=0;
	testthf=1;
	res2=stap=out[5]=out[6]=out[7]=0.0;
	(*funct)(m,n,par,fu2);
	rn=vecvec(1,m,0,fu2,fu2);
	out[3]=sqrt(rn);
	feval=1;
	dampingon=0;
	fail=0;
	it=1;
	do {
		out[5]=it;
		(*jacobian)(m,n,par,fu2,jac);
		if (!testthf) {
			text=7;
			fail=1;
			break;
		}
		lsqortdec(jac,m,n,aux,aid,ci);
		if (aux[3] != n) {
			text=5;
			fail=1;
			break;
		}
		lsqsol(jac,m,n,aid,ci,fu2);
		dupvec(1,n,0,sol,fu2);
		stap=vecvec(1,n,0,sol,sol);
		rho=2.0;
		normx=vecvec(1,n,0,par,par);
		if (stap > reltolpar*normx+abstolpar || it == 1 && stap > 0.0) {
			inr=0;
			do {
				rho /= 2.0;
				if (inr > 0) {
					res1=res2;
					dupvec(1,m,0,rv,fu2);
					dampingon = inr > 1;
				}
				for (i=1; i<=n; i++) pr[i]=par[i]-sol[i]*rho;
				feval++;
				if (!(*funct)(m,n,pr,fu2)) {
					text=6;
					fail=1;
					break;
				}
				res2=vecvec(1,m,0,fu2,fu2);
				conv = inr >= inrmax;
				inr++;
			} while ((inr == 1) ? (dampingon || res2 >= rn) :
						(!conv && (rn <= res1 || res2 < res1)));
			if (fail) break;
			if (conv) {
				mit++;
				if (mit < tim) conv=0;
			} else
				mit=0;
			if (inr > 1) {
				rho *= 2.0;
				elmvec(1,n,0,par,sol,-rho);
				rn=res1;
				if (inr > 2) out[7]=it;
			} else {
				dupvec(1,n,0,par,pr);
				rn=res2;
				dupvec(1,m,0,rv,fu2);
			}
			if (rn <= abstolres) {
				text=1;
				itmax=it;
			} else
				if (conv && inrmax > 0) {
					text=3;
					itmax=it;
				} else
					dupvec(1,m,0,fu2,rv);
		} else {
			text=2;
			rho=1.0;
			itmax=it;
		}
		it++;
	} while (it <= itmax && feval < fevalmax);
	if (!fail) {
		lsqinv(jac,n,aid,ci);
		for (i=1; i<=n; i++) {
			jjinv[i][i]=jac[i][i];
			for (j=i+1; j<=n; j++) jjinv[i][j]=jjinv[j][i]=jac[i][j];
		}
	}
	out[6]=sqrt(stap)*rho;
	out[2]=sqrt(rn);
	out[4]=feval;
	out[1]=text;
	out[8]=aux[3];
	out[9]=aux[5];
	free_integer_vector(ci,1);
	free_real_vector(pr,1);
	free_real_vector(aid,1);
	free_real_vector(sol,1);
	free_real_vector(fu2,1);
	free_real_matrix(jac,1,m+1,1);
}
Ejemplo n.º 7
0
void praxis( int n, double *x, int *data, double (*funct)(double *, void *data), double *in, double *out) {

	int illc,i,j,k,k2,nl,maxf,nf,kl,kt,ktm,emergency;
	double s,sl,dn,dmin,fx,f1,lds,ldt,sf,df,qf1,qd0,qd1,qa,qb,qc,m2,m4,
			small,vsmall,large,vlarge,scbd,ldfac,t2,macheps,reltol,
			abstol,h,**v,*d,*y,*z,*q0,*q1,**a,em[8],l;

	/*
	 *  Seed random number generator
	 */
#ifdef MSWIN
	srand(34084320);
#else
	srand48(34084320);
#endif

//	for (i=0; i<8; ++i) x[i+1] = (double)data->x[i];
	d=allocate_real_vector(1,n);
	y=allocate_real_vector(1,n);
	z=allocate_real_vector(1,n);
	q0=allocate_real_vector(1,n);
	q1=allocate_real_vector(1,n);
	v=allocate_real_matrix(1,n,1,n);
	a=allocate_real_matrix(1,n,1,n);

    //  heuristic numbers:
    //
    //  If the axes may be badly scaled (which is to be avoided if
    //  possible), then set scbd = 10.  otherwise set scbd=1.
    //
    //  If the problem is known to be ill-conditioned, set ILLC = true.
    //
    //  KTM is the number of iterations without improvement before the
    //  algorithm terminates.  KTM = 4 is very cautious; usually KTM = 1
    //  is satisfactory.
    //

	macheps=in[0];
	reltol=in[1];
	abstol=in[2];
	maxf=in[5];
	h=in[6];
	scbd=in[7];
	ktm=in[8];
	illc = in[9] < 0.0;
	small=macheps*macheps;
	vsmall=small*small;
	large=1.0/small;
	vlarge=1.0/vsmall;
	m2=reltol;
	m4=sqrt(m2);
	srand(1);
	ldfac = (illc ? 0.1 : 0.01);
	kt=nl=0;
	nf=1;
	out[3]=qf1=fx=(*funct)(x, data);
	abstol=t2=small+fabs(abstol);
	dmin=small;
	if (h < abstol*100.0) h=abstol*100;
	ldt=h;
	inimat(1,n,1,n,v,0.0);
	for (i=1; i<=n; i++) v[i][i]=1.0;
	d[1]=qd0=qd1=0.0;
	dupvec(1,n,0,q1,x);
	inivec(1,n,q0,0.0);
	emergency=0;

	while (1) {
		sf=d[1];
		d[1]=s=0.0;
		praxismin(1,2,&(d[1]),&s,&fx,0,
					n,x,v,&qa,&qb,&qc,qd0,qd1,q0,q1,&nf,
					&nl,&fx,m2,m4,dmin,ldt,reltol,abstol,small,h,funct, data);
		if (s <= 0.0) mulcol(1,n,1,1,v,v,-1.0);
		if (sf <= 0.9*d[1] || 0.9*sf >= d[1]) inivec(2,n,d,0.0);
		for (k=2; k<=n; k++) {
			dupvec(1,n,0,y,x);
			sf=fx;
			illc = (illc || kt > 0);
			while (1) {
				kl=k;
				df=0.0;
				if (illc) {
					/* random stop to get off resulting valley */
					for (i=1; i<=n; i++) {
						s=z[i]=(0.1*ldt+t2*pow(10.0,kt))*
#ifdef MSWIN
									((double)(rand())/RAND_MAX-0.5);
#else
									(drand48()-0.5);
#endif
						elmveccol(1,n,i,x,v,s);
					}
					fx=(*funct)(x, data);
					nf++;
				}
				for (k2=k; k2<=n; k2++) {
					sl=fx;
					s=0.0;
					praxismin(k2,2,&(d[k2]),&s,&fx,0,
						n,x,v,&qa,&qb,&qc,qd0,qd1,q0,q1,&nf,
						&nl,&fx,m2,m4,dmin,ldt,reltol,abstol,small,h,funct, data);
					s = illc ? d[k2]*(s+z[k2])*(s+z[k2]) : sl-fx;
					if (df < s) {
						df=s;
						kl=k2;
					}
				}
				if (!illc && df < fabs(100.0*macheps*fx))
					illc=1;
				else
					break;
			}
			for (k2=1; k2<=k-1; k2++) {
				s=0.0;
				praxismin(k2,2,&(d[k2]),&s,&fx,0,
					n,x,v,&qa,&qb,&qc,qd0,qd1,q0,q1,&nf,
					&nl,&fx,m2,m4,dmin,ldt,reltol,abstol,small,h,funct, data);
			}
			f1=fx;
			fx=sf;
			lds=0.0;
			for (i=1; i<=n; i++) {
				sl=x[i];
				x[i]=y[i];
				y[i] = sl -= y[i];
				lds += sl*sl;
			}
			lds=sqrt(lds);
			if (lds > small) {
				for (i=kl-1; i>=k; i--) {
					for (j=1; j<=n; j++) v[j][i+1]=v[j][i];
					d[i+1]=d[i];
				}
				d[k]=0.0;
				dupcolvec(1,n,k,v,y);
				mulcol(1,n,k,k,v,v,1.0/lds);
				praxismin(k,4,&(d[k]),&lds,&f1,1,
					n,x,v,&qa,&qb,&qc,qd0,qd1,q0,q1,&nf,
					&nl,&fx,m2,m4,dmin,ldt,reltol,abstol,small,h,funct, data);
				if (lds <= 0.0) {
					lds = -lds;
					mulcol(1,n,k,k,v,v,-1.0);
				}
			}
			ldt *= ldfac;
			if (ldt < lds) ldt=lds;
			t2=m2*sqrt(vecvec(1,n,0,x,x))+abstol;
			kt = (ldt > 0.5*t2) ? 0 : kt+1;
			if (kt > ktm) {
				out[1]=0.0;
				emergency=1;
			}
		}
		if (emergency) break;
		/* quad */
		s=fx;
		fx=qf1;
		qf1=s;
		qd1=0.0;
		for (i=1; i<=n; i++) {
			s=x[i];
			x[i]=l=q1[i];
			q1[i]=s;
			qd1 += (s-l)*(s-l);
		}
		l=qd1=sqrt(qd1);
		s=0.0;
		if ((qd0*qd1 > DBL_MIN) && (nl >=3*n*n)) {
			praxismin(0,2,&s,&l,&qf1,1,
					n,x,v,&qa,&qb,&qc,qd0,qd1,q0,q1,&nf,
					&nl,&fx,m2,m4,dmin,ldt,reltol,abstol,small,h,funct, data);
			qa=l*(l-qd1)/(qd0*(qd0+qd1));
			qb=(l+qd0)*(qd1-l)/(qd0*qd1);
			qc=l*(l+qd0)/(qd1*(qd0+qd1));
		} else {
			fx=qf1;
			qa=qb=0.0;
			qc=1.0;
		}
		qd0=qd1;
		for (i=1; i<=n; i++) {
			s=q0[i];
			q0[i]=x[i];
			x[i]=qa*s+qb*x[i]+qc*q1[i];
		}
		/* end of quad */
		dn=0.0;
		for (i=1; i<=n; i++) {
			d[i]=1.0/sqrt(d[i]);
			if (dn < d[i]) dn=d[i];
		}
		for (j=1; j<=n; j++) {
			s=d[j]/dn;
			mulcol(1,n,j,j,v,v,s);
		}
		if (scbd > 1.0) {
			s=vlarge;
			for (i=1; i<=n; i++) {
				sl=z[i]=sqrt(mattam(1,n,i,i,v,v));
				if (sl < m4) z[i]=m4;
				if (s > sl) s=sl;
			}
			for (i=1; i<=n; i++) {
				sl=s/z[i];
				z[i]=1.0/sl;
				if (z[i] > scbd) {
					sl=1.0/scbd;
					z[i]=scbd;
				}
				mulrow(1,n,i,i,v,v,sl);
			}
		}
		for (i=1; i<=n; i++) ichrowcol(i+1,n,i,i,v);
		em[0]=em[2]=macheps;
		em[4]=10*n;
		em[6]=vsmall;
		dupmat(1,n,1,n,a,v);
		if (qrisngvaldec(a,n,n,d,v,em) != 0) {
			out[1]=2.0;
			emergency=1;
		}
		if (emergency) break;
		if (scbd > 1.0) {
			for (i=1; i<=n; i++) mulrow(1,n,i,i,v,v,z[i]);
			for (i=1; i<=n; i++) {
				s=sqrt(tammat(1,n,i,i,v,v));
				d[i] *= s;
				s=1.0/s;
				mulcol(1,n,i,i,v,v,s);
			}
		}
		for (i=1; i<=n; i++) {
			s=dn*d[i];
			d[i] = (s > large) ? vsmall :
						((s < small) ? vlarge : 1.0/(s*s));
		}
		/* sort */
		for (i=1; i<=n-1; i++) {
			k=i;
			s=d[i];
			for (j=i+1; j<=n; j++)
				if (d[j] > s) {
					k=j;
					s=d[j];
				}
			if (k > i) {
				d[k]=d[i];
				d[i]=s;
				for (j=1; j<=n; j++) {
					s=v[j][i];
					v[j][i]=v[j][k];
					v[j][k]=s;
				}
			}
		}
		/* end of sort */
		dmin=d[n];
		if (dmin < small) dmin=small;
		illc = (m2*d[1]) > dmin;
		if (nf >= maxf) {
			out[1]=1.0;
			break;
		}
	}
	out[2]=fx;
	out[4]=nf;
	out[5]=nl;
	out[6]=ldt;
	free_real_vector(d,1);
	free_real_vector(y,1);
	free_real_vector(z,1);
	free_real_vector(q0,1);
	free_real_vector(q1,1);
	free_real_matrix(v,1,n,1);
	free_real_matrix(a,1,n,1);

//	for (i=0; i<40; ++i) data->x[i] = (double)x[i+1];

}
Ejemplo n.º 8
0
void peide(int n, int m, int nobs, int *nbp, real_t par[],
		real_t res[], int bp[], real_t **jtjinv,
		real_t in[], real_t out[],
		int (*deriv)(int,int,real_t [],real_t [],real_t,real_t []),
		int (*jacdfdy)(int,int,real_t [],real_t [],real_t,real_t **),
		int (*jacdfdp)(int,int,real_t [],real_t [],real_t,real_t **),
		void (*callystart)(int,int,real_t [],real_t [],real_t[]),
		void (*data)(int,real_t [],real_t [],int[]),
		void (*monitor)(int,int,int,real_t [],real_t [],int,int))
{
	int i,j,weight,ncol,nrow,away,max,nfe,nis,*cobs,
			first,sec,clean,nbpold,maxfe,fe,it,err,emergency;
	real_t eps1,res1,in3,in4,fac3,fac4,aux[4],*obs,*save,*tobs,
			**yp,*ymax,*y,**fy,**fp,w,**aid,temp,
			vv,ww,w2,mu,res2,fpar,fparpres,lambda,lambdamin,p,pw,
			reltolres,abstolres,em[8],*val,*b,*bb,*parpres,**jaco;
	static real_t save1[35]={1.0, 1.0, 9.0, 4.0, 0.0, 2.0/3.0, 1.0,
			1.0/3.0, 36.0, 20.25, 1.0, 6.0/11.0, 1.0, 6.0/11.0,
			1.0/11.0, 84.028, 53.778, 0.25, 0.48, 1.0, 0.7, 0.2,
			0.02, 156.25, 108.51, 0.027778, 120.0/274.0, 1.0,
			225.0/274.0, 85.0/274.0, 15.0/274.0, 1.0/274.0, 0.0,
			187.69, 0.0047361};

	nbpold=(*nbp);
	cobs=allocate_integer_vector(1,nobs);
	obs=allocate_real_vector(1,nobs);
	save=allocate_real_vector(-38,6*n);
	tobs=allocate_real_vector(0,nobs);
	ymax=allocate_real_vector(1,n);
	y=allocate_real_vector(1,6*n*(nbpold+m+1));
	yp=allocate_real_matrix(1,nbpold+nobs,1,nbpold+m);
	fy=allocate_real_matrix(1,n,1,n);
	fp=allocate_real_matrix(1,n,1,m+nbpold);
	aid=allocate_real_matrix(1,m+nbpold,1,m+nbpold);

	for (i=0; i<=34; i++) save[-38+i]=save1[i];
	(*data)(nobs,tobs,obs,cobs);
	weight=1;
	first=sec=0;
	clean=(*nbp > 0);
	aux[2]=FLT_EPSILON;
	eps1=1.0e10;
	out[1]=0.0;
	bp[0]=max=0;
	/* smooth integration without break-points */
	if (!peidefunct(nobs,m,par,res,
			n,m,nobs,nbp,first,&sec,&max,&nis,eps1,weight,bp,
			save,ymax,y,yp,fy,fp,cobs,tobs,obs,in,aux,clean,deriv,
			jacdfdy,jacdfdp,callystart,monitor)) goto Escape;
	res1=sqrt(vecvec(1,nobs,0,res,res));
	nfe=1;
	if (in[5] == 1.0) {
		out[1]=1.0;
		goto Escape;
	}
	if (clean) {
		first=1;
		clean=0;
		fac3=sqrt(sqrt(in[3]/res1));
		fac4=sqrt(sqrt(in[4]/res1));
		eps1=res1*fac4;
		if (!peidefunct(nobs,m,par,res,
				n,m,nobs,nbp,first,&sec,&max,&nis,eps1,weight,bp,
				save,ymax,y,yp,fy,fp,cobs,tobs,obs,in,aux,clean,deriv,
				jacdfdy,jacdfdp,callystart,monitor)) goto Escape;
		first=0;
	} else
		nfe=0;
	ncol=m+(*nbp);
	nrow=nobs+(*nbp);
	sec=1;
	in3=in[3];
	in4=in[4];
	in[3]=res1;
	weight=away=0;
	out[4]=out[5]=w=0.0;
	temp=sqrt(weight)+1.0;
	weight=temp*temp;
	while (weight != 16 && *nbp > 0) {
		if (away == 0 && w != 0.0) {
			/* if no break-points were omitted then one function
				function evaluation is saved */
			w=weight/w;
			for (i=nobs+1; i<=nrow; i++) {
				for (j=1; j<=ncol; j++) yp[i][j] *= w;
				res[i] *= w;
			}
			sec=1;
			nfe--;
		}
		in[3] *= fac3*weight;
		in[4]=eps1;
		(*monitor)(2,ncol,nrow,par,res,weight,nis);
		/* marquardt's method */
		val=allocate_real_vector(1,ncol);
		b=allocate_real_vector(1,ncol);
		bb=allocate_real_vector(1,ncol);
		parpres=allocate_real_vector(1,ncol);
		jaco=allocate_real_matrix(1,nrow,1,ncol);
		vv=10.0;
		w2=0.5;
		mu=0.01;
		ww = (in[6] < 1.0e-7) ? 1.0e-8 : 1.0e-1*in[6];
		em[0]=em[2]=em[6]=in[0];
		em[4]=10*ncol;
		reltolres=in[3];
		abstolres=in[4]*in[4];
		maxfe=in[5];
		err=0;
		fe=it=1;
		p=fpar=res2=0.0;
		pw = -log(ww*in[0])/2.30;
		if (!peidefunct(nrow,ncol,par,res,
					n,m,nobs,nbp,first,&sec,&max,&nis,eps1,
					weight,bp,save,ymax,y,yp,fy,fp,cobs,tobs,obs,
					in,aux,clean,deriv,jacdfdy,jacdfdp,
					callystart,monitor))
			err=3;
		else {
			fpar=vecvec(1,nrow,0,res,res);
			out[3]=sqrt(fpar);
			emergency=0;
			it=1;
			do {
				dupmat(1,nrow,1,ncol,jaco,yp);
				i=qrisngvaldec(jaco,nrow,ncol,val,aid,em);
				if (it == 1)
					lambda=in[6]*vecvec(1,ncol,0,val,val);
				else
					if (p == 0.0) lambda *= w2;
				for (i=1; i<=ncol; i++)
					b[i]=val[i]*tamvec(1,nrow,i,jaco,res);
				while (1) {
					for (i=1; i<=ncol; i++)
						bb[i]=b[i]/(val[i]*val[i]+lambda);
					for (i=1; i<=ncol; i++)
						parpres[i]=par[i]-matvec(1,ncol,i,aid,bb);
					fe++;
					if (fe >= maxfe)
						err=1;
					else
						if (!peidefunct(nrow,ncol,parpres,res,
								n,m,nobs,nbp,first,&sec,&max,&nis,
								eps1,weight,bp,save,ymax,y,yp,fy,fp,
								cobs,tobs,obs,in,aux,clean,deriv,
								jacdfdy,jacdfdp,callystart,monitor))
							err=2;
					if (err != 0) {
						emergency=1;
						break;
					}
					fparpres=vecvec(1,nrow,0,res,res);
					res2=fpar-fparpres;
					if (res2 < mu*vecvec(1,ncol,0,b,bb)) {
						p += 1.0;
						lambda *= vv;
						if (p == 1.0) {
							lambdamin=ww*vecvec(1,ncol,0,val,val);
							if (lambda < lambdamin) lambda=lambdamin;
						}
						if (p >= pw) {
							err=4;
							emergency=1;
							break;
						}
					} else {
						dupvec(1,ncol,0,par,parpres);
						fpar=fparpres;
						break;
					}
				}
				if (emergency) break;
				it++;
			} while (fpar>abstolres &&
							res2>reltolres*fpar+abstolres);
			for (i=1; i<=ncol; i++)
				mulcol(1,ncol,i,i,jaco,aid,1.0/(val[i]+in[0]));
			for (i=1; i<=ncol; i++)
				for (j=1; j<=i; j++)
					aid[i][j]=aid[j][i]=mattam(1,ncol,i,j,jaco,jaco);
			lambda=lambdamin=val[1];
			for (i=2; i<=ncol; i++)
				if (val[i] > lambda)
					lambda=val[i];
				else
					if (val[i] < lambdamin) lambdamin=val[i];
			temp=lambda/(lambdamin+in[0]);
			out[7]=temp*temp;
			out[2]=sqrt(fpar);
			out[6]=sqrt(res2+fpar)-out[2];
		}
		out[4]=fe;
		out[5]=it-1;
		out[1]=err;
		free_real_vector(val,1);
		free_real_vector(b,1);
		free_real_vector(bb,1);
		free_real_vector(parpres,1);
		free_real_matrix(jaco,1,nrow,1);
		if (out[1] > 0.0) goto Escape;
		/* the relative starting value of lambda is adjusted
			to the last value of lambda used */
		away=out[4]-out[5]-1.0;
		in[6] *= pow(5.0,away)*pow(2.0,away-out[5]);
		nfe += out[4];
		w=weight;
		temp=sqrt(weight)+1.0;
		eps1=temp*temp*in[4]*fac4;
		away=0;
		/* omit useless break-points */
		for (j=1; j<=(*nbp); j++)
			if (fabs(obs[bp[j]]+res[bp[j]]-par[j+m]) < eps1) {
				(*nbp)--;
				for (i=j; i<=(*nbp); i++) bp[i]=bp[i+1];
				dupvec(j+m,(*nbp)+m,1,par,par);
				j--;
				away++;
				bp[*nbp+1]=0;
			}
		ncol -= away;
		nrow -= away;
		temp=sqrt(weight)+1.0;
		weight=temp*temp;
	}
	in[3]=in3;
	in[4]=in4;
	*nbp=0;
	weight=1;
	(*monitor)(2,m,nobs,par,res,weight,nis);
	/* marquardt's method */
	val=allocate_real_vector(1,m);
	b=allocate_real_vector(1,m);
	bb=allocate_real_vector(1,m);
	parpres=allocate_real_vector(1,m);
	jaco=allocate_real_matrix(1,nobs,1,m);
	vv=10.0;
	w2=0.5;
	mu=0.01;
	ww = (in[6] < 1.0e-7) ? 1.0e-8 : 1.0e-1*in[6];
	em[0]=em[2]=em[6]=in[0];
	em[4]=10*m;
	reltolres=in[3];
	abstolres=in[4]*in[4];
	maxfe=in[5];
	err=0;
	fe=it=1;
	p=fpar=res2=0.0;
	pw = -log(ww*in[0])/2.30;
	if (!peidefunct(nobs,m,par,res,
				n,m,nobs,nbp,first,&sec,&max,&nis,eps1,weight,bp,
				save,ymax,y,yp,fy,fp,cobs,tobs,obs,in,aux,clean,
				deriv,jacdfdy,jacdfdp,callystart,monitor))
		err=3;
	else {
		fpar=vecvec(1,nobs,0,res,res);
		out[3]=sqrt(fpar);
		emergency=0;
		it=1;
		do {
			dupmat(1,nobs,1,m,jaco,yp);
			i=qrisngvaldec(jaco,nobs,m,val,jtjinv,em);
			if (it == 1)
				lambda=in[6]*vecvec(1,m,0,val,val);
			else
				if (p == 0.0) lambda *= w2;
			for (i=1; i<=m; i++)
				b[i]=val[i]*tamvec(1,nobs,i,jaco,res);
			while (1) {
				for (i=1; i<=m; i++)
					bb[i]=b[i]/(val[i]*val[i]+lambda);
				for (i=1; i<=m; i++)
					parpres[i]=par[i]-matvec(1,m,i,jtjinv,bb);
				fe++;
				if (fe >= maxfe)
					err=1;
				else
					if (!peidefunct(nobs,m,parpres,res,
							n,m,nobs,nbp,first,&sec,&max,&nis,eps1,
							weight,bp,save,ymax,y,yp,fy,fp,cobs,tobs,
							obs,in,aux,clean,deriv,jacdfdy,jacdfdp,
							callystart,monitor))
						err=2;
				if (err != 0) {
					emergency=1;
					break;
				}
				fparpres=vecvec(1,nobs,0,res,res);
				res2=fpar-fparpres;
				if (res2 < mu*vecvec(1,m,0,b,bb)) {
					p += 1.0;
					lambda *= vv;
					if (p == 1.0) {
						lambdamin=ww*vecvec(1,m,0,val,val);
						if (lambda < lambdamin) lambda=lambdamin;
					}
					if (p >= pw) {
						err=4;
						emergency=1;
						break;
					}
				} else {
					dupvec(1,m,0,par,parpres);
					fpar=fparpres;
					break;
				}
			}
			if (emergency) break;
			it++;
		} while (fpar>abstolres && res2>reltolres*fpar+abstolres);
		for (i=1; i<=m; i++)
			mulcol(1,m,i,i,jaco,jtjinv,1.0/(val[i]+in[0]));
		for (i=1; i<=m; i++)
			for (j=1; j<=i; j++)
				jtjinv[i][j]=jtjinv[j][i]=mattam(1,m,i,j,jaco,jaco);
		lambda=lambdamin=val[1];
		for (i=2; i<=m; i++)
			if (val[i] > lambda)
				lambda=val[i];
			else
				if (val[i] < lambdamin) lambdamin=val[i];
		temp=lambda/(lambdamin+in[0]);
		out[7]=temp*temp;
		out[2]=sqrt(fpar);
		out[6]=sqrt(res2+fpar)-out[2];
	}
	out[4]=fe;
	out[5]=it-1;
	out[1]=err;
	free_real_vector(val,1);
	free_real_vector(b,1);
	free_real_vector(bb,1);
	free_real_vector(parpres,1);
	free_real_matrix(jaco,1,nobs,1);
	nfe += out[4];

	Escape:
	if (out[1] == 3.0)
		out[1]=2.0;
	else
		if (out[1] == 4.0) out[1]=6.0;
	if (save[-3] != 0.0) out[1]=save[-3];
	out[3]=res1;
	out[4]=nfe;
	out[5]=max;
	free_integer_vector(cobs,1);
	free_real_vector(obs,1);
	free_real_vector(save,-38);
	free_real_vector(tobs,0);
	free_real_vector(ymax,1);
	free_real_vector(y,1);
	free_real_matrix(yp,1,nbpold+nobs,1);
	free_real_matrix(fy,1,n,1);
	free_real_matrix(fp,1,n,1);
	free_real_matrix(aid,1,m+nbpold,1);
}
Ejemplo n.º 9
0
void efsirk(real_t *x, real_t xe, int m, real_t y[],
			real_t *delta, void (*derivative)(int, real_t[], real_t *),
			void (*jacobian)(int, real_t **, real_t [], real_t *),
			real_t **j, int *n, real_t aeta, real_t reta, real_t hmin,
			real_t hmax, int linear,
			void (*output)(real_t, real_t, int, real_t [],
								real_t, real_t **, int))
{
	int *allocate_integer_vector(int, int);
	real_t *allocate_real_vector(int, int);
	real_t **allocate_real_matrix(int, int, int, int);
	void free_integer_vector(int *, int);
	void free_real_vector(real_t *, int);
	void free_real_matrix(real_t **, int, int, int);
	real_t vecvec(int, int, int, real_t [], real_t []);
	real_t matmat(int, int, int, int, real_t **, real_t **);
	real_t matvec(int, int, int, real_t **, real_t []);
	void gsselm(real_t **, int, real_t [], int [], int []);
	void solelm(real_t **, int, int [], int [], real_t []);
	int k,l,lin,*ri,*ci;
	real_t step,h,mu0,mu1,mu2,theta0,theta1,nu1,nu2,nu3,yk,fk,c1,c2,
			d,*f,*k0,*labda,**j1,aux[8],discr,eta,s,z1,z2,e,alpha1,a,b;

	ri=allocate_integer_vector(1,m);
	ci=allocate_integer_vector(1,m);
	f=allocate_real_vector(1,m);
	k0=allocate_real_vector(1,m);
	labda=allocate_real_vector(1,m);
	j1=allocate_real_matrix(1,m,1,m);

	aux[2]=FLT_EPSILON;
	aux[4]=8.0;
	for (k=1; k<=m; k++) f[k]=y[k];
	*n = 0;
	(*output)(*x,xe,m,y,*delta,j,*n);
	step=0.0;
	do {
		(*n)++;
		/* difference scheme */
		(*derivative)(m,f,delta);
		/* step size */
		if (linear)
			s=h=hmax;
		else
			if (*n == 1 || hmin == hmax)
				s=h=hmin;
			else {
				eta=aeta+reta*sqrt(vecvec(1,m,0,y,y));
				c1=nu3*step;
				for (k=1; k<=m; k++) labda[k] += c1*f[k]-y[k];
				discr=sqrt(vecvec(1,m,0,labda,labda));
				s=h=(eta/(0.75*(eta+discr))+0.33)*h;
				if (h < hmin)
					s=h=hmin;
				else
					if (h > hmax) s=h=hmax;
			}
		if ((*x)+s > xe) s=xe-(*x);
		lin=((step == s) && linear);
		step=s;
		if (!linear || *n == 1) (*jacobian)(m,j,y,delta);
		if (!lin) {
			/* coefficient */
			z1=step*(*delta);
			if (*n == 1) z2=z1+z1;
			if (fabs(z2-z1) > 1.0e-6*fabs(z1) || z2 > -1.0) {
				a=z1*z1+12.0;
				b=6.0*z1;
				if (fabs(z1) < 0.1)
					alpha1=(z1*z1/140.0-1.0)*z1/30.0;
				else if (z1 < 1.0e-14)
					alpha1=1.0/3.0;
				else if (z1 < -33.0)
					alpha1=(a+b)/(3.0*z1*(2.0+z1));
				else {
					e=((z1 < 230.0) ? exp(z1) : FLT_MAX);
					alpha1=((a-b)*e-a-b)/(((2.0-z1)*e-2.0-z1)*3.0*z1);
				}
				mu2=(1.0/3.0+alpha1)*0.25;
				mu1 = -(1.0+alpha1)*0.5;
				mu0=(6.0*mu1+2.0)/9.0;
				theta0=0.25;
				theta1=0.75;
				a=3.0*alpha1;
				nu3=(1.0+a)/(5.0-a)*0.5;
				a=nu3+nu3;
				nu1=0.5-a;
				nu2=(1.0+a)*0.75;
				z2=z1;
			}
			c1=step*mu1;
			d=step*step*mu2;
			for (k=1; k<=m; k++) {
				for (l=1; l<=m; l++)
					j1[k][l]=d*matmat(1,m,k,l,j,j)+c1*j[k][l];
				j1[k][k] += 1.0;
			}
			gsselm(j1,m,aux,ri,ci);
		}
		c1=step*step*mu0;
		d=step*2.0/3.0;
		for (k=1; k<=m; k++) {
			k0[k]=fk=f[k];
			labda[k]=d*fk+c1*matvec(1,m,k,j,f);
		}
		solelm(j1,m,ri,ci,labda);
		for (k=1; k<=m; k++) f[k]=y[k]+labda[k];
		(*derivative)(m,f,delta);
		c1=theta0*step;
		c2=theta1*step;
		d=nu1*step;
		for (k=1; k<=m; k++) {
			yk=y[k];
			fk=f[k];
			labda[k]=yk+d*fk+nu2*labda[k];
			y[k]=f[k]=yk+c1*k0[k]+c2*fk;
		}
		(*x) += step;
		(*output)(*x,xe,m,y,*delta,j,*n);
	} while (*x < xe);
	free_integer_vector(ri,1);
	free_integer_vector(ci,1);
	free_real_vector(f,1);
	free_real_vector(k0,1);
	free_real_vector(labda,1);
	free_real_matrix(j1,1,m,1);
}
Ejemplo n.º 10
0
real_t flemin(int n, real_t x[], real_t g[], real_t h[],
					real_t (*funct)(int, real_t[], real_t[]),
					real_t in[], real_t out[])
{
	real_t *allocate_real_vector(int, int);
	void free_real_vector(real_t *, int);
	real_t vecvec(int, int, int, real_t [], real_t []);
	void elmvec(int, int, int, real_t [], real_t [], real_t);
	real_t symmatvec(int, int, int, real_t [], real_t []);
	void inivec(int, int, real_t [], real_t);
	void inisymd(int, int, int, real_t [], real_t);
	void mulvec(int, int, int, real_t [], real_t [], real_t);
	void dupvec(int, int, int, real_t [], real_t []);
	void linemin(int, real_t [], real_t [], real_t, real_t *, real_t [],
					real_t (*)(int, real_t[], real_t[]), real_t, real_t *,
					real_t, real_t *, int *, int, real_t []);
	void davupd(real_t [], int, real_t [], real_t [], real_t, real_t);
	void fleupd(real_t [], int, real_t [], real_t [], real_t, real_t);
	int i,it,cntl,evl,evlmax;
	real_t f,f0,fmin,mu,dg,dg0,nrmdelta,alfa,reltol,abstol,eps,tolg,
			aid,*v,*delta,*s;

	v=allocate_real_vector(1,n);
	delta=allocate_real_vector(1,n);
	s=allocate_real_vector(1,n);

	reltol=in[1];
	abstol=in[2];
	mu=in[3];
	tolg=in[4];
	fmin=in[5];
	alfa=in[6];
	evlmax=in[7];
	out[4]=0.0;
	it=0;
	f=(*funct)(n,x,g);
	evl=1;
	cntl=0;
	if (alfa > 0.0) {
		inivec(1,(n*(n+1))/2,h,0.0);
		inisymd(1,n,0,h,alfa);
	}
	for (i=1; i<=n; i++) delta[i] = -symmatvec(1,n,i,h,g);
	dg=sqrt(vecvec(1,n,0,g,g));
	nrmdelta=sqrt(vecvec(1,n,0,delta,delta));
	eps=sqrt(vecvec(1,n,0,x,x))*reltol+abstol;
	dg0=vecvec(1,n,0,delta,g);
	it++;
	while ((nrmdelta > eps || dg > tolg) && (evl < evlmax)) {
		dupvec(1,n,0,s,x);
		dupvec(1,n,0,v,g);
		if (it >= n)
			alfa=1.0;
		else {
			if (it != 1)
				alfa /= nrmdelta;
			else {
				alfa=2.0*(fmin-f)/dg0;
				if (alfa > 1.0) alfa=1.0;
			}
		}
		elmvec(1,n,0,x,delta,alfa);
		f0=f;
		f=(*funct)(n,x,g);
		evl++;
		dg=vecvec(1,n,0,delta,g);
		if (it == 1 || f0-f < -mu*dg0*alfa) {
			/* line minimization */
			i=evlmax-evl;
			cntl++;
			linemin(n,s,delta,nrmdelta,&alfa,g,funct,f0,&f,
						dg0,&dg,&i,0,in);
			evl += i;
			dupvec(1,n,0,x,s);
		}
		if (alfa != 1.0) mulvec(1,n,0,delta,delta,alfa);
		mulvec(1,n,0,v,v,-1.0);
		elmvec(1,n,0,v,g,1.0);
		for (i=1; i<=n; i++) s[i]=symmatvec(1,n,i,h,v);
		aid=vecvec(1,n,0,v,s);
		dg=(dg-dg0)*alfa;
		if (dg > 0.0)
			if (dg >= aid)
				fleupd(h,n,delta,s,1.0/dg,(1.0+aid/dg)/dg);
			else
				davupd(h,n,delta,s,1.0/dg,1.0/aid);
		for (i=1; i<=n; i++) delta[i] = -symmatvec(1,n,i,h,g);
		alfa *= nrmdelta;
		nrmdelta=sqrt(vecvec(1,n,0,delta,delta));
		eps=sqrt(vecvec(1,n,0,x,x))*reltol+abstol;
		dg=sqrt(vecvec(1,n,0,g,g));
		dg0=vecvec(1,n,0,delta,g);
		if (dg0 > 0.0) {
			out[4] = -1.0;
			break;
		}
		it++;
	}
	out[0]=nrmdelta;
	out[1]=dg;
	out[2]=evl;
	out[3]=cntl;
	free_real_vector(v,1);
	free_real_vector(delta,1);
	free_real_vector(s,1);
	return f;
}
Ejemplo n.º 11
0
void ark(real_t *t, real_t *te, int *m0, int *m, real_t u[],
			void (*derivative)(int *, int *, real_t *, real_t[]),
			real_t data[],
			void (*out)(int *, int *, real_t *, real_t *, real_t [],
							real_t []))
{
	real_t *allocate_real_vector(int, int);
	real_t **allocate_real_matrix(int, int, int, int);
	void free_real_vector(real_t *, int);
	void free_real_matrix(real_t **, int, int, int);
	void inivec(int, int, real_t [], real_t);
	void mulvec(int, int, int, real_t [], real_t [], real_t);
	void dupvec(int, int, int, real_t [], real_t []);
	real_t vecvec(int, int, int, real_t [], real_t []);
	void elmvec(int, int, int, real_t [], real_t [], real_t);
	void decsol(real_t **, int, real_t [], real_t []);
	real_t arkmui(int, int, int, real_t []);
	real_t arklabda(int, int, int, int, real_t []);
	static real_t th1[8] = {1.0, 0.5, 1.0/6.0, 1.0/3.0, 1.0/24.0,
		1.0/12.0, 0.125, 0.25};
	static real_t ec0,ec1,ec2,tau0,tau1,tau2,taus,t2;
	int p,n,q,start,step1,last,i,j,k,l,n1,m00;
	real_t thetanm1,tau,betan,qinv,eta,*mu,*lambda,*thetha,*ro,*r,
			**alfa,th[9],aux[4],s,ss,theta0,tauacc,taustab,
			aa,bb,cc,ec,mt,lt;

	n=data[1];
	m00=(*m0);
	mu=allocate_real_vector(1,n);
	lambda=allocate_real_vector(1,n);
	thetha=allocate_real_vector(0,n);
	ro=allocate_real_vector(m00,*m);
	r=allocate_real_vector(m00,*m);
	alfa=allocate_real_matrix(1,8,1,n+1);

	p=data[2];
	ec1=ec2=0.0;
	betan=data[3];
	thetanm1 = (p == 3) ? 0.75 : 1.0;
	theta0=1.0-thetanm1;
	s=1.0;
	for (j=n-1; j>=1; j--) {
		s = -s*theta0+data[n+10-j];
		mu[j]=data[n+11-j]/s;
		lambda[j]=mu[j]-theta0;
	}
	for (i=1; i<=8; i++)
		for (j=0; j<=n; j++)
			if (i == 1) alfa[i][j+1]=1.0;
			else if (j == 0) alfa[i][j+1]=0.0;
			else if (i == 2 || i == 4 || i == 8)
					alfa[i][j+1]=pow(arkmui(j,n,p,lambda),(i+2)/3);
			else if ((i == 3 || i == 6) && j > 1) {
				s=0.0;
				for (l=1; l<=j-1; l++)
					s += arklabda(j,l,n,p,lambda)*
								pow(arkmui(l,n,p,lambda),i/3);
				alfa[i][j+1]=s;
			}
			else if (i == 5 && j > 2) {
				s=0.0;
				for (l=2; l<=j-1; l++) {
					ss=0.0;
					for (k=1; k<=l-1; k++)
						ss += arklabda(l,k,n,p,lambda)*
									arkmui(k,n,p,lambda);
					s += arklabda(j,l,n,p,lambda)*ss;
				}
				alfa[i][j+1]=s;
			}
			else if (i == 7 && j > 1) {
				s=0.0;
				for (l=1; l<=j-1; l++)
					s += arklabda(j,l,n,p,lambda)*arkmui(l,n,p,lambda);
				alfa[i][j+1]=s*arkmui(j,n,p,lambda);
			}
			else alfa[i][j+1]=0.0;
	n1 = ((n < 4) ? n+1 : ((n < 7) ? 4 : 8));
	for (i=1; i<=8; i++) th[i]=th1[i-1];
	if (p == 3 && n < 7) th[1]=th[2]=0.0;
	aux[2]=FLT_EPSILON;
	decsol(alfa,n1,aux,th);
	inivec(0,n,thetha,0.0);
	dupvec(0,n1-1,1,thetha,th);
	if (!(p == 3 && n < 7)) {
		thetha[0] -= theta0;
		thetha[n-1] -= thetanm1;
		q=p+1;
	} else
		q=3;
	qinv=1.0/q;
	start=(data[8] == 0.0);
	data[10]=0.0;
	last=0;
	dupvec(*m0,*m,0,r,u);
	(*derivative)(m0,m,t,r);
	do {
		/* stepsize */
		eta=sqrt(vecvec(*m0,*m,0,u,u))*data[7]+data[6];
		if (eta > 0.0) {
			if (start) {
				if (data[8] == 0) {
					tauacc=data[5];
					step1=1;
				} else
					if (step1) {
						tauacc=pow(eta/ec2,qinv);
						if (tauacc > 10.0*tau2)
							tauacc=10.0*tau2;
						else
							step1=0;
					} else {
						bb=(ec2-ec1)/tau1;
						cc = -bb*t2+ec2;
						ec=bb*(*t)+cc;
						tauacc = (ec < 0.0) ? tau2 : pow(eta/ec,qinv);
						start=0;
					}
			} else {
				aa=((ec0-ec1)/tau0+(ec2-ec1)/tau1)/(tau1+tau0);
				bb=(ec2-ec1)/tau1-(2.0*t2-tau1)*aa;
				cc = -(aa*t2+bb)*t2+ec2;
				ec=(aa*(*t)+bb)*(*t)+cc;
				tauacc = ((ec < 0.0) ? taus : pow(eta/ec,qinv));
				if (tauacc > 2.0*taus) tauacc=2.0*taus;
				if (tauacc < taus/2.0) tauacc=taus/2.0;
			}
		} else
			tauacc=data[5];
		if (tauacc < data[5]) tauacc=data[5];
		taustab=betan/data[4];
		if (taustab < data[5]) {
			data[10]=1.0;
			break;
		}
		tau = ((tauacc > taustab) ? taustab : tauacc);
		taus=tau;
		if (tau >= (*te)-(*t)) {
			tau=(*te)-(*t);
			last=1;
		}
		tau0=tau1;
		tau1=tau2;
		tau2=tau;
		/* difference scheme */
		mulvec(*m0,*m,0,ro,r,thetha[0]);
		if (p == 3) elmvec(*m0,*m,0,u,r,0.25*tau);
		for (i=1; i<=n-1; i++) {
			mt=mu[i]*tau;
			lt=lambda[i]*tau;
			for (j=(*m0); j<=(*m); j++) r[j]=lt*r[j]+u[j];
			s=(*t)+mt;
			(*derivative)(m0,m,&s,r);
			if (thetha[i] != 0.0) elmvec(*m0,*m,0,ro,r,thetha[i]);
			if (i == n) {
				data[9]=sqrt(vecvec(*m0,*m,0,ro,ro))*tau;
				ec0=ec1;
				ec1=ec2;
				ec2=data[9]/pow(tau,q);
			}
		}
		elmvec(*m0,*m,0,u,r,thetanm1*tau);
		dupvec(*m0,*m,0,r,u);
		s=(*t)+tau;
		(*derivative)(m0,m,&s,r);
		if (thetha[n] != 0.0) elmvec(*m0,*m,0,ro,r,thetha[n]);
		data[9]=sqrt(vecvec(*m0,*m,0,ro,ro))*tau;
		ec0=ec1;
		ec1=ec2;
		ec2=data[9]/pow(tau,q);
		t2=(*t);
		if (last) {
			last=0;
			(*t)=(*te);
		} else
			(*t) += tau;
		data[8] += 1.0;
		(*out)(m0,m,t,te,u,data);
	} while ((*t) != (*te));
	free_real_vector(mu,1);
	free_real_vector(lambda,1);
	free_real_vector(thetha,0);
	free_real_vector(ro,m00);
	free_real_vector(r,m00);
	free_real_matrix(alfa,1,8,1);
}
Ejemplo n.º 12
0
void quanewbnd(int n, int lw, int rw,
					real_t x[], real_t f[], real_t jac[],
					int (*funct)(int, int, int, real_t[], real_t[]),
					real_t in[], real_t out[])
{
	real_t *allocate_real_vector(int, int);
	void free_real_vector(real_t *, int);
	real_t vecvec(int, int, int, real_t [], real_t []);
	void elmvec(int, int, int, real_t [], real_t [], real_t);
	void mulvec(int, int, int, real_t [], real_t [], real_t);
	void dupvec(int, int, int, real_t [], real_t []);
	void decsolbnd(real_t [], int, int, int, real_t [], real_t []);
	int l,it,fcnt,fmax,err,b,i,j,k,r,m;
	real_t macheps,reltol,abstol,tolres,nd,mz,res,*delta,mul,crit,
			*pp,*s,aux[6],*lu;

	delta=allocate_real_vector(1,n);
	nd=0.0;
	macheps=in[0];
	reltol=in[1];
	abstol=in[2];
	tolres=in[3];
	fmax=in[4];
	mz=macheps*macheps;
	it=fcnt=0;
	b=lw+rw;
	l=(n-1)*b+n;
	b++;
	res=sqrt(vecvec(1,n,0,f,f));
	err=0;
	while (1) {
		if (err != 0 || (res < tolres &&
			sqrt(nd) < sqrt(vecvec(1,n,0,x,x))*reltol+abstol)) break;
		it++;
		if (it != 1) {
			/* update jac */
			pp=allocate_real_vector(1,n);
			s=allocate_real_vector(1,n);
			crit=nd*mz;
			for (i=1; i<=n; i++) pp[i]=delta[i]*delta[i];
			r=k=1;
			m=rw+1;
			for (i=1; i<=n; i++) {
				mul=0.0;
				for (j=r; j<=m; j++) mul += pp[j];
				j=r-k;
				if (fabs(mul) > crit) elmvec(k,m-j,j,jac,delta,f[i]/mul);
				k += b;
				if (i > lw)
					r++;
				else
					k--;
				if (m < n) m++;
			}
			free_real_vector(pp,1);
			free_real_vector(s,1);
		}
		/* direction */
		lu=allocate_real_vector(1,l);
		aux[2]=macheps;
		mulvec(1,n,0,delta,f,-1.0);
		dupvec(1,l,0,lu,jac);
		decsolbnd(lu,n,lw,rw,aux,delta);
		free_real_vector(lu,1);
		if (aux[3] != n) {
			err=3;
			break;
		} else {
			elmvec(1,n,0,x,delta,1.0);
			nd=vecvec(1,n,0,delta,delta);
			/* evaluate */
			fcnt += n;
			if (!((*funct)(n,1,n,x,f))) {
				err=2;
				break;
			}
			if (fcnt > fmax) err=1;
			res=sqrt(vecvec(1,n,0,f,f));
		}
	}
	out[1]=sqrt(nd);
	out[2]=res;
	out[3]=fcnt;
	out[4]=it;
	out[5]=err;
	free_real_vector(delta,1);
}
Ejemplo n.º 13
0
/*-------------------------------------------------------------------------------
 calculate the least squares solution of an overdetermined system of nonlinear equations
 with Marquardt's method 

-------------------------------------------------------------------------------*/
void Ti_Optimization::MarquardtforCylinderFitting(
			   int m,
			   int n, 
			   double**g_pnt,
			   double* const par, 
			   double*& g, 
			   double**v,
			   int (*funct)(int m, int n, double* const par, double* g,double**g_pnt),
			   void (*jacobian)(int m, int n, double* const par, double*& g, double **jac,double**g_pnt),
			   double in[], 
			   double out[]
			   )
{
	int		maxfe,fe,it,i,j,err,emergency;
	double	vv,ww,w,mu,res,fpar,fparpres,lambda,lambdamin,p,pw,reltolres,
			abstolres,em[8],*val,*b,*bb,*parpres,**jac,temp;

	val		= allocate_real_vector(1,n);
	b		= allocate_real_vector(1,n);
	bb		= allocate_real_vector(1,n);
	parpres = allocate_real_vector(1,n);
	jac		= allocate_real_matrix(1,m,1,n);
	assert( (val != NULL) &&
		    (b   != NULL) &&
			(bb  != NULL) &&
			(parpres!= NULL)&&
			(jac != NULL)
			);
	vv      = 10.0;
	w		= 0.5;
	mu		= 0.01;
	ww		= (in[6] < 1.0e-7) ? 1.0e-8 : 1.0e-1*in[6];
	em[0] = em[2] = em[6] = in[0];
	em[4] = 10*n;
	reltolres =in[3];
	abstolres=in[4]*in[4];
	maxfe=(int)in[5];
	err=0;
	fe=it=1;
	p=fpar=res=0.0;
	pw = -log(ww*in[0])/2.30;
	if (!(*funct)(m,n,par,g,g_pnt))
	{
		err=3;
		out[4]=fe;
		out[5]=it-1;
		out[1]=err;
		free_real_vector(val,1);
		free_real_vector(b,1);
		free_real_vector(bb,1);
		free_real_vector(parpres,1);
		free_real_matrix(jac,1,m,1);
		return;
	}
	fpar=vecvec(1,m,0,g,g);// norm of residual vector
	out[3]=sqrt(fpar);
	emergency=0;
	it=1;
	do {
 		(*jacobian)(m,n,par,g,jac,g_pnt);
		i = qrisngvaldec(jac,m,n,val,v,em);
		if (it == 1)
			lambda = in[6]*vecvec(1,n,0,val,val);
		else
			if (p == 0.0)
				lambda *= w;
		for (i=1; i<=n; i++) 
			b[i] = val[i]*tamvec(1,m,i,jac,g);
		while (1)
		{
			for (i=1; i<=n; i++) 
				bb[i]=b[i]/(val[i]*val[i]+lambda);
			for (i=1; i<=n; i++)
				parpres[i]=par[i]-matvec(1,n,i,v,bb);

	     	//normalization ,this section only used for cylinder fitting, 
			//when it is used in other situations,it should be removed
			  temp = sqrt(parpres[4]*parpres[4]+parpres[5]*parpres[5]+parpres[6]*parpres[6]);
			  parpres[4] /= temp;
			  parpres[5] /= temp;
			  parpres[6] /= temp;
            //end normalization
			fe++;
			if (fe >= maxfe)
				err=1;
			else
				if (!(*funct)(m,n,parpres,g,g_pnt))
					err=2;
			if (err != 0) 
			{
				emergency = 1;
				break;
			}
			fparpres=vecvec(1,m,0,g,g);
			res=fpar-fparpres;
			if (res < mu*vecvec(1,n,0,b,bb)) 
			{
				p += 1.0;
				lambda *= vv;
				if (p == 1.0) 
				{
					lambdamin=ww*vecvec(1,n,0,val,val);
					if (lambda < lambdamin)
						lambda=lambdamin;
				}
				if (p >= pw)
				{
					err=4;
					emergency=1;
					break;
				}
			} // end if
			else 
			{

				dupvec(1,n,0,par,parpres);
				fpar=fparpres;
				break;
			} // end else
		} // end while
		if (emergency) 
			break;
		it++;
	} 
	while ( 
			(fpar > abstolres) &&   
		    (res > reltolres*fpar+abstolres)
		  );

	for (i=1; i<=n; i++) 
		mulcol(1,n,i,i,jac,v,1.0/(val[i]+in[0]));
	for (i=1; i<=n; i++)
	{
		for (j=1; j<=i; j++) 
			v[i][j]=v[j][i]=mattam(1,n,i,j,jac,jac);
		lambda=lambdamin=val[1];
	}
	for (i=2; i<=n; i++)
	{
		if (val[i] > lambda)
			lambda=val[i];
		else
		{
			if (val[i] < lambdamin) 
				lambdamin=val[i];
		}
	}
	temp=lambda/(lambdamin+in[0]);
	out[7]=temp*temp;
	out[2]=sqrt(fpar);
	out[6]=sqrt(res+fpar)-out[2];
	out[4]=fe;
	out[5]=it-1;
	out[1]=err;

    if(val != NULL)
	{
		free_real_vector(val,1);
		val = NULL;
	}
	if (b != NULL)
	{
		free_real_vector(b,1);
		b = NULL;
	}
	if(bb!=NULL)
	{
		free_real_vector(bb,1);
		bb  = NULL;
	}
	if(parpres != NULL)
	{
		free_real_vector(parpres,1);
		parpres = NULL;
	}
	if (jac != NULL)
	{
		free_real_matrix(jac,1,m,1);
		jac = NULL;
	}
}