source: git/Singular/misc_ip.cc @ e7c7bf

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