source: git/Singular/misc_ip.cc @ b38bc9

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