help-gsl
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-gsl] adding gsl_combination elements to gprtarray in glib, but can


From: Analabha Roy
Subject: [Help-gsl] adding gsl_combination elements to gprtarray in glib, but cannot retrieve
Date: Mon, 26 Aug 2013 22:47:10 +0530

Hi all,

I'm trying to generate combinations of M integers from a set of M^2
consecutive integers, then exclude some combinations based on some
(currently irrelevant) criteria, and trying to store them dynamically in
memory so that I may be able to access them later.

The combinations are being generated using the 'gsl_combination' structure
in the GNU Scientific Library. As you can see from the code snippet below,
the combinations are being generated okay (and exclusions are as desired).

Then I need to append each combination structure object to memory for later
retrieval. I'm trying to use glib pointer arrays to do this.



#include <glib.h>
#include <gsl/gsl_combination.h>

//Hardcoded the size of the rigol lattice
//MUST BE ODD !!!
#define M 3

//Returns GSL_SUCCESS if the combination contains a forbidden site,
otherwise returns GSL_FAILURE
int
is_forbidden (const gsl_combination * test)
{
  int i, forbidden = GSL_FAILURE;   //Not forbidden by default
  size_t k = test->k;
  size_t *data = test->data;
  int halfway = ((M * M - 1) / 2);  //The halfway point in the 2D grid

      ...
      ... //Some code for updating 'forbidden'
      ...
  return forbidden;
}


#undef __FUNCT__
#define __FUNCT__ "main"
int
main (int argc, char **argv)
{
  long count, istride;
  //Generate the basis states
  gsl_combination *c = gsl_combination_calloc (M * M, M);
  //An array that will contain selected basis vectors.
  GPtrArray *basis = g_ptr_array_new ();
  count = 0;
  do
    {
      //c is the ith combination of M integers from M^2 integers
      //Get all the non forbidden elements
      if (is_forbidden (c) == GSL_FAILURE)
    {           //If the site is NOT forbidden, append c to basis
      g_ptr_array_add (basis, c);   //Appends c to basis
      {
        printf ("count %ld {", count);
        gsl_combination_fprintf (stdout,
                    g_ptr_array_index (basis, count), " %u");
        printf (" }\n");
      }
      count++;
    }
    }
  while (gsl_combination_next (c) == GSL_SUCCESS);
  printf("\n\n");
  //Now, access each basis element from GArray
  for (istride = 0; istride < basis->len; istride++)
    {
      printf ("istride %ld {", istride);
      gsl_combination_fprintf (stdout, g_ptr_array_index (basis, istride),
                  " %u");
      printf (" }\n");

    }

  gsl_combination_free (c);
  g_ptr_array_free (basis, TRUE);
  return 0;
}



After compiling and running it, the output is very strange ( here at
pastebin <http://pastebin.ca/2437949>)

As you can see, it prints the combinations correctly in the do...while loop
where the combinations are iterated, but only prints the lexicographically
final combination when the gptrarray elements are iterated in the
subsequent loop.

What am I doing wrong?
Thanks in advance,
AR



-- 
---
Analabha Roy
C.S.I.R  Senior Research Associate
Saha Institute of Nuclear Physics
Section 1, Block AF
Bidhannagar, Calcutta 700064
India
Emails: address@hidden, address@hidden
Webpage: http://www.ph.utexas.edu/~daneel/


reply via email to

[Prev in Thread] Current Thread [Next in Thread]