source: git/Singular/misc_ip.cc @ 22303d

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