source: git/Singular/misc_ip.cc @ 981087

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