source: git/Singular/misc_ip.cc @ 237b3e4

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