void test_constructor_vfunc ()
{
  Function_vector vfunc;
  vfunc.push_back(cube_function_1);
  vfunc.push_back(cube_function_2);

  Labeling_function lfunc(vfunc);

  Point_3 p1(0.5, 0.5, 0.5);
  Point_3 p2(2.5, 2.5, 2.5);
  Point_3 p3(1.5, 1.5, 1.5);
  Point_3 p_out(4., 4., 4.);

  Labeling_function::return_type rp1 = lfunc(p1);
  Labeling_function::return_type rp2 = lfunc(p2);
  Labeling_function::return_type rp3 = lfunc(p3);
  Labeling_function::return_type rp_out = lfunc(p_out);

  assert(rp1 != 0);
  assert(rp2 != 0);
  assert(rp3 != 0);
  assert(rp_out == 0);

  assert(rp1 != rp2);
  assert(rp1 != rp3);
  assert(rp2 != rp3);
}
示例#2
0
loliObj* eval_list(loliObj* lst, loliObj* env){
  loliObj* car = lcons(lst)->head()->eval(env);
  loliObj* cdr = lcons(lst)->tail();
  std::cout<<"HEAD: "<<car->toString()<<"\tTAIL: "<<cdr->toString()<<std::endl;
  //    std::cout<<cdr->type->toString()<<std::endl;
  if(lfunc(car)->rtype){
    if(lcons(lcons(lcons(cdr)->tail())->head())->hd == NULL){
      std::cout<<lcons(lcons(lcons(cdr)->tail())->head())->toString()<<std::endl;
      return c_apply(car, cdr->eval(env), env);
    }
    return c_apply(car, cdr, env);
  }
  else{
    loli_err(car->toString() + " is not a function!");
  }
  return nil;
}
示例#3
0
/*ARGSUSED1*/
void key (unsigned char key, int x, int y) {
    switch (key) {
    case 'b': bfunc(); break;
    case 'c': cfunc(); break;
    case 'l': lfunc(); break;
    case 't': tfunc(); break;
    case 'f': ffunc(); break;
    case 'n': nfunc(); break;
    case 'u': ufunc(); break;
    case 'U': Ufunc(); break;
    case 'p': pfunc(); break;
    case 'P': Pfunc(); break;
    case 'w': wfunc(); break;
    case 'x': xfunc(); break;
    case 'X': Xfunc(); break;
    case 'y': yfunc(); break;
    case '\033': exit(EXIT_SUCCESS); break;
    default: break;
    }
}
示例#4
0
GDens *GDens2InitNew(double fac, double mean, double stdev, int n, GDensFunc_tp *lfunc, char *lfunc_arg)
{
    /* 
       arguments are:

       limits are mean +- fac*stdev, and n-regions.

       lfunc: the function returning log(f(x,lfunc_arg)), unormalized
     */
    
    GDens *ptr;
    int i;
    double dx, df, f, *cpoints;
    
    fac = ABS(fac);

    ptr       = Calloc(1, GDens);
    ptr->xmin = mean -fac*stdev;
    ptr->xmax = mean +fac*stdev;
    ptr->n    = n;
    ptr->elm  = Calloc(n+1, GDensElm);


    cpoints = cut_points(fac, n);

    for(i=0;i<ptr->n;i++)
    {
	dx              = stdev*(cpoints[i+1]-cpoints[i]);
        ptr->elm[i].xl  = mean + cpoints[i]*stdev;
	ptr->elm[i].xr  = ptr->elm[i].xl + dx;
	ptr->elm[i].lfl = (i==0 ? lfunc(ptr->elm[i].xl, lfunc_arg) : ptr->elm[i-1].lfr);	
	ptr->elm[i].lfr = lfunc(ptr->elm[i].xr, lfunc_arg);

	/* 
	   now, try to decide a nice place to place the midpoint. currently, i try to split the
	   region into (approximately) equal integrals
	*/
	df = ptr->elm[i].lfr - ptr->elm[i].lfl;
	f  = (ISZERO(df) ? 0.5 : log(0.5*(exp(df)+1.0))/df);

	/* 
	   need also a more safe option than ISZERO
	 */
	if (ISZERO(f) || ISZERO(1.-f)) f = 0.5;
	if (f < FLT_EPSILON || 1.-f < FLT_EPSILON) f = 0.5;
	
	if (f == 0.5)
	{
	    /* 
	       this will emulate a log-linear spline
	    */
	    ptr->elm[i].xm = ptr->elm[i].xl + dx*f;
	    ptr->elm[i].lfm = 0.5*(ptr->elm[i].lfl + ptr->elm[i].lfr);
	}
	else
	{
	    ptr->elm[i].xm  = ptr->elm[i].xl + dx*f;
	    ptr->elm[i].lfm = lfunc(ptr->elm[i].xm, lfunc_arg);
	}
    }
    for(i=0;i<ptr->n-1;i++) ptr->elm[i].lfr = ptr->elm[i+1].lfl;
    ptr->elm[ptr->n-1].lfr = lfunc(ptr->elm[ptr->n-1].xr, lfunc_arg);

    GDens2Update(ptr);

    return ptr;
}
示例#5
0
GDens *GDens2Init(double xmin, double xmax, int n, int n_min, GDensFunc_tp *lfunc, char *lfunc_arg)
{
    /* 
       arguments are:

       xmin : left  limit
       xmax : right limit
       n    : total number of interior points, which makes n+1 cells
       n_min: make first n_min interior points by equal division of (xmax-xmin), then
              n-n_min divisions by dividing the cell with the largest probability in half.
       lfunc: the function returning log(f(x,lfunc_arg)), unormalized
     */
    
    GDens *ptr;
    int i, nn;
    double dx, df, f;
    
    ptr       = Calloc(1, GDens);
    ptr->order= 2;
    ptr->xmin = MIN(xmin, xmax);
    ptr->xmax = MAX(xmin, xmax);
    ptr->n    = MAX(1, n_min);
    nn        = MAX(ptr->n, n);
    ptr->elm  = Calloc(nn+1, GDensElm);

    /* 
       make first equal spaced points
     */
    dx = (ptr->xmax -ptr->xmin)/((double)n_min);
    for(i=0;i<ptr->n;i++)
    {
	ptr->elm[i].xl  = ptr->xmin + i*dx;
	ptr->elm[i].xr  = ptr->elm[i].xl + dx;
	ptr->elm[i].lfl = (i==0 ? lfunc(ptr->elm[i].xl, lfunc_arg) : ptr->elm[i-1].lfr);	
	ptr->elm[i].lfr = lfunc(ptr->elm[i].xr, lfunc_arg);

	/* 
	   now, try to decide a nice place to place the midpoint. currently, i try to split the
	   region into (approximately) equal integrals
	*/
	df = ptr->elm[i].lfr - ptr->elm[i].lfl;
	f  = (ISZERO(df) ? 0.5 : log(0.5*(exp(df)+1.0))/df);

	/* 
	   need also a more safe option than ISZERO
	 */
	if (ISZERO(f) || ISZERO(1.-f)) f = 0.5;
	if (f < FLT_EPSILON || 1.-f < FLT_EPSILON) f = 0.5;
	
	if (f == 0.5)
	{
	    /* 
	       this will emulate a log-linear spline
	    */
	    ptr->elm[i].xm = ptr->elm[i].xl + dx*f;
	    ptr->elm[i].lfm = 0.5*(ptr->elm[i].lfl + ptr->elm[i].lfr);
	}
	else
	{
	    ptr->elm[i].xm  = ptr->elm[i].xl + dx*f;
	    ptr->elm[i].lfm = lfunc(ptr->elm[i].xm, lfunc_arg);
	}
    }
    for(i=0;i<ptr->n-1;i++) ptr->elm[i].lfr = ptr->elm[i+1].lfl;
    ptr->elm[ptr->n-1].lfr = lfunc(ptr->elm[ptr->n-1].xr, lfunc_arg);

    GDens2Update(ptr);

    /* 
       decide now the rest of the points by dividing the cell with largest probability in half. try
       to split the region into (approximately) equal integrals
     */
    for(i=n_min;i<nn;i++)
    {
	/* 
	   split the first elm, and add one at the end
	 */
	ptr->elm[ptr->n].lfl = ptr->elm[0].lfm;
	ptr->elm[ptr->n].xl  = ptr->elm[0].xm;
	ptr->elm[ptr->n].lfr = ptr->elm[0].lfr;
	ptr->elm[ptr->n].xr  = ptr->elm[0].xr;
	df = ptr->elm[ptr->n].lfr - ptr->elm[ptr->n].lfl;
	f  = (ISZERO(df) ? 0.5 : log(0.5*(exp(df)+1.0))/df);
	if (ISZERO(f) || ISZERO(1.-f)) f = 0.5;
	if (f < FLT_EPSILON || 1.-f < FLT_EPSILON) f = 0.5;
	if (f == 0.5)
	{
	    /* 
	       this will emulate a log-linear spline
	    */
	    ptr->elm[ptr->n].xm  = ptr->elm[ptr->n].xl + f*(ptr->elm[ptr->n].xr -ptr->elm[ptr->n].xl);
	    ptr->elm[ptr->n].lfm = 0.5*(ptr->elm[ptr->n].lfl + ptr->elm[ptr->n].lfr);
	}
	else
	{
	    ptr->elm[ptr->n].xm  = ptr->elm[ptr->n].xl + f*(ptr->elm[ptr->n].xr -ptr->elm[ptr->n].xl);
	    ptr->elm[ptr->n].lfm = lfunc(ptr->elm[ptr->n].xm, lfunc_arg);
	}

	ptr->elm[0].lfr = ptr->elm[0].lfm;
	ptr->elm[0].xr  = ptr->elm[0].xm;
	df = ptr->elm[0].lfr - ptr->elm[0].lfl;
	f  = (ISZERO(df) ? 0.5 : log(0.5*(exp(df)+1.0))/df);
	if (ISZERO(f) || ISZERO(1.-f)) f = 0.5;
	if (f < FLT_EPSILON || 1.-f < FLT_EPSILON) f = 0.5;
	if (f == 0.5)
	{
	    /* 
	       this will emulate a log-linear spline
	    */
	    ptr->elm[0].xm  = ptr->elm[0].xl + f*(ptr->elm[0].xr -ptr->elm[0].xl);
	    ptr->elm[0].lfm = 0.5*(ptr->elm[0].lfl + ptr->elm[0].lfr);
	}
	else
	{
	    ptr->elm[0].xm  = ptr->elm[0].xl + f*(ptr->elm[0].xr -ptr->elm[0].xl);
	    ptr->elm[0].lfm = lfunc(ptr->elm[0].xm, lfunc_arg);
	}
	
	ptr->n++;
	GDens2Update(ptr);
    }

    return ptr;
}
示例#6
0
GDens *GDens1Init(double xmin, double xmax, int n, int n_min, GDensFunc_tp *lfunc, char *lfunc_arg)
{
    /* 
       arguments are:

       xmin : left  limit
       xmax : right limit
       n    : total number of interior points, which makes n+1 cells
       n_min: make first n_min interior points by equal division of (xmax-xmin), then
              n-n_min divisions by dividing the cell with the largest probability in half.
       lfunc: the function returning log(f(x)), unormalized

     */
    
    GDens *ptr;
    int i, nn;
    double xnew, f, df, dx;
    
    ptr       = Calloc(1, GDens);
    ptr->order= 1;
    ptr->xmin = MIN(xmin, xmax);
    ptr->xmax = MAX(xmin, xmax);
    ptr->n    = MAX(2, n_min);
    nn        = MAX(ptr->n, n);
    ptr->elm  = Calloc(nn+1, GDensElm);

    /* 
       make first equal spaced points
     */
    dx = (ptr->xmax -ptr->xmin)/((double)n_min);
    for(i=0;i<ptr->n;i++)
    {
	ptr->elm[i].x   = ptr->xmin + i*dx;
	ptr->elm[i].dx  = dx;
	ptr->elm[i].lfl = lfunc(ptr->elm[i].x, lfunc_arg);
    }
    for(i=0;i<ptr->n-1;i++) ptr->elm[i].lfr = ptr->elm[i+1].lfl;
    ptr->elm[ptr->n-1].lfr = lfunc(ptr->elm[ptr->n-1].x + ptr->elm[ptr->n-1].dx, lfunc_arg);

    GDens1Update(ptr);

    /* 
       decide now the rest of the points by dividing the cell with largest probability in half
     */
    for(i=n_min;i<n;i++)
    {
	/* 
	   split the first elm, and add one at the end
	 */
	if (ABS(ptr->elm[0].b) < FLT_EPSILON)
	    f = 0.5;
	else
	{
	    df = ptr->elm[0].b*ptr->elm[0].dx;
	    f  = log(0.5*(exp(df)+1.0))/df;
	}

	xnew                 = ptr->elm[0].x + ptr->elm[0].dx*f;
	ptr->elm[ptr->n].lfr = ptr->elm[0].lfr;
	ptr->elm[ptr->n].x   = xnew;
	ptr->elm[ptr->n].dx  = ptr->elm[0].dx*(1.-f);
	ptr->elm[0].lfr      = ptr->elm[ptr->n].lfl = lfunc(xnew, lfunc_arg);
	ptr->elm[0].dx      *= f;
	
	ptr->n++;
	GDens1Update(ptr);
    }

    return ptr;
}