source: git/Singular/misc_ip.cc @ a3c063

spielwiese
Last change on this file since a3c063 was a3c063, checked in by Hans Schoenemann <hannes@…>, 13 years ago
mem. leak git-svn-id: file:///usr/local/Singular/svn/trunk@12942 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 31.1 KB
Line 
1/*****************************************************************************\
2 * Computer Algebra System SINGULAR
3\*****************************************************************************/
4/** @file misc_ip.cc
5 *
6 * This file provides miscellaneous functionality.
7 *
8 * For more general information, see the documentation in
9 * misc_ip.h.
10 *
11 * @author Frank Seelisch
12 *
13 * @internal @version \$Id$
14 *
15 **/
16/*****************************************************************************/
17
18// include header files
19#include "mod2.h"
20#include "lists.h"
21#include "longrat.h" /* We only need bigints. */
22#include "misc_ip.h"
23
24/* This works by Newton iteration, i.e.,
25      a(1)   = n;
26      a(i+1) = a(i)/2 + n/2/a(i), i > 0.
27   This sequence is guaranteed to decrease monotonously and
28   it is known to converge fast.
29   All used numbers are bigints. */
30number approximateSqrt(const number n)
31{
32  if (nlIsZero(n)) { number zero = nlInit(0, NULL); return zero; }
33  number temp1; number temp2;
34  number one = nlInit(1, NULL);
35  number two = nlInit(2, NULL);
36  number m = nlCopy(n);
37  number mOld = nlSub(m, one); /* initially required to be different from m */
38  number nHalf = nlIntDiv(n, two);
39  bool check = true;
40  while (!nlEqual(m, mOld) && check)
41  {
42    temp1 = nlIntDiv(m, two);
43    temp2 = nlIntDiv(nHalf, m);
44    mOld = m;
45    m = nlAdd(temp1, temp2);
46    nlDelete(&temp1, NULL); nlDelete(&temp2, NULL);
47    temp1 = nlMult(m, m);
48    check = nlGreater(temp1, n);
49    nlDelete(&temp1, NULL);
50  }
51  nlDelete(&mOld, NULL); nlDelete(&two, NULL); nlDelete(&nHalf, NULL);
52  while (!check)
53  {
54    temp1 = nlAdd(m, one);
55    nlDelete(&m, NULL);
56    m = temp1;
57    temp1 = nlMult(m, m);
58    check = nlGreater(temp1, n);
59    nlDelete(&temp1, NULL);
60  }
61  temp1 = nlSub(m, one);
62  nlDelete(&m, NULL);
63  nlDelete(&one, NULL);
64  m = temp1;
65  return m;
66}
67
68/* returns the quotient resulting from division of n by the prime as many
69   times as possible without remainder; afterwards, the parameter times
70   will contain the highest exponent e of p such that p^e divides n
71   e.g., divTimes(48, 4, t) = 3 with t = 2, since 48 = 4*4*3;
72   n is expected to be a bigint; returned type is also bigint */
73number divTimes(const number n, const int p, int* times)
74{
75  number nn = nlCopy(n);
76  number dd = nlInit(p, NULL);
77  number rr = nlIntMod(nn, dd);
78  *times = 0;
79  while (nlIsZero(rr))
80  {
81    (*times)++;
82    number temp = nlIntDiv(nn, dd);
83    nlDelete(&nn, NULL);
84    nn = temp;
85    nlDelete(&rr, NULL);
86    rr = nlIntMod(nn, dd);
87  }
88  nlDelete(&rr, NULL); nlDelete(&dd, NULL);
89  return nn;
90}
91
92/* returns an object of type lists which contains the entries
93   theInts[0..(length-1)] as INT_CMDs*/
94lists makeListsObject(const int* theInts, int length)
95{
96  lists L=(lists)omAllocBin(slists_bin);
97  L->Init(length);
98  for (int i = 0; i < length; i++)
99    { L->m[i].rtyp = INT_CMD; L->m[i].data = (void*)theInts[i]; }
100  return L;
101}
102
103/* returns the i-th bit of the binary number which arises by
104   concatenating array[length-1], ..., array[1], array[0],
105   where array[0] contains the 32 lowest bits etc.;
106   i is assumed to be small enough to address a valid index
107   in the given array */
108bool getValue(const unsigned i, const unsigned int* ii)
109{
110  if (i==2) return true;
111  if ((i & 1)==0) return false;
112  unsigned I= i/2;
113  unsigned index = I / 32;
114  unsigned offset = I % 32;
115  unsigned int v = 1 << offset;
116  return ((ii[index] & v) != 0);
117}
118
119/* sets the i-th bit of the binary number which arises by
120   concatenating array[length-1], ..., array[1], array[0],
121   where array[0] contains the 32 lowest bits etc.;
122   i is assumed to be small enough to address a valid index
123   in the given array */
124void setValue(const unsigned i, bool value, unsigned int* ii)
125{
126  if ((i&1)==0) return; // ignore odd numbers
127  unsigned I=i/2;
128  unsigned index = I / 32;
129  unsigned offset = I % 32;
130  unsigned int v = 1 << offset;
131  if (value) { ii[index] |= v;  }
132  else       { ii[index] &= ~v; }
133}
134
135/* returns whether i is less than or equal to the bigint number n */
136bool isLeq(const int i, const number n)
137{
138  number iN = nlInit(i - 1, NULL);
139  bool result = nlGreater(n, iN);
140  nlDelete(&iN, NULL);
141  return result;
142}
143
144#if 0
145lists primeFactorisation(const number n, const int pBound)
146{
147  number nn = nlCopy(n); int i;
148  int pCounter = 0; /* for counting the number of mutually distinct
149                       prime factors in n */
150  /* we assume that there are at most 1000 mutually distinct prime
151     factors in n */
152  int* primes = new int[1000]; int* multiplicities = new int[1000];
153
154  /* extra treatment for the primes 2 and 3;
155     all other primes are equal to +1/-1 mod 6 */
156  int e; number temp;
157  temp = divTimes(nn, 2, &e); nlDelete(&nn, NULL); nn = temp;
158  if (e > 0) { primes[pCounter] = 2; multiplicities[pCounter++] = e; }
159  temp = divTimes(nn, 3, &e); nlDelete(&nn, NULL); nn = temp;
160  if (e > 0) { primes[pCounter] = 3; multiplicities[pCounter++] = e; }
161
162  /* now we simultaneously:
163     - build the sieve of Erathostenes up to s,
164     - divide out each prime factor of nn that we find along the way
165       (This may result in an earlier termination.) */
166
167  int s = 1<<25;       /* = 2^25 */
168  int maxP = 2147483647; /* = 2^31 - 1, by the way a Mersenne prime */
169  if ((pBound != 0) && (pBound < maxP))
170  {
171    maxP = pBound;
172  }
173  if (maxP< (2147483647-63) ) s=(maxP+63)/64;
174  else                        s=2147483647/64+1;
175  unsigned int* isPrime = new unsigned int[s];
176  /* the lowest bit of isPrime[0] stores whether 1 is a prime,
177     next bit is for 3, next for 5, etc. i.e.
178     intended usage is: isPrime[0] = ...
179     We shall make use only of bits which correspond to numbers =
180     -1 or +1 mod 6. */
181  //for (i = 0; i < s; i++) isPrime[i] = ~0;/*4294967295*/; /* all 32 bits set */
182  memset(isPrime,0xff,s*sizeof(unsigned int));
183  int p=9; while((p<maxP) && (p>0)) { setValue(p,false,isPrime); p+=6; }
184  p = 5; bool add2 = true;
185  /* due to possible overflows, we need to check whether p > 0, and
186     likewise i > 0 below */
187  while ((0 < p) && (p <= maxP) && (isLeq(p, nn)))
188  {
189    /* at this point, p is guaranteed to be a prime;
190       we divide nn by the highest power of p and store p
191       if nn is at all divisible by p */
192    temp = divTimes(nn, p, &e);
193    nlDelete(&nn, NULL); nn = temp;
194    if (e > 0)
195    { primes[pCounter] = p; multiplicities[pCounter++] = e; }
196    /* invalidate all multiples of p, starting with 2*p */
197    i = 2 * p;
198    while ((0 < i) && (i < maxP)) { setValue(i, false, isPrime); i += p; }
199    /* move on to the next prime in the sieve; we either add 2 or 4
200       in order to visit just the numbers equal to -1/+1 mod 6 */
201    if (add2) { p += 2; add2 = false; }
202    else      { p += 4; add2 = true;  }
203    while ((0 < p) && (p <= maxP) && (isLeq(p, nn)) && (!getValue(p, isPrime)))
204    {
205      if (add2) { p += 2; add2 = false; }
206      else      { p += 4; add2 = true;  }
207    }
208  }
209
210  /* build return structure and clean up */
211  delete [] isPrime;
212  lists primesL = makeListsObject(primes, pCounter);
213  lists multiplicitiesL = makeListsObject(multiplicities, pCounter);
214  delete [] primes; delete [] multiplicities;
215  lists L=(lists)omAllocBin(slists_bin);
216  L->Init(3);
217  L->m[0].rtyp = BIGINT_CMD; L->m[0].data = (void *)nn;
218  /* try to fit nn into an int: */
219  int nnAsInt = nlInt(nn, NULL);
220  if (nlIsZero(nn) || (nnAsInt != 0))
221  {
222    nlDelete(&nn,NULL):
223    L->m[0].rtyp = INT_CMD;
224    L->m[0].data = (void *)nnAsInt;
225  }
226  L->m[1].rtyp = LIST_CMD; L->m[1].data = (void *)primesL;
227  L->m[2].rtyp = LIST_CMD; L->m[2].data = (void *)multiplicitiesL;
228  return L;
229}
230#else
231/* Factoring , from gmp-demos
232
233Copyright 1995, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2005 Free Software
234Foundation, Inc.
235
236This program is free software; you can redistribute it and/or modify it under
237the terms of the GNU General Public License as published by the Free Software
238Foundation; either version 3 of the License, or (at your option) any later
239version.
240
241This program is distributed in the hope that it will be useful, but WITHOUT ANY
242WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
243PARTICULAR PURPOSE.  See the GNU General Public License for more details.
244
245You should have received a copy of the GNU General Public License along with
246this program.  If not, see http://www.gnu.org/licenses/.  */
247
248
249#include <stdlib.h>
250#include <stdio.h>
251#include <string.h>
252
253#include <si_gmp.h>
254
255static unsigned add[] = {4, 2, 4, 2, 4, 6, 2, 6};
256
257void factor_using_division (mpz_t t, unsigned int limit, int *L, int &L_ind, int *ex)
258{
259  mpz_t q, r;
260  unsigned long int f;
261  int ai;
262  unsigned *addv = add;
263  unsigned int failures;
264
265  mpz_init (q);
266  mpz_init (r);
267
268  f = mpz_scan1 (t, 0);
269  mpz_div_2exp (t, t, f);
270  while (f)
271  {
272    if ((L_ind>0) && (L[L_ind-1]==2)) ex[L_ind-1]++;
273    else
274    {
275      L[L_ind]=2;
276      L_ind++;
277    }
278    --f;
279  }
280
281  for (;;)
282  {
283    mpz_tdiv_qr_ui (q, r, t, 3);
284    if (mpz_cmp_ui (r, 0) != 0) break;
285    mpz_set (t, q);
286    if ((L_ind>0) && (L[L_ind-1]==3)) ex[L_ind-1]++;
287    else
288    {
289      L[L_ind]=3;
290      L_ind++;
291    }
292  }
293
294  for (;;)
295  {
296    mpz_tdiv_qr_ui (q, r, t, 5);
297    if (mpz_cmp_ui (r, 0) != 0) break;
298    mpz_set (t, q);
299    if ((L_ind>0) && (L[L_ind-1]==5)) ex[L_ind-1]++;
300    else
301    {
302      L[L_ind]=5;
303      L_ind++;
304    }
305  }
306
307  failures = 0;
308  f = 7;
309  ai = 0;
310  while (mpz_cmp_ui (t, 1) != 0)
311  {
312    mpz_tdiv_qr_ui (q, r, t, f);
313    if (mpz_cmp_ui (r, 0) != 0)
314    {
315        f += addv[ai];
316        if (mpz_cmp_ui (q, f) < 0) break;
317        ai = (ai + 1) & 7;
318        failures++;
319        if (failures > limit) break;
320    }
321    else
322    {
323      mpz_swap (t, q);
324      if ((L_ind>0) && (L[L_ind-1]==f)) ex[L_ind-1]++;
325      else
326      {
327        L[L_ind]=f;
328        L_ind++;
329      }
330      failures = 0;
331    }
332  }
333
334  mpz_clear (q);
335  mpz_clear (r);
336}
337
338void factor_using_pollard_rho (mpz_t n, int a_int, int *L, int &L_ind, int *ex)
339{
340  mpz_t x, x1, y, P;
341  mpz_t a;
342  mpz_t g;
343  mpz_t t1, t2;
344  int k, l, c, i;
345
346  mpz_init (g);
347  mpz_init (t1);
348  mpz_init (t2);
349
350  mpz_init_set_si (a, a_int);
351  mpz_init_set_si (y, 2);
352  mpz_init_set_si (x, 2);
353  mpz_init_set_si (x1, 2);
354  k = 1;
355  l = 1;
356  mpz_init_set_ui (P, 1);
357  c = 0;
358
359  while (mpz_cmp_ui (n, 1) != 0)
360  {
361S2:
362    mpz_mul (x, x, x); mpz_add (x, x, a); mpz_mod (x, x, n);
363    mpz_sub (t1, x1, x); mpz_mul (t2, P, t1); mpz_mod (P, t2, n);
364    c++;
365    if (c == 20)
366    {
367      c = 0;
368      mpz_gcd (g, P, n);
369      if (mpz_cmp_ui (g, 1) != 0) goto S4;
370      mpz_set (y, x);
371    }
372S3:
373    k--;
374    if (k > 0) goto S2;
375
376    mpz_gcd (g, P, n);
377    if (mpz_cmp_ui (g, 1) != 0) goto S4;
378
379    mpz_set (x1, x);
380    k = l;
381    l = 2 * l;
382    for (i = 0; i < k; i++)
383    {
384      mpz_mul (x, x, x); mpz_add (x, x, a); mpz_mod (x, x, n);
385    }
386    mpz_set (y, x);
387    c = 0;
388    goto S2;
389S4:
390    do
391    {
392      mpz_mul (y, y, y); mpz_add (y, y, a); mpz_mod (y, y, n);
393      mpz_sub (t1, x1, y); mpz_gcd (g, t1, n);
394    }
395    while (mpz_cmp_ui (g, 1) == 0);
396
397    mpz_div (n, n, g);        /* divide by g, before g is overwritten */
398
399    if (!mpz_probab_prime_p (g, 3))
400    {
401      do
402      {
403        mp_limb_t a_limb;
404        mpn_random (&a_limb, (mp_size_t) 1);
405        a_int = (int) a_limb;
406      }
407      while (a_int == -2 || a_int == 0);
408
409      factor_using_pollard_rho (g, a_int,L,L_ind,ex);
410    }
411    else
412    {
413      if ((L_ind>0) && (mpz_cmp_si(g,L[L_ind-1])==0)) ex[L_ind-1]++;
414      else
415      {
416        L[L_ind]=mpz_get_si(g);
417        L_ind++;
418      }
419    }
420    mpz_mod (x, x, n);
421    mpz_mod (x1, x1, n);
422    mpz_mod (y, y, n);
423    if (mpz_probab_prime_p (n, 3))
424    {
425      if ((L_ind>0) && (mpz_cmp_si(n,L[L_ind-1])==0)) ex[L_ind-1]++;
426      else
427      {
428        L[L_ind]=mpz_get_si(n);
429        L_ind++;
430      }
431      break;
432    }
433  }
434
435  mpz_clear (g);
436  mpz_clear (P);
437  mpz_clear (t2);
438  mpz_clear (t1);
439  mpz_clear (a);
440  mpz_clear (x1);
441  mpz_clear (x);
442  mpz_clear (y);
443}
444
445void mpz_factor (mpz_t t, int *L, int & L_ind, int *ex)
446{
447  unsigned int division_limit;
448
449  if (mpz_sgn (t) == 0)
450    return;
451
452  /* Set the trial division limit according the size of t.  */
453  division_limit = mpz_sizeinbase (t, 2);
454  if (division_limit > 1000)
455    division_limit = 1000 * 1000;
456  else
457    division_limit = division_limit * division_limit;
458
459  factor_using_division (t, division_limit, L, L_ind, ex);
460
461  if (mpz_cmp_ui (t, 1) != 0)
462  {
463    if (mpz_probab_prime_p (t, 3))
464    {
465      if ((L_ind>0) && (mpz_cmp_si(t,L[L_ind-1])==0)) ex[L_ind-1]++;
466      else
467      {
468        L[L_ind]=mpz_get_si(t);
469        L_ind++;
470      }
471    }
472    else
473      factor_using_pollard_rho (t, 1, L,L_ind,ex);
474  }
475}
476
477lists primeFactorisation(const number n, const int pBound)
478{
479  mpz_t t;
480  number nn = nlCopy(n);
481  lists L=(lists)omAllocBin(slists_bin);
482  L->Init(3);
483  L->m[0].rtyp = BIGINT_CMD; L->m[0].data = (void *)nn;
484  /* try to fit nn into an int: */
485  int nnAsInt = nlInt(nn, NULL);
486  if (nlIsZero(nn) || (nnAsInt != 0))
487  {
488    nlDelete(&nn,NULL);
489    L->m[0].rtyp = INT_CMD;
490    L->m[0].data = (void *)nnAsInt;
491    mpz_init_set_si(t,nnAsInt);
492  }
493  else
494  {
495    mpz_init_set(t,(mpz_ptr)nn);
496  }
497  int *LL=(int*)omAlloc0(1000*sizeof(int));
498  int *ex=(int*)omAlloc0(1000*sizeof(int));
499  int L_ind=0;
500  mpz_factor (t,LL,L_ind,ex);
501
502  int i;
503  for(i=0;i<L_ind;i++) ex[i]++;
504  L->m[1].rtyp = LIST_CMD; L->m[1].data = (void *)makeListsObject(LL,L_ind);
505  L->m[2].rtyp = LIST_CMD; L->m[2].data = (void *)makeListsObject(ex,L_ind);
506  return L;
507}
508#endif
509
510#include <string.h>
511#include <unistd.h>
512#include <stdio.h>
513#include <stddef.h>
514#include <stdlib.h>
515#include <time.h>
516
517#include <mylimits.h>
518#include "omalloc.h"
519#include "options.h"
520#include "febase.h"
521#include "cntrlc.h"
522#include "page.h"
523#include "ipid.h"
524#include "ipshell.h"
525#include "kstd1.h"
526#include "subexpr.h"
527#include "timer.h"
528#include "intvec.h"
529#include "ring.h"
530#include "omSingularConfig.h"
531#include "p_Procs.h"
532/* Needed for debug Version of p_SetRingOfLeftv, Oliver */
533#ifdef PDEBUG
534#include "p_polys.h"
535#endif
536#include "version.h"
537
538#include "static.h"
539#ifdef HAVE_STATIC
540#undef HAVE_DYN_RL
541#endif
542
543#define SI_DONT_HAVE_GLOBAL_VARS
544
545//#ifdef HAVE_LIBPARSER
546//#  include "libparse.h"
547//#endif /* HAVE_LIBPARSER */
548
549#ifdef HAVE_FACTORY
550#include <factory.h>
551// libfac:
552  extern const char * libfac_version;
553  extern const char * libfac_date;
554#endif
555
556/* version strings */
557#include <si_gmp.h>
558#ifdef HAVE_MPSR
559#include <MP_Config.h>
560#endif
561
562/*2
563* initialize components of Singular
564*/
565int inits(void)
566{
567  int t;
568/*4 signal handler:*/
569  init_signals();
570/*4 randomize: */
571  t=initTimer();
572  /*t=(int)time(NULL);*/
573  if (t==0) t=1;
574#ifdef HAVE_RTIMER
575  initRTimer();
576#endif
577#ifdef buildin_rand
578  siSeed=t;
579#else
580  srand((unsigned int)t);
581#endif
582#ifdef HAVE_FACTORY
583  factoryseed(t);
584#endif
585/*4 private data of other modules*/
586  memset(&sLastPrinted,0,sizeof(sleftv));
587  sLastPrinted.rtyp=NONE;
588  return t;
589}
590
591/*2
592* the renice routine for very large jobs
593* works only on unix machines,
594* testet on : linux, HP 9.0
595*
596*#include <sys/times.h>
597*#include <sys/resource.h>
598*extern "C" int setpriority(int,int,int);
599*void very_nice()
600*{
601*#ifndef NO_SETPRIORITY
602*  setpriority(PRIO_PROCESS,0,19);
603*#endif
604*  sleep(10);
605*}
606*/
607
608void singular_example(char *str)
609{
610  assume(str!=NULL);
611  char *s=str;
612  while (*s==' ') s++;
613  char *ss=s;
614  while (*ss!='\0') ss++;
615  while (*ss<=' ')
616  {
617    *ss='\0';
618    ss--;
619  }
620  idhdl h=IDROOT->get(s,myynest);
621  if ((h!=NULL) && (IDTYP(h)==PROC_CMD))
622  {
623    char *lib=iiGetLibName(IDPROC(h));
624    if((lib!=NULL)&&(*lib!='\0'))
625    {
626      Print("// proc %s from lib %s\n",s,lib);
627      s=iiGetLibProcBuffer(IDPROC(h), 2);
628      if (s!=NULL)
629      {
630        if (strlen(s)>5)
631        {
632          iiEStart(s,IDPROC(h));
633          return;
634        }
635        else omFree((ADDRESS)s);
636      }
637    }
638  }
639  else
640  {
641    char sing_file[MAXPATHLEN];
642    FILE *fd=NULL;
643    char *res_m=feResource('m', 0);
644    if (res_m!=NULL)
645    {
646      sprintf(sing_file, "%s/%s.sing", res_m, s);
647      fd = feFopen(sing_file, "r");
648    }
649    if (fd != NULL)
650    {
651
652      int old_echo = si_echo;
653      int length, got;
654      char* s;
655
656      fseek(fd, 0, SEEK_END);
657      length = ftell(fd);
658      fseek(fd, 0, SEEK_SET);
659      s = (char*) omAlloc((length+20)*sizeof(char));
660      got = fread(s, sizeof(char), length, fd);
661      fclose(fd);
662      if (got != length)
663      {
664        Werror("Error while reading file %s", sing_file);
665        omFree(s);
666      }
667      else
668      {
669        s[length] = '\0';
670        strcat(s, "\n;return();\n\n");
671        si_echo = 2;
672        iiEStart(s, NULL);
673        si_echo = old_echo;
674      }
675    }
676    else
677    {
678      Werror("no example for %s", str);
679    }
680  }
681}
682
683
684struct soptionStruct
685{
686  const char * name;
687  unsigned   setval;
688  unsigned   resetval;
689};
690
691struct soptionStruct optionStruct[]=
692{
693  {"prot",         Sy_bit(OPT_PROT),           ~Sy_bit(OPT_PROT)   },
694  {"redSB",        Sy_bit(OPT_REDSB),          ~Sy_bit(OPT_REDSB)   },
695  {"notBuckets",   Sy_bit(OPT_NOT_BUCKETS),    ~Sy_bit(OPT_NOT_BUCKETS)   },
696  {"notSugar",     Sy_bit(OPT_NOT_SUGAR),      ~Sy_bit(OPT_NOT_SUGAR)   },
697  {"interrupt",    Sy_bit(OPT_INTERRUPT),      ~Sy_bit(OPT_INTERRUPT)   },
698  {"sugarCrit",    Sy_bit(OPT_SUGARCRIT),      ~Sy_bit(OPT_SUGARCRIT)   },
699  {"teach",     Sy_bit(OPT_DEBUG),          ~Sy_bit(OPT_DEBUG)  },
700  /* 9 return SB in syz, quotient, intersect */
701  {"returnSB",     Sy_bit(OPT_RETURN_SB),      ~Sy_bit(OPT_RETURN_SB)  },
702  {"fastHC",       Sy_bit(OPT_FASTHC),         ~Sy_bit(OPT_FASTHC)  },
703  /* 11-19 sort in L/T */
704  {"staircaseBound",Sy_bit(OPT_STAIRCASEBOUND),~Sy_bit(OPT_STAIRCASEBOUND)  },
705  {"multBound",    Sy_bit(OPT_MULTBOUND),      ~Sy_bit(OPT_MULTBOUND)  },
706  {"degBound",     Sy_bit(OPT_DEGBOUND),       ~Sy_bit(OPT_DEGBOUND)  },
707  /* 25 no redTail(p)/redTail(s) */
708  {"redTail",      Sy_bit(OPT_REDTAIL),        ~Sy_bit(OPT_REDTAIL)  },
709  {"redThrough",   Sy_bit(OPT_REDTHROUGH),     ~Sy_bit(OPT_REDTHROUGH)  },
710  {"lazy",         Sy_bit(OPT_OLDSTD),         ~Sy_bit(OPT_OLDSTD)  },
711  {"intStrategy",  Sy_bit(OPT_INTSTRATEGY),    ~Sy_bit(OPT_INTSTRATEGY)  },
712  {"infRedTail",   Sy_bit(OPT_INFREDTAIL),     ~Sy_bit(OPT_INFREDTAIL)  },
713  /* 30: use not regularity for syz */
714  {"notRegularity",Sy_bit(OPT_NOTREGULARITY),  ~Sy_bit(OPT_NOTREGULARITY)  },
715  {"weightM",      Sy_bit(OPT_WEIGHTM),        ~Sy_bit(OPT_WEIGHTM)  },
716/*special for "none" and also end marker for showOption:*/
717  {"ne",           0,                          0 }
718};
719
720struct soptionStruct verboseStruct[]=
721{
722  {"mem",      Sy_bit(V_SHOW_MEM),  ~Sy_bit(V_SHOW_MEM)   },
723  {"yacc",     Sy_bit(V_YACC),      ~Sy_bit(V_YACC)       },
724  {"redefine", Sy_bit(V_REDEFINE),  ~Sy_bit(V_REDEFINE)   },
725  {"reading",  Sy_bit(V_READING),   ~Sy_bit(V_READING)    },
726  {"loadLib",  Sy_bit(V_LOAD_LIB),  ~Sy_bit(V_LOAD_LIB)   },
727  {"debugLib", Sy_bit(V_DEBUG_LIB), ~Sy_bit(V_DEBUG_LIB)  },
728  {"loadProc", Sy_bit(V_LOAD_PROC), ~Sy_bit(V_LOAD_PROC)  },
729  {"defRes",   Sy_bit(V_DEF_RES),   ~Sy_bit(V_DEF_RES)    },
730  {"usage",    Sy_bit(V_SHOW_USE),  ~Sy_bit(V_SHOW_USE)   },
731  {"Imap",     Sy_bit(V_IMAP),      ~Sy_bit(V_IMAP)       },
732  {"prompt",   Sy_bit(V_PROMPT),    ~Sy_bit(V_PROMPT)     },
733  {"length",   Sy_bit(V_LENGTH),    ~Sy_bit(V_LENGTH)     },
734  {"notWarnSB",Sy_bit(V_NSB),       ~Sy_bit(V_NSB)        },
735  {"contentSB",Sy_bit(V_CONTENTSB), ~Sy_bit(V_CONTENTSB)  },
736  {"cancelunit",Sy_bit(V_CANCELUNIT),~Sy_bit(V_CANCELUNIT)},
737  {"modpsolve",Sy_bit(V_MODPSOLVSB),~Sy_bit(V_MODPSOLVSB)},
738  {"geometricSB",Sy_bit(V_UPTORADICAL),~Sy_bit(V_UPTORADICAL)},
739  {"findMonomials",Sy_bit(V_FINDMONOM),~Sy_bit(V_FINDMONOM)},
740  {"coefStrat",Sy_bit(V_COEFSTRAT), ~Sy_bit(V_COEFSTRAT)},
741  {"qringNF",  Sy_bit(V_QRING),     ~Sy_bit(V_QRING)},
742/*special for "none" and also end marker for showOption:*/
743  {"ne",         0,          0 }
744};
745
746BOOLEAN setOption(leftv res, leftv v)
747{
748  const char *n;
749  do
750  {
751    if (v->Typ()==STRING_CMD)
752    {
753      n=(const char *)v->CopyD(STRING_CMD);
754    }
755    else
756    {
757      if (v->name==NULL)
758        return TRUE;
759      if (v->rtyp==0)
760      {
761        n=v->name;
762        v->name=NULL;
763      }
764      else
765      {
766        n=omStrDup(v->name);
767      }
768    }
769
770    int i;
771
772    if(strcmp(n,"get")==0)
773    {
774      intvec *w=new intvec(2);
775      (*w)[0]=test;
776      (*w)[1]=verbose;
777      res->rtyp=INTVEC_CMD;
778      res->data=(void *)w;
779      goto okay;
780    }
781    if(strcmp(n,"set")==0)
782    {
783      if((v->next!=NULL)
784      &&(v->next->Typ()==INTVEC_CMD))
785      {
786        v=v->next;
787        intvec *w=(intvec*)v->Data();
788        test=(*w)[0];
789        verbose=(*w)[1];
790#if 0
791        if (TEST_OPT_INTSTRATEGY && (currRing!=NULL)
792        && rField_has_simple_inverse()
793#ifdef HAVE_RINGS
794        && !rField_is_Ring(currRing)
795#endif
796        ) {
797          test &=~Sy_bit(OPT_INTSTRATEGY);
798        }
799#endif
800        goto okay;
801      }
802    }
803    if(strcmp(n,"none")==0)
804    {
805      test=0;
806      verbose=0;
807      goto okay;
808    }
809    for (i=0; (i==0) || (optionStruct[i-1].setval!=0); i++)
810    {
811      if (strcmp(n,optionStruct[i].name)==0)
812      {
813        if (optionStruct[i].setval & validOpts)
814        {
815          test |= optionStruct[i].setval;
816          // optOldStd disables redthrough
817          if (optionStruct[i].setval == Sy_bit(OPT_OLDSTD))
818            test &= ~Sy_bit(OPT_REDTHROUGH);
819        }
820        else
821          Warn("cannot set option");
822#if 0
823        if (TEST_OPT_INTSTRATEGY && (currRing!=NULL)
824        && rField_has_simple_inverse()
825#ifdef HAVE_RINGS
826        && !rField_is_Ring(currRing)
827#endif
828        ) {
829          test &=~Sy_bit(OPT_INTSTRATEGY);
830        }
831#endif
832        goto okay;
833      }
834      else if ((strncmp(n,"no",2)==0)
835      && (strcmp(n+2,optionStruct[i].name)==0))
836      {
837        if (optionStruct[i].setval & validOpts)
838        {
839          test &= optionStruct[i].resetval;
840        }
841        else
842          Warn("cannot clear option");
843        goto okay;
844      }
845    }
846    for (i=0; (i==0) || (verboseStruct[i-1].setval!=0); i++)
847    {
848      if (strcmp(n,verboseStruct[i].name)==0)
849      {
850        verbose |= verboseStruct[i].setval;
851        #ifdef YYDEBUG
852        #if YYDEBUG
853        /*debugging the bison grammar --> grammar.cc*/
854        extern int    yydebug;
855        if (BVERBOSE(V_YACC)) yydebug=1;
856        else                  yydebug=0;
857        #endif
858        #endif
859        goto okay;
860      }
861      else if ((strncmp(n,"no",2)==0)
862      && (strcmp(n+2,verboseStruct[i].name)==0))
863      {
864        verbose &= verboseStruct[i].resetval;
865        #ifdef YYDEBUG
866        #if YYDEBUG
867        /*debugging the bison grammar --> grammar.cc*/
868        extern int    yydebug;
869        if (BVERBOSE(V_YACC)) yydebug=1;
870        else                  yydebug=0;
871        #endif
872        #endif
873        goto okay;
874      }
875    }
876    Werror("unknown option `%s`",n);
877  okay:
878    if (currRing != NULL)
879      currRing->options = test & TEST_RINGDEP_OPTS;
880    omFree((ADDRESS)n);
881    v=v->next;
882  } while (v!=NULL);
883  #ifdef HAVE_TCL
884    if (tclmode)
885    {
886      BITSET tmp;
887      int i;
888      StringSetS("");
889      if ((test!=0)||(verbose!=0))
890      {
891        tmp=test;
892        if(tmp)
893        {
894          for (i=0; optionStruct[i].setval!=0; i++)
895          {
896            if (optionStruct[i].setval & test)
897            {
898              StringAppend(" %s",optionStruct[i].name);
899              tmp &=optionStruct[i].resetval;
900            }
901          }
902        }
903        tmp=verbose;
904        if (tmp)
905        {
906          for (i=0; verboseStruct[i].setval!=0; i++)
907          {
908            if (verboseStruct[i].setval & tmp)
909            {
910              StringAppend(" %s",verboseStruct[i].name);
911              tmp &=verboseStruct[i].resetval;
912            }
913          }
914        }
915        PrintTCLS('O',StringAppendS(""));
916        StringSetS("");
917      }
918      else
919      {
920        PrintTCLS('O'," ");
921      }
922    }
923  #endif
924    // set global variable to show memory usage
925    if (BVERBOSE(V_SHOW_MEM)) om_sing_opt_show_mem = 1;
926    else om_sing_opt_show_mem = 0;
927  return FALSE;
928}
929
930char * showOption()
931{
932  int i;
933  BITSET tmp;
934
935  StringSetS("//options:");
936  if ((test!=0)||(verbose!=0))
937  {
938    tmp=test;
939    if(tmp)
940    {
941      for (i=0; optionStruct[i].setval!=0; i++)
942      {
943        if (optionStruct[i].setval & test)
944        {
945          StringAppend(" %s",optionStruct[i].name);
946          tmp &=optionStruct[i].resetval;
947        }
948      }
949      for (i=0; i<32; i++)
950      {
951        if (tmp & Sy_bit(i)) StringAppend(" %d",i);
952      }
953    }
954    tmp=verbose;
955    if (tmp)
956    {
957      for (i=0; verboseStruct[i].setval!=0; i++)
958      {
959        if (verboseStruct[i].setval & tmp)
960        {
961          StringAppend(" %s",verboseStruct[i].name);
962          tmp &=verboseStruct[i].resetval;
963        }
964      }
965      for (i=1; i<32; i++)
966      {
967        if (tmp & Sy_bit(i)) StringAppend(" %d",i+32);
968      }
969    }
970    return omStrDup(StringAppendS(""));
971  }
972  else
973    return omStrDup(StringAppendS(" none"));
974}
975
976char * versionString()
977{
978  char* str = StringSetS("");
979  StringAppend("Singular for %s version %s (%d-%lu)  %s\nwith\n",
980               S_UNAME, S_VERSION1, SINGULAR_VERSION,
981               feVersionId,singular_date);
982  StringAppendS("\t");
983#ifdef HAVE_FACTORY
984              StringAppend("factory(%s),", factoryVersion);
985              StringAppend("libfac(%s,%s),\n\t",libfac_version,libfac_date);
986#endif
987#if defined (__GNU_MP_VERSION) && defined (__GNU_MP_VERSION_MINOR)
988              StringAppend("GMP(%d.%d),",__GNU_MP_VERSION,__GNU_MP_VERSION_MINOR);
989#else
990              StringAppendS("GMP(1.3),");
991#endif
992#ifdef HAVE_NTL
993#include "NTL/version.h"
994              StringAppend("NTL(%s),",NTL_VERSION);
995#endif
996#ifdef HAVE_MPSR
997              StringAppend("MP(%s),",MP_VERSION);
998#endif
999#if SIZEOF_VOIDP == 8
1000              StringAppendS("64bit,");
1001#else
1002              StringAppendS("32bit,");
1003#endif
1004#if defined(HAVE_DYN_RL)
1005              if (fe_fgets_stdin==fe_fgets_dummy)
1006                StringAppendS("no input,");
1007              else if (fe_fgets_stdin==fe_fgets)
1008                StringAppendS("fgets,");
1009              if (fe_fgets_stdin==fe_fgets_stdin_drl)
1010                StringAppendS("dynamic readline,");
1011              #ifdef HAVE_FEREAD
1012              else if (fe_fgets_stdin==fe_fgets_stdin_emu)
1013                StringAppendS("emulated readline,");
1014              #endif
1015              else
1016                StringAppendS("unknown fgets method,");
1017#else
1018  #if defined(HAVE_READLINE) && !defined(FEREAD)
1019              StringAppendS("static readline,");
1020  #else
1021    #ifdef HAVE_FEREAD
1022              StringAppendS("emulated readline,");
1023    #else
1024              StringAppendS("fgets,");
1025    #endif
1026  #endif
1027#endif
1028#ifdef HAVE_PLURAL
1029              StringAppendS("Plural,");
1030#endif
1031#ifdef HAVE_DBM
1032              StringAppendS("DBM,\n\t");
1033#else
1034              StringAppendS("\n\t");
1035#endif
1036#ifdef HAVE_DYNAMIC_LOADING
1037              StringAppendS("dynamic modules,");
1038#endif
1039              if (p_procs_dynamic) StringAppendS("dynamic p_Procs,");
1040#ifdef TEST
1041              StringAppendS("TESTs,");
1042#endif
1043#if YYDEBUG
1044              StringAppendS("YYDEBUG=1,");
1045#endif
1046#ifdef HAVE_ASSUME
1047             StringAppendS("ASSUME,");
1048#endif
1049#ifdef MDEBUG
1050              StringAppend("MDEBUG=%d,",MDEBUG);
1051#endif
1052#ifdef OM_CHECK
1053              StringAppend("OM_CHECK=%d,",OM_CHECK);
1054#endif
1055#ifdef OM_TRACK
1056              StringAppend("OM_TRACK=%d,",OM_TRACK);
1057#endif
1058#ifdef OM_NDEBUG
1059              StringAppendS("OM_NDEBUG,");
1060#endif
1061#ifdef PDEBUG
1062              StringAppendS("PDEBUG,");
1063#endif
1064#ifdef KDEBUG
1065              StringAppendS("KDEBUG,");
1066#endif
1067#ifndef __OPTIMIZE__
1068              StringAppendS("-g,");
1069#endif
1070#ifdef HAVE_EIGENVAL
1071              StringAppendS("eigenvalues,");
1072#endif
1073#ifdef HAVE_GMS
1074              StringAppendS("Gauss-Manin system,");
1075#endif
1076#ifdef HAVE_RATGRING
1077              StringAppendS("ratGB,");
1078#endif
1079              StringAppend("random=%d\n",siRandomStart);
1080              StringAppend("\tCC=%s,\n\tCXX=%s"
1081#ifdef __GNUC__
1082              "(" __VERSION__ ")"
1083#endif
1084              "\n",CC,CXX);
1085              feStringAppendResources(0);
1086              feStringAppendBrowsers(0);
1087              StringAppendS("\n");
1088              return str;
1089}
1090
1091#ifdef PDEBUG
1092#if (OM_TRACK > 2) && defined(OM_TRACK_CUSTOM)
1093void p_SetRingOfLeftv(leftv l, ring r)
1094{
1095  switch(l->rtyp)
1096  {
1097    case INT_CMD:
1098    case BIGINT_CMD:
1099    case IDHDL:
1100    case DEF_CMD:
1101      break;
1102    case POLY_CMD:
1103    case VECTOR_CMD:
1104    {
1105      poly p=(poly)l->data;
1106      while(p!=NULL) { p_SetRingOfLm(p,r); pIter(p); }
1107      break;
1108    }
1109    case IDEAL_CMD:
1110    case MODUL_CMD:
1111    case MATRIX_CMD:
1112    {
1113      ideal I=(ideal)l->data;
1114      int i;
1115      for(i=IDELEMS(I)-1;i>=0;i--)
1116      {
1117        poly p=I->m[i];
1118        while(p!=NULL) { p_SetRingOfLm(p,r); pIter(p); }
1119      }
1120      break;
1121    }
1122    case COMMAND:
1123    {
1124      command d=(command)l->data;
1125      p_SetRingOfLeftv(&d->arg1, r);
1126      if (d->argc>1) p_SetRingOfLeftv(&d->arg2, r);
1127      if (d->argc>2) p_SetRingOfLeftv(&d->arg3, r);
1128      break;
1129    }
1130    default:
1131     printf("type %d not yet implementd in p_SetRingOfLeftv\n",l->rtyp);
1132     break;
1133  }
1134}
1135#endif
1136#endif
1137
1138void listall(int showproc)
1139{
1140      idhdl hh=basePack->idroot;
1141      PrintS("====== Top ==============\n");
1142      while (hh!=NULL)
1143      {
1144        if (showproc || (IDTYP(hh)!=PROC_CMD))
1145        {
1146          if (IDDATA(hh)==(void *)currRing) PrintS("(R)");
1147          else if (IDDATA(hh)==(void *)currPack) PrintS("(P)");
1148          else PrintS("   ");
1149          Print("::%s, typ %s level %d data %lx",
1150                 IDID(hh),Tok2Cmdname(IDTYP(hh)),IDLEV(hh),(long)IDDATA(hh));
1151          if ((IDTYP(hh)==RING_CMD)
1152          || (IDTYP(hh)==QRING_CMD))
1153            Print(" ref: %d\n",IDRING(hh)->ref);
1154          else
1155            PrintLn();
1156        }
1157        hh=IDNEXT(hh);
1158      }
1159      hh=basePack->idroot;
1160      while (hh!=NULL)
1161      {
1162        if (IDDATA(hh)==(void *)basePack)
1163          Print("(T)::%s, typ %s level %d data %lx\n",
1164          IDID(hh),Tok2Cmdname(IDTYP(hh)),IDLEV(hh),(long)IDDATA(hh));
1165        else
1166        if ((IDTYP(hh)==RING_CMD)
1167        || (IDTYP(hh)==QRING_CMD)
1168        || (IDTYP(hh)==PACKAGE_CMD))
1169        {
1170          Print("====== %s ==============\n",IDID(hh));
1171          idhdl h2=IDRING(hh)->idroot;
1172          while (h2!=NULL)
1173          {
1174            if (showproc || (IDTYP(h2)!=PROC_CMD))
1175            {
1176              if ((IDDATA(h2)==(void *)currRing)
1177              && ((IDTYP(h2)==RING_CMD)||(IDTYP(h2)==QRING_CMD)))
1178                PrintS("(R)");
1179              else if (IDDATA(h2)==(void *)currPack) PrintS("(P)");
1180              else PrintS("   ");
1181              Print("%s::%s, typ %s level %d data %lx\n",
1182              IDID(hh),IDID(h2),Tok2Cmdname(IDTYP(h2)),IDLEV(h2),(long)IDDATA(h2));
1183            }
1184            h2=IDNEXT(h2);
1185          }
1186        }
1187        hh=IDNEXT(hh);
1188      }
1189      Print("currRing:%lx, currPack:%lx,basePack:%lx\n",(long)currRing,(long)currPack,(long)basePack);
1190      iiCheckPack(currPack);
1191}
1192#ifndef NDEBUG
1193void checkall()
1194{
1195      idhdl hh=basePack->idroot;
1196      while (hh!=NULL)
1197      {
1198        omCheckAddr(hh);
1199        omCheckAddr((ADDRESS)IDID(hh));
1200        if (RingDependend(IDTYP(hh))) Print("%s typ %d in Top\n",IDID(hh),IDTYP(hh));
1201        hh=IDNEXT(hh);
1202      }
1203      hh=basePack->idroot;
1204      while (hh!=NULL)
1205      {
1206        if (IDTYP(hh)==PACKAGE_CMD)
1207        {
1208          idhdl h2=IDPACKAGE(hh)->idroot;
1209          while (h2!=NULL)
1210          {
1211            omCheckAddr(h2);
1212            omCheckAddr((ADDRESS)IDID(h2));
1213            if (RingDependend(IDTYP(h2))) Print("%s typ %d in %s\n",IDID(h2),IDTYP(h2),IDID(hh));
1214            h2=IDNEXT(h2);
1215          }
1216        }
1217        hh=IDNEXT(hh);
1218      }
1219}
1220#endif
1221
1222#include <sys/types.h>
1223#include <sys/stat.h>
1224#include <unistd.h>
1225
1226extern "C"
1227int singular_fstat(int fd, struct stat *buf)
1228{
1229  return fstat(fd,buf);
1230}
1231
Note: See TracBrowser for help on using the repository browser.