Skip to content
Gijs Molenaar edited this page Feb 13, 2014 · 4 revisions

Strided Iterators

_ create your own VellsMath _

If for some reason you cannot use vellsmath (eg. in MeqFunctional) you can use the Vells::Strides to loop over your Vells. This is what is needed:

ASsuming you want to do some calculateions with a vector of Vells:

   std::vector<const Vells *> myvells;
  //get the shapes of your Vells:
  Ndim_=myvells.size();
  const Vells::Shape * shapes[Ndim_];
  for(int i=0;i<Ndim_;i++)
       shapes[i]=&(myvells[i]->shape());

  // create strides
  Vells::Strides *strides = new Vells::Strides[Ndim_];

  // Compute strides
  Vells::Shape outshape;
  Vells::computeStrides(outshape,strides,Ndim_,shapes,"Functional::evaluateTensors");


  //create iterators
  Vells::DimCounter counter(outshape);
  std::vector<Vells::ConstStridedIterator<double> > strided_iter(Ndim_);
  for(int i =0; i<Ndim_;i++){
    strided_iter[i]= Vells::ConstStridedIterator<double>(myvells[i],strides_[i]);
    
  }

  //now loop over your vells
  Vells output =  Vells(0.,outshape);
  double * res = output.getStorage<double>();
  while(true){
    *res=0;
    for(int i =0; i<Ndim_;i++)
        //do some calculation
        *res +=  *(strided_iter[i]);
        
    //to next point in my output
    res++;
    //increment iterators
    int ndim = counter.incr(); 
    if( !ndim )    // break out when counter is finished
      break;
    for(int i =0; i<Ndim_;i++)
      strided_iter[i].incr(ndim);

  }



Clone this wiki locally