Thomas D. Dean on Mon, 17 Sep 2007 19:45:57 +0200


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

Libpari Function Somme


I have a problem using the libpari function, somme.

Help on sum shows:

The library syntax is somme(entree *ep, GEN a, GEN b, char *expr, GEN
x).  This is to be used as follows: ep represents the dummy variable
used in the expression expr

   /* compute a^2 + ... + b^2 */
   {
     /* define the dummy variable "i" */
     entree *ep = is_entry("i");
     /* sum for a <= i <= b */
     return somme(ep, a, b, "i^2", gen_0);
   }
   
However, my code

entree *ep = is_entry("i");
s = somme(ep, gen_1, n, "i^2", gen_0);

produces the fault:

  ***   segmentation fault: bug in PARI or calling program.
  ***   Error in the PARI system. End of program.

>From gdb, I see

18        s = somme(ep, gen_1, n, "i^2", gen_0);
(gdb) n

Program received signal SIGSEGV, Segmentation fault.
0x28217194 in new_val_cell () from /usr/X11R6/lib/libpari.so.2
(gdb) bt
#0  0x28217194 in new_val_cell () from /usr/X11R6/lib/libpari.so.2
#1  0x00002028 in ?? ()
#2  0x08151fdc in ?? ()
#3  0x00000000 in ?? ()
#4  0x282cd644 in __JCR_LIST__ () from /usr/X11R6/lib/libpari.so.2
#5  0x08151fdc in ?? ()
#6  0x08152000 in ?? ()
#7  0x08151fe8 in ?? ()
#8  0x2823b50d in somme () from /usr/X11R6/lib/libpari.so.2
#9  0x2804eb64 in elf_hash () from /libexec/ld-elf.so.1
Previous frame inner to this frame (corrupt stack?)

What am I doing wrong?

The code is below.

tomdean

#include <stdio.h>
#include <pari/pari.h>

#define MK_GEN(n) utoi((ulong)(n))

void calc_sum(int i) {
  GEN d,s,two,five,z,nr,q;
  GEN n;
  n = MK_GEN(i);
  entree *ep = is_entry("i");
  pariprintf("n=%Z\n",n);
  s = somme(ep, gen_1, n, "i^2", gen_0);
  d = denom(s);
  two= gen_0;
  while (ggcd(MK_GEN(2),d) != gen_1) {
	d = gdiv(d,MK_GEN(2));
	two = gadd(two,gen_1);
  }
  five = gen_0;
  while (ggcd(MK_GEN(5),d) != gen_1) {
	d = gdiv(d,MK_GEN(5));
	five = gadd(five,gen_1);
  }
  z=znorder(gmodulo(MK_GEN(10),d),gen_0);
  nr = gmax(two, five);
  q = gfloor(s);
  pariprintf("%Z %Z %Z %Z %Z\n",n,q,nr,z);
}

int main(int argc, char **argv) {
  int i;
  pari_sp av;

  pari_init(1*1024*1024,2);
  for (i=10; i<20; i++) {
	av = avma;  // initial pari stack pointer
	calc_sum(i);
	avma = av;  // clear the pari stack
  }
  return 0;
}