Exemplo n.º 1
0
void
test_heapsort (size_t N)
{
  int status;

  double *orig = (double *) malloc (N * sizeof (double));
  double *data = (double *) malloc (N * sizeof (double));
  size_t  *p = (size_t *) malloc (N * sizeof(size_t));

  initialize (orig, N);

  /* Already sorted */
  cpy (data, orig, N);

  status = gsl_heapsort_index (p, data, N, sizeof (double), (gsl_comparison_fn_t) & cmp_dbl);
  status |= pcheck (p, data, orig, N);
  gsl_test (status, "indexing array, n = %u, ordered", N);

  gsl_heapsort (data, N, sizeof (double), (gsl_comparison_fn_t) & cmp_dbl);
  status = check (data, orig, N);

  gsl_test (status, "sorting, array, n = %u, ordered", N);

  /* Reverse the data */

  cpy (data, orig, N);
  reverse (data, N);

  status = gsl_heapsort_index (p, data, N, sizeof (double), (gsl_comparison_fn_t) & cmp_dbl);
  status |= pcheck (p, data, orig, N);
  gsl_test (status, "indexing array, n = %u, reversed", N);

  gsl_heapsort (data, N, sizeof (double), (gsl_comparison_fn_t) & cmp_dbl);
  status = check (data, orig, N);

  gsl_test (status, "sorting, array, n = %u, reversed", N);

  /* Perform some shuffling */

  cpy (data, orig, N);
  randomize (data, N);

  status = gsl_heapsort_index (p, data, N, sizeof (double), (gsl_comparison_fn_t) & cmp_dbl);
  status |= pcheck (p, data, orig, N);
  gsl_test (status, "indexing array, n = %u, randomized", N);

  gsl_heapsort (data, N, sizeof (double), (gsl_comparison_fn_t) & cmp_dbl);
  status = check (data, orig, N);

  gsl_test (status, "sorting, array, n = %u, randomized", N);

  free (orig);
  free (data);
  free (p);
}
Exemplo n.º 2
0
void weighted_random_sample(const gsl_rng * r, const size_t numSample, const double * probs, const size_t N, int * returnSample)
// Naive weighted random sample algorithm
{

  size_t * perm  = (size_t *) malloc(sizeof(size_t) * N);

  gsl_heapsort_index(perm, probs, N, sizeof(double), (gsl_comparison_fn_t) compare_doubles_decr);

  double * cumProb = (double *) malloc(sizeof(double) * N);
  cumProb[0] = probs[perm[0]];
  for(size_t i = 1; i < N; i++)
    cumProb[i] = cumProb[i-1] + probs[perm[i]];

  size_t idx;
  double runif;

  for(size_t i = 0; i < numSample; i++){
    runif = gsl_rng_uniform(r);
    for(idx = 0; idx < N; idx++)
      if(runif < cumProb[idx])
        break;
    returnSample[i] = perm[idx];
  }

  free(cumProb);
  free(perm);
  return;
}
Exemplo n.º 3
0
static VALUE rb_gsl_heapsort_index_vector_complex(VALUE obj)
{
  gsl_vector_complex *v = NULL;
  gsl_permutation *p = NULL;
  if (!rb_block_given_p()) rb_raise(rb_eRuntimeError, "Proc is not given");
  Data_Get_Struct(obj, gsl_vector_complex, v);
  p = gsl_permutation_alloc(v->size);
  gsl_heapsort_index(p->data, v->data, v->size, sizeof(gsl_complex), rb_gsl_comparison_complex);
  return Data_Wrap_Struct(cgsl_permutation, 0, gsl_permutation_free, p);
}
Exemplo n.º 4
0
void main() {
	size_t * perm = malloc(sizeof(size_t) * 1000);
	struct rtree_t t = {0};
	int i, j, k;
	float pos[1000][3];
	float sml[1000];
	intptr_t key[1000];
	int l = 0;
	for(i = 0; i < 10; i++) {
		for(j = 0; j < 10; j++) {
			for(k = 0; k < 10; k++) {
				pos[l][0] = i * 0.1 * (1<<20);
				pos[l][1] = j * 0.1 * (1<<20);
				pos[l][2] = k * 0.1 * (1<<20);
				sml[l] = 0;
				key[l] = peano_hilbert_key(pos[l][0], pos[l][1], pos[l][2], 20);
				l++;
			}
		}
	}

	gsl_heapsort_index(perm, key, 1000, sizeof(intptr_t), (void*)intptr_t_compare);

	float (*__pos)[3] = permute(perm, pos, 3 * sizeof(float), 3 * sizeof(float), 1000, 1000);
	float * __sml = permute(perm, sml, sizeof(float), sizeof(float), 1000, 1000);
	intptr_t * __key = permute(perm, key, sizeof(intptr_t), sizeof(intptr_t), 1000, 1000);
	rtree_build(&t, __pos, __sml, __key, 1000);
	float dist[10];
	intptr_t nei[10];
	int nused = 0;
	float hhint = 100000;
	float p[3] = {499999, 499999,499999};

	rtree_neighbours(&t, p, __pos, 1000, nei, dist, 10, &nused, NULL);
	printf("hello\n");
}
Exemplo n.º 5
0
void gsl_matrix_hungarian(gsl_matrix* gm_C,gsl_matrix* gm_P,gsl_vector* gv_col_inc, gsl_permutation* gp_sol, int _bprev_init, gsl_matrix *gm_C_denied, bool bgreedy)
{
//  mexPrintf("VV\n");  
  long dim, startdim, enddim, n1,n2;
  double *C;
  int i,j;
  int **m;
  double *z;
  hungarian_problem_t p, *q;
  int matrix_size;
  double C_min=gsl_matrix_min(gm_C)-1;
  n1 = gm_C->size1;    /* first dimension of the cost matrix */
  n2 = gm_C->size2;    /* second dimension of the cost matrix */
  C = gm_C->data; 


   //greedy solution
   if (bgreedy)
   {
	int ind,ind1,ind2;
	size_t *C_ind=new size_t[n1*n2];
	gsl_heapsort_index(C_ind,C,n1*n2,sizeof(double),compare_doubles);
        bool* bperm_fix_1=new bool[n1]; bool* bperm_fix_2=new bool[n2]; int inummatch=0;
	for (i=0;i<n1;i++) {bperm_fix_1[i]=false;bperm_fix_2[i]=false;};
	gsl_matrix_set_zero(gm_P);
	for (long l=0;l<n1*n2;l++)
	{
		ind=C_ind[l];
		ind1=floor(ind/n1);
		ind2=ind%n2;
		
		if (!bperm_fix_1[ind1] and !bperm_fix_2[ind2])
		{
			bperm_fix_1[ind1]=true; bperm_fix_2[ind2]=true;
			gm_P->data[ind]=1;inummatch++;
		};
		if (inummatch==n1) break;
	};
	delete[] bperm_fix_1;delete[] bperm_fix_2;
	//because C is a transpose matrix
	gsl_matrix_transpose(gm_P);
	return;	
   };
  double C_max=((gsl_matrix_max(gm_C)-C_min>1)?(gsl_matrix_max(gm_C)-C_min):1)*(n1>n2?n1:n2);
  m = (int**)calloc(n1,sizeof(int*)); 
//			mexPrintf("C[2] = %f \n",C[2]);
  for (i=0;i<n1;i++)
        {
        	m[i] = (int*)calloc(n2,sizeof(int));  
        	for (j=0;j<n2;j++)
            		m[i][j] = (int) (C[i+n1*j] - C_min);
//			mexPrintf("m[%d][%d] = %f  %f\n",i,j,m[i][j],C[i+n1*j] - C_min);
		if (gm_C_denied!=NULL)
		for (j=0;j<n2;j++){
			if (j==30)
				int dbg=1;
			bool bden=(gm_C_denied->data[n2*i+j]<1e-10);
            		if (bden) m[i][j] =C_max;
			else 
				int dbg=1;
			};
 	};
    //normalization: rows and columns
//			mexPrintf("C[2] = %f \n",C[2]);
    double dmin;
    for (i=0;i<n1;i++)
        {
        	dmin=m[i][0];
        	for (j=1;j<n2;j++)
            		dmin= (m[i][j]<dmin)? m[i][j]:dmin;
        	for (j=0;j<n2;j++)
            		m[i][j]-=dmin;
 	};
    for (j=0;j<n2;j++)
        {
        	dmin=m[0][j];
        	for (i=1;i<n1;i++)
            		dmin= (m[i][j]<dmin)? m[i][j]:dmin;
        	for (i=0;i<n1;i++)
            		m[i][j]-=dmin;
 	};
   if ((_bprev_init) &&(gv_col_inc !=NULL))
	{
	//dual solution v substraction
		for (j=0;j<n2;j++)
        		for (i=0;i<n1;i++)
				m[i][j]-=gv_col_inc->data[j];
	//permutation of m columns
		int *mt = new int[n2];
		for (i=0;i<n1;i++)
		{
			for (j=0;j<n2;j++) mt[j]=m[i][j];
			for (j=0;j<n2;j++) m[i][j]=mt[gsl_permutation_get(gp_sol,j)];
		};
		delete[] mt;
		
	};

   
  /* initialize the hungarian_problem using the cost matrix*/
   matrix_size = hungarian_init(&p, m , n1,n2, HUNGARIAN_MODE_MINIMIZE_COST) ;
  /* solve the assignement problem */
  hungarian_solve(&p);
  q = &p;
  //gsl_matrix* gm_P=gsl_matrix_alloc(n1,n2);
  gsl_permutation* gp_sol_inv=gsl_permutation_alloc(n2);
  if (gp_sol!=NULL)
  	gsl_permutation_inverse(gp_sol_inv,gp_sol);
  else
	gsl_permutation_init(gp_sol_inv);
  for (i=0;i<n1;i++)
         for (j=0;j<n2;j++)
              gsl_matrix_set(gm_P,i,j,q->assignment[i][gp_sol_inv->data[j]]);
  //initialization by the previous solution
  if ((_bprev_init) &&(gv_col_inc !=NULL))
        for (j=0;j<n2;j++)
		gv_col_inc->data[j]=q->col_inc[gp_sol_inv->data[j]];
  if ((_bprev_init) && (gp_sol!=NULL))
  {
  for (i=0;i<n1;i++)
         for (j=0;j<n2;j++)
  		if (gsl_matrix_get(gm_P,i,j)==HUNGARIAN_ASSIGNED)
			gp_sol->data[i]=j;
  };
  /* free used memory */
  gsl_permutation_free(gp_sol_inv);
  hungarian_free(&p);
  for (i=0;i<n1;i++)
        free(m[i]);
  free(m);

/*  for (int i=0;i<gm_C->size1;i++)
        {
        	for (int j=0;j<gm_C->size1;j++)
		{
			mexPrintf("G[%d][%d] = %f  %f \n",i,j,gsl_matrix_get(gm_P,i,j),gsl_matrix_get(gm_C,i,j));
		}
	}*/



//  mexPrintf("AAA");
  //return gm_P;
}