Bill Allombert on Sun, 08 Oct 2006 16:33:42 +0200


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

Defining variable priority [alternative solution]


On Wed, Sep 20, 2006 at 12:23:14AM +0200, Bill Allombert wrote:
> Hello PARI-dev,
> 
> This is an experimental patch that allow to define new variables with
> specified priority under GP.

This is an alternative patch toward the same goal.
The interface is the same but this is quite different internally:
Instead of supplementing variable numbers by priority, we simply allow
the user to create new variable with prescribed variable number.

The result is a patch which is much less intrusive (in particular it
does not break binary storage) but less powerful:
Variable numbers are quite constrained and cannot be changed.
For example, if you have a variable x of priority 0 and another of
priority 1, you cannot create a new variable between x and y because
there is no available integer and you cannot renumber y to 2 either.

Also since variable numbers are non-negative and x priority is 0 you
cannot create variable with higher priority than x. That could be fixed
by changing variable numbers to be between [-MAXVARN/2,MAXVARN/2].

I am posting this patch in the hope we find an intermediate solution
between both approach.

An example of usage :
parisize = 4000000, primelimit = 500000
? newvar(y,2);
? newvar(z,1);
? y+z
%1 = z + y

Cheers,
Bill.
Index: src/headers/paridecl.h
===================================================================
RCS file: /home/cvs/pari/src/headers/paridecl.h,v
retrieving revision 1.602
diff -u -r1.602 paridecl.h
--- src/headers/paridecl.h	8 Oct 2006 13:11:33 -0000	1.602
+++ src/headers/paridecl.h	8 Oct 2006 13:11:59 -0000
@@ -263,6 +263,7 @@
 void    kill0(entree *ep);
 long    manage_var(long n, entree *ep);
 void    name_var(long n, char *s);
+void    newvar(char *s, long p);
 GEN     readseq(char *t);
 GEN     strtoGENstr(const char *s);
 GEN     strtoi(char *s);
Index: src/language/anal.c
===================================================================
RCS file: /home/cvs/pari/src/language/anal.c,v
retrieving revision 1.259
diff -u -r1.259 anal.c
--- src/language/anal.c	2 Oct 2006 09:45:37 -0000	1.259
+++ src/language/anal.c	8 Oct 2006 13:14:37 -0000
@@ -2601,11 +2601,24 @@
   return *table = ep;
 }
 
+static long nvar; /* first GP free variable */
+
+void create_var(entree *ep, long var)
+{
+  GEN p = (GEN)ep->value;
+  /* create pol_x[var] */
+  p[0] = evaltyp(t_POL) | evallg(4);
+  p[1] = evalsigne(1) | evalvarn(var);
+  gel(p,2) = gen_0;
+  gel(p,3) = gen_1;
+  varentries[var] = ep;
+}
+
+
 long
 manage_var(long n, entree *ep)
 {
   static long max_avail = MAXVARN; /* max variable not yet used */
-  static long nvar; /* first GP free variable */
   long var;
 
   switch(n) {
@@ -2625,24 +2638,35 @@
       default: pari_err(talker, "panic");
   }
 
-  if (nvar == max_avail) pari_err(talker2,"no more variables available",
-                             mark.identifier, mark.start);
   if (ep)
   {
-    GEN p = (GEN)ep->value;
+    while (nvar<max_avail && varentries[nvar]) nvar++;
+    if (nvar == max_avail) pari_err(talker2,"no more variables available",
+                             mark.identifier, mark.start);
     var = nvar++;
-    /* create pol_x[var] */
-    p[0] = evaltyp(t_POL) | evallg(4);
-    p[1] = evalsigne(1) | evalvarn(var);
-    gel(p,2) = gen_0;
-    gel(p,3) = gen_1;
-    varentries[var] = ep;
+    create_var(ep,var);
   }
   else
     var = max_avail--;
   return var;
 }
 
+void
+newvar(char *s, long p)
+{
+  char *t = s;
+  entree **funhash = functions_hash + hashvalue(&t);
+  entree *ep = findentry(s, t - s, *funhash);
+  if (ep)
+    pari_err(talker, "%s already exists", s);
+  if (p<0 || p>MAXVARN)
+    pari_err(talker, "invalid priority %ld", p);
+  if (varentries[p])
+    pari_err(talker, "priority %ld already in use", p);
+  ep = installep(NULL,s,strlen(s),EpVAR, SIZEOF_VAR_POLS, funhash);
+  create_var(ep,p);
+}
+
 long
 fetch_var(void)
 {
--- /dev/null	2006-05-11 11:25:12.000000000 +0200
+++ src/functions/programming/newvar	2006-09-19 23:51:01.000000000 +0200
@@ -0,0 +1,6 @@
+Function: newvar
+Section: programming
+C-Name: newvar
+Prototype: vrL
+Help: newvar(var,p): create a new variable named var of priority p. 
+