source: git/Singular/misc_ip.cc @ 4c4340

spielwiese
Last change on this file since 4c4340 was 4c4340, checked in by Hans Schoenemann <hannes@…>, 12 years ago
fix: primeFactorization redone with trial dvivision/Pollard-Rho from gmp Conflicts: Singular/misc_ip.cc
  • Property mode set to 100644
File size: 28.6 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 misc_ip.h.
9 *
10 **/
11/*****************************************************************************/
12
13// include header files
14#include <misc/auxiliary.h>
15#include "config.h"
16#include <kernel/mod2.h>
17
18#ifdef HAVE_FACTORY
19#define SI_DONT_HAVE_GLOBAL_VARS
20#include <factory/factory.h>
21#endif
22
23#include <coeffs/si_gmp.h>
24#include <coeffs/coeffs.h>
25
26#include <polys/ext_fields/algext.h>
27#include <polys/ext_fields/transext.h>
28
29#include "misc_ip.h"
30#include "ipid.h"
31#include "feOpt.h"
32#include "silink.h"
33
34// the following 2 inline functions are just convenience shortcuts for Frank's code:
35static inline void number2mpz(number n, mpz_t m){ n_MPZ(m, n, coeffs_BIGINT); }
36static inline number mpz2number(mpz_t m){ return n_InitMPZ(m, coeffs_BIGINT); }
37
38
39void setListEntry(lists L, int index, mpz_t n)
40{ /* assumes n > 0 */
41  /* try to fit nn into an int: */
42  if (mpz_size1(n)<=1)
43  {
44    int ui=(int)mpz_get_si(n);
45    if ((((ui<<3)>>3)==ui)
46    && (mpz_cmp_si(n,(long)ui)==0))
47    {
48      L->m[index].rtyp = INT_CMD; L->m[index].data = (void*)ui;
49      return;
50    }
51  }
52  number nn = mpz2number(n);
53  L->m[index].rtyp = BIGINT_CMD; L->m[index].data = (void*)nn;
54}
55
56void setListEntry_ui(lists L, int index, unsigned long ui)
57{ /* assumes n > 0 */
58  /* try to fit nn into an int: */
59  int i=(int)ui;
60  if ((((unsigned long)i)==ui) && (((i<<3)>>3)==i))
61  {
62    L->m[index].rtyp = INT_CMD; L->m[index].data = (void*)i;
63  }
64  else
65  {
66    number nn = n_Init(ui, coeffs_BIGINT);
67    L->m[index].rtyp = BIGINT_CMD; L->m[index].data = (void*)nn;
68  }
69}
70
71
72/* Factoring with Pollard's rho method. stolen from GMP/demos */
73static unsigned add[] = {4, 2, 4, 2, 4, 6, 2, 6};
74
75void factor_using_division (mpz_t t, unsigned int limit,lists primes, int *multiplicities,int &index)
76{
77  mpz_t q, r;
78  unsigned long int f;
79  int ai;
80  unsigned *addv = add;
81  unsigned int failures;
82
83  mpz_init (q);
84  mpz_init (r);
85
86  f = mpz_scan1 (t, 0);
87  mpz_div_2exp (t, t, f);
88  if (f>0)
89  {
90    setListEntry_ui(primes, index, 2);
91    multiplicities[index++] = f;
92  }
93
94  f=0;
95  loop
96  {
97    mpz_tdiv_qr_ui (q, r, t, 3);
98    if (mpz_cmp_ui (r, 0) != 0)
99        break;
100    mpz_set (t, q);
101    f++;
102  }
103  if (f>0)
104  {
105    setListEntry_ui(primes, index, 3);
106    multiplicities[index++] = f;
107  }
108  f=0;
109  loop
110  {
111    mpz_tdiv_qr_ui (q, r, t, 5);
112    if (mpz_cmp_ui (r, 0) != 0)
113        break;
114    mpz_set (t, q);
115    f++;
116  }
117  if (f>0)
118  {
119    setListEntry_ui(primes, index, 5);
120    multiplicities[index++] = f;
121  }
122
123  failures = 0;
124  f = 7;
125  ai = 0;
126  unsigned last_f=0;
127  while (mpz_cmp_ui (t, 1) != 0)
128  {
129    mpz_tdiv_qr_ui (q, r, t, f);
130    if (mpz_cmp_ui (r, 0) != 0)
131    {
132      f += addv[ai];
133      if (mpz_cmp_ui (q, f) < 0)
134        break;
135      ai = (ai + 1) & 7;
136      failures++;
137      if (failures > limit)
138        break;
139    }
140    else
141    {
142      mpz_swap (t, q);
143      if (f!=last_f)
144      {
145        setListEntry_ui(primes, index, f);
146        multiplicities[index]++;
147        index++;
148      }
149      else
150      {
151        multiplicities[index-1]++;
152      }
153      last_f=f;
154      failures = 0;
155    }
156  }
157
158  mpz_clears (q, r, NULL);
159}
160
161void factor_using_pollard_rho (mpz_t n, unsigned long a, unsigned long p,lists primes, int * multiplicities,int &index)
162{
163  mpz_t x, x1, y, P;
164  mpz_t t1, t2;
165  mpz_t last_f;
166  unsigned long long k, l, i;
167
168  mpz_init (t1);
169  mpz_init (t2);
170  mpz_init_set_si (last_f, 0);
171  mpz_init_set_si (y, 2);
172  mpz_init_set_si (x, 2);
173  mpz_init_set_si (x1, 2);
174  mpz_init_set_ui (P, 1);
175  k = 1;
176  l = 1;
177
178  while (mpz_cmp_ui (n, 1) != 0)
179  {
180    loop
181    {
182      do
183      {
184        if (p != 0)
185        {
186          mpz_powm_ui (x, x, p, n);
187          mpz_add_ui (x, x, a);
188        }
189        else
190        {
191          mpz_mul (t1, x, x);
192          mpz_mod (x, t1, n);
193          mpz_add_ui (x, x, a);
194        }
195
196        mpz_sub (t1, x1, x);
197        mpz_mul (t2, P, t1);
198        mpz_mod (P, t2, n);
199
200        if (k % 32 == 1)
201        {
202          mpz_gcd (t1, P, n);
203          if (mpz_cmp_ui (t1, 1) != 0)
204            goto factor_found;
205          mpz_set (y, x);
206        }
207      }
208      while (--k != 0);
209
210      mpz_gcd (t1, P, n);
211      if (mpz_cmp_ui (t1, 1) != 0)
212        goto factor_found;
213
214      mpz_set (x1, x);
215      k = l;
216      l = 2 * l;
217      for (i = 0; i < k; i++)
218      {
219        if (p != 0)
220        {
221          mpz_powm_ui (x, x, p, n);
222          mpz_add_ui (x, x, a);
223        }
224        else
225        {
226          mpz_mul (t1, x, x);
227          mpz_mod (x, t1, n);
228          mpz_add_ui (x, x, a);
229        }
230      }
231      mpz_set (y, x);
232    }
233
234  factor_found:
235    do
236    {
237      if (p != 0)
238      {
239        mpz_powm_ui (y, y, p, n); mpz_add_ui (y, y, a);
240      }
241      else
242      {
243        mpz_mul (t1, y, y);
244        mpz_mod (y, t1, n);
245        mpz_add_ui (y, y, a);
246      }
247      mpz_sub (t1, x1, y);
248      mpz_gcd (t1, t1, n);
249    }
250    while (mpz_cmp_ui (t1, 1) == 0);
251
252    mpz_divexact (n, n, t1);        /* divide by t1, before t1 is overwritten */
253
254    if (!mpz_probab_prime_p (t1, 10))
255    {
256      do
257      {
258        mp_limb_t a_limb;
259        mpn_random (&a_limb, (mp_size_t) 1);
260        a = a_limb;
261      }
262      while (a == 0);
263
264      factor_using_pollard_rho (t1, a, p, primes,multiplicities,index);
265    }
266    else
267    {
268      if (mpz_cmp(t1,last_f)==0)
269      {
270        multiplicities[index-1]++;
271      }
272      else
273      {
274        mpz_set(last_f,t1);
275        setListEntry(primes, index, t1);
276        multiplicities[index++] = 1;
277      }
278    }
279    mpz_mod (x, x, n);
280    mpz_mod (x1, x1, n);
281    mpz_mod (y, y, n);
282    if (mpz_probab_prime_p (n, 10))
283    {
284      if (mpz_cmp(n,last_f)==0)
285      {
286        multiplicities[index-1]++;
287      }
288      else
289      {
290        mpz_set(last_f,n);
291        setListEntry(primes, index, n);
292        multiplicities[index++] = 1;
293      }
294      break;
295    }
296  }
297
298  mpz_clears (P,t2,t1,x1,x,y,last_f,NULL);
299}
300
301void factor (mpz_t t,lists primes,int *multiplicities,int &index)
302{
303  unsigned int division_limit;
304
305  if (mpz_sgn (t) == 0)
306    return;
307
308  /* Set the trial division limit according the size of t.  */
309  division_limit = mpz_sizeinbase (t, 2);
310  if (division_limit > 1000)
311    division_limit = 1000 * 1000;
312  else
313    division_limit = division_limit * division_limit;
314
315  factor_using_division (t, division_limit,primes,multiplicities,index);
316
317  if (mpz_cmp_ui (t, 1) != 0)
318  {
319    //if (flag_verbose > 0)
320    //{
321    //  printf ("[is number prime?] ");
322    //  fflush (stdout);
323    //}
324    if (mpz_probab_prime_p (t, 10))
325    {
326      setListEntry(primes, index, t);
327      multiplicities[index++] = 1;
328    }
329    else
330      factor_using_pollard_rho (t, 1L, 0,primes,multiplicities,index);
331  }
332  mpz_set_ui(t,1);
333}
334/* n and pBound are assumed to be bigint numbers */
335lists primeFactorisation(const number n, const number pBound)
336{
337  int i;
338  int index=0;
339  mpz_t nn; number2mpz(n, nn);
340  lists primes = (lists)omAllocBin(slists_bin); primes->Init(1000);
341  int* multiplicities = (int*)omAlloc0(1000*sizeof(int));
342  int positive=1; int probTest = 0;
343
344  if (!n_IsZero(n, coeffs_BIGINT))
345  {
346    if (!n_GreaterZero(n, coeffs_BIGINT))
347    {
348      positive=-1;
349      mpz_neg(nn,nn);
350    }
351    factor(nn,primes,multiplicities,index);
352  }
353
354  lists primesL = (lists)omAllocBin(slists_bin);
355  primesL->Init(index);
356  for (i = 0; i < index; i++)
357  {
358    primesL->m[i].rtyp = primes->m[i].rtyp;
359    primesL->m[i].data = primes->m[i].data;
360  }
361  omFreeSize((ADDRESS)primes->m, (primes->nr + 1) * sizeof(sleftv));
362  omFreeBin((ADDRESS)primes, slists_bin);
363
364  lists multiplicitiesL = (lists)omAllocBin(slists_bin);
365  multiplicitiesL->Init(index);
366  for (i = 0; i < index; i++)
367  {
368    multiplicitiesL->m[i].rtyp = INT_CMD;
369    multiplicitiesL->m[i].data = (void*)multiplicities[i];
370  }
371  delete[] multiplicities;
372
373  lists L=(lists)omAllocBin(slists_bin);
374  L->Init(4);
375  if (positive==-1) mpz_neg(nn,nn);
376  L->m[0].rtyp = LIST_CMD; L->m[0].data = (void*)primesL;
377  L->m[1].rtyp = LIST_CMD; L->m[1].data = (void*)multiplicitiesL;
378  setListEntry(L, 2, nn);
379  L->m[3].rtyp =  INT_CMD; L->m[3].data = (void*)probTest;
380  mpz_clear(nn);
381
382  return L;
383}
384
385#include <omalloc/omalloc.h>
386#include <misc/mylimits.h>
387
388#include <misc/options.h>
389#include <misc/intvec.h>
390
391#include <polys/monomials/ring.h>
392#include <polys/templates/p_Procs.h>
393
394#include <kernel/febase.h>
395#include <kernel/kstd1.h>
396#include <kernel/timer.h>
397
398
399#include "subexpr.h"
400#include "cntrlc.h"
401#include "ipid.h"
402#include "ipshell.h"
403
404#include "version.h"
405
406#include "fehelp.h"
407
408#ifdef HAVE_STATIC
409#undef HAVE_DYN_RL
410#endif
411
412#define SI_DONT_HAVE_GLOBAL_VARS
413
414//#ifdef HAVE_LIBPARSER
415//#  include "libparse.h"
416//#endif /* HAVE_LIBPARSER */
417
418
419/*2
420* the renice routine for very large jobs
421* works only on unix machines,
422* testet on : linux, HP 9.0
423*
424*#include <sys/times.h>
425*#include <sys/resource.h>
426*extern "C" int setpriority(int,int,int);
427*void very_nice()
428*{
429*#ifndef NO_SETPRIORITY
430*  setpriority(PRIO_PROCESS,0,19);
431*#endif
432*  sleep(10);
433*}
434*/
435
436#include <string.h>
437#include <unistd.h>
438#include <stdio.h>
439#include <stddef.h>
440#include <stdlib.h>
441#include <time.h>
442
443
444void singular_example(char *str)
445{
446  assume(str!=NULL);
447  char *s=str;
448  while (*s==' ') s++;
449  char *ss=s;
450  while (*ss!='\0') ss++;
451  while (*ss<=' ')
452  {
453    *ss='\0';
454    ss--;
455  }
456  idhdl h=IDROOT->get(s,myynest);
457  if ((h!=NULL) && (IDTYP(h)==PROC_CMD))
458  {
459    char *lib=iiGetLibName(IDPROC(h));
460    if((lib!=NULL)&&(*lib!='\0'))
461    {
462      Print("// proc %s from lib %s\n",s,lib);
463      s=iiGetLibProcBuffer(IDPROC(h), 2);
464      if (s!=NULL)
465      {
466        if (strlen(s)>5)
467        {
468          iiEStart(s,IDPROC(h));
469          omFree((ADDRESS)s);
470          return;
471        }
472        else omFree((ADDRESS)s);
473      }
474    }
475  }
476  else
477  {
478    char sing_file[MAXPATHLEN];
479    FILE *fd=NULL;
480    char *res_m=feResource('m', 0);
481    if (res_m!=NULL)
482    {
483      sprintf(sing_file, "%s/%s.sing", res_m, s);
484      fd = feFopen(sing_file, "r");
485    }
486    if (fd != NULL)
487    {
488
489      int old_echo = si_echo;
490      int length, got;
491      char* s;
492
493      fseek(fd, 0, SEEK_END);
494      length = ftell(fd);
495      fseek(fd, 0, SEEK_SET);
496      s = (char*) omAlloc((length+20)*sizeof(char));
497      got = fread(s, sizeof(char), length, fd);
498      fclose(fd);
499      if (got != length)
500      {
501        Werror("Error while reading file %s", sing_file);
502      }
503      else
504      {
505        s[length] = '\0';
506        strcat(s, "\n;return();\n\n");
507        si_echo = 2;
508        iiEStart(s, NULL);
509        si_echo = old_echo;
510      }
511      omFree(s);
512    }
513    else
514    {
515      Werror("no example for %s", str);
516    }
517  }
518}
519
520
521struct soptionStruct
522{
523  const char * name;
524  unsigned   setval;
525  unsigned   resetval;
526};
527
528struct soptionStruct optionStruct[]=
529{
530  {"prot",         Sy_bit(OPT_PROT),           ~Sy_bit(OPT_PROT)   },
531  {"redSB",        Sy_bit(OPT_REDSB),          ~Sy_bit(OPT_REDSB)   },
532  {"notBuckets",   Sy_bit(OPT_NOT_BUCKETS),    ~Sy_bit(OPT_NOT_BUCKETS)   },
533  {"notSugar",     Sy_bit(OPT_NOT_SUGAR),      ~Sy_bit(OPT_NOT_SUGAR)   },
534  {"interrupt",    Sy_bit(OPT_INTERRUPT),      ~Sy_bit(OPT_INTERRUPT)   },
535  {"sugarCrit",    Sy_bit(OPT_SUGARCRIT),      ~Sy_bit(OPT_SUGARCRIT)   },
536  {"teach",        Sy_bit(OPT_DEBUG),          ~Sy_bit(OPT_DEBUG)  },
537  {"notSyzMinim",  Sy_bit(OPT_NO_SYZ_MINIM),   ~Sy_bit(OPT_NO_SYZ_MINIM)  },
538  /* 9 return SB in syz, quotient, intersect */
539  {"returnSB",     Sy_bit(OPT_RETURN_SB),      ~Sy_bit(OPT_RETURN_SB)  },
540  {"fastHC",       Sy_bit(OPT_FASTHC),         ~Sy_bit(OPT_FASTHC)  },
541  /* 11-19 sort in L/T */
542  {"staircaseBound",Sy_bit(OPT_STAIRCASEBOUND),~Sy_bit(OPT_STAIRCASEBOUND)  },
543  {"multBound",    Sy_bit(OPT_MULTBOUND),      ~Sy_bit(OPT_MULTBOUND)  },
544  {"degBound",     Sy_bit(OPT_DEGBOUND),       ~Sy_bit(OPT_DEGBOUND)  },
545  /* 25 no redTail(p)/redTail(s) */
546  {"redTail",      Sy_bit(OPT_REDTAIL),        ~Sy_bit(OPT_REDTAIL)  },
547  {"redThrough",   Sy_bit(OPT_REDTHROUGH),     ~Sy_bit(OPT_REDTHROUGH)  },
548  {"lazy",         Sy_bit(OPT_OLDSTD),         ~Sy_bit(OPT_OLDSTD)  },
549  {"intStrategy",  Sy_bit(OPT_INTSTRATEGY),    ~Sy_bit(OPT_INTSTRATEGY)  },
550  {"infRedTail",   Sy_bit(OPT_INFREDTAIL),     ~Sy_bit(OPT_INFREDTAIL)  },
551  /* 30: use not regularity for syz */
552  {"notRegularity",Sy_bit(OPT_NOTREGULARITY),  ~Sy_bit(OPT_NOTREGULARITY)  },
553  {"weightM",      Sy_bit(OPT_WEIGHTM),        ~Sy_bit(OPT_WEIGHTM)  },
554/*special for "none" and also end marker for showOption:*/
555  {"ne",           0,                          0 }
556};
557
558struct soptionStruct verboseStruct[]=
559{
560  {"mem",      Sy_bit(V_SHOW_MEM),  ~Sy_bit(V_SHOW_MEM)   },
561  {"yacc",     Sy_bit(V_YACC),      ~Sy_bit(V_YACC)       },
562  {"redefine", Sy_bit(V_REDEFINE),  ~Sy_bit(V_REDEFINE)   },
563  {"reading",  Sy_bit(V_READING),   ~Sy_bit(V_READING)    },
564  {"loadLib",  Sy_bit(V_LOAD_LIB),  ~Sy_bit(V_LOAD_LIB)   },
565  {"debugLib", Sy_bit(V_DEBUG_LIB), ~Sy_bit(V_DEBUG_LIB)  },
566  {"loadProc", Sy_bit(V_LOAD_PROC), ~Sy_bit(V_LOAD_PROC)  },
567  {"defRes",   Sy_bit(V_DEF_RES),   ~Sy_bit(V_DEF_RES)    },
568  {"usage",    Sy_bit(V_SHOW_USE),  ~Sy_bit(V_SHOW_USE)   },
569  {"Imap",     Sy_bit(V_IMAP),      ~Sy_bit(V_IMAP)       },
570  {"prompt",   Sy_bit(V_PROMPT),    ~Sy_bit(V_PROMPT)     },
571  {"length",   Sy_bit(V_LENGTH),    ~Sy_bit(V_LENGTH)     },
572  {"notWarnSB",Sy_bit(V_NSB),       ~Sy_bit(V_NSB)        },
573  {"contentSB",Sy_bit(V_CONTENTSB), ~Sy_bit(V_CONTENTSB)  },
574  {"cancelunit",Sy_bit(V_CANCELUNIT),~Sy_bit(V_CANCELUNIT)},
575  {"modpsolve",Sy_bit(V_MODPSOLVSB),~Sy_bit(V_MODPSOLVSB)},
576  {"geometricSB",Sy_bit(V_UPTORADICAL),~Sy_bit(V_UPTORADICAL)},
577  {"findMonomials",Sy_bit(V_FINDMONOM),~Sy_bit(V_FINDMONOM)},
578  {"coefStrat",Sy_bit(V_COEFSTRAT), ~Sy_bit(V_COEFSTRAT)},
579  {"qringNF",  Sy_bit(V_QRING),     ~Sy_bit(V_QRING)},
580  {"warn",     Sy_bit(V_ALLWARN),   ~Sy_bit(V_ALLWARN)},
581  {"interedSyz",Sy_bit(V_INTERSECT_SYZ), ~Sy_bit(V_INTERSECT_SYZ)},
582  {"interedElim",Sy_bit(V_INTERSECT_ELIM), ~Sy_bit(V_INTERSECT_ELIM)},
583/*special for "none" and also end marker for showOption:*/
584  {"ne",         0,          0 }
585};
586
587BOOLEAN setOption(leftv res, leftv v)
588{
589  const char *n;
590  do
591  {
592    if (v->Typ()==STRING_CMD)
593    {
594      n=(const char *)v->CopyD(STRING_CMD);
595    }
596    else
597    {
598      if (v->name==NULL)
599        return TRUE;
600      if (v->rtyp==0)
601      {
602        n=v->name;
603        v->name=NULL;
604      }
605      else
606      {
607        n=omStrDup(v->name);
608      }
609    }
610
611    int i;
612
613    if(strcmp(n,"get")==0)
614    {
615      intvec *w=new intvec(2);
616      (*w)[0]=si_opt_1;
617      (*w)[1]=si_opt_2;
618      res->rtyp=INTVEC_CMD;
619      res->data=(void *)w;
620      goto okay;
621    }
622    if(strcmp(n,"set")==0)
623    {
624      if((v->next!=NULL)
625      &&(v->next->Typ()==INTVEC_CMD))
626      {
627        v=v->next;
628        intvec *w=(intvec*)v->Data();
629        si_opt_1=(*w)[0];
630        si_opt_2=(*w)[1];
631#if 0
632        if (TEST_OPT_INTSTRATEGY && (currRing!=NULL)
633        && rField_has_simple_inverse()
634#ifdef HAVE_RINGS
635        && !rField_is_Ring(currRing)
636#endif
637        ) {
638          si_opt_1 &=~Sy_bit(OPT_INTSTRATEGY);
639        }
640#endif
641        goto okay;
642      }
643    }
644    if(strcmp(n,"none")==0)
645    {
646      si_opt_1=0;
647      si_opt_2=0;
648      goto okay;
649    }
650    for (i=0; (i==0) || (optionStruct[i-1].setval!=0); i++)
651    {
652      if (strcmp(n,optionStruct[i].name)==0)
653      {
654        if (optionStruct[i].setval & validOpts)
655        {
656          si_opt_1 |= optionStruct[i].setval;
657          // optOldStd disables redthrough
658          if (optionStruct[i].setval == Sy_bit(OPT_OLDSTD))
659            si_opt_1 &= ~Sy_bit(OPT_REDTHROUGH);
660        }
661        else
662          Warn("cannot set option");
663#if 0
664        if (TEST_OPT_INTSTRATEGY && (currRing!=NULL)
665        && rField_has_simple_inverse()
666#ifdef HAVE_RINGS
667        && !rField_is_Ring(currRing)
668#endif
669        ) {
670          test &=~Sy_bit(OPT_INTSTRATEGY);
671        }
672#endif
673        goto okay;
674      }
675      else if ((strncmp(n,"no",2)==0)
676      && (strcmp(n+2,optionStruct[i].name)==0))
677      {
678        if (optionStruct[i].setval & validOpts)
679        {
680          si_opt_1 &= optionStruct[i].resetval;
681        }
682        else
683          Warn("cannot clear option");
684        goto okay;
685      }
686    }
687    for (i=0; (i==0) || (verboseStruct[i-1].setval!=0); i++)
688    {
689      if (strcmp(n,verboseStruct[i].name)==0)
690      {
691        si_opt_2 |= verboseStruct[i].setval;
692        #ifdef YYDEBUG
693        #if YYDEBUG
694        /*debugging the bison grammar --> grammar.cc*/
695        extern int    yydebug;
696        if (BVERBOSE(V_YACC)) yydebug=1;
697        else                  yydebug=0;
698        #endif
699        #endif
700        goto okay;
701      }
702      else if ((strncmp(n,"no",2)==0)
703      && (strcmp(n+2,verboseStruct[i].name)==0))
704      {
705        si_opt_2 &= verboseStruct[i].resetval;
706        #ifdef YYDEBUG
707        #if YYDEBUG
708        /*debugging the bison grammar --> grammar.cc*/
709        extern int    yydebug;
710        if (BVERBOSE(V_YACC)) yydebug=1;
711        else                  yydebug=0;
712        #endif
713        #endif
714        goto okay;
715      }
716    }
717    Werror("unknown option `%s`",n);
718  okay:
719    if (currRing != NULL)
720      currRing->options = si_opt_1 & TEST_RINGDEP_OPTS;
721    omFree((ADDRESS)n);
722    v=v->next;
723  } while (v!=NULL);
724
725#ifdef OM_SINGULAR_CONFIG_H
726   // set global variable to show memory usage
727  extern int om_sing_opt_show_mem;
728  if (BVERBOSE(V_SHOW_MEM)) om_sing_opt_show_mem = 1;
729  else om_sing_opt_show_mem = 0;
730#endif
731
732  return FALSE;
733}
734
735char * showOption()
736{
737  int i;
738  BITSET tmp;
739
740  StringSetS("//options:");
741  if ((si_opt_1!=0)||(si_opt_2!=0))
742  {
743    tmp=si_opt_1;
744    if(tmp)
745    {
746      for (i=0; optionStruct[i].setval!=0; i++)
747      {
748        if (optionStruct[i].setval & tmp)
749        {
750          StringAppend(" %s",optionStruct[i].name);
751          tmp &=optionStruct[i].resetval;
752        }
753      }
754      for (i=0; i<32; i++)
755      {
756        if (tmp & Sy_bit(i)) StringAppend(" %d",i);
757      }
758    }
759    tmp=si_opt_2;
760    if (tmp)
761    {
762      for (i=0; verboseStruct[i].setval!=0; i++)
763      {
764        if (verboseStruct[i].setval & tmp)
765        {
766          StringAppend(" %s",verboseStruct[i].name);
767          tmp &=verboseStruct[i].resetval;
768        }
769      }
770      for (i=1; i<32; i++)
771      {
772        if (tmp & Sy_bit(i)) StringAppend(" %d",i+32);
773      }
774    }
775    return StringEndS();
776  }
777  StringAppendS(" none");
778  return StringEndS();
779}
780
781char * versionString()
782{
783  StringSetS("");
784  StringAppend("Singular for %s version %s (%d-%s)  %s\nwith\n",
785               S_UNAME, S_VERSION1, SINGULAR_VERSION,
786               feVersionId,singular_date);
787  StringAppendS("\t");
788#ifdef HAVE_FACTORY
789  StringAppend("factory(%s)", factoryVersion);
790#ifdef HAVE_LIBFAC
791  // libfac:
792//  extern const char * libfac_version;
793//  extern const char * libfac_date;
794  StringAppend("+libfac");
795#endif // #ifdef HAVE_LIBFAC
796  StringAppend(",");
797#endif
798
799#if defined (__GNU_MP_VERSION) && defined (__GNU_MP_VERSION_MINOR)
800              StringAppend("GMP(%d.%d),",__GNU_MP_VERSION,__GNU_MP_VERSION_MINOR);
801#else
802              StringAppendS("GMP(1.3),");
803#endif
804#ifdef HAVE_NTL
805#include <NTL/version.h>
806              StringAppend("NTL(%s),",NTL_VERSION);
807#endif
808#if SIZEOF_VOIDP == 8
809              StringAppendS("64bit,");
810#else
811              StringAppendS("32bit,");
812#endif
813#if defined(HAVE_DYN_RL)
814              if (fe_fgets_stdin==fe_fgets_dummy)
815                StringAppendS("no input,");
816              else if (fe_fgets_stdin==fe_fgets)
817                StringAppendS("fgets,");
818              if (fe_fgets_stdin==fe_fgets_stdin_drl)
819                StringAppendS("dynamic readline,");
820              #ifdef HAVE_FEREAD
821              else if (fe_fgets_stdin==fe_fgets_stdin_emu)
822                StringAppendS("emulated readline,");
823              #endif
824              else
825                StringAppendS("unknown fgets method,");
826#else
827  #if defined(HAVE_READLINE) && !defined(FEREAD)
828              StringAppendS("static readline,");
829  #else
830    #ifdef HAVE_FEREAD
831              StringAppendS("emulated readline,");
832    #else
833              StringAppendS("fgets,");
834    #endif
835  #endif
836#endif
837#ifdef HAVE_PLURAL
838              StringAppendS("Plural,");
839#endif
840#ifdef HAVE_DBM
841              StringAppendS("DBM,\n\t");
842#else
843              StringAppendS("\n\t");
844#endif
845#ifdef HAVE_DYNAMIC_LOADING
846              StringAppendS("dynamic modules,");
847#endif
848              if (p_procs_dynamic) StringAppendS("dynamic p_Procs,");
849#ifdef TEST
850              StringAppendS("TESTs,");
851#endif
852#if YYDEBUG
853              StringAppendS("YYDEBUG=1,");
854#endif
855#ifdef HAVE_ASSUME
856             StringAppendS("ASSUME,");
857#endif
858#ifdef MDEBUG
859              StringAppend("MDEBUG=%d,",MDEBUG);
860#endif
861#ifdef OM_CHECK
862              StringAppend("OM_CHECK=%d,",OM_CHECK);
863#endif
864#ifdef OM_TRACK
865              StringAppend("OM_TRACK=%d,",OM_TRACK);
866#endif
867#ifdef OM_NDEBUG
868              StringAppendS("OM_NDEBUG,");
869#endif
870#ifdef PDEBUG
871              StringAppendS("PDEBUG,");
872#endif
873#ifdef KDEBUG
874              StringAppendS("KDEBUG,");
875#endif
876#ifndef __OPTIMIZE__
877              StringAppendS("-g,");
878#endif
879#ifdef HAVE_EIGENVAL
880              StringAppendS("eigenvalues,");
881#endif
882#ifdef HAVE_GMS
883              StringAppendS("Gauss-Manin system,");
884#endif
885#ifdef HAVE_RATGRING
886              StringAppendS("ratGB,");
887#endif
888              StringAppend("random=%d\n",siRandomStart);
889              StringAppend("\tCC=%s,\n\tCXX=%s"
890#ifdef __GNUC__
891              "(" __VERSION__ ")"
892#endif
893              "\n",CC,CXX);
894              feStringAppendResources(0);
895              feStringAppendBrowsers(0);
896              StringAppendS("\n");
897              return StringEndS();
898}
899
900#ifdef PDEBUG
901#if (OM_TRACK > 2) && defined(OM_TRACK_CUSTOM)
902void p_SetRingOfLeftv(leftv l, ring r)
903{
904  switch(l->rtyp)
905  {
906    case INT_CMD:
907    case BIGINT_CMD:
908    case IDHDL:
909    case DEF_CMD:
910      break;
911    case POLY_CMD:
912    case VECTOR_CMD:
913    {
914      poly p=(poly)l->data;
915      while(p!=NULL) { p_SetRingOfLm(p,r); pIter(p); }
916      break;
917    }
918    case IDEAL_CMD:
919    case MODUL_CMD:
920    case MATRIX_CMD:
921    {
922      ideal I=(ideal)l->data;
923      int i;
924      for(i=IDELEMS(I)-1;i>=0;i--)
925      {
926        poly p=I->m[i];
927        while(p!=NULL) { p_SetRingOfLm(p,r); pIter(p); }
928      }
929      break;
930    }
931    case COMMAND:
932    {
933      command d=(command)l->data;
934      p_SetRingOfLeftv(&d->arg1, r);
935      if (d->argc>1) p_SetRingOfLeftv(&d->arg2, r);
936      if (d->argc>2) p_SetRingOfLeftv(&d->arg3, r);
937      break;
938    }
939    default:
940     printf("type %d not yet implementd in p_SetRingOfLeftv\n",l->rtyp);
941     break;
942  }
943}
944#endif
945#endif
946
947#if 0 /* debug only */
948void listall(int showproc)
949{
950      idhdl hh=basePack->idroot;
951      PrintS("====== Top ==============\n");
952      while (hh!=NULL)
953      {
954        if (showproc || (IDTYP(hh)!=PROC_CMD))
955        {
956          if (IDDATA(hh)==(void *)currRing) PrintS("(R)");
957          else if (IDDATA(hh)==(void *)currPack) PrintS("(P)");
958          else PrintS("   ");
959          Print("::%s, typ %s level %d data %lx",
960                 IDID(hh),Tok2Cmdname(IDTYP(hh)),IDLEV(hh),(long)IDDATA(hh));
961          if ((IDTYP(hh)==RING_CMD)
962          || (IDTYP(hh)==QRING_CMD))
963            Print(" ref: %d\n",IDRING(hh)->ref);
964          else
965            PrintLn();
966        }
967        hh=IDNEXT(hh);
968      }
969      hh=basePack->idroot;
970      while (hh!=NULL)
971      {
972        if (IDDATA(hh)==(void *)basePack)
973          Print("(T)::%s, typ %s level %d data %lx\n",
974          IDID(hh),Tok2Cmdname(IDTYP(hh)),IDLEV(hh),(long)IDDATA(hh));
975        else
976        if ((IDTYP(hh)==RING_CMD)
977        || (IDTYP(hh)==QRING_CMD)
978        || (IDTYP(hh)==PACKAGE_CMD))
979        {
980          Print("====== %s ==============\n",IDID(hh));
981          idhdl h2=IDRING(hh)->idroot;
982          while (h2!=NULL)
983          {
984            if (showproc || (IDTYP(h2)!=PROC_CMD))
985            {
986              if ((IDDATA(h2)==(void *)currRing)
987              && ((IDTYP(h2)==RING_CMD)||(IDTYP(h2)==QRING_CMD)))
988                PrintS("(R)");
989              else if (IDDATA(h2)==(void *)currPack) PrintS("(P)");
990              else PrintS("   ");
991              Print("%s::%s, typ %s level %d data %lx\n",
992              IDID(hh),IDID(h2),Tok2Cmdname(IDTYP(h2)),IDLEV(h2),(long)IDDATA(h2));
993            }
994            h2=IDNEXT(h2);
995          }
996        }
997        hh=IDNEXT(hh);
998      }
999      Print("currRing:%lx, currPack:%lx,basePack:%lx\n",(long)currRing,(long)currPack,(long)basePack);
1000      iiCheckPack(currPack);
1001}
1002#endif
1003
1004#ifndef NDEBUG
1005void checkall()
1006{
1007      idhdl hh=basePack->idroot;
1008      while (hh!=NULL)
1009      {
1010        omCheckAddr(hh);
1011        omCheckAddr((ADDRESS)IDID(hh));
1012        if (RingDependend(IDTYP(hh))) Print("%s typ %d in Top\n",IDID(hh),IDTYP(hh));
1013        hh=IDNEXT(hh);
1014      }
1015      hh=basePack->idroot;
1016      while (hh!=NULL)
1017      {
1018        if (IDTYP(hh)==PACKAGE_CMD)
1019        {
1020          idhdl h2=IDPACKAGE(hh)->idroot;
1021          while (h2!=NULL)
1022          {
1023            omCheckAddr(h2);
1024            omCheckAddr((ADDRESS)IDID(h2));
1025            if (RingDependend(IDTYP(h2))) Print("%s typ %d in %s\n",IDID(h2),IDTYP(h2),IDID(hh));
1026            h2=IDNEXT(h2);
1027          }
1028        }
1029        hh=IDNEXT(hh);
1030      }
1031}
1032#endif
1033
1034#include <sys/types.h>
1035#include <sys/stat.h>
1036#include <unistd.h>
1037
1038extern "C"
1039int singular_fstat(int fd, struct stat *buf)
1040{
1041  return fstat(fd,buf);
1042}
1043
1044/*2
1045* the global exit routine of Singular
1046*/
1047extern "C" {
1048
1049void m2_end(int i)
1050{
1051  fe_reset_input_mode();
1052  #ifdef PAGE_TEST
1053  mmEndStat();
1054  #endif
1055  fe_reset_input_mode();
1056  idhdl h = IDROOT;
1057  while(h != NULL)
1058  {
1059    if(IDTYP(h) == LINK_CMD)
1060    {
1061      idhdl hh=h->next;
1062      killhdl(h, currPack);
1063      h = hh;
1064    }
1065    else
1066    {
1067      h = h->next;
1068    }
1069  }
1070  if(!singular_in_batchmode)
1071  {
1072    if (i<=0)
1073    {
1074      if (TEST_V_QUIET)
1075      {
1076        if (i==0)
1077          printf("Auf Wiedersehen.\n");
1078        else
1079          printf("\n$Bye.\n");
1080      }
1081      //#ifdef sun
1082      //  #ifndef __svr4__
1083      //    _cleanup();
1084      //    _exit(0);
1085      //  #endif
1086      //#endif
1087      i=0;
1088    }
1089    else
1090    {
1091        printf("\nhalt %d\n",i);
1092    }
1093  }
1094  exit(i);
1095}
1096}
1097
1098const char *singular_date=__DATE__ " " __TIME__;
1099
1100extern "C"
1101{
1102  void omSingOutOfMemoryFunc()
1103  {
1104    fprintf(stderr, "\nSingular error: no more memory\n");
1105    omPrintStats(stderr);
1106    m2_end(14);
1107    /* should never get here */
1108    exit(1);
1109  }
1110}
1111
1112/*2
1113* initialize components of Singular
1114*/
1115void siInit(char *name)
1116{
1117#ifdef HAVE_FACTORY
1118// factory default settings: -----------------------------------------------
1119  On(SW_USE_NTL);
1120  On(SW_USE_NTL_GCD_0); // On -> seg11 in Old/algnorm, Old/factor...
1121  On(SW_USE_NTL_GCD_P); // On -> cyle in Short/brnoeth_s: fixed
1122  On(SW_USE_EZGCD);
1123  On(SW_USE_CHINREM_GCD);
1124  //On(SW_USE_FF_MOD_GCD);
1125  On(SW_USE_EZGCD_P);
1126  On(SW_USE_QGCD);
1127  Off(SW_USE_NTL_SORT); // may be changed by an command line option
1128  factoryError=WerrorS;
1129#endif
1130
1131// memory initialization: -----------------------------------------------
1132    om_Opts.OutOfMemoryFunc = omSingOutOfMemoryFunc;
1133#ifndef OM_NDEBUG
1134#ifndef __OPTIMIZE__
1135    om_Opts.ErrorHook = dErrorBreak;
1136#endif
1137#endif
1138    omInitInfo();
1139#ifdef OM_SING_KEEP
1140    om_Opts.Keep = OM_SING_KEEP;
1141#endif
1142
1143// interpreter tables etc.: -----------------------------------------------
1144#ifdef INIT_BUG
1145  jjInitTab1();
1146#endif
1147  memset(&sLastPrinted,0,sizeof(sleftv));
1148  sLastPrinted.rtyp=NONE;
1149
1150  extern int iiInitArithmetic(); iiInitArithmetic(); // iparith.cc
1151
1152  basePack=(package)omAlloc0(sizeof(*basePack));
1153  currPack=basePack;
1154  idhdl h;
1155  h=enterid("Top", 0, PACKAGE_CMD, &IDROOT, TRUE);
1156  IDPACKAGE(h)->language = LANG_TOP;
1157  IDPACKAGE(h)=basePack;
1158  currPackHdl=h;
1159  basePackHdl=h;
1160
1161  coeffs_BIGINT = nInitChar(n_Q,NULL);
1162
1163#if 1
1164   // def HAVE_POLYEXTENSIONS
1165  if(TRUE)
1166  {
1167    n_coeffType type = nRegister(n_algExt, naInitChar);
1168    assume(type == n_algExt);
1169
1170    type = nRegister(n_transExt, ntInitChar);
1171    assume(type == n_transExt);
1172     
1173    (void)type;
1174  }
1175#endif
1176
1177// random generator: -----------------------------------------------
1178  int t=initTimer();
1179  if (t==0) t=1;
1180  initRTimer();
1181  siSeed=t;
1182#ifdef HAVE_FACTORY
1183  factoryseed(t);
1184#endif
1185  siRandomStart=t;
1186  feOptSpec[FE_OPT_RANDOM].value = (void*) ((long)siRandomStart);
1187
1188// ressource table: ----------------------------------------------------
1189  // Don't worry: ifdef OM_NDEBUG, then all these calls are undef'ed
1190  // hack such that all shared' libs in the bindir are loaded correctly
1191  feInitResources(name);
1192
1193// singular links: --------------------------------------------------
1194  slStandardInit();
1195  myynest=0;
1196
1197// loading standard.lib -----------------------------------------------
1198  if (! feOptValue(FE_OPT_NO_STDLIB))
1199  {
1200    BITSET save1,save2;
1201    SI_SAVE_OPT(save1,save2);
1202    si_opt_2 &= ~Sy_bit(V_LOAD_LIB);
1203    iiLibCmd(omStrDup("standard.lib"), TRUE,TRUE,TRUE);
1204    SI_RESTORE_OPT(save1,save2);
1205  }
1206  errorreported = 0;
1207}
1208
1209/*
1210#ifdef LIBSINGULAR
1211#ifdef HAVE_FACTORY
1212// the init routines of factory need mmInit
1213int mmInit( void )
1214{
1215  return 1;
1216}
1217#endif
1218#endif
1219*/
Note: See TracBrowser for help on using the repository browser.