source: git/Singular/misc_ip.cc @ cdf5c3c

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