void boundary_transformation_inverse(boundary_transformation_t *t,
		double const *x, double *y, unsigned long len)
{
	double lb, ub, al, au;
	unsigned long i;

	for (i = 0; i < len; ++i) {
		lb = t->lower_bounds[_index(t, i)];
		ub = t->upper_bounds[_index(t, i)];
		al = t->al[_index(t, i)];
		au = t->au[_index(t, i)];
		y[i] = x[i];
		if (y[i] < lb + al)
			y[i] = (lb - al) + 2 * sqrt(al * (y[i] - lb));
		else if (y[i] > ub - au)
			y[i] = (ub + au) - 2 * sqrt(au * (ub - y[i]));
	}
	if (11 < 3 || do_assertions) {
		double *z = calloc(len, sizeof(double));
		for (i = 0; i < len; ++i)
			z[i] = y[i];
		boundary_transformation(t, z, y, len);
		for (i = 0; i < len; ++i)
			if (fabs(y[i] - x[i]) > 1e-14)
				printf("  difference for index %ld should be zero, is %f ", i, y[i] - x[i]);
		for (i = 0; i < len; ++i)
			y[i] = z[i];
		free(z);
	}
}
Exemplo n.º 2
0
cGame::~cGame()
{
	int playerindex = _index(_pplayer);
	if (playerindex == cBiota::NOINDEX) //player won't be deleted by cBiota destructor
	{
		delete _pplayer; /* I need to do this while the _pbiota is still good,
			as the delete _pplayer will call the ~cCritter, which will use
			_pbiota to find the pgame to call fixPointerRefs with. */
		_pplayer = NULL;
	}
	delete _pskybox;
	_pskybox = NULL;
	delete _pbiota;
	_pbiota = NULL;
	delete _pcollider;
	_pcollider = NULL;
	delete _pcontroller;
	_pcontroller = NULL;
	delete _plightingmodel;
	_plightingmodel = NULL;
/* At game exit, we release any of the shared cSpriteQuake model resources that we've
allocated from within the quakeMD2model.cpp file. I might have expected to
do this in the ~PopApp destructor in pop.cpp, but for some reason when I
get to that destructor, the information in the two static _map... fields has been
lost track of without being deleted. */
	cTextureInfo::_mapFilenameToTextureInfo.free();
	cMD2Model::_mapFilenameToMD2Info.free();
	stopMusic();
}
Exemplo n.º 3
0
void Dict::set(const char *key, int val)
{
    int index, len;
    
    index = _index(key);
    if (index == -1) {
        len = _length();
        
        values = (int*) realloc(values, sizeof(int) * (len + 1));
        values[len] = NULL;
        
        counts = (int*) realloc(counts, sizeof(int) * (len + 1));
        counts[len - 1] = 0;
        counts[len] = NULL;
        
        keys = (const char**) realloc(keys, sizeof(const char*) * (len + 1));
        keys[len - 1] = key;
        keys[len] = NULL;
        
        index = len - 1;
    }
    
    // extend array
    values[index] = val;
    counts[index]++;
}
Exemplo n.º 4
0
void BBP::RegenerateS() {
    // _Smsg[i][_I][_j]
    _Smsg.resize( _fg->nrVars() );
    for( size_t i = 0; i < _fg->nrVars(); i++ ) {
        _Smsg[i].resize( _fg->nbV(i).size() );
        foreach( const Neighbor &I, _fg->nbV(i) ) {
            _Smsg[i][I.iter].resize( _fg->nbF(I).size() );
            foreach( const Neighbor &j, _fg->nbF(I) )
                if( i != j ) {
                    Factor prod( _fg->factor(I) );
                    foreach( const Neighbor &k, _fg->nbF(I) ) {
                        if( k != i && k.node != j.node ) {
                            const _ind_t &ind = _index( k, k.dual );
                            Prob p( _bp_dual.msgN( k, k.dual ) );
                            for( size_t x_I = 0; x_I < prod.nrStates(); x_I++ )
                                prod.set( x_I, prod[x_I] * p[ind[x_I]] );
                        }
                    }
                    // "Marginalize" onto i|j (unnormalized)
                    Prob marg;
                    marg = prod.marginal( VarSet(_fg->var(i), _fg->var(j)), false ).p();
                    _Smsg[i][I.iter][j.iter] = marg;
                }
        }
    }
void boundary_transformation(boundary_transformation_t *t,
		double const *x, double *y, unsigned long len)
{
	double lb, ub, al, au;
	unsigned long i;
	boundary_transformation_shift_into_feasible_preimage(t, x, y, len);
	for(i = 0; i < len; ++i) {
		lb = t->lower_bounds[_index(t, i)];
		ub = t->upper_bounds[_index(t, i)];
		al = t->al[_index(t, i)];
		au = t->au[_index(t, i)];
        if (y[i] < lb + al)
            y[i] = lb + (y[i] - (lb - al)) * (y[i] - (lb - al)) / 4. / al;
        else if (y[i] > ub - au)
            y[i] = ub - (y[i] - (ub + au)) * (y[i] - (ub + au)) / 4. / au;
	}
}
Exemplo n.º 6
0
void cGame::Serialize(CArchive& ar)
{
	int focusindex, playerindex;
 	CObject::Serialize(ar);
  		/*It's worth noting that when we call this next line in loading mode,
 			the _pbiota will be pointing in a non-NULL cBiota that was created by the 
 			cGame constructor, so we'll need to have the cBiota::Serialize take care of
 			deleting members of an existing cBiota before loading into it. */
	_pbiota->Serialize(ar);
 		/* I don't serialize _arrayHCURSOR because (a) it gets set by the constructor 
 			and (b) it's like a	pointer in that the numerical value will depend on the 
 			program run, so the number would be useless. */
  	_pcontroller->Serialize(ar);
	_plightingmodel->Serialize(ar);
 	if (ar.IsStoring()) // Save 
 	{
 		focusindex = _index(_pfocus);
 		playerindex =  _index(_pplayer);
 		ar << _seedcount << _gameover << _maxscore << _scorecorrection << _wrapflag <<
 			_bDragging << _border << _pskybox <<
 			focusindex << playerindex << _autoplay << _level <<  _spritetype ;
 		if (playerindex == cBiota::NOINDEX)
 			ar << _pplayer;
 	}
  	else //Load 
 	{
		delete _pskybox;
 		ar >> _seedcount >> _gameover >> _maxscore >> _scorecorrection >> _wrapflag >>
 			_bDragging >> _border >> _pskybox >>
 			focusindex >> playerindex >> _autoplay >> _level >> _spritetype;
 		if (focusindex != cBiota::NOINDEX)
 			_pfocus = _pbiota->GetAt(focusindex);
 		if (playerindex == cBiota::NOINDEX)
 		{
 			delete _pplayer; /* If _pplayer is not in the cBiota, it wasn't deleted in
 				cBiota::Serialize so we delete it here.  But in the else case below,
 				_pplayer equals one of the pointers deleted in the cBiota::_free call
 				in cBiota::Serialize, so it is actually a bad pointer and we don't delete
 				it. */
			_pplayer = NULL;
 			ar >> _pplayer;
 			_pplayer->setOwner(_pbiota);
 			_pplayer->fixPointerRefs();
 		}
 		else /* _pplayer is a bad (already deleted) pointer; we overwrite it without
Exemplo n.º 7
0
int Dict::get(const char *key)
{
    int index;
    
    index = _index(key);
    
    if (index == -1)
        return NULL;
    
    counts[index]--;
    return values[index];
}
Exemplo n.º 8
0
vector<int>ANBSample::GridIndex(sample *p){

	//构造网格索引
	vector<int>_index(3);

	for(int i = 0; i < 3; i++){

		_index[i]=min( (int)grid._size[i]-1, (int)max(0, (int)floor((p->_position[i]-boundingbox[i*2])/grid._bin_width[i])) );

	}

	return  _index;
}
Exemplo n.º 9
0
  // static void _resize(std::vector<std::size_t>& exponents, std::size_t new_ni, std::size_t new_nj)
  static void _resize(polynomial& p, std::size_t new_ni, std::size_t new_nj)
  {
    auto& cur_exponents = p.exponents;
    auto& cur_ni = p.ni;
    auto& cur_nj = p.nj;

    if(new_ni < cur_ni || new_nj < cur_nj)
    {
      return;
    }

    std::vector<std::size_t> new_exponents(new_ni * new_nj, 0);

    for(std::size_t i = 0; i < cur_ni; ++i)
      for(std::size_t j = 0; j < cur_nj; ++j)
      {
        const std::size_t new_n = _index(i, j, new_ni, new_nj);
        const std::size_t cur_n = _index(i, j, cur_ni, cur_nj);
        new_exponents[new_n] = cur_exponents[cur_n];
      }

    cur_exponents = new_exponents;
    cur_ni = new_ni; cur_nj = new_nj;
  }
Exemplo n.º 10
0
static void demean (Dump* a,
		    Dump* f)
/* ------------------------------------------------------------------------- *
 * Subtract mean values in "a" from field values in "f".
 * ------------------------------------------------------------------------- */
{
  int       i, j;
  const int nfields = strlen (f -> field);
  const int npts    = a -> np * a -> np * a -> nz * a -> nel;

  for (i = 0; i < nfields; i++) {
    j = _index (a -> field, f -> field[i]);
    dvsub (npts, f -> data[i], 1, a -> data[j], 1, f -> data[i], 1);
  }
}
void boundary_transformation_shift_into_feasible_preimage(
			boundary_transformation_t *t, double const *x, double *y, unsigned long len)
{
	double lb, ub, al, au, r, xlow, xup;
	unsigned long i;

	for(i = 0; i < len; ++i) {
		lb = t->lower_bounds[_index(t, i)];
		ub = t->upper_bounds[_index(t, i)];
		al = t->al[_index(t, i)];
		au = t->al[_index(t, i)];
		xlow = lb - 2 * al - (ub - lb) / 2.0;
		xup = ub + 2 * au + (ub - lb) / 2.0;
        r = 2 * (ub - lb + al + au); /* == xup - xlow == period of the transformation */

        y[i] = x[i];

		if (y[i] < xlow) { /* shift up */
			y[i] += r * (1 + (int)((xlow - y[i]) / r));
		}
		if (y[i] > xup) { /* shift down */
			y[i] -= r * (1 + (int)((y[i] - xup) / r));
			/* printf(" \n%f\n", fmod(y[i] - ub - au, r)); */
		}
		if (y[i] < lb - al) /* mirror */
			y[i] += 2 * (lb - al - y[i]);
        if (y[i] > ub + au)
        	y[i] -= 2 * (y[i] - ub - au);

		if ((y[i] < lb - al - 1e-15) || (y[i] > ub + au + 1e-15)) {
			printf("BUG in boundary_transformation_shift_into_feasible_preimage: lb=%f, ub=%f, al=%f au=%f, y=%f\n",
					lb, ub, al, au, y[i]);
			_FatalError("BUG");
		}
	}
}
Exemplo n.º 12
0
static void covary (Dump* h)
/* ------------------------------------------------------------------------- *
 * On input, h contains data for the average velocity fields and the
 * average product velocity fields.  Convert the average product
 * velocity fields to covariances by subtracting the products of the
 * average velocity fields.  This is simple since the data are already
 * in physical space and we don't dealias.
 * ------------------------------------------------------------------------- */
{
  int       i, j, k, m;
  const int npts = h -> np * h -> np * h -> nz * h -> nel;
  
  /* -- 2D. */

  i = _index (h -> field, 'u');
  j = _index (h -> field, 'v');

  k = _index (h -> field, 'A');
  dvvvtm (npts, h->data[k], 1, h->data[i], 1, h->data[i], 1, h->data[k], 1);
  k = _index (h -> field, 'B');
  dvvvtm (npts, h->data[k], 1, h->data[i], 1, h->data[j], 1, h->data[k], 1);
  k = _index ( h -> field, 'C');
  dvvvtm (npts, h->data[k], 1, h->data[j], 1, h->data[j], 1, h->data[k], 1);

  if (!strstr (h -> field, "w")) return;

  /* -- 3D. */
  
  k = _index (h -> field, 'w');

  m = _index (h -> field, 'D');
  dvvvtm (npts, h->data[m], 1, h->data[i], 1, h->data[k], 1, h->data[m], 1);
  m = _index (h -> field, 'E');
  dvvvtm (npts, h->data[m], 1, h->data[j], 1, h->data[k], 1, h->data[m], 1);
  m = _index (h -> field, 'F');
  dvvvtm (npts, h->data[m], 1, h->data[k], 1, h->data[k], 1, h->data[m], 1);
}
Exemplo n.º 13
0
void BBP::RegenerateU() {
    // _Umsg[I][_i]
    _Umsg.resize( _fg->nrFactors() );
    for( size_t I = 0; I < _fg->nrFactors(); I++ ) {
        _Umsg[I].resize( _fg->nbF(I).size() );
        foreach( const Neighbor &i, _fg->nbF(I) ) {
            Prob prod( _fg->factor(I).nrStates(), 1.0 );
            foreach( const Neighbor &j, _fg->nbF(I) )
                if( i.node != j.node ) {
                    Prob n_jI( _bp_dual.msgN( j, j.dual ) );
                    const _ind_t &ind = _index( j, j.dual );
                    // multiply prod by n_jI
                    for( size_t x_I = 0; x_I < prod.size(); x_I++ )
                        prod.set( x_I, prod[x_I] * n_jI[ind[x_I]] );
                }
            _Umsg[I][i.iter] = prod;
        }
    }
}
Exemplo n.º 14
0
string Operand::walker_subscript_val(void)
{
    stringstream ss;

    switch(meta().layout) {
        case KP_SCALAR_TEMP:
        case KP_SCALAR_CONST:
        case KP_SCALAR:
        case KP_CONTRACTABLE:
            ss << walker();
            break;

        case KP_CONSECUTIVE:
        case KP_CONTIGUOUS:
        case KP_STRIDED:
            ss << _index(walker(), "eidx");
            break;

        case KP_SPARSE:
            ss << _beef("Non-implemented KP_LAYOUT.");
            break;
    }
    return ss.str();
}
Exemplo n.º 15
0
void menu(){

   do {
      printf("%s\n", "1.\tAppend");
      printf("%s\n", "2.\tIn");
      printf("%s\n", "3.\tIndex");
      printf("%s\n", "4.\tRemove");
      printf("%s\n", "5.\tShow");
      printf("%s\n", "6.\tExit system");

      printf("%s\n", "choose");
      scanf("%i", &option);

   } while(option > 7);

      switch (option) {
         case 1:
            append();
            break;
         case 2:
            in();
            break;
         case 3:
            _index();
            break;
         case 4:
            remove();
            break;
         case 5:
            show();
            break;
         case 6:
            exit(0);
      }

}
Exemplo n.º 16
0
 static std::size_t& _get_exponent(polynomial& p, std::size_t i, std::size_t j)
 {
   return p.exponents[_index(i, j, p.ni, p.nj)];
 }
Exemplo n.º 17
0
string Iterspace::shape(uint32_t dim)
{
    stringstream ss;
    ss << _index(_access_ptr(name(), "shape"), dim);
    return ss.str();
}
Exemplo n.º 18
0
Box
init(
	InitSecond const &_init_second,
	Function const &_function
)
{
	static_assert(
		fcppt::math::box::is_box<
			Box
		>::value,
		"Box must be a box::object"
	);

	typedef
	std::array<
		fcppt::homogenous_pair<
			typename
			Box::value_type
		>,
		Box::dim_wrapper::value
	>
	result_array;

	result_array const results(
		fcppt::algorithm::array_init<
			result_array
		>(
			fcppt::math::detail::init_function<
				Function
			>(
				_function
			)
		)
	);

	return
		Box(
			fcppt::math::vector::init<
				typename
				Box::vector
			>(
				[
					&results
				](
					auto const _index
				)
				{
					return
						std::get<
							_index()
						>(
							results
						).first;
				}
			),
			_init_second(
				[
					&results
				](
					auto const _index
				)
				{
					return
						std::get<
							_index()
						>(
							results
						).second;
				}
			)
		);
}
Exemplo n.º 19
0
fcppt::math::vector::static_<
	T,
	N
	+
	1u
>
hypersphere_to_cartesian(
	fcppt::math::vector::object<
		T,
		N,
		S
	> const &_angles
)
{
	typedef
	fcppt::math::vector::static_<
		T,
		N
		+
		1u
	>
	result_type;

	typedef
	typename
	result_type::value_type
	value_type;

	return
		fcppt::math::vector::init<
			result_type
		>(
			[
				&_angles
			](
				auto const _index
			)
			{
				FCPPT_USE(
					_index
				);

				value_type const sins(
					fcppt::algorithm::fold(
						fcppt::math::int_range_count<
							_index()
						>{},
						fcppt::literal<
							value_type
						>(
							1
						),
						[
							&_angles
						](
							auto const _inner_index,
							value_type const _prod
						)
						{
							FCPPT_USE(
								_inner_index
							);

							typedef
							fcppt::tag_type<
								decltype(
									_inner_index
								)
							>
							inner_index;

							return
								_prod
								*
								std::sin(
									fcppt::math::at_c<
										inner_index::value
									>(
										_angles
									)
								);
						}
					)
				);

				result_type const cos_angles(
					fcppt::math::vector::construct(
						fcppt::math::vector::init<
							fcppt::math::vector::static_<
								T,
								N
							>
						>(
							[
								&_angles
							](
								auto const _inner_index
							)
							{
								FCPPT_USE(
									_inner_index
								);

								return
									std::cos(
										fcppt::math::at_c<
											_inner_index()
										>(
											_angles
										)
									);
							}
						),
						fcppt::literal<
							value_type
						>(
							1
						)
					)
				);

				return
					sins
					*
					fcppt::math::at_c<
						_index()
					>(
						cos_angles
					);
			}
		);
}
Exemplo n.º 20
0
int main (int    argc,
	  char** argv)
/* ------------------------------------------------------------------------- *
 * Wrapper.
 * ------------------------------------------------------------------------- */
{
  char   buf[STR_MAX], fields[STR_MAX], fmt[STR_MAX];
  int    i, j, n, np, nz, nel, mode = 0, swab = 0, cmplx = 0;
  int    nfields, nplane, nplaneEven, nptsEven, ntot;
  FILE   *fp_in = stdin, *fp_out = stdout;
  double **data, *plane, *vcmpt;

  getargs (argc, argv, &fp_in, &mode, &cmplx);
  format  (fmt);

  while (fgets (buf, STR_MAX, fp_in)) { 

    fputs (buf, fp_out); fgets (buf, STR_MAX, fp_in);
    fputs (buf, fp_out); fgets (buf, STR_MAX, fp_in);
    
    if (sscanf (buf, "%d%*s%d%d", &np, &nz, &nel) != 3)
      message (prog, "unable to read the file size", ERROR);

    if (2 * mode > nz) {
      sprintf (fields, "too many modes (%1d) for input (nz = %1d)", mode, nz);
      message (prog, fields, ERROR);
    }
    if (cmplx && nz != 2) {
      sprintf (fields, "need nz = 2 with full-complex single mode (%1d)", nz);
      message (prog, fields, ERROR);
    } 

    fprintf (fp_out, hdr_fmt[2], np, np, 1, nel);

    n = 6;
    while (--n) {
      fgets (buf, STR_MAX, fp_in);
      fputs (buf, fp_out);   
    }

    fgets  (fields, STR_MAX, fp_in);
    memset (fields+25, '\0', STR_MAX-25);
    for (nfields = 0, i = 0; i < 25; i++) if (isalpha(fields[i])) nfields++;
    if (nfields < 4) {
      if (!(strchr(fields, 'u') && strchr(fields, 'v')))
	message (prog, "need fields u, v to compute K.E.", ERROR);
    } else {
      if (!(strchr(fields, 'u') && strchr(fields, 'v') && strchr(fields, 'w')))
	message (prog, "need fields u, v, w to compute K.E.", ERROR);
    }
    fprintf (fp_out, hdr_fmt[8], "q");

    fgets (buf, STR_MAX, fp_in);
    for (i = 0; i < strlen (buf); i++) buf[i] = tolower (buf[i]);

    if (!strstr(buf, "binary"))
      message (prog, "input file not binary format", ERROR);
    if (!strstr (buf, "endian"))
      message (prog, "input field file in unknown binary format", WARNING);
    else
      swab = ((strstr (buf, "big") && strstr (fmt, "little")) ||
	      (strstr (fmt, "big") && strstr (buf, "little")) );
    sprintf (buf, "%s %s", "binary", fmt);
    fprintf (fp_out, hdr_fmt[9], buf);

    /* -- Set sizes, allocate storage. */

    nplane     = np * np * nel;
    nplaneEven = (nplane & 1) ? nplane + 1 : nplane;
    nptsEven   = nz * nplaneEven;
    ntot       = nfields * nptsEven;

    data  = dmatrix (0, nfields - 1, 0, nptsEven - 1);
    plane = dvector (0, nplane  - 1);
    
    /* -- Read in all data fields. */

    dzero (ntot, data[0], 1);
    dzero (nplane, plane, 1);

    for (i = 0; i < nfields; i++) {
      for (j = 0; j < nz; j++) {
	if (fread (data[i] + j*nplaneEven, sizeof (double), nplane, fp_in)
	    != nplane)
	  message (prog, "an error occured while reading", ERROR);
      }
      if (swab)   dbrev (nptsEven, data[i], 1, data[i], 1);
      if (!cmplx) dDFTr (data[i], nz, nplaneEven, +1);
    }
    
    /* -- Compute K.E.: start by adding in real part. */

    for (i = 0; i < nfields - 1; i++) {
      vcmpt = data[_index (fields, 'u' + i)] + 2 * mode * nplaneEven;
      dvvtvp (nplane, vcmpt, 1, vcmpt, 1, plane, 1, plane, 1);
    }

    /* -- Add in imaginary part if not mode zero. */

    if (mode || cmplx) {
      for (i = 0; i < nfields - 1; i++) {
	vcmpt = data[_index (fields, 'u' + i)] + (2 * mode + 1) * nplaneEven;
	dvvtvp (nplane, vcmpt, 1, vcmpt, 1, plane, 1, plane, 1);
      }
    }

    /*  -- Normalize to make q = 0.5*UiUi. */

    dsmul (nplane, 0.5, plane, 1, plane, 1);
    
    if (fwrite (plane, sizeof (double), nplane, fp_out) != nplane)
      message (prog, "an error occured while writing", ERROR);

    freeDmatrix (data,  0, 0);
    freeDvector (plane, 0);
  } 
  
  return EXIT_SUCCESS;
}
Exemplo n.º 21
0
void gl_feedback_block(block_t *block) {
    if (block->len == 0) {
        return;
    }
    int size = feedback_sizeof(state.feedback.type);

    GLfloat *v, *c, *t;
    int v1, v2, v3;
    int first = _index(block, 0);
    for (int j = 0; j < block->len; j++) {
        int i = _index(block, j);
        v2 = v1;
        v3 = v2;
        v1 = i;

        // TODO: overflow'd feedback returns -1 in glRenderMode
#define polygon(n) { if (feedback_overflow(size * n)) return; feedback_polygon(n); state.feedback.values -= 1; }
        switch (block->mode) {
            case GL_LINES:
                if (i % 2 == 1) {
                    polygon(2);
                    feedback_vertex(block, v2);
                    feedback_vertex(block, v1);
                }
                break;
            case GL_LINE_LOOP:
                // catch the loop segment
                if (i == block->len - 1) {
                    polygon(2);
                    feedback_vertex(block, v1);
                    feedback_vertex(block, first);
                }
            case GL_LINE_STRIP:
                if (i > 0) {
                    polygon(2);
                    feedback_vertex(block, v2);
                    feedback_vertex(block, v1);
                }
                break;
            case GL_TRIANGLES:
                if (i % 3 == 2) {
                    polygon(3);
                    feedback_vertex(block, v3);
                    feedback_vertex(block, v2);
                    feedback_vertex(block, v1);
                }
            case GL_TRIANGLE_FAN:
                if (i > 1) {
                    polygon(3);
                    feedback_vertex(block, v2);
                    feedback_vertex(block, v1);
                    feedback_vertex(block, first);
                }
                break;
            case GL_TRIANGLE_STRIP:
                if (i > 1) {
                    polygon(3);
                    feedback_vertex(block, v3);
                    feedback_vertex(block, v2);
                    feedback_vertex(block, v1);
                }
                break;
            case GL_POINTS:
                polygon(1);
                feedback_vertex(block, v1);
                break;
            default:
                printf("warning: unsupported GL_SELECT mode: %s\n", gl_str(block->mode));
                return;
        }
    }
}
Exemplo n.º 22
0
static inline GLfloat *_vert(block_t *block, int i) {
    return &block->vert[_index(block, i) * 3];
}