Example #1
0
void CoordHomogenization::compute(vector<Point2f> points) {
	if (points.empty())
		throw std::runtime_error("CoordHomogenization::compute - empty dataset");

	Mean = std::accumulate(points.begin(), points.end(), Point2f(0.0f, 0.0f) );
	Mean *= 1.0 / points.size();

	SumDist sumDist(Mean);
	Scale = std::accumulate(points.begin(), points.end(), 0.0f, sumDist ) / points.size();
}
Example #2
0
File: getMLE.cpp Project: axrt/gbra
// [[Rcpp::export]]
RcppExport SEXP getMLE(SEXP mtx, SEXP steps, SEXP epsilon){
  
  NumericMatrix mt(mtx);
  const NumericVector st(steps);
  const NumericVector eps(epsilon);
  const size_t stps=st[0];
  const double epsil=eps[0];
  
  const NumericVector * gprimePrior=E(mt);
  NumericVector * gprime= new NumericVector(*gprimePrior);
  delete gprimePrior;

  
  NumericMatrix track(stps,2);
  fillInMatrix(track,0);
  
  double gDist(sumDist(mt,*gprime));
  double deltaG=0;
  R_len_t counter=0;
  
  while ((counter < stps) && (deltaG > epsil)){

    track(counter,0)=gDist;
    track(counter,1)=deltaG;
    
    NumericMatrix * devisable = new NumericMatrix(mt.nrow(),mt.ncol());
    for(int i=0;i<mt.nrow();i++){
      NumericVector row=mt(i,_);
      const NumericVector * ddr = diffDistRatio(row,*gprime);
      (*devisable)(i,_)=*ddr;
      delete ddr;
    }
    
    const NumericVector * rs=rowSums(*devisable);
    delete devisable;
    
    NumericMatrix * devider = new NumericMatrix(mt.nrow(),mt.ncol());
    for(int i=0;i<mt.nrow();i++){
      NumericVector row=mt(i,_);
      const NumericVector * ddr = diffDistInverse(row,*gprime);
      (*devisable)(i,_)=*ddr;
      delete ddr;
    }
    
    const double suDiv=sumMatrix(*devider);
    delete devider;
    
    for(int i=0;i<gprime->size();i++){
      (*gprime)[i]=(*rs)[i]/suDiv;
    }
    delete rs;
    
    gDist=double(sumDist(mt,*gprime));
    deltaG=fabs(track(counter,0)-gDist);
    counter++;
  }
  
  List ret;
  ret.push_back(*gprime);
  delete gprime;
  ret.push_back(track);
  
  return ret;
}
Example #3
0
int main(int argc, char** argv){
	// first input is the task number (5 = sumSlow)
	// second input is the maximal n
	int Task;
	if(argc > 1)
	{
		if ((atoi(argv[1])>0) && (atoi(argv[1])<6))
			Task = atoi(argv[1]);
		else
			Task=4;
	}
	else
		Task = 4;

	int iterations;
	if(argc > 2)
	{
		if (atoi(argv[2])>0) 
			iterations = atoi(argv[2]);
		else
			iterations=13;
	}	
	else
		iterations = 13;

	//printf("%d\n",iterations);
	int rank = 0;
	int i; // Generic loop variable.
	double startTime; // Storing the start time while measuring.
	double S = (M_PI*M_PI)/6; // The limit of the series.
	MPI_Init(&argc,&argv);

	// Setting up the size of the partial sums to generate. This should be altered to read something from the command line.
	//Sint iterations = 13; // Number of different summing lengths.
	int N[iterations]; // Vector with the summetion lengths.
	double* Sn     = (double*)malloc(iterations*sizeof(double));
	double* SnSlow = (double*)malloc(iterations*sizeof(double)); // Vectors of the partial sums.
	basicSetup(iterations, N, Sn, SnSlow);

	
	if (Task==1)
	{		
		printf("running the non-parallelized programm (Task1)\n");
		printf("n \terror \t\ttime\n");
		//for(i=0; i<iterations; ++i)
		i=iterations-1;
		{
			startTime= WallTime();
			Sn[i] = sum(N[i]);
			printf("%d \t%e \t%e\n",N[i], S-Sn[i],startTime- WallTime());
		}
			
	}
	if (Task==2)
	{	
		printf("running the openMP-parallelized programm (Task2)\n");
		printf("n \terror \t\ttime\n");
		//for(i=0; i<iterations; ++i)
		i=iterations-1;
		{
			startTime= WallTime();
			Sn[i] = sumShared(N[i]);
			printf("%d \t%e \t%e\n",N[i], S-Sn[i],startTime- WallTime());
		}
			
	}
	if (Task==3)
	{	
		MPI_Comm_rank(MPI_COMM_WORLD,&rank);
		if(rank==0)
		{			
			printf("running the MPI-parallelized programm (Task3)\n");
			printf("n \terror \t\ttime\n");
		}
		//for(i=0; i<iterations; ++i)
		i=iterations-1;
		{
			if(rank==0)
				startTime= WallTime();
			Sn[i] = sumDist(N[i],&rank);
			if(rank==0)
				printf("%d \t%e \t%e\n",N[i], S-Sn[i],startTime- WallTime());
		}
			
	}
	if (Task==4)
	{	
		MPI_Comm_rank(MPI_COMM_WORLD,&rank);
		if(rank==0)
		{
			printf("running the openMP- and MPI-parallelized programm (Task4)\n");
			printf("n \terror \t\ttime\n");
		}
		//for(i=0; i<iterations; ++i)
		i=iterations-1;
		{
			if(rank==0)
				startTime= WallTime();
			Sn[i] = sumHybrid(N[i],&rank);
			if(rank==0)
				printf("%d \t%e \t%e\n",N[i], S-Sn[i],startTime- WallTime());
		}			
	}
	if (Task==5)
	{		
		printf("running the non-parallelized programm with better summation order(Task1)\n");
		printf("n \terror \t\ttime\n");
		//for(i=0; i<iterations; ++i)
		i=iterations-1;
		{
			startTime= WallTime();
			Sn[i] = sumSlow(N[i]);
			printf("%d \t%e \t%e\n",N[i], S-Sn[i],startTime- WallTime());
		}
			
	}
	
	free(Sn); free(SnSlow);
	
	MPI_Finalize();
	return 0;
}