예제 #1
0
파일: FLANN.cpp 프로젝트: rforge/clusterds
RcppExport SEXP GetAllPoints(SEXP x,SEXP n,SEXP c) {

    try {
	Rcpp::XPtr< flann::Index<flann::L2<float> >  > index(x);
	Rcpp::NumericVector npoints(n);
	Rcpp::NumericVector cn(c);
	int colNum = cn[0];

	float *data = new float[colNum];
	
	for(int i=0;i<colNum;i++) {
	    data[i] = 0;
	    i++;
	}

	flann::Matrix<float> dataset = flann::Matrix<float>(data,1,colNum);

	delete [] data;

	std::vector< std::vector<int> > indices;
	std::vector< std::vector<float> > dists;

	index->knnSearch(dataset,indices,dists,npoints[0],flann::SearchParams(-1));

	std::sort (indices[0].begin(), indices[0].end()); 

	Rcpp::NumericMatrix results(indices[0].size(), colNum);
	Rcpp::IntegerVector rownames;

	int num = indices[0].size();

	//#pragma omp parallel for ordered schedule(dynamic)
	for(int i=0;i<num;i++) {
	    float* indexPoint = index->getPoint(indices[0][i]);
	    for(int j=0;j<colNum;j++) {
		results(i,j)=(*(indexPoint+j));
	    }

	    //#pragma omp ordered
	    rownames.push_back(indices[0][i]);
	}

	Rcpp::List dimnms = Rcpp::List::create(rownames, Rcpp::Range(1,colNum));
	results.attr("dimnames") = dimnms;

	return results;

    } catch( std::exception &ex ) {		// or use END_RCPP macro
	forward_exception_to_r( ex );
    } catch(...) {
	::Rf_error( "c++ exception (unknown reason)" );
    }
    return R_NilValue; // -Wall
}
예제 #2
0
/**
 * @return The mean and standard deviation of the current set of simulations
 */
SimulationWithErrors SimulationAggregator::average() const {
  const size_t maxorder(results[0].maxorder),
      ntimes(results[0].counts[0].size()), nruns(results.size());
  SimulationWithErrors retval(maxorder, ntimes);

  for (size_t i = 0; i < maxorder; ++i) {
    auto &orderCounts = retval.sim.counts[i];
    auto &orderErrors = retval.errors[i];
    for (size_t j = 0; j < ntimes; ++j) {
      double mean(0.0);
      size_t npoints(0);
      for (size_t k = 0; k < nruns; ++k) {
        const double val = results[k].counts[i][j];
        if (val > 0.0) {
          mean += val;
          npoints += 1;
        }
      }
      if (npoints < 2) {
        orderCounts[j] = 0.0;
        orderErrors[j] = 0.0;
      } else {
        const double dblPts = static_cast<double>(npoints);
        orderCounts[j] = mean / dblPts;
        // error is std dev
        double sumsq(0.0);
        for (size_t k = 0; k < nruns; ++k) {
          const double val = results[k].counts[i][j];
          if (val > 0.0) {
            const double diff = (val - mean);
            sumsq += diff * diff;
          }
        }
        orderErrors[j] = sqrt(sumsq / (dblPts * (dblPts - 1)));
      }
    }
  }

  return retval;
}