int main(){
  ElemType data;
  void* pointer = &data;
  vector vec = new_vector(pointer,10);
  ElemType *pointer2 = (ElemType*)vec.data;
  printf("%d\n",pointer2->number);
  delete_vector(vec);
  return 0;
}
void RungeKuttaSolver::rkqs(double y[], double dydx[], int n, double *x, double htry, double eps, double yscal[], double *hdid, double *hnext, RungeKuttaEquation *Equations[])
{
	int i;
	double errmax,h,xnew,*yerr,*ytemp;

	yerr=new_vector(1,n);
	ytemp=new_vector(1,n);
	h=htry;
	for (;;)
	{
		rkck(y,dydx,n,*x,h,ytemp,yerr,Equations);
		errmax=0.0;
		for (i=1;i<=n;i++) 
			errmax=FMAX(errmax,fabs(yerr[i]/yscal[i]));
		errmax /= eps;
		if (errmax > 1.0)
		{
			h=SAFETY*h*pow(errmax,PSHRNK);
			if (h < 0.1*h)
				h *= 0.1;

			xnew=(*x)+h;
			if (xnew == *x)	
				nrerror("stepsize underflow in rkqs");
			continue;
		}
		else
		{
			if (errmax > ERRCON) 
				*hnext=SAFETY*h*pow(errmax,PGROW);
			else 
				*hnext=5.0*h;
			*x += (*hdid=h);
			for (i=1;i<=n;i++) 
				y[i]=ytemp[i];
			break;
		}
	}
	delete_vector(ytemp,1,n);
	delete_vector(yerr,1,n);
}
void RungeKuttaSolver::rkck(double y[], double dydx[], int n, double x, double h, double yout[], double yerr[], RungeKuttaEquation *Equations[])
{
	int i;
	double dc1=c1-2825.0/27648.0;
	double dc3=c3-18575.0/48384.0;
	double dc4=c4-13525.0/55296.0;
	double dc6=c6-0.25;
	double *ak2,*ak3,*ak4,*ak5,*ak6,*ytemp;

	ak2=new_vector(1,n);
	ak3=new_vector(1,n);
	ak4=new_vector(1,n);
	ak5=new_vector(1,n);
	ak6=new_vector(1,n);
	ytemp=new_vector(1,n);

	for (i = 1; i <= n; i++)
		ytemp[i]=y[i]+b21*h*dydx[i];
	// (*derivs)(x+a2*h,ytemp,ak2); DJA: This is the old call which we've replaced with the following two lines
	for (i = 1; i <= n; i++)
		ak2[i] = Equations[i]->GetDerivative(ytemp[i], y[5]);
	
	for (i = 1; i <= n; i++)
		ytemp[i]=y[i]+h*(b31*dydx[i]+b32*ak2[i]);
	// (*derivs)(x+a3*h,ytemp,ak3); DJA: This is the old call which we've replaced with the following two lines	
	for (i = 1; i <= n; i++)
		ak3[i] = Equations[i]->GetDerivative(ytemp[i], y[5]);

	for (i = 1; i <= n; i++)
		ytemp[i]=y[i]+h*(b41*dydx[i]+b42*ak2[i]+b43*ak3[i]);
	// (*derivs)(x+a4*h,ytemp,ak4); DJA: This is the old call which we've replaced with the following two lines	
	for (i = 1; i <= n; i++)
		ak4[i] = Equations[i]->GetDerivative(ytemp[i], y[5]);

	for (i = 1; i <= n; i++)
		ytemp[i]=y[i]+h*(b51*dydx[i]+b52*ak2[i]+b53*ak3[i]+b54*ak4[i]);
	// (*derivs)(x+a5*h,ytemp,ak5); DJA: This is the old call which we've replaced with the following two lines	
	for (i = 1; i <= n; i++)
		ak5[i] = Equations[i]->GetDerivative(ytemp[i], y[5]);

	for (i = 1; i <= n; i++)
		ytemp[i]=y[i]+h*(b61*dydx[i]+b62*ak2[i]+b63*ak3[i]+b64*ak4[i]+b65*ak5[i]);
	//(*derivs)(x+a6*h,ytemp,ak6); DJA: This is the old call which we've replaced with the following two lines	
	for (i = 1; i <= n; i++)
		ak6[i] = Equations[i]->GetDerivative(ytemp[i], y[5]);

	for (i = 1; i <= n; i++)
		yout[i]=y[i]+h*(c1*dydx[i]+c3*ak3[i]+c4*ak4[i]+c6*ak6[i]);
	for (i = 1; i <= n; i++)
		yerr[i]=h*(dc1*dydx[i]+dc3*ak3[i]+dc4*ak4[i]+dc5*ak5[i]+dc6*ak6[i]);

	delete_vector(ytemp,1,n);
	delete_vector(ak6,1,n);
	delete_vector(ak5,1,n);
	delete_vector(ak4,1,n);
	delete_vector(ak3,1,n);
	delete_vector(ak2,1,n);
}
void RungeKuttaSolver::ode_rk(int nvar, double x1, double x2, double eps, double h1, double hmin, int *nok, int *nbad, RungeKuttaEquation *Equations[])
{
	int nstp,i;
	double xsav, x, hnext, hdid, h;	  // DJA: h is the current time step value
	double *yscal,*y,*dydx;

	yscal=new_vector(1,nvar);
	y=new_vector(1,nvar);
	dydx=new_vector(1,nvar);
	x=x1;
	h=SIGN(h1,x2-x1);
	*nok = (*nbad) = kount = 0;
	for (i = 1; i <= nvar ; i++) 
		y[i] = Equations[i]->InitialY;
	if (kmax > 0) xsav=x-dxsav*2.0;
	for (nstp=1;nstp<=MAXSTP;nstp++)
	{
		// (*derivs)(x,y,dydx); DJA: This is the old call which we've replaced with the following two lines
		for (i = 1; i <= nvar ; i++)
			dydx[i] = Equations[i]->GetDerivative(y[i], y[5]);

		for (i=1;i<=nvar;i++)
			yscal[i]=fabs(y[i])+fabs(dydx[i]*h)+TINY;

		if (kmax > 0 && kount < kmax-1 && fabs(x-xsav) > fabs(dxsav))
		{
			xp[++kount]=x;
			for (i=1;i<=nvar;i++) yp[i][kount]=y[i];
			xsav=x;
		}

		if ((x+h-x2)*(x+h-x1) > 0.0) 
			h=x2-x;

		rkqs(y,dydx,nvar,&x,h,eps,yscal,&hdid,&hnext,Equations);

		if (hdid == h) 
			++(*nok);
		else 
			++(*nbad);

		if ((x-x2)*(x2-x1) >= 0.0)
		{
			for (i=1;i<=nvar;i++) 
				Equations[i]->InitialY = y[i];

			if (kmax)
			{
				xp[++kount]=x;
				for (i=1;i<=nvar;i++) 
					yp[i][kount]=y[i];
			}
			delete_vector(dydx,1,nvar);
			delete_vector(y,1,nvar);
			delete_vector(yscal,1,nvar);
			return;
		}
		//  printf("hnext %f hmin %f\n",hnext,hmin);
		if (fabs(hnext) <= hmin)
		{
			nrerror("Step size too small in odeint");
		}
		h=hnext;
	}
	nrerror("Too many steps in routine odeint");
}
示例#5
0
void delete_datapoint(Datapoint* datapoint){
    delete_vector(datapoint->position);
    free(datapoint);
}
示例#6
0
void delete_centroid(Centroid* centroid){
    delete_vector(centroid->center);
    free(centroid);
}
示例#7
0
void
GraverAPI<T>::compute()
{
    check_consistency();
    
    Algorithm <T>* algorithm;
    DefaultController <T> * controller;
    std::ofstream* log_file = 0;
    if (ZSolveAPI<T>::options.loglevel () > 0) {
        std::string log_name = ZSolveAPI<T>::options.project () + ".log";
        log_file = new std::ofstream (log_name.c_str(), ZSolveAPI<T>::options.resume () ? std::ios::out | std::ios::app : std::ios::out);
    }
    controller = new DefaultController <T> (&std::cout, log_file, ZSolveAPI<T>::options);


    if (ZSolveAPI<T>::mat) {
        /// @TODO: transfer rhs, ub, lb, sign and rel.
        T* rhs_vec = create_zero_vector <T> (ZSolveAPI<T>::mat->data.height());
        if (ZSolveAPI<T>::rhs) { 
            for (size_t i = 0; i < ZSolveAPI<T>::rhs->data.width(); ++i) {
                rhs_vec[i] = ZSolveAPI<T>::rhs->data[0][i];
            }
        }
        LinearSystem <T> * system = new LinearSystem <T> (ZSolveAPI<T>::mat->data, rhs_vec,
							  ZSolveAPI<T>::free_default, 
							  ZSolveAPI<T>::lower_default, 
							  ZSolveAPI<T>::upper_default);
        delete_vector(rhs_vec);
        if (ZSolveAPI<T>::sign) {
            for (size_t i = 0; i < ZSolveAPI<T>::sign->data.width(); ++i) {
                switch (ZSolveAPI<T>::sign->data[0][i]) {
		case 0:
		    system->get_variable(i).set(true);
		    break;
		case 1:
		    system->get_variable(i).set(false, 0, -1);
		    break;
		case -1:
		    system->get_variable(i).set(false, 1, 0);
		    break;
		case 2:
		    system->get_variable(i).set(false);
		    break;
		default:
		    /// @TODO: The following error message should be more informative.
		    throw IOException("Unknown sign value.");
                }
            }
        }
        if (ZSolveAPI<T>::rel) {
            for (size_t i = 0; i < ZSolveAPI<T>::rel->data.width(); ++i) {
                switch (ZSolveAPI<T>::rel->data[0][i]) {
		case 0:
		    system->get_relation(i).set(Relation<T> :: Equal);
		    break;
		case 1:
		    system->get_relation(i).set(Relation<T> :: GreaterEqual);
		    break;
		case -1:
		    system->get_relation(i).set(Relation<T> :: LesserEqual);
		    break;
		default:
		    /// @TODO: The following error message should be more informative.
		    throw IOException("Unknown relation value.");
                }
            }
        }
        if (ZSolveAPI<T>::lb) {
            for (size_t i = 0; i < ZSolveAPI<T>::lb->data.width(); ++i) {
                system->get_variable(i).set_bound(true, ZSolveAPI<T>::lb->data[0][i]);
            }
        }
        if (ZSolveAPI<T>::ub) {
            for (size_t i = 0; i < ZSolveAPI<T>::ub->data.width(); ++i) {
                system->get_variable(i).set_bound(false, ZSolveAPI<T>::ub->data[0][i]);
            }
        }

        system->cancel_down();
        algorithm = new ExtendedPottier <T>;
	algorithm->init(system, controller);
        delete system;
    }
    else if (ZSolveAPI<T>::lat) {
        /// @TODO: transfer ub, lb, and sign.
        Lattice <T> * lattice = new Lattice <T> (& ZSolveAPI<T>::lat->data, 
						 ZSolveAPI<T>::free_default, 
						 ZSolveAPI<T>::lower_default, 
						 ZSolveAPI<T>::upper_default);
        if (ZSolveAPI<T>::sign) {
            for (size_t i = 0; i < ZSolveAPI<T>::sign->data.width(); ++i) {
                switch (ZSolveAPI<T>::sign->data[0][i]) {
		case 0:
		    lattice->get_variable(i).set(true);
		    break;
		case 1:
		    lattice->get_variable(i).set(false, 0, -1);
		    break;
		case -1:
		    lattice->get_variable(i).set(false, 1, 0);
		    break;
		case 2:
		    lattice->get_variable(i).set(false);
		    break;
		default:
		    /// @TODO: The following error message should be more informative.
		    throw IOException("Unknown sign value.");
                }
            }
        }
        if (ZSolveAPI<T>::lb) {
            for (size_t i = 0; i < ZSolveAPI<T>::lb->data.width(); ++i) {
                lattice->get_variable(i).set_bound(true, ZSolveAPI<T>::lb->data[0][i]);
            }
        }
        if (ZSolveAPI<T>::ub) {
            for (size_t i = 0; i < ZSolveAPI<T>::ub->data.width(); ++i) {
                lattice->get_variable(i).set_bound(false, ZSolveAPI<T>::ub->data[0][i]);
            }
        }

        lattice->reduce_gaussian();
        algorithm = new ExtendedPottier <T>;
	algorithm->init(lattice, controller);
        delete lattice;
    }
    else {
        throw IOException ("Neither " + ZSolveAPI<T>::options.project () + ".mat, " + 
			   ZSolveAPI<T>::options.project () + ".lat, nor "
			   + ZSolveAPI<T>::options.project () + ".backup found!");
    }

    // Actual computation starts here.
    algorithm->compute (ZSolveAPI<T>::options.backup_frequency ());

    algorithm->log_maxnorm ();

    extract_results(algorithm);

    delete algorithm;
    delete controller;
    if (log_file) { delete log_file; }
}