source: git/Singular/misc_ip.cc @ 1b2216

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