source: git/Singular/misc_ip.cc @ 598a84

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