Beispiel #1
0
int main()
{
    const int dim=3000;
    const int n1=1000;
    const int n2=2000;
    int i,j;

/* test calculating a kernel with double entries */
    double *data1 = (double*)memalign(16,dim*n1*sizeof(double));
    double *data2 = (double*)memalign(16,dim*n2*sizeof(double));
    double *K = (double*)malloc(n1*n2*sizeof(double));
    if ((!data1) || (!data2) || (!K)) {
       free(data1);
       free(data2);
       free(K);
       return 1;
    }

    const clock_t before_init=clock();
    for (i=0;i<n1*dim;i++)
    	data1[i]=1./(double)(i+1.);
    for (i=0;i<n2*dim;i++)
    	data2[i]=1./(double)(i+1.);
    const clock_t after_init=clock();
    printf("init time: %8.4f\n",(after_init-before_init)*1./CLOCKS_PER_SEC);

    const clock_t before_chi2=clock();
    const double mean_K = chi2_distance_double(dim, n1, data1, n2, data2, K);
    const clock_t after_chi2=clock();
    printf("chi2 time: %8.4f\n",(after_chi2-before_chi2)*1./CLOCKS_PER_SEC);

    printf("result: %e\n",mean_K);

    free(data1);
    free(data2);
    free(K);
    
    return 0;
}
Beispiel #2
0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
	if (nrhs == 0)
	{
		mexPrintf("Usage: d = chi2_mex(X,Y);\n");
		mexPrintf("where X and Y are matrices of dimension [dim,npts]\n");
		mexPrintf("\nExample\n a = rand(2,10);\n b = rand(2,20);\n d = chi2_mex(a,b);\n");
		return;
	}

	if (nrhs != 3){
		mexPrintf("three input arguments expected.");
		return;
	}

	if (mxGetNumberOfDimensions(prhs[0]) != 2 || mxGetNumberOfDimensions(prhs[1]) != 2)
	{
		mexPrintf("inputs must be two dimensional");
		return;
	}

	unsigned int dim = mxGetM(prhs[0]);

	if (dim != mxGetM(prhs[1]))
	{
		mexPrintf("Dimension mismatch");
		return;
	}

	unsigned int ptsA = mxGetN(prhs[0]);
	unsigned int ptsB = mxGetN(prhs[1]);

	unsigned int dims[2];
	dims[0]= ptsA;
	dims[1]= ptsB;

	mxClassID type = mxGetClassID(prhs[0]);

	switch (type) {
	case mxSINGLE_CLASS:{
		float *vecA = (float*)mxGetPr(prhs[0]);
		float *vecB = (float*)mxGetPr(prhs[1]);
		float *weights = (float*)mxGetPr(prhs[2]);
		plhs[0] =  mxCreateNumericArray(2, dims, type, mxREAL);
		float *dist = (float *)mxGetPr(plhs[0]);
		chi2_distance_float(dim,ptsB,vecB,ptsA,vecA,dist,weights);
		break;
	}
	case mxDOUBLE_CLASS:{
		double *vecA = mxGetPr(prhs[0]);
		double *vecB = mxGetPr(prhs[1]);
		double *weights = mxGetPr(prhs[2]);
		plhs[0] =  mxCreateNumericArray(2, dims, type, mxREAL);
		double *dist = (double *)mxGetPr(plhs[0]);
		chi2_distance_double(dim,ptsB,vecB,ptsA,vecA,dist,weights);
		break;
	}
	default:
		mexPrintf("input type not supported\n");
		return;
	}
	
	return;
}
Beispiel #3
0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
  double *vecA, *vecB, *pA,*pB;
  double *dist;
  unsigned int i, ptsA, ptsB, dim, kptsA, kptsB;
  int *sym;
  
  if (nrhs == 0)
  {
    mexPrintf("Usage: d = chi2_mex(X,Y);\n");
    mexPrintf("where X and Y are matrices of dimension [dim,npts]\n");
    mexPrintf("\nExample\n a = rand(2,10);\n b = rand(2,20);\n d = chi2_mex(a,b);\n");
    return;
  }

  if (nrhs != 3) {
    mexPrintf("three input arguments expected: A, B, and sym, sym says wether A and B are the same");
    return;
  }

  if (mxGetNumberOfDimensions(prhs[0]) != 2 || mxGetNumberOfDimensions(prhs[1]) != 2) {
    mexPrintf("inputs must be two dimensional");
    return;
  }
  
  mxClassID the_class = mxGetClassID(prhs[0]);
    
  if(the_class != mxDOUBLE_CLASS) {
    mexErrMsgTxt("Histograms should have double precision!\n");
  }
            

  vecA = (double *)mxGetPr(prhs[0]);
  vecB = (double *)mxGetPr(prhs[1]);
  sym = (int *)mxGetPr(prhs[2]);
  
  ptsA = mxGetN(prhs[0]);
  ptsB = mxGetN(prhs[1]);
  dim = mxGetM(prhs[0]);
  
  if (dim != mxGetM(prhs[1]))
  {
    mexPrintf("Dimension mismatch");
    return;
  }
  
  const mwSize ndims[2] = {ptsA, ptsB};
  
  mxArray* mxdist = mxCreateNumericArray(2, ndims,the_class,mxREAL);    
  dist = (double *)mxGetData(mxdist);
  /*plhs[0] = mxCreateDoubleMatrix(ptsA,ptsB,mxREAL);*/
  /*dist = (float *)mxGetPr(plhs[0]);*/
    
  /*chi2_distance_float(dim,ptsB,vecB,ptsA,vecA,dist);    */
          
  /* printf("get_num_threads: %d\n", omp_get_num_threads()); 
     printf("hello");*/
 
  if(*sym) {
    chi2sym_distance_double(dim, ptsB, vecB, dist); 
  } else {
    chi2_distance_double(dim, ptsB, vecB, ptsA, vecA, dist); 
  }
  
  plhs[0] = mxdist;
  
  return;
}
Beispiel #4
0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
	if (nrhs == 0)
	{
		mexPrintf("Usage: d = chi2_mex(X,Y);\n");
		mexPrintf("where X and Y are matrices of dimension [dim,npts]\n");
		mexPrintf("\nExample\n a = rand(2,10);\n b = rand(2,20);\n d = chi2_mex(a,b);\n");
		return;
	}

	if (nrhs != 1 && nrhs != 2){
		mexPrintf("one or two arguments required");
		return;
	}

	switch (nrhs) {
	case 1:{
		unsigned int dim = mxGetM(prhs[0]);
		unsigned int pts = mxGetN(prhs[0]);
		mwSize dims[2];
		dims[0] = (mwSize)pts; dims[1] = (mwSize)pts;

		mxClassID type = mxGetClassID(prhs[0]);

		switch (type) {
		case mxSINGLE_CLASS:{
			float *vecA = (float*)mxGetPr(prhs[0]);
			plhs[0] =  mxCreateNumericArray(2, dims, type, mxREAL);
			float *dist = (float *)mxGetPr(plhs[0]);
			for (unsigned int i=0; i<pts; i++){
				dist[i*pts+i]=0.0;
				for (unsigned int j=i+1; j<pts; j++){
					chi2_distance_float(dim,(int)1,&vecA[i*dim],(int)1,&vecA[j*dim],&dist[i*pts+j]);
					dist[j*pts+i] = dist[i*pts+j];
				}
			}
			break;
		}
		case mxDOUBLE_CLASS:{
			double *vecA = mxGetPr(prhs[0]);
			plhs[0] =  mxCreateNumericArray(2, dims, type, mxREAL);
			double *dist = (double *)mxGetPr(plhs[0]);
			for (unsigned int i=0; i<pts; i++){
				dist[i*pts+i]=0.0;
				for (unsigned int j=i; j<pts; j++){
					chi2_distance_double(dim,1,&vecA[i*dim],1,&vecA[j*dim],&dist[i*pts+j]);
					dist[j*pts+i] = dist[i*pts+j];
				}
			}
			break;

		}
		default:
			mexPrintf("input type not supported\n");
			return;
		}
		break;
	}
	case 2:{

		if (mxGetNumberOfDimensions(prhs[0]) != 2 || mxGetNumberOfDimensions(prhs[1]) != 2)
		{
			mexPrintf("inputs must be two dimensional");
			return;
		}

		unsigned int dim = mxGetM(prhs[0]);

		if (dim != mxGetM(prhs[1]))
		{
			mexPrintf("Dimension mismatch");
			return;
		}

		unsigned int ptsA = mxGetN(prhs[0]);
		unsigned int ptsB = mxGetN(prhs[1]);

		mwSize dims[2];
		dims[0]= ptsA;
		dims[1]= ptsB;

		mxClassID type = mxGetClassID(prhs[0]);

		switch (type) {
		case mxSINGLE_CLASS:{
			float *vecA = (float*)mxGetPr(prhs[0]);
			float *vecB = (float*)mxGetPr(prhs[1]);
			plhs[0] =  mxCreateNumericArray(2, dims, type, mxREAL);
			float *dist = (float *)mxGetPr(plhs[0]);
			chi2_distance_float(dim,ptsB,vecB,ptsA,vecA,dist);
			break;
		}
		case mxDOUBLE_CLASS:{
			double *vecA = mxGetPr(prhs[0]);
			double *vecB = mxGetPr(prhs[1]);
			plhs[0] =  mxCreateNumericArray(2, dims, type, mxREAL);
			double *dist = (double *)mxGetPr(plhs[0]);
			chi2_distance_double(dim,ptsB,vecB,ptsA,vecA,dist);
			break;
		}
		default:
			mexPrintf("input type not supported\n");
			return;
		}
		break;
	}
	default:{
		assert(0);
	}
	}
		
	return;
}