source: git/Singular/ipshell.cc @ 41cf23

spielwiese
Last change on this file since 41cf23 was 41cf23, checked in by Hans Schönemann <hannes@…>, 19 years ago
*hannes: memory leak fixed: nuLagSolve git-svn-id: file:///usr/local/Singular/svn/trunk@7684 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 101.2 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: ipshell.cc,v 1.95 2005-01-24 15:04:39 Singular Exp $ */
5/*
6* ABSTRACT:
7*/
8
9//#include <stdlib.h>
10#include <stdio.h>
11#include <string.h>
12#include <ctype.h>
13#include <math.h>
14
15#include "mod2.h"
16#include "tok.h"
17#include "ipid.h"
18#include "intvec.h"
19#include "omalloc.h"
20#include "febase.h"
21#include "polys.h"
22#include "ideals.h"
23#include "matpol.h"
24#include "kstd1.h"
25#include "ring.h"
26#include "subexpr.h"
27#include "maps.h"
28#include "syz.h"
29#include "numbers.h"
30#include "lists.h"
31#include "attrib.h"
32#include "ipconv.h"
33#include "silink.h"
34#include "stairc.h"
35#include "weight.h"
36#include "semic.h"
37#include "splist.h"
38#include "spectrum.h"
39#include "gnumpfl.h"
40#include "mpr_base.h"
41#include "ffields.h"
42#include "clapsing.h"
43#include "hutil.h"
44#include "ipshell.h"
45#ifdef HAVE_FACTORY
46#define SI_DONT_HAVE_GLOBAL_VARS
47#include <factory.h>
48#endif
49
50// define this if you want to use the fast_map routine for mapping ideals
51#define FAST_MAP
52
53#ifdef FAST_MAP
54#include "fast_maps.h"
55#endif
56
57leftv iiCurrArgs=NULL;
58int  traceit = 0;
59char *lastreserved=NULL;
60
61int  myynest = -1;
62
63static BOOLEAN iiNoKeepRing=TRUE;
64
65/*0 implementation*/
66
67char * Tok2Cmdname(int tok)
68{
69  int i = 0;
70  if (tok < 0)
71  {
72    return cmds[0].name;
73  }
74  if (tok==ANY_TYPE) return "any_type";
75  if (tok==NONE) return "nothing";
76  //if (tok==IFBREAK) return "if_break";
77  //if (tok==VECTOR_FROM_POLYS) return "vector_from_polys";
78  //if (tok==ORDER_VECTOR) return "ordering";
79  //if (tok==REF_VAR) return "ref";
80  //if (tok==OBJECT) return "object";
81  //if (tok==PRINT_EXPR) return "print_expr";
82  if (tok==IDHDL) return "identifier";
83  while (cmds[i].tokval!=0)
84  {
85    if ((cmds[i].tokval == tok)&&(cmds[i].alias==0))
86    {
87      return cmds[i].name;
88    }
89    i++;
90  }
91  return cmds[0].name;
92}
93
94char * iiTwoOps(int t)
95{
96  if (t<127)
97  {
98    static char ch[2];
99    switch (t)
100    {
101      case '&':
102        return "and";
103      case '|':
104        return "or";
105      default:
106        ch[0]=t;
107        ch[1]='\0';
108        return ch;
109    }
110  }
111  switch (t)
112  {
113    case COLONCOLON:  return "::";
114    case DOTDOT:      return "..";
115    //case PLUSEQUAL:   return "+=";
116    //case MINUSEQUAL:  return "-=";
117    case MINUSMINUS:  return "--";
118    case PLUSPLUS:    return "++";
119    case EQUAL_EQUAL: return "==";
120    case LE:          return "<=";
121    case GE:          return ">=";
122    case NOTEQUAL:    return "<>";
123    default:          return Tok2Cmdname(t);
124  }
125}
126
127static void list1(char* s, idhdl h,BOOLEAN c, BOOLEAN fullname)
128{
129  char buffer[22];
130  int l;
131  char buf2[128];
132
133  if(fullname) sprintf(buf2, "%s::%s", "", IDID(h));
134  else sprintf(buf2, "%s", IDID(h));
135
136  Print("%s%-20.20s [%d]  ",s,buf2,IDLEV(h));
137  if (h == currRingHdl) PrintS("*");
138  PrintS(Tok2Cmdname((int)IDTYP(h)));
139
140  ipListFlag(h);
141  switch(IDTYP(h))
142  {
143    case INT_CMD:   Print(" %d",IDINT(h)); break;
144    case INTVEC_CMD:Print(" (%d)",IDINTVEC(h)->length()); break;
145    case INTMAT_CMD:Print(" %d x %d",IDINTVEC(h)->rows(),IDINTVEC(h)->cols());
146                    break;
147    case POLY_CMD:
148    case VECTOR_CMD:if (c)
149                    {
150                      PrintS(" ");wrp(IDPOLY(h));
151                      if(IDPOLY(h) != NULL)
152                      {
153                        Print(", %d monomial(s)",pLength(IDPOLY(h)));
154                      }
155                    }
156                    break;
157    case MODUL_CMD: Print(", rk %d", IDIDEAL(h)->rank);
158    case IDEAL_CMD: Print(", %u generator(s)",
159                    IDELEMS(IDIDEAL(h)),IDIDEAL(h)->rank); break;
160    case MAP_CMD:
161                    Print(" from %s",IDMAP(h)->preimage); break;
162    case MATRIX_CMD:Print(" %u x %u"
163                      ,MATROWS(IDMATRIX(h))
164                      ,MATCOLS(IDMATRIX(h))
165                    );
166                    break;
167    case PACKAGE_CMD:
168                    PrintS(" (");
169                    switch (IDPACKAGE(h)->language)
170                    {
171                        case LANG_SINGULAR: PrintS("S"); break;
172                        case LANG_C:        PrintS("C"); break;
173                        case LANG_TOP:      PrintS("T"); break;
174                        case LANG_NONE:     PrintS("N"); break;
175                        default:            PrintS("U");
176                    }
177                    if(IDPACKAGE(h)->libname!=NULL)
178                      Print(",%s", IDPACKAGE(h)->libname);
179                    PrintS(")");
180                    break;
181    case PROC_CMD: if(strlen(IDPROC(h)->libname)>0)
182                     Print(" from %s",IDPROC(h)->libname);
183                   if(IDPROC(h)->is_static)
184                     PrintS(" (static)");
185                   break;
186    case STRING_CMD:
187                   {
188                     char *s;
189                     l=strlen(IDSTRING(h));
190                     memset(buffer,0,22);
191                     strncpy(buffer,IDSTRING(h),min(l,20));
192                     if ((s=strchr(buffer,'\n'))!=NULL)
193                     {
194                       *s='\0';
195                     }
196                     PrintS(" ");
197                     PrintS(buffer);
198                     if((s!=NULL) ||(l>20))
199                     {
200                       Print("..., %d char(s)",l);
201                     }
202                     break;
203                   }
204    case LIST_CMD: Print(", size: %d",IDLIST(h)->nr+1);
205                   break;
206    case QRING_CMD:
207    case RING_CMD:
208                   if ((IDRING(h)==currRing) && (currRingHdl!=h))
209                     PrintS("(*)"); /* this is an alias to currRing */
210#ifdef RDEBUG
211                   if (traceit &TRACE_SHOW_RINGS)
212                     Print(" <%x>",IDRING(h));
213#endif
214                   break;
215    /*default:     break;*/
216  }
217  PrintLn();
218}
219
220void type_cmd(idhdl h)
221{
222  BOOLEAN oldShortOut = FALSE;
223
224  if (currRing != NULL)
225  {
226    oldShortOut = currRing->ShortOut;
227    currRing->ShortOut = 1;
228  }
229  list1("// ",h,FALSE,FALSE);
230  if (IDTYP(h)!=INT_CMD)
231  {
232    sleftv expr;
233    memset(&expr,0,sizeof(expr));
234    expr.rtyp=IDHDL;
235    expr.name=IDID(h);
236    expr.data=(void *)h;
237    expr.Print();
238  }
239  if (currRing != NULL)
240    currRing->ShortOut = oldShortOut;
241}
242
243static void killlocals0(int v, idhdl * localhdl)
244{
245  idhdl h = *localhdl;
246  while (h!=NULL)
247  {
248    int vv;
249    //Print("consider %s, lev: %d:",IDID(h),IDLEV(h));
250    if ((vv=IDLEV(h))>0)
251    {
252      if (vv < v)
253      {
254        if (iiNoKeepRing)
255        {
256          //PrintS(" break\n");
257          return;
258        }
259        h = IDNEXT(h);
260        //PrintLn();
261      }
262      else if (vv >= v)
263      {
264        idhdl nexth = IDNEXT(h);
265        killhdl2(h,localhdl,currRing);
266        h = nexth;
267        //PrintS("kill\n");
268      }
269    }
270    else
271    {
272      h = IDNEXT(h);
273      //PrintLn();
274    }
275  }
276}
277#ifndef HAVE_NS
278void killlocals(int v)
279{
280  killlocals0(v,&IDROOT);
281
282  if ((iiRETURNEXPR_len > myynest)
283  && ((iiRETURNEXPR[myynest].Typ()==RING_CMD)
284    || (iiRETURNEXPR[myynest].Typ()==QRING_CMD)))
285  {
286    leftv h=&iiRETURNEXPR[myynest];
287    killlocals0(v,&(((ring)h->data)->idroot));
288  }
289
290  idhdl sh=currRingHdl;
291  ring sr=currRing;
292  BOOLEAN changed=FALSE;
293  idhdl h = IDROOT;
294
295//  Print("killlocals in %s\n",IDID(currPackHdl));
296  while (h!=NULL)
297  {
298    if (((IDTYP(h)==QRING_CMD) || (IDTYP(h) == RING_CMD))
299    && (IDRING(h)->idroot!=NULL))
300    {
301      if (IDRING(h)!=currRing) {changed=TRUE;rSetHdl(h);}
302      killlocals0(v,&(IDRING(h)->idroot));
303    }
304    else if (IDTYP(h) == PACKAGE_CMD)
305    {
306      killlocals0(v,&(IDPACKAGE(h)->idroot));
307    }
308    h = IDNEXT(h);
309  }
310  if (changed)
311  {
312    currRing=NULL;
313    currRingHdl=NULL;
314    if (sh!=NULL) rSetHdl(sh);
315    else if (sr!=NULL)
316    {
317      sh=rFindHdl(sr,NULL,NULL);
318      rSetHdl(sh);
319    }
320  }
321
322  if (myynest<=1) iiNoKeepRing=TRUE;
323  //Print("end killlocals  >= %d\n",v);
324  //listall();
325}
326#endif
327#ifdef HAVE_NS
328void killlocals_rec(idhdl *root,int v, ring r)
329{
330  idhdl h=*root;
331  while (h!=NULL)
332  {
333    if (IDLEV(h)>=v)
334    {
335//      Print("kill %s, lev %d for lev %d\n",IDID(h),IDLEV(h),v);
336      idhdl n=IDNEXT(h);
337      killhdl2(h,root,r);
338      h=n;
339    }
340    else if (IDTYP(h)==PACKAGE_CMD)
341    {
342 //     Print("into pack %s, lev %d for lev %d\n",IDID(h),IDLEV(h),v);
343      if (IDPACKAGE(h)!=basePack)
344        killlocals_rec(&(IDRING(h)->idroot),v,r);
345      h=IDNEXT(h);
346    }
347    else if ((IDTYP(h)==RING_CMD)
348    ||(IDTYP(h)==QRING_CMD))
349    {
350      if (IDRING(h)->idroot!=NULL)
351      {
352  //    Print("into ring %s, lev %d for lev %d\n",IDID(h),IDLEV(h),v);
353        killlocals_rec(&(IDRING(h)->idroot),v,IDRING(h));
354      }
355      h=IDNEXT(h);
356    }
357    else
358    {
359//      Print("skip %s lev %d for lev %d\n",IDID(h),IDLEV(h),v);
360      h=IDNEXT(h);
361    }
362  }
363}
364BOOLEAN killlocals_list(int v, lists L)
365{
366  if (L==NULL) return FALSE;
367  BOOLEAN changed=FALSE;
368  int n=L->nr;
369  for(;n>=0;n--)
370  {
371    leftv h=&(L->m[n]);
372    void *d=h->data;
373    if (((h->rtyp==RING_CMD) || (h->rtyp==QRING_CMD))
374    && (((ring)d)->idroot!=NULL))
375    {
376      if (d!=currRing) {changed=TRUE;rChangeCurrRing((ring)d);}
377      killlocals0(v,&(((ring)h->data)->idroot));
378    }
379    else if (h->rtyp==LIST_CMD)
380      changed|=killlocals_list(v,(lists)d);
381  }
382  return changed;
383}
384void killlocals(int v)
385{
386  BOOLEAN changed=FALSE;
387  idhdl sh=currRingHdl;
388  ring cr=currRing;
389  if (sh!=NULL) changed=((IDLEV(sh)<v) || (IDRING(sh)->ref>0));
390  //if (changed) Print("currRing=%s(%x), lev=%d,ref=%d\n",IDID(sh),IDRING(sh),IDLEV(sh),IDRING(sh)->ref);
391
392  killlocals_rec(&(basePack->idroot),v,currRing);
393
394  if (iiRETURNEXPR_len > myynest)
395  {
396    int t=iiRETURNEXPR[myynest].Typ();
397    if ((/*iiRETURNEXPR[myynest].Typ()*/ t==RING_CMD)
398    || (/*iiRETURNEXPR[myynest].Typ()*/ t==QRING_CMD))
399    {
400      leftv h=&iiRETURNEXPR[myynest];
401      if (((ring)h->data)->idroot!=NULL)
402        killlocals0(v,&(((ring)h->data)->idroot));
403    }
404    else if (/*iiRETURNEXPR[myynest].Typ()*/ t==LIST_CMD)
405    {
406      leftv h=&iiRETURNEXPR[myynest];
407      changed |=killlocals_list(v,(lists)h->data);
408    }
409  }
410  if (changed)
411  {
412    currRing=cr;
413    currRingHdl=rFindHdl(cr,NULL,NULL);
414    if (currRingHdl==NULL) currRingHdl=sh;
415  }
416
417  if (myynest<=1) iiNoKeepRing=TRUE;
418  //Print("end killlocals  >= %d\n",v);
419  //listall();
420}
421#endif
422
423void list_cmd(int typ, const char* what, char *prefix,BOOLEAN iterate, BOOLEAN fullname)
424{
425  idhdl h,start;
426  BOOLEAN all = typ<0;
427  BOOLEAN really_all=FALSE;
428  BOOLEAN do_packages=FALSE;
429
430  if ( typ == -1 ) do_packages=TRUE;
431  if ( typ==0 )
432  {
433    if (strcmp(what,"all")==0)
434    {
435      really_all=TRUE;
436#ifdef HAVE_NS
437      h=basePack->idroot;
438#else
439      h=IDROOT;
440#endif
441    }
442    else
443    {
444      h = ggetid(what);
445      if (h!=NULL)
446      {
447        if (iterate) list1(prefix,h,TRUE,fullname);
448        if ((IDTYP(h)==RING_CMD)
449            || (IDTYP(h)==QRING_CMD)
450#ifdef HAVE_NS
451            //|| (IDTYP(h)==PACKE_CMD)
452#endif
453        )
454        {
455          h=IDRING(h)->idroot;
456        }
457        else if((IDTYP(h)==PACKAGE_CMD) || (IDTYP(h)==POINTER_CMD))
458        {
459          Print("list_cmd:package or pointer\n");
460          all=TRUE;typ=PROC_CMD;fullname=TRUE;really_all=TRUE;
461          h=IDPACKAGE(h)->idroot;
462        }
463        else
464          return;
465      }
466      else
467      {
468        Werror("%s is undefined",what);
469        return;
470      }
471    }
472    all=TRUE;
473  }
474  else if (RingDependend(typ))
475  {
476    h = currRing->idroot;
477  }
478  else
479    h = IDROOT;
480  start=h;
481  while (h!=NULL)
482  {
483    if ((all && (IDTYP(h)!=PROC_CMD) &&(IDTYP(h)!=PACKAGE_CMD))
484    || (typ == IDTYP(h))
485    || ((IDTYP(h)==QRING_CMD) && (typ==RING_CMD)))
486    {
487      list1(prefix,h,start==currRingHdl, fullname);
488      if (((IDTYP(h)==RING_CMD)||(IDTYP(h)==QRING_CMD))
489        && (really_all || (all && (h==currRingHdl)))
490        && ((IDLEV(h)==0)||(IDLEV(h)==myynest)))
491      {
492        list_cmd(0,IDID(h),"//      ",FALSE);
493      }
494#ifdef HAVE_NS
495      if (IDTYP(h)==PACKAGE_CMD && really_all)
496      {
497        list_cmd(0,IDID(h),"//      ",FALSE);
498      }
499#endif /* HAVE_NS */
500    }
501    h = IDNEXT(h);
502  }
503}
504
505void test_cmd(int i)
506{
507  int ii=(char)i;
508
509  if (i == (-32))
510  {
511    test = 0;
512  }
513  else
514  {
515    if (i<0)
516    {
517      ii= -i;
518      if (Sy_bit(ii) & kOptions)
519      {
520        Warn("Gerhard, use the option command");
521        test &= ~Sy_bit(ii);
522      }
523      else if (Sy_bit(ii) & validOpts)
524        test &= ~Sy_bit(ii);
525    }
526    else if (i<32)
527    {
528      if (Sy_bit(ii) & kOptions)
529      {
530        Warn("Gerhard, use the option command");
531        test |= Sy_bit(ii);
532      }
533      else if (Sy_bit(ii) & validOpts)
534        test |= Sy_bit(ii);
535    }
536  }
537}
538
539int exprlist_length(leftv v)
540{
541  int rc = 0;
542  while (v!=NULL)
543  {
544    switch (v->Typ())
545    {
546      case INT_CMD:
547      case POLY_CMD:
548      case VECTOR_CMD:
549      case NUMBER_CMD:
550        rc++;
551        break;
552      case INTVEC_CMD:
553      case INTMAT_CMD:
554        rc += ((intvec *)(v->Data()))->length();
555        break;
556      case MATRIX_CMD:
557      case IDEAL_CMD:
558      case MODUL_CMD:
559        {
560          matrix mm = (matrix)(v->Data());
561          rc += mm->rows() * mm->cols();
562        }
563        break;
564      case LIST_CMD:
565        rc+=((lists)v->Data())->nr+1;
566        break;
567      default:
568        rc++;
569    }
570    v = v->next;
571  }
572  return rc;
573}
574
575int IsPrime(int p)  /* brute force !!!! */
576{
577  int i,j;
578  if      (p == 0)    return 0;
579  else if (p == 1)    return 1/*1*/;
580  else if (p == 2)    return p;
581  else if (p < 0)     return (-IsPrime(-p));
582  else if (!(p & 1)) return IsPrime(p-1);
583#ifdef HAVE_FACTORY
584  else if (p<=32749) // max. small prime in factory
585  {
586    int a=0;
587    int e=cf_getNumSmallPrimes()-1;
588    i=e/2;
589    do
590    {
591      if (p==(j=cf_getSmallPrime(i))) return p;
592      if (p<j) e=i-1;
593      else     a=i+1;
594      i=a+(e-a)/2;
595    } while ( a<= e);
596    if (p>j) return j;
597    else     return cf_getSmallPrime(i-1);
598  }
599#endif
600#ifdef HAVE_FACTORY
601  int end_i=cf_getNumSmallPrimes()-1;
602#else
603  int end_i=p/2;
604#endif 
605  int end_p=(int)sqrt((double)p);
606restart:
607  for (i=0; i<end_i; i++)
608  {
609#ifdef HAVE_FACTORY
610    j=cf_getSmallPrime(i);
611#else
612    if (i==0) j=2;
613    else j=2*i-1;
614#endif   
615    if ((p%j) == 0)
616    {
617    #ifdef HAVE_FACTORY
618      if (p<=32751) return IsPrime(p-2);
619    #endif 
620      p-=2;
621      goto restart;
622    }
623    if (j > end_p) return p;
624  }
625  return p;
626}
627
628BOOLEAN iiWRITE(leftv res,leftv v)
629{
630  sleftv vf;
631  if (iiConvert(v->Typ(),LINK_CMD,iiTestConvert(v->Typ(),LINK_CMD),v,&vf))
632  {
633    WerrorS("link expected");
634    return TRUE;
635  }
636  si_link l=(si_link)vf.Data();
637  if (vf.next == NULL)
638  {
639    WerrorS("write: need at least two arguments");
640    return TRUE;
641  }
642
643  BOOLEAN b=slWrite(l,vf.next); /* iiConvert preserves next */
644  if (b)
645  {
646    const char *s;
647    if ((l!=NULL)&&(l->name!=NULL)) s=l->name;
648    else                            s=sNoName;
649    Werror("cannot write to %s",s);
650  }
651  vf.CleanUp();
652  return b;
653}
654
655leftv iiMap(map theMap, char * what)
656{
657  idhdl w,r;
658  leftv v;
659  int i;
660  nMapFunc nMap;
661
662  r=IDROOT->get(theMap->preimage,myynest);
663#ifdef HAVE_NS
664  if ((currPack!=basePack)
665  &&((r==NULL) || ((r->typ != RING_CMD) && (r->typ != QRING_CMD))))
666    r=basePack->idroot->get(theMap->preimage,myynest);
667  if ((r==NULL) && (currRingHdl!=NULL)
668  && (strcmp(theMap->preimage,IDID(currRingHdl))==0))
669  {
670    r=currRingHdl;
671  }
672#endif /* HAVE_NS */
673  if ((r!=NULL) && ((r->typ == RING_CMD) || (r->typ== QRING_CMD)))
674  {
675    //if ((nMap=nSetMap(rInternalChar(IDRING(r)),
676    //             IDRING(r)->parameter,
677    //             rPar(IDRING(r)),
678    //             IDRING(r)->minpoly)))
679    if ((nMap=nSetMap(IDRING(r)))==NULL)
680    {
681      if (rEqual(IDRING(r),currRing))
682      {
683        nMap=nCopy;
684      }
685      else
686      {
687        Werror("can not map from ground field of %s to current ground field",
688          theMap->preimage);
689        return NULL;
690      }
691    }
692    if (IDELEMS(theMap)<IDRING(r)->N)
693    {
694      theMap->m=(polyset)omReallocSize((ADDRESS)theMap->m,
695                                 IDELEMS(theMap)*sizeof(poly),
696                                 (IDRING(r)->N)*sizeof(poly));
697      for(i=IDELEMS(theMap);i<IDRING(r)->N;i++)
698        theMap->m[i]=NULL;
699      IDELEMS(theMap)=IDRING(r)->N;
700    }
701    if (what==NULL)
702    {
703      WerrorS("argument of a map must have a name");
704    }
705    else if ((w=IDRING(r)->idroot->get(what,myynest))!=NULL)
706    {
707      v=(leftv)omAlloc0Bin(sleftv_bin);
708      sleftv tmpW;
709      memset(&tmpW,0,sizeof(sleftv));
710      tmpW.rtyp=IDTYP(w);
711      tmpW.data=IDDATA(w);
712      #ifdef FAST_MAP
713      if ((tmpW.rtyp==IDEAL_CMD) && (nMap==nCopy)
714      #ifdef HAVE_PLURAL
715      && (!rIsPluralRing(currRing))
716      #endif
717      )
718      {
719        v->rtyp=IDEAL_CMD;
720        v->data=fast_map(IDIDEAL(w), IDRING(r), (ideal)theMap, currRing);
721      }
722      else
723      #endif
724      if (maApplyFetch(MAP_CMD,theMap,v,&tmpW,IDRING(r),NULL,NULL,0,nMap))
725      {
726        Werror("cannot map %s(%d)",Tok2Cmdname(w->typ),w->typ);
727        omFreeBin((ADDRESS)v, sleftv_bin);
728        return NULL;
729      }
730      return v;
731    }
732    else
733    {
734      Werror("%s undefined in %s",what,theMap->preimage);
735    }
736  }
737  else
738  {
739    Werror("cannot find preimage %s",theMap->preimage);
740  }
741  return NULL;
742}
743
744#ifdef OLD_RES
745void  iiMakeResolv(resolvente r, int length, int rlen, char * name, int typ0,
746                   intvec ** weights)
747{
748  lists L=liMakeResolv(r,length,rlen,typ0,weights);
749  int i=0;
750  idhdl h;
751  char * s=(char *)omAlloc(strlen(name)+5);
752
753  while (i<=L->nr)
754  {
755    sprintf(s,"%s(%d)",name,i+1);
756    if (i==0)
757      h=enterid(s,myynest,typ0,&(currRing->idroot), FALSE);
758    else
759      h=enterid(s,myynest,MODUL_CMD,&(currRing->idroot), FALSE);
760    if (h!=NULL)
761    {
762      h->data.uideal=(ideal)L->m[i].data;
763      h->attribute=L->m[i].attribute;
764      if (BVERBOSE(V_DEF_RES))
765        Print("//defining: %s as %d-th syzygy module\n",s,i+1);
766    }
767    else
768    {
769      idDelete((ideal *)&(L->m[i].data));
770      Warn("cannot define %s",s);
771    }
772    //L->m[i].data=NULL;
773    //L->m[i].rtyp=0;
774    //L->m[i].attribute=NULL;
775    i++;
776  }
777  omFreeSize((ADDRESS)L->m,(L->nr+1)*sizeof(sleftv));
778  omFreeBin((ADDRESS)L, slists_bin);
779  omFreeSize((ADDRESS)s,strlen(name)+5);
780}
781#endif
782
783//resolvente iiFindRes(char * name, int * len, int *typ0)
784//{
785//  char *s=(char *)omAlloc(strlen(name)+5);
786//  int i=-1;
787//  resolvente r;
788//  idhdl h;
789//
790//  do
791//  {
792//    i++;
793//    sprintf(s,"%s(%d)",name,i+1);
794//    h=currRing->idroot->get(s,myynest);
795//  } while (h!=NULL);
796//  *len=i-1;
797//  if (*len<=0)
798//  {
799//    Werror("no objects %s(1),.. found",name);
800//    omFreeSize((ADDRESS)s,strlen(name)+5);
801//    return NULL;
802//  }
803//  r=(ideal *)omAlloc(/*(len+1)*/ i*sizeof(ideal));
804//  memset(r,0,(*len)*sizeof(ideal));
805//  i=-1;
806//  *typ0=MODUL_CMD;
807//  while (i<(*len))
808//  {
809//    i++;
810//    sprintf(s,"%s(%d)",name,i+1);
811//    h=currRing->idroot->get(s,myynest);
812//    if (h->typ != MODUL_CMD)
813//    {
814//      if ((i!=0) || (h->typ!=IDEAL_CMD))
815//      {
816//        Werror("%s is not of type module",s);
817//        omFreeSize((ADDRESS)r,(*len)*sizeof(ideal));
818//        omFreeSize((ADDRESS)s,strlen(name)+5);
819//        return NULL;
820//      }
821//      *typ0=IDEAL_CMD;
822//    }
823//    if ((i>0) && (idIs0(r[i-1])))
824//    {
825//      *len=i-1;
826//      break;
827//    }
828//    r[i]=IDIDEAL(h);
829//  }
830//  omFreeSize((ADDRESS)s,strlen(name)+5);
831//  return r;
832//}
833
834static resolvente iiCopyRes(resolvente r, int l)
835{
836  int i;
837  resolvente res=(ideal *)omAlloc0((l+1)*sizeof(ideal));
838
839  for (i=0; i<l; i++)
840    res[i]=idCopy(r[i]);
841  return res;
842}
843
844BOOLEAN jjMINRES(leftv res, leftv v)
845{
846  int len=0;
847  int typ0;
848  resolvente rr=liFindRes((lists)v->Data(),&len,&typ0);
849  if (rr==NULL) return TRUE;
850  resolvente r=iiCopyRes(rr,len);
851
852  syMinimizeResolvente(r,len,0);
853  omFreeSize((ADDRESS)rr,len*sizeof(ideal));
854  len++;
855  res->data=(char *)liMakeResolv(r,len,-1,typ0,NULL);
856  return FALSE;
857}
858
859BOOLEAN jjBETTI(leftv res, leftv u)
860{
861  sleftv tmp;
862  memset(&tmp,0,sizeof(tmp));
863  tmp.rtyp=INT_CMD;
864  tmp.data=(void *)1; 
865  if ((u->Typ()==IDEAL_CMD)
866  || (u->Typ()==MODUL_CMD))
867    return jjBETTI2_ID(res,u,&tmp);
868  else
869    return jjBETTI2(res,u,&tmp);
870}
871
872BOOLEAN jjBETTI2_ID(leftv res, leftv u, leftv v)
873{
874  lists l=(lists) omAllocBin(slists_bin);
875  l->Init(1);
876  l->m[0].rtyp=u->Typ();
877  l->m[0].data=u->Data();
878  l->m[0].attribute=u->attribute;
879  sleftv tmp2;
880  memset(&tmp2,0,sizeof(tmp2));
881  tmp2.rtyp=LIST_CMD;
882  tmp2.data=(void *)l;
883  BOOLEAN r=jjBETTI2(res,&tmp2,v);
884  l->m[0].data=NULL;
885  l->m[0].attribute=NULL;
886  l->m[0].rtyp=DEF_CMD;
887  l->Clean();
888  return r;
889}
890
891BOOLEAN jjBETTI2(leftv res, leftv u, leftv v)
892{
893  resolvente r;
894  int len;
895  int reg,typ0;
896  lists l=(lists)u->Data();
897
898  intvec *weights=NULL;
899  int add_row_shift=0;
900  intvec *ww=(intvec *)atGet(&(l->m[0]),"isHomog",INTVEC_CMD); 
901  if (ww!=NULL)
902  {
903     weights=ivCopy(ww);
904     add_row_shift = ww->min_in();
905     (*weights) -= add_row_shift;
906  }
907  //Print("attr:%x\n",weights);
908
909  r=liFindRes(l,&len,&typ0);
910  if (r==NULL) return TRUE;
911  res->data=(char *)syBetti(r,len,&reg,weights,(int)v->Data());
912  omFreeSize((ADDRESS)r,(len)*sizeof(ideal));
913  if (add_row_shift!=0) 
914    atSet(res,omStrDup("rowShift"),(void*)add_row_shift,INT_CMD);
915  if (weights!=NULL) delete weights;
916  return FALSE;
917}
918
919int iiRegularity(lists L)
920{
921  int len,reg,typ0;
922
923  resolvente r=liFindRes(L,&len,&typ0);
924
925  if (r==NULL)
926    return -2;
927  intvec * dummy=syBetti(r,len,&reg);
928  omFreeSize((ADDRESS)r,len*sizeof(ideal));
929  delete dummy;
930  return reg+1;
931}
932
933BOOLEAN iiDebugMarker=TRUE;
934#define BREAK_LINE_LENGTH 80
935void iiDebug()
936{
937  Print("\n-- break point in %s --\n",VoiceName());
938  if (iiDebugMarker) VoiceBackTrack();
939  char * s;
940  iiDebugMarker=FALSE;
941  s = (char *)omAlloc(BREAK_LINE_LENGTH+4);
942  loop
943  {
944    memset(s,0,80);
945    fe_fgets_stdin("",s,BREAK_LINE_LENGTH);
946    if (s[BREAK_LINE_LENGTH-1]!='\0')
947    {
948      Print("line too long, max is %d chars\n",BREAK_LINE_LENGTH);
949    }
950    else
951      break;
952  }
953  if (*s=='\n')
954  {
955    iiDebugMarker=TRUE;
956  }
957#if MDEBUG
958  else if(strncmp(s,"cont;",5)==0)
959  {
960    iiDebugMarker=TRUE;
961  }
962#endif /* MDEBUG */
963  else
964  {
965    strcat( s, "\n;~\n");
966    newBuffer(s,BT_execute);
967  }
968}
969
970lists scIndIndset(ideal S, BOOLEAN all, ideal Q)
971{
972  int i;
973  indset save;
974  lists res=(lists)omAlloc0Bin(slists_bin);
975
976  hexist = hInit(S, Q, &hNexist);
977  if ((hNexist == 0) || (hisModule!=0))
978  {
979    res->Init(0);
980    return res;
981  }
982  save = ISet = (indset)omAlloc0Bin(indlist_bin);
983  hMu = 0;
984  hwork = (scfmon)omAlloc(hNexist * sizeof(scmon));
985  hvar = (varset)omAlloc((pVariables + 1) * sizeof(int));
986  hpure = (scmon)omAlloc((1 + (pVariables * pVariables)) * sizeof(Exponent_t));
987  hrad = hexist;
988  hNrad = hNexist;
989  radmem = hCreate(pVariables - 1);
990  hCo = pVariables + 1;
991  hNvar = pVariables;
992  hRadical(hrad, &hNrad, hNvar);
993  hSupp(hrad, hNrad, hvar, &hNvar);
994  if (hNvar)
995  {
996    hCo = hNvar;
997    memset(hpure, 0, (pVariables + 1) * sizeof(Exponent_t));
998    hPure(hrad, 0, &hNrad, hvar, hNvar, hpure, &hNpure);
999    hLexR(hrad, hNrad, hvar, hNvar);
1000    hDimSolve(hpure, hNpure, hrad, hNrad, hvar, hNvar);
1001  }
1002  if (hCo && (hCo < pVariables))
1003  {
1004    hIndMult(hpure, hNpure, hrad, hNrad, hvar, hNvar);
1005  }
1006  if (hMu!=0)
1007  {
1008    ISet = save;
1009    hMu2 = 0;
1010    if (all && (hCo+1 < pVariables))
1011    {
1012      JSet = (indset)omAlloc0Bin(indlist_bin);
1013      hIndAllMult(hpure, hNpure, hrad, hNrad, hvar, hNvar);
1014      i=hMu+hMu2;
1015      res->Init(i);
1016      if (hMu2 == 0)
1017      {
1018        omFreeBin((ADDRESS)JSet, indlist_bin);
1019      }
1020    }
1021    else
1022    {
1023      res->Init(hMu);
1024    }
1025    for (i=0;i<hMu;i++)
1026    {
1027      res->m[i].data = (void *)save->set;
1028      res->m[i].rtyp = INTVEC_CMD;
1029      ISet = save;
1030      save = save->nx;
1031      omFreeBin((ADDRESS)ISet, indlist_bin);
1032    }
1033    omFreeBin((ADDRESS)save, indlist_bin);
1034    if (hMu2 != 0)
1035    {
1036      save = JSet;
1037      for (i=hMu;i<hMu+hMu2;i++)
1038      {
1039        res->m[i].data = (void *)save->set;
1040        res->m[i].rtyp = INTVEC_CMD;
1041        JSet = save;
1042        save = save->nx;
1043        omFreeBin((ADDRESS)JSet, indlist_bin);
1044      }
1045      omFreeBin((ADDRESS)save, indlist_bin);
1046    }
1047  }
1048  else
1049  {
1050    res->Init(0);
1051    omFreeBin((ADDRESS)ISet,  indlist_bin);
1052  }
1053  hKill(radmem, pVariables - 1);
1054  omFreeSize((ADDRESS)hpure, (1 + (pVariables * pVariables)) * sizeof(Exponent_t));
1055  omFreeSize((ADDRESS)hvar, (pVariables + 1) * sizeof(int));
1056  omFreeSize((ADDRESS)hwork, hNexist * sizeof(scmon));
1057  hDelete(hexist, hNexist);
1058  return res;
1059}
1060
1061int iiDeclCommand(leftv sy, leftv name, int lev,int t, idhdl* root,BOOLEAN isring, BOOLEAN init_b)
1062{
1063  BOOLEAN res=FALSE;
1064  char *id = name->name;
1065
1066  memset(sy,0,sizeof(sleftv));
1067  if ((name->name==NULL)||(isdigit(name->name[0])))
1068  {
1069    WerrorS("object to declare is not a name");
1070    res=TRUE;
1071  }
1072  else
1073  {
1074    //if (name->rtyp!=0)
1075    //{
1076    //  Warn("`%s` is already in use",name->name);
1077    //}
1078    {
1079      sy->data = (char *)enterid(id,lev,t,root,init_b);
1080    }
1081    if (sy->data!=NULL)
1082    {
1083      sy->rtyp=IDHDL;
1084      currid=sy->name=IDID((idhdl)sy->data);
1085      // name->name=NULL; /* used in enterid */
1086      //sy->e = NULL;
1087      if (name->next!=NULL)
1088      {
1089        sy->next=(leftv)omAllocBin(sleftv_bin);
1090        res=iiDeclCommand(sy->next,name->next,lev,t,root, isring);
1091      }
1092    }
1093    else res=TRUE;
1094  }
1095  name->CleanUp();
1096  return res;
1097}
1098
1099BOOLEAN iiParameter(leftv p)
1100{
1101  if (iiCurrArgs==NULL)
1102  {
1103    if (strcmp(p->name,"#")==0) return FALSE;
1104    Werror("not enough arguments for proc %s",VoiceName());
1105    p->CleanUp();
1106    return TRUE;
1107  }
1108  leftv h=iiCurrArgs;
1109  if (strcmp(p->name,"#")==0)
1110  {
1111    iiCurrArgs=NULL;
1112  }
1113  else
1114  {
1115    iiCurrArgs=h->next;
1116    h->next=NULL;
1117  }
1118  BOOLEAN res=iiAssign(p,h);
1119  omFreeBin((ADDRESS)h, sleftv_bin);
1120  return res;
1121}
1122
1123static BOOLEAN iiInternalExport (leftv v, int toLev)
1124{
1125  idhdl h=(idhdl)v->data;
1126  //Print("iiInternalExport('%s',%d)%s\n", v->name, toLev,"");
1127  if (IDLEV(h)==0) Warn("`%s` is already global",IDID(h));
1128  else
1129  {
1130    h=IDROOT->get(v->name,toLev);
1131    idhdl *root=&IDROOT;
1132    if ((h==NULL)&&(currRing!=NULL))
1133    {
1134      h=currRing->idroot->get(v->name,toLev);
1135      root=&currRing->idroot;
1136    }
1137    BOOLEAN keepring=FALSE;
1138    if ((h!=NULL)&&(IDLEV(h)==toLev))
1139    {
1140      if (IDTYP(h)==v->Typ())
1141      {
1142        if (((IDTYP(h)==RING_CMD)||(IDTYP(h)==QRING_CMD))
1143        && (v->Data()==IDDATA(h)))
1144        {
1145          IDRING(h)->ref++;
1146          keepring=TRUE;
1147          IDLEV(h)=toLev;
1148          //WarnS("keepring");
1149          return FALSE;
1150        }
1151        if (BVERBOSE(V_REDEFINE))
1152        {
1153          Warn("redefining %s",IDID(h));
1154        }
1155#ifdef USE_IILOCALRING
1156        if (iiLocalRing[0]==IDRING(h) && (!keepring)) iiLocalRing[0]=NULL;
1157#else
1158        proclevel *p=procstack;
1159        while (p->next!=NULL) p=p->next;
1160        if ((p->cRing==IDRING(h)) && (!keepring))
1161        {
1162          p->cRing=NULL;
1163          p->cRingHdl=NULL;
1164        }
1165#endif
1166        killhdl2(h,root,currRing);
1167      }
1168      else
1169      {
1170        return TRUE;
1171      }
1172    }
1173    h=(idhdl)v->data;
1174    IDLEV(h)=toLev;
1175    if (keepring) IDRING(h)->ref--;
1176    iiNoKeepRing=FALSE;
1177    //Print("export %s\n",IDID(h));
1178  }
1179  return FALSE;
1180}
1181
1182#ifdef HAVE_NS
1183BOOLEAN iiInternalExport (leftv v, int toLev, idhdl roothdl)
1184{
1185  idhdl h=(idhdl)v->data;
1186  if(h==NULL)
1187  {
1188    Warn("'%s': no such identifier\n", v->name);
1189    return FALSE;
1190  }
1191  package frompack=v->req_packhdl; 
1192  if (frompack==NULL) frompack=currPack;
1193  package rootpack = IDPACKAGE(roothdl);
1194//  Print("iiInternalExport('%s',%d,%s->%s) typ:%d\n", v->name, toLev, IDID(currPackHdl),IDID(roothdl),v->Typ());
1195  if ((RingDependend(IDTYP(h)))
1196  || ((IDTYP(h)==LIST_CMD)
1197     && (lRingDependend(IDLIST(h)))
1198     )
1199  )
1200  {
1201    //Print("// ==> Ringdependent set nesting to 0\n");
1202    return (iiInternalExport(v, toLev));
1203  }
1204  else
1205  {
1206    IDLEV(h)=toLev;
1207    v->req_packhdl=rootpack;
1208    if (h==frompack->idroot)
1209    {
1210      frompack->idroot=h->next;
1211    }
1212    else
1213    {
1214      idhdl hh=frompack->idroot;
1215      while ((hh!=NULL) && (hh->next!=h))
1216        hh=hh->next;
1217      if ((hh!=NULL) && (hh->next==h))
1218        hh->next=h->next;
1219      else
1220      {
1221        Werror("`%s` not found",v->Name());
1222        return TRUE;
1223      }
1224    }
1225    h->next=rootpack->idroot;
1226    rootpack->idroot=h;
1227  }
1228  return FALSE;
1229}
1230#endif /* HAVE_NS */
1231
1232BOOLEAN iiExport (leftv v, int toLev)
1233{
1234#ifdef HAVE_NS
1235  checkall();
1236#endif
1237  BOOLEAN nok=FALSE;
1238  leftv r=v;
1239  while (v!=NULL)
1240  {
1241    if ((v->name==NULL)||(v->rtyp==0)||(v->e!=NULL))
1242    {
1243      WerrorS("cannot export");
1244      nok=TRUE;
1245    }
1246    else
1247    {
1248      if(iiInternalExport(v, toLev))
1249      {
1250        r->CleanUp();
1251        return TRUE;
1252      }
1253    }
1254    v=v->next;
1255  }
1256  r->CleanUp();
1257#ifdef HAVE_NS
1258  checkall();
1259#endif
1260  return nok;
1261}
1262
1263/*assume root!=idroot*/
1264#ifdef HAVE_NS
1265BOOLEAN iiExport (leftv v, int toLev, idhdl root)
1266{
1267  checkall();
1268  //  Print("iiExport1: pack=%s\n",IDID(root));
1269  BOOLEAN nok=FALSE;
1270  leftv rv=v;
1271  while (v!=NULL)
1272  {
1273    if ((v->name==NULL)||(v->rtyp==0)||(v->e!=NULL)
1274    )
1275    {
1276      WerrorS("cannot export");
1277      nok=TRUE;
1278    }
1279    else
1280    {
1281      idhdl old=root->get(v->name,toLev);
1282      if (old!=NULL)
1283      {
1284        if (IDTYP(old)==v->Typ())
1285        {
1286          if (BVERBOSE(V_REDEFINE))
1287          {
1288            Warn("redefining %s",IDID(old));
1289          }
1290          killhdl2(old,&root,currRing);
1291        }
1292        else
1293        {
1294          rv->CleanUp();
1295          return TRUE;
1296        }
1297      }
1298      //Print("iiExport: pack=%s\n",IDID(root));
1299      if(iiInternalExport(v, toLev, root))
1300      {
1301        rv->CleanUp();
1302        return TRUE;
1303      }
1304    }
1305    v=v->next;
1306  }
1307  rv->CleanUp();
1308  checkall();
1309  return nok;
1310}
1311#endif
1312
1313BOOLEAN iiCheckRing(int i)
1314{
1315  if (currRingHdl==NULL)
1316  {
1317    #ifdef SIQ
1318    if (siq<=0)
1319    {
1320    #endif
1321      if (RingDependend(i))
1322      {
1323        WerrorS("no ring active");
1324        return TRUE;
1325      }
1326    #ifdef SIQ
1327    }
1328    #endif
1329  }
1330  return FALSE;
1331}
1332
1333poly    iiHighCorner(ideal I, int ak)
1334{
1335  int i;
1336  if(!idIsZeroDim(I)) return NULL; // not zero-dim.
1337  poly po=NULL;
1338  if (currRing->OrdSgn== -1)
1339  {
1340    scComputeHC(I,currQuotient,ak,po);
1341    if (po!=NULL)
1342    {
1343      pGetCoeff(po)=nInit(1);
1344      for (i=pVariables; i>0; i--)
1345      {
1346        if (pGetExp(po, i) > 0) pDecrExp(po,i);
1347      }
1348      pSetComp(po,ak);
1349      pSetm(po);
1350    }
1351  }
1352  else
1353    po=pOne();
1354  return po;
1355}
1356
1357#ifdef HAVE_NS
1358void iiCheckPack(package &p)
1359{
1360  if (p==basePack) return;
1361
1362  idhdl t=basePack->idroot;
1363
1364  while ((t!=NULL) && (IDTYP(t)!=PACKAGE_CMD) && (IDPACKAGE(t)!=p)) t=t->next;
1365
1366  if (t==NULL)
1367  {
1368    WarnS("package not found\n");
1369    p=basePack;
1370  }
1371  return;
1372}
1373#endif
1374
1375idhdl rDefault(char *s)
1376{
1377  idhdl tmp=NULL;
1378
1379  if (s!=NULL) tmp = enterid(s, myynest, RING_CMD, &IDROOT);
1380  if (tmp==NULL) return NULL;
1381
1382  if (ppNoether!=NULL) pDelete(&ppNoether);
1383  if (sLastPrinted.RingDependend())
1384  {
1385    sLastPrinted.CleanUp();
1386    memset(&sLastPrinted,0,sizeof(sleftv));
1387  }
1388
1389  ring r = IDRING(tmp);
1390
1391  r->ch    = 32003;
1392  r->N     = 3;
1393  /*r->P     = 0; Alloc0 in idhdl::set, ipid.cc*/
1394  /*names*/
1395  r->names = (char **) omAlloc0(3 * sizeof(char_ptr));
1396  r->names[0]  = omStrDup("x");
1397  r->names[1]  = omStrDup("y");
1398  r->names[2]  = omStrDup("z");
1399  /*weights: entries for 3 blocks: NULL*/
1400  r->wvhdl = (int **)omAlloc0(3 * sizeof(int_ptr));
1401  /*order: dp,C,0*/
1402  r->order = (int *) omAlloc(3 * sizeof(int *));
1403  r->block0 = (int *)omAlloc0(3 * sizeof(int *));
1404  r->block1 = (int *)omAlloc0(3 * sizeof(int *));
1405  /* ringorder dp for the first block: var 1..3 */
1406  r->order[0]  = ringorder_dp;
1407  r->block0[0] = 1;
1408  r->block1[0] = 3;
1409  /* ringorder C for the second block: no vars */
1410  r->order[1]  = ringorder_C;
1411  /* the last block: everything is 0 */
1412  r->order[2]  = 0;
1413  /*polynomial ring*/
1414  r->OrdSgn    = 1;
1415
1416  /* complete ring intializations */
1417  rComplete(r);
1418  rSetHdl(tmp);
1419  return currRingHdl;
1420}
1421
1422idhdl rFindHdl(ring r, idhdl n, idhdl w)
1423{
1424  idhdl h=rSimpleFindHdl(r,IDROOT,n);
1425  if (h!=NULL)  return h;
1426#ifdef HAVE_NS
1427  if (IDROOT!=basePack->idroot) h=rSimpleFindHdl(r,basePack->idroot,n);
1428  if (h!=NULL)  return h;
1429  proclevel *p=procstack;
1430  while(p!=NULL)
1431  {
1432    if ((p->cPack!=basePack)
1433    && (p->cPack!=currPack))
1434      h=rSimpleFindHdl(r,p->cPack->idroot,n);
1435    if (h!=NULL)  return h;
1436    p=p->next;
1437  }
1438  idhdl tmp=basePack->idroot;
1439  while (tmp!=NULL)
1440  {
1441    if (IDTYP(tmp)==PACKAGE_CMD)
1442      h=rSimpleFindHdl(r,IDPACKAGE(tmp)->idroot,n);
1443    if (h!=NULL)  return h;
1444    tmp=IDNEXT(tmp);
1445  }
1446#endif
1447  return NULL;
1448}
1449
1450lists rDecompose(ring r)
1451{
1452  // 0: char/ cf - ring
1453  // 1: list (var)
1454  // 2: list (ord)
1455  // 3: qideal
1456  lists L=(lists)omAlloc0Bin(slists_bin);
1457  L->Init(4);
1458  // ----------------------------------------
1459  // 0: char/ cf - ring
1460  #if 0 /* TODO */
1461  if (rIsExtension(r))
1462    rDecomposeCF(&(L->m[0]),r);
1463  else
1464  #endif
1465  {
1466    L->m[0].rtyp=INT_CMD;
1467    L->m[0].data=(void *)r->ch;
1468  }
1469  // ----------------------------------------
1470  // 1: list (var)
1471  lists LL=(lists)omAlloc0Bin(slists_bin);
1472  LL->Init(r->N);
1473  int i;
1474  for(i=0; i<r->N; i++)
1475  {
1476    LL->m[i].rtyp=STRING_CMD;
1477    LL->m[i].data=(void *)omStrDup(r->names[i]);
1478  }
1479  L->m[1].rtyp=LIST_CMD;
1480  L->m[1].data=(void *)LL;
1481  // ----------------------------------------
1482  // 2: list (ord)
1483  LL=(lists)omAlloc0Bin(slists_bin);
1484  i=rBlocks(r)-1;
1485  LL->Init(i);
1486  i--;
1487  lists LLL;
1488  for(; i>=0; i--)
1489  {
1490    intvec *iv;
1491    int j;
1492    LL->m[i].rtyp=LIST_CMD;
1493    LLL=(lists)omAlloc0Bin(slists_bin);
1494    LLL->Init(2);
1495    LLL->m[0].rtyp=STRING_CMD;
1496    LLL->m[0].data=(void *)omStrDup(rSimpleOrdStr(r->order[i]));
1497    if (r->block1[i]-r->block0[i] >=0 )
1498    {
1499      j=r->block1[i]-r->block0[i];
1500      iv=new intvec(j+1);
1501      if ((r->wvhdl!=NULL) && (r->wvhdl[i]!=NULL))
1502      {
1503        for(;j>=0; j--) (*iv)[j]=r->wvhdl[i][j];
1504      }
1505      else switch (r->order[i])
1506      {
1507        case ringorder_dp:
1508        case ringorder_Dp:
1509        case ringorder_ds:
1510        case ringorder_Ds:
1511        case ringorder_lp:
1512          for(;j>=0; j--) (*iv)[j]=1;
1513          break;
1514        default: /* do nothing */;
1515      }
1516    }
1517    else
1518    {
1519      iv=new intvec(1);
1520    }
1521    LLL->m[1].rtyp=INTVEC_CMD;
1522    LLL->m[1].data=(void *)iv;
1523    LL->m[i].data=(void *)LLL;
1524  }
1525  L->m[2].rtyp=LIST_CMD;
1526  L->m[2].data=(void *)LL;
1527  // ----------------------------------------
1528  // 3: qideal
1529  L->m[3].rtyp=IDEAL_CMD;
1530  if (r->qideal==NULL)
1531    L->m[3].data=(void *)idInit(1,1);
1532  else
1533    L->m[3].data=(void *)idCopy(r->qideal);
1534  // ----------------------------------------
1535  return L;
1536}
1537
1538ring rCompose(lists  L)
1539{
1540  if (L->nr!=3) return NULL;
1541  // 0: char/ cf - ring
1542  // 1: list (var)
1543  // 2: list (ord)
1544  // 3: qideal
1545  ring R=(ring) omAlloc0Bin(sip_sring_bin);
1546  if (L->m[0].Typ()==INT_CMD)
1547  {
1548    R->ch=(int)L->m[0].Data();
1549  }
1550  else if (L->m[0].Typ()==LIST_CMD)
1551  {
1552    R->algring=rCompose((lists)L->m[0].Data());
1553    if (R->algring==NULL)
1554    {
1555      WerrorS("could not create rational function coefficient field");
1556      goto rCompose_err;
1557    }
1558    R->ch=R->algring->ch;
1559    R->parameter=R->algring->names;
1560    R->P=R->algring->N;
1561  }
1562  else
1563  {
1564    WerrorS("coefficient field must be described by `int` or `list`");
1565    goto rCompose_err;
1566  }
1567  // ------------------------- VARS ---------------------------
1568  if (L->m[1].Typ()==LIST_CMD)
1569  {
1570    lists v=(lists)L->m[1].Data();
1571    R->N = v->nr+1;
1572    R->names   = (char **)omAlloc0(R->N * sizeof(char_ptr));
1573    int i;
1574    for(i=0;i<R->N;i++)
1575    {
1576      if (v->m[i].Typ()==STRING_CMD)
1577        R->names[i]=omStrDup((char *)v->m[i].Data());
1578      else if (v->m[i].Typ()==POLY_CMD)
1579      {
1580        poly p=(poly)v->m[i].Data();
1581        int nr=pIsPurePower(p);
1582        if (nr>0)
1583          R->names[i]=omStrDup(currRing->names[nr-1]);
1584        else
1585        {
1586          Werror("var name %d must be a string or a ring variable",i+1);
1587          goto rCompose_err;
1588        }
1589      }
1590      else
1591      {
1592        Werror("var name %d must be `string`",i+1);
1593        goto rCompose_err;
1594      }
1595    }
1596  }
1597  else
1598  {
1599    WerrorS("variable must be given as `list`");
1600    goto rCompose_err;
1601  }
1602  // ------------------------ ORDER ------------------------------
1603  if (L->m[2].Typ()==LIST_CMD)
1604  {
1605    lists v=(lists)L->m[2].Data();
1606    int n= v->nr+2;
1607    int j;
1608    // initialize fields of R
1609    R->order=(int *)omAlloc0(n*sizeof(int));
1610    R->block0=(int *)omAlloc0(n*sizeof(int));
1611    R->block1=(int *)omAlloc0(n*sizeof(int));
1612    R->wvhdl=(int**)omAlloc0(n*sizeof(int_ptr));
1613    // init order, so that rBlocks works correctly
1614    for (j=0; j < n-2; j++)
1615      R->order[j] = (int) ringorder_unspec;
1616    // orderings
1617    R->OrdSgn=1;
1618    for(j=0;j<n-1;j++)
1619    {
1620    // todo: a(..), M
1621      if (v->m[j].Typ()!=LIST_CMD)
1622      {
1623        WerrorS("ordering must be list of lists");
1624        goto rCompose_err;
1625      }
1626      lists vv=(lists)v->m[j].Data();
1627      if ((vv->nr!=1)
1628      || (vv->m[0].Typ()!=STRING_CMD)
1629      || ((vv->m[1].Typ()!=INTVEC_CMD) && (vv->m[1].Typ()!=INT_CMD)))
1630      {
1631        WerrorS("ordering name must be a (string,intvec)");
1632        goto rCompose_err;
1633      }
1634      R->order[j]=rOrderName(omStrDup((char*)vv->m[0].Data())); // assume STRING
1635      if (j==0) R->block0[0]=1;
1636      else      R->block0[j]=R->block1[j-1]+1;
1637      intvec *iv;
1638      if (vv->m[1].Typ()==INT_CMD)
1639        iv=new intvec((int)vv->m[1].Data(),(int)vv->m[1].Data());
1640      else
1641        iv=ivCopy((intvec*)vv->m[1].Data()); //assume INTVEC
1642      R->block1[j]=max(R->block0[j],R->block0[j]+iv->length()-1);
1643      int i;
1644      switch (R->order[j])
1645      {
1646         case ringorder_ws:
1647         case ringorder_Ws:
1648            R->OrdSgn=-1;
1649         case ringorder_wp:
1650         case ringorder_Wp:
1651           R->wvhdl[j] =( int *)omAlloc((iv->length())*sizeof(int));
1652           for (i=0; i<iv->length();i++) R->wvhdl[j][i]=(*iv)[i];
1653           break;
1654         case ringorder_ls:
1655         case ringorder_ds:
1656         case ringorder_Ds:
1657           R->OrdSgn=-1;
1658         case ringorder_lp:
1659         case ringorder_dp:
1660         case ringorder_Dp:
1661         case ringorder_rp:
1662           break;
1663         case ringorder_S:
1664           break;
1665         case ringorder_c:
1666         case ringorder_C:
1667           R->block1[j]=R->block0[j]-1;
1668           break;
1669         case ringorder_aa:
1670         case ringorder_a:
1671           R->wvhdl[j] =( int *)omAlloc((iv->length())*sizeof(int));
1672           for (i=1; i<iv->length();i++) R->wvhdl[n][i-1]=(*iv)[i];
1673         // todo
1674           break;
1675         case ringorder_M:
1676         // todo
1677           break;
1678      }
1679    }
1680    // sanity check
1681    j=n-2;
1682    if ((R->order[j]==ringorder_c)
1683    || (R->order[j]==ringorder_C)) j--;
1684    if (R->block1[j] != R->N)
1685    {
1686      if (((R->order[j]==ringorder_dp) ||
1687           (R->order[j]==ringorder_ds) ||
1688           (R->order[j]==ringorder_Dp) ||
1689           (R->order[j]==ringorder_Ds) ||
1690           (R->order[j]==ringorder_rp) ||
1691           (R->order[j]==ringorder_lp) ||
1692           (R->order[j]==ringorder_ls))
1693          &&
1694            R->block0[j] <= R->N)
1695      {
1696        R->block1[j] = R->N;
1697      }
1698      else
1699      {
1700        Werror("ordering incomplete: size (%d) should be %d",R->block1[j],R->N);
1701        goto rCompose_err;
1702      }
1703    }
1704  }
1705  else
1706  {
1707    WerrorS("ordering must be given as `list`");
1708    goto rCompose_err;
1709  }
1710  // ------------------------ Q-IDEAL ------------------------
1711  if (L->m[3].Typ()==IDEAL_CMD)
1712  {
1713    ideal q=(ideal)L->m[3].Data();
1714    if (q->m[0]!=NULL)
1715      R->qideal=idCopy(q);
1716  }
1717  else
1718  {
1719    WerrorS("q-ideal must be given as `ideal`");
1720    goto rCompose_err;
1721  }
1722
1723  // todo
1724  rComplete(R);
1725  return R;
1726
1727rCompose_err:
1728  if (R->N>0)
1729  {
1730    int i;
1731    if (R->names!=NULL)
1732    {
1733      i=R->N;
1734      while (i>=0) { if (R->names[i]!=NULL) omFree(R->names[i]); i--; }
1735      omFree(R->names);
1736    }
1737  }
1738  if (R->order!=NULL) omFree(R->order);
1739  if (R->block0!=NULL) omFree(R->block0);
1740  if (R->block1!=NULL) omFree(R->block1);
1741  if (R->wvhdl!=NULL) omFree(R->wvhdl);
1742  omFree(R);
1743  return NULL;
1744}
1745
1746// from matpol.cc
1747
1748/*2
1749* compute the jacobi matrix of an ideal
1750*/
1751BOOLEAN mpJacobi(leftv res,leftv a)
1752{
1753  int     i,j;
1754  matrix result;
1755  ideal id=(ideal)a->Data();
1756
1757  result =mpNew(IDELEMS(id),pVariables);
1758  for (i=1; i<=IDELEMS(id); i++)
1759  {
1760    for (j=1; j<=pVariables; j++)
1761    {
1762      MATELEM(result,i,j) = pDiff(id->m[i-1],j);
1763    }
1764  }
1765  res->data=(char *)result;
1766  return FALSE;
1767}
1768
1769/*2
1770* returns the Koszul-matrix of degree d of a vectorspace with dimension n
1771* uses the first n entrees of id, if id <> NULL
1772*/
1773BOOLEAN mpKoszul(leftv res,leftv c/*ip*/, leftv b/*in*/, leftv id)
1774{
1775  int n=(int)b->Data();
1776  int d=(int)c->Data();
1777  int     k,l,sign,row,col;
1778  matrix  result;
1779  ideal temp;
1780  BOOLEAN bo;
1781  poly    p;
1782
1783  if ((d>n) || (d<1) || (n<1))
1784  {
1785    res->data=(char *)mpNew(1,1);
1786    return FALSE;
1787  }
1788  int *choise = (int*)omAlloc(d*sizeof(int));
1789  if (id==NULL)
1790    temp=idMaxIdeal(1);
1791  else
1792    temp=(ideal)id->Data();
1793
1794  k = binom(n,d);
1795  l = k*d;
1796  l /= n-d+1;
1797  result =mpNew(l,k);
1798  col = 1;
1799  idInitChoise(d,1,n,&bo,choise);
1800  while (!bo)
1801  {
1802    sign = 1;
1803    for (l=1;l<=d;l++)
1804    {
1805      if (choise[l-1]<=IDELEMS(temp))
1806      {
1807        p = pCopy(temp->m[choise[l-1]-1]);
1808        if (sign == -1) p = pNeg(p);
1809        sign *= -1;
1810        row = idGetNumberOfChoise(l-1,d,1,n,choise);
1811        MATELEM(result,row,col) = p;
1812      }
1813    }
1814    col++;
1815    idGetNextChoise(d,n,&bo,choise);
1816  }
1817  if (id==NULL) idDelete(&temp);
1818
1819  res->data=(char *)result;
1820  return FALSE;
1821}
1822
1823// from syz1.cc
1824/*2
1825* read out the Betti numbers from resolution
1826* (interpreter interface)
1827*/
1828BOOLEAN syBetti2(leftv res, leftv u, leftv w)
1829{
1830  syStrategy syzstr=(syStrategy)u->Data();
1831  BOOLEAN minim=(int)w->Data();
1832  int row_shift=0;
1833  int add_row_shift=0;
1834  intvec *weights=NULL;
1835  intvec *ww=(intvec *)atGet(u,"isHomog",INTVEC_CMD);
1836  if (ww!=NULL)
1837  {
1838     weights=ivCopy(ww);
1839     add_row_shift = ww->min_in();
1840     (*weights) -= add_row_shift;
1841  }
1842  res->data=(void *)syBettiOfComputation(syzstr,minim,&row_shift,weights);
1843  //row_shift += add_row_shift;
1844  //Print("row_shift=%d, add_row_shift=%d\n",row_shift,add_row_shift);
1845  atSet(res,omStrDup("rowShift"),(void*)add_row_shift,INT_CMD);
1846  return FALSE;
1847}
1848BOOLEAN syBetti1(leftv res, leftv u)
1849{
1850  sleftv tmp;
1851  memset(&tmp,0,sizeof(tmp));
1852  tmp.rtyp=INT_CMD;
1853  tmp.data=(void *)1; 
1854  return syBetti2(res,u,&tmp);
1855}
1856
1857/*3
1858* converts a resolution into a list of modules
1859*/
1860lists syConvRes(syStrategy syzstr,BOOLEAN toDel)
1861{
1862  if ((syzstr->fullres==NULL) && (syzstr->minres==NULL))
1863  {
1864    if (syzstr->hilb_coeffs==NULL)
1865    {
1866      syzstr->fullres = syReorder(syzstr->res,syzstr->length,syzstr);
1867    }
1868    else
1869    {
1870      syzstr->minres = syReorder(syzstr->orderedRes,syzstr->length,syzstr);
1871      syKillEmptyEntres(syzstr->minres,syzstr->length);
1872    }
1873  }
1874  resolvente tr;
1875  int typ0=IDEAL_CMD;
1876  if (syzstr->minres!=NULL)
1877    tr = syzstr->minres;
1878  else
1879    tr = syzstr->fullres;
1880  resolvente trueres=NULL;
1881  intvec ** w=NULL;
1882  if (syzstr->length>0)
1883  {
1884    trueres=(resolvente)omAlloc0((syzstr->length)*sizeof(ideal));
1885    for (int i=(syzstr->length)-1;i>=0;i--)
1886    {
1887      if (tr[i]!=NULL)
1888      {
1889        trueres[i] = idCopy(tr[i]);
1890      }
1891    }
1892    if (idRankFreeModule(trueres[0]) > 0)
1893      typ0 = MODUL_CMD;
1894    if (syzstr->weights!=NULL)
1895    {
1896      w = (intvec**)omAlloc0((syzstr->length)*sizeof(intvec*));
1897      for (int i=(syzstr->length)-1;i>=0;i--)
1898      {
1899        if (syzstr->weights[i]!=NULL) w[i] = ivCopy(syzstr->weights[i]);
1900      }
1901    }
1902  }
1903  lists li = liMakeResolv(trueres,syzstr->length,syzstr->list_length,typ0,w);
1904  if (w != NULL) omFreeSize(w, (syzstr->length)*sizeof(intvec*));
1905  if (toDel) syKillComputation(syzstr);
1906  return li;
1907}
1908
1909/*3
1910* converts a list of modules into a resolution
1911*/
1912syStrategy syConvList(lists li,BOOLEAN toDel)
1913{
1914  int typ0;
1915  syStrategy result=(syStrategy)omAlloc0(sizeof(ssyStrategy));
1916
1917  resolvente fr = liFindRes(li,&(result->length),&typ0,&(result->weights));
1918  if (fr != NULL)
1919  {
1920
1921    result->fullres = (resolvente)omAlloc0((result->length+1)*sizeof(ideal));
1922    for (int i=result->length-1;i>=0;i--)
1923    {
1924      if (fr[i]!=NULL)
1925        result->fullres[i] = idCopy(fr[i]);
1926    }
1927    result->list_length=result->length;
1928    omFreeSize((ADDRESS)fr,(result->length)*sizeof(ideal));
1929  }
1930  else
1931  {
1932    omFreeSize(result, sizeof(ssyStrategy));
1933    result = NULL;
1934  }
1935  if (toDel) li->Clean();
1936  return result;
1937}
1938
1939/*3
1940* converts a list of modules into a minimal resolution
1941*/
1942syStrategy syForceMin(lists li)
1943{
1944  int typ0;
1945  syStrategy result=(syStrategy)omAlloc0(sizeof(ssyStrategy));
1946
1947  resolvente fr = liFindRes(li,&(result->length),&typ0);
1948  result->minres = (resolvente)omAlloc0((result->length+1)*sizeof(ideal));
1949  for (int i=result->length-1;i>=0;i--)
1950  {
1951    if (fr[i]!=NULL)
1952      result->minres[i] = idCopy(fr[i]);
1953  }
1954  omFreeSize((ADDRESS)fr,(result->length)*sizeof(ideal));
1955  return result;
1956}
1957// from weight.cc
1958BOOLEAN kWeight(leftv res,leftv id)
1959{
1960  ideal F=(ideal)id->Data();
1961  intvec * iv = new intvec(pVariables);
1962  polyset s;
1963  int  sl, n, i;
1964  int  *x;
1965
1966  res->data=(char *)iv;
1967  s = F->m;
1968  sl = IDELEMS(F) - 1;
1969  n = pVariables;
1970  wNsqr = (double)2.0 / (double)n;
1971  wFunctional = wFunctionalBuch;
1972  x = (int * )omAlloc(2 * (n + 1) * sizeof(int));
1973  wCall(s, sl, x);
1974  for (i = n; i!=0; i--)
1975    (*iv)[i-1] = x[i + n + 1];
1976  omFreeSize((ADDRESS)x, 2 * (n + 1) * sizeof(int));
1977  return FALSE;
1978}
1979
1980BOOLEAN kQHWeight(leftv res,leftv v)
1981{
1982  res->data=(char *)idQHomWeight((ideal)v->Data());
1983  if (res->data==NULL)
1984    res->data=(char *)new intvec(pVariables);
1985  return FALSE;
1986}
1987/*==============================================================*/
1988// from clapsing.cc
1989#if 0
1990BOOLEAN jjIS_SQR_FREE(leftv res, leftv u)
1991{
1992  BOOLEAN b=singclap_factorize((poly)(u->Data()), &v, 0);
1993  res->data=(void *)b;
1994}
1995#endif
1996
1997BOOLEAN jjRESULTANT(leftv res, leftv u, leftv v, leftv w)
1998{
1999  res->data=singclap_resultant((poly)u->Data(),(poly)v->Data(), (poly)w->Data());
2000  return errorreported;
2001}
2002BOOLEAN jjCHARSERIES(leftv res, leftv u)
2003{
2004  res->data=singclap_irrCharSeries((ideal)u->Data());
2005  return (res->data==NULL);
2006}
2007
2008// from semic.cc
2009#ifdef HAVE_SPECTRUM
2010
2011// ----------------------------------------------------------------------------
2012//  Initialize a  spectrum  deep from another  spectrum
2013// ----------------------------------------------------------------------------
2014
2015void spectrum::copy_deep( const spectrum &spec )
2016{
2017    mu = spec.mu;
2018    pg = spec.pg;
2019    n  = spec.n;
2020
2021    copy_new( n );
2022
2023    for( int i=0; i<n; i++ )
2024    {
2025        s[i] = spec.s[i];
2026        w[i] = spec.w[i];
2027    }
2028}
2029
2030// ----------------------------------------------------------------------------
2031//  Initialize a  spectrum  deep from a  singular  lists
2032// ----------------------------------------------------------------------------
2033
2034void spectrum::copy_deep( lists l )
2035{
2036    mu = (int)(l->m[0].Data( ));
2037    pg = (int)(l->m[1].Data( ));
2038    n  = (int)(l->m[2].Data( ));
2039
2040    copy_new( n );
2041
2042    intvec  *num = (intvec*)l->m[3].Data( );
2043    intvec  *den = (intvec*)l->m[4].Data( );
2044    intvec  *mul = (intvec*)l->m[5].Data( );
2045
2046    for( int i=0; i<n; i++ )
2047    {
2048        s[i] = (Rational)((*num)[i])/(Rational)((*den)[i]);
2049        w[i] = (*mul)[i];
2050    }
2051}
2052
2053// ----------------------------------------------------------------------------
2054//  singular lists  constructor for  spectrum
2055// ----------------------------------------------------------------------------
2056
2057spectrum::spectrum( lists l )
2058{
2059    copy_deep( l );
2060}
2061
2062// ----------------------------------------------------------------------------
2063//  generate a Singular  lists  from a spectrum
2064// ----------------------------------------------------------------------------
2065
2066lists   spectrum::thelist( void )
2067{
2068    lists   L  = (lists)omAllocBin( slists_bin);
2069
2070    L->Init( 6 );
2071
2072    intvec            *num  = new intvec( n );
2073    intvec            *den  = new intvec( n );
2074    intvec            *mult = new intvec( n );
2075
2076    for( int i=0; i<n; i++ )
2077    {
2078        (*num) [i] = s[i].get_num_si( );
2079        (*den) [i] = s[i].get_den_si( );
2080        (*mult)[i] = w[i];
2081    }
2082
2083    L->m[0].rtyp = INT_CMD;    //  milnor number
2084    L->m[1].rtyp = INT_CMD;    //  geometrical genus
2085    L->m[2].rtyp = INT_CMD;    //  # of spectrum numbers
2086    L->m[3].rtyp = INTVEC_CMD; //  numerators
2087    L->m[4].rtyp = INTVEC_CMD; //  denomiantors
2088    L->m[5].rtyp = INTVEC_CMD; //  multiplicities
2089
2090    L->m[0].data = (void*)mu;
2091    L->m[1].data = (void*)pg;
2092    L->m[2].data = (void*)n;
2093    L->m[3].data = (void*)num;
2094    L->m[4].data = (void*)den;
2095    L->m[5].data = (void*)mult;
2096
2097    return  L;
2098}
2099// from spectrum.cc
2100// ----------------------------------------------------------------------------
2101//  print out an error message for a spectrum list
2102// ----------------------------------------------------------------------------
2103
2104void    list_error( semicState state )
2105{
2106    switch( state )
2107    {
2108        case semicListTooShort:
2109            WerrorS( "the list is too short" );
2110            break;
2111        case semicListTooLong:
2112            WerrorS( "the list is too long" );
2113            break;
2114
2115        case semicListFirstElementWrongType:
2116            WerrorS( "first element of the list should be int" );
2117            break;
2118        case semicListSecondElementWrongType:
2119            WerrorS( "second element of the list should be int" );
2120            break;
2121        case semicListThirdElementWrongType:
2122            WerrorS( "third element of the list should be int" );
2123            break;
2124        case semicListFourthElementWrongType:
2125            WerrorS( "fourth element of the list should be intvec" );
2126            break;
2127        case semicListFifthElementWrongType:
2128            WerrorS( "fifth element of the list should be intvec" );
2129            break;
2130        case semicListSixthElementWrongType:
2131            WerrorS( "sixth element of the list should be intvec" );
2132            break;
2133
2134        case semicListNNegative:
2135            WerrorS( "first element of the list should be positive" );
2136            break;
2137        case semicListWrongNumberOfNumerators:
2138            WerrorS( "wrong number of numerators" );
2139            break;
2140        case semicListWrongNumberOfDenominators:
2141            WerrorS( "wrong number of denominators" );
2142            break;
2143        case semicListWrongNumberOfMultiplicities:
2144            WerrorS( "wrong number of multiplicities" );
2145            break;
2146
2147        case semicListMuNegative:
2148            WerrorS( "the Milnor number should be positive" );
2149            break;
2150        case semicListPgNegative:
2151            WerrorS( "the geometrical genus should be nonnegative" );
2152            break;
2153        case semicListNumNegative:
2154            WerrorS( "all numerators should be positive" );
2155            break;
2156        case semicListDenNegative:
2157            WerrorS( "all denominators should be positive" );
2158            break;
2159        case semicListMulNegative:
2160            WerrorS( "all multiplicities should be positive" );
2161            break;
2162
2163        case semicListNotSymmetric:
2164            WerrorS( "it is not symmetric" );
2165            break;
2166        case semicListNotMonotonous:
2167            WerrorS( "it is not monotonous" );
2168            break;
2169
2170        case semicListMilnorWrong:
2171            WerrorS( "the Milnor number is wrong" );
2172            break;
2173        case semicListPGWrong:
2174            WerrorS( "the geometrical genus is wrong" );
2175            break;
2176
2177        default:
2178            WerrorS( "unspecific error" );
2179            break;
2180    }
2181}
2182// ----------------------------------------------------------------------------
2183//  this is the main spectrum computation function
2184// ----------------------------------------------------------------------------
2185
2186spectrumState   spectrumCompute( poly h,lists *L,int fast )
2187{
2188  int i,j;
2189
2190  #ifdef SPECTRUM_DEBUG
2191  #ifdef SPECTRUM_PRINT
2192  #ifdef SPECTRUM_IOSTREAM
2193    cout << "spectrumCompute\n";
2194    if( fast==0 ) cout << "    no optimization" << endl;
2195    if( fast==1 ) cout << "    weight optimization" << endl;
2196    if( fast==2 ) cout << "    symmetry optimization" << endl;
2197  #else
2198    fprintf( stdout,"spectrumCompute\n" );
2199    if( fast==0 ) fprintf( stdout,"    no optimization\n" );
2200    if( fast==1 ) fprintf( stdout,"    weight optimization\n" );
2201    if( fast==2 ) fprintf( stdout,"    symmetry optimization\n" );
2202  #endif
2203  #endif
2204  #endif
2205
2206  // ----------------------
2207  //  check if  h  is zero
2208  // ----------------------
2209
2210  if( h==(poly)NULL )
2211  {
2212    return  spectrumZero;
2213  }
2214
2215  // ----------------------------------
2216  //  check if  h  has a constant term
2217  // ----------------------------------
2218
2219  if( hasConstTerm( h ) )
2220  {
2221    return  spectrumBadPoly;
2222  }
2223
2224  // --------------------------------
2225  //  check if  h  has a linear term
2226  // --------------------------------
2227
2228  if( hasLinearTerm( h ) )
2229  {
2230    *L = (lists)omAllocBin( slists_bin);
2231    (*L)->Init( 1 );
2232    (*L)->m[0].rtyp = INT_CMD;    //  milnor number
2233    /* (*L)->m[0].data = (void*)0;a  -- done by Init */
2234
2235    return  spectrumNoSingularity;
2236  }
2237
2238  // ----------------------------------
2239  //  compute the jacobi ideal of  (h)
2240  // ----------------------------------
2241
2242  ideal J = NULL;
2243  J = idInit( pVariables,1 );
2244
2245  #ifdef SPECTRUM_DEBUG
2246  #ifdef SPECTRUM_PRINT
2247  #ifdef SPECTRUM_IOSTREAM
2248    cout << "\n   computing the Jacobi ideal...\n";
2249  #else
2250    fprintf( stdout,"\n   computing the Jacobi ideal...\n" );
2251  #endif
2252  #endif
2253  #endif
2254
2255  for( i=0; i<pVariables; i++ )
2256  {
2257    J->m[i] = pDiff( h,i+1); //j );
2258
2259    #ifdef SPECTRUM_DEBUG
2260    #ifdef SPECTRUM_PRINT
2261    #ifdef SPECTRUM_IOSTREAM
2262      cout << "        ";
2263    #else
2264      fprintf( stdout,"        " );
2265    #endif
2266      pWrite( J->m[i] );
2267    #endif
2268    #endif
2269  }
2270
2271  // --------------------------------------------
2272  //  compute a standard basis  stdJ  of  jac(h)
2273  // --------------------------------------------
2274
2275  #ifdef SPECTRUM_DEBUG
2276  #ifdef SPECTRUM_PRINT
2277  #ifdef SPECTRUM_IOSTREAM
2278    cout << endl;
2279    cout << "    computing a standard basis..." << endl;
2280  #else
2281    fprintf( stdout,"\n" );
2282    fprintf( stdout,"    computing a standard basis...\n" );
2283  #endif
2284  #endif
2285  #endif
2286
2287  ideal stdJ = kStd(J,currQuotient,isNotHomog,NULL);
2288  idSkipZeroes( stdJ );
2289
2290  #ifdef SPECTRUM_DEBUG
2291  #ifdef SPECTRUM_PRINT
2292    for( i=0; i<IDELEMS(stdJ); i++ )
2293    {
2294      #ifdef SPECTRUM_IOSTREAM
2295        cout << "        ";
2296      #else
2297        fprintf( stdout,"        " );
2298      #endif
2299
2300      pWrite( stdJ->m[i] );
2301    }
2302  #endif
2303  #endif
2304
2305  idDelete( &J );
2306
2307  // ------------------------------------------
2308  //  check if the  h  has a singularity
2309  // ------------------------------------------
2310
2311  if( hasOne( stdJ ) )
2312  {
2313    // -------------------------------
2314    //  h is smooth in the origin
2315    //  return only the Milnor number
2316    // -------------------------------
2317
2318    *L = (lists)omAllocBin( slists_bin);
2319    (*L)->Init( 1 );
2320    (*L)->m[0].rtyp = INT_CMD;    //  milnor number
2321    /* (*L)->m[0].data = (void*)0;a  -- done by Init */
2322
2323    return  spectrumNoSingularity;
2324  }
2325
2326  // ------------------------------------------
2327  //  check if the singularity  h  is isolated
2328  // ------------------------------------------
2329
2330  for( i=pVariables; i>0; i-- )
2331  {
2332    if( hasAxis( stdJ,i )==FALSE )
2333    {
2334      return  spectrumNotIsolated;
2335    }
2336  }
2337
2338  // ------------------------------------------
2339  //  compute the highest corner  hc  of  stdJ
2340  // ------------------------------------------
2341
2342  #ifdef SPECTRUM_DEBUG
2343  #ifdef SPECTRUM_PRINT
2344  #ifdef SPECTRUM_IOSTREAM
2345    cout << "\n    computing the highest corner...\n";
2346  #else
2347    fprintf( stdout,"\n    computing the highest corner...\n" );
2348  #endif
2349  #endif
2350  #endif
2351
2352  poly hc = (poly)NULL;
2353
2354  scComputeHC( stdJ,currQuotient, 0,hc );
2355
2356  if( hc!=(poly)NULL )
2357  {
2358    pGetCoeff(hc) = nInit(1);
2359
2360    for( i=pVariables; i>0; i-- )
2361    {
2362      if( pGetExp( hc,i )>0 ) pDecrExp( hc,i );
2363    }
2364    pSetm( hc );
2365  }
2366  else
2367  {
2368    return  spectrumNoHC;
2369  }
2370
2371  #ifdef SPECTRUM_DEBUG
2372  #ifdef SPECTRUM_PRINT
2373  #ifdef SPECTRUM_IOSTREAM
2374    cout << "       ";
2375  #else
2376    fprintf( stdout,"       " );
2377  #endif
2378    pWrite( hc );
2379  #endif
2380  #endif
2381
2382  // ----------------------------------------
2383  //  compute the Newton polygon  nph  of  h
2384  // ----------------------------------------
2385
2386  #ifdef SPECTRUM_DEBUG
2387  #ifdef SPECTRUM_PRINT
2388  #ifdef SPECTRUM_IOSTREAM
2389    cout << "\n    computing the newton polygon...\n";
2390  #else
2391    fprintf( stdout,"\n    computing the newton polygon...\n" );
2392  #endif
2393  #endif
2394  #endif
2395
2396  newtonPolygon nph( h );
2397
2398  #ifdef SPECTRUM_DEBUG
2399  #ifdef SPECTRUM_PRINT
2400    cout << nph;
2401  #endif
2402  #endif
2403
2404  // -----------------------------------------------
2405  //  compute the weight corner  wc  of  (stdj,nph)
2406  // -----------------------------------------------
2407
2408  #ifdef SPECTRUM_DEBUG
2409  #ifdef SPECTRUM_PRINT
2410  #ifdef SPECTRUM_IOSTREAM
2411    cout << "\n    computing the weight corner...\n";
2412  #else
2413    fprintf( stdout,"\n    computing the weight corner...\n" );
2414  #endif
2415  #endif
2416  #endif
2417
2418  poly    wc = ( fast==0 ? pCopy( hc ) :
2419               ( fast==1 ? computeWC( nph,(Rational)pVariables ) :
2420              /* fast==2 */computeWC( nph,((Rational)pVariables)/(Rational)2 ) ) );
2421
2422  #ifdef SPECTRUM_DEBUG
2423  #ifdef SPECTRUM_PRINT
2424  #ifdef SPECTRUM_IOSTREAM
2425    cout << "        ";
2426  #else
2427    fprintf( stdout,"        " );
2428  #endif
2429    pWrite( wc );
2430  #endif
2431  #endif
2432
2433  // -------------
2434  //  compute  NF
2435  // -------------
2436
2437  #ifdef SPECTRUM_DEBUG
2438  #ifdef SPECTRUM_PRINT
2439  #ifdef SPECTRUM_IOSTREAM
2440    cout << "\n    computing NF...\n" << endl;
2441  #else
2442    fprintf( stdout,"\n    computing NF...\n" );
2443  #endif
2444  #endif
2445  #endif
2446
2447  spectrumPolyList NF( &nph );
2448
2449  computeNF( stdJ,hc,wc,&NF );
2450
2451  #ifdef SPECTRUM_DEBUG
2452  #ifdef SPECTRUM_PRINT
2453    cout << NF;
2454  #ifdef SPECTRUM_IOSTREAM
2455    cout << endl;
2456  #else
2457    fprintf( stdout,"\n" );
2458  #endif
2459  #endif
2460  #endif
2461
2462  // ----------------------------
2463  //  compute the spectrum of  h
2464  // ----------------------------
2465
2466  return  NF.spectrum( L,fast );
2467}
2468
2469// ----------------------------------------------------------------------------
2470//  this procedure is called from the interpreter
2471// ----------------------------------------------------------------------------
2472//  first  = polynomial
2473//  result = list of spectrum numbers
2474// ----------------------------------------------------------------------------
2475
2476BOOLEAN spectrumProc( leftv result,leftv first )
2477{
2478  spectrumState state = spectrumOK;
2479
2480  // -------------------
2481  //  check consistency
2482  // -------------------
2483
2484  //  check for a local ring
2485
2486  if( !ringIsLocal( ) )
2487  {
2488    WerrorS( "only works for local orderings" );
2489    state = spectrumWrongRing;
2490  }
2491
2492  //  no quotient rings are allowed
2493
2494  else if( currRing->qideal != NULL )
2495  {
2496    WerrorS( "does not work in quotient rings" );
2497    state = spectrumWrongRing;
2498  }
2499  else
2500  {
2501    lists   L    = (lists)NULL;
2502    int     flag = 1; // weight corner optimization is safe
2503
2504    state = spectrumCompute( (poly)first->Data( ),&L,flag );
2505
2506    if( state==spectrumOK )
2507    {
2508      result->rtyp = LIST_CMD;
2509      result->data = (char*)L;
2510    }
2511    else
2512    {
2513      spectrumPrintError(state);
2514    }
2515  }
2516
2517  return  (state!=spectrumOK);
2518}
2519
2520// ----------------------------------------------------------------------------
2521//  this procedure is called from the interpreter
2522// ----------------------------------------------------------------------------
2523//  first  = polynomial
2524//  result = list of spectrum numbers
2525// ----------------------------------------------------------------------------
2526
2527BOOLEAN spectrumfProc( leftv result,leftv first )
2528{
2529  spectrumState state = spectrumOK;
2530
2531  // -------------------
2532  //  check consistency
2533  // -------------------
2534
2535  //  check for a local polynomial ring
2536
2537  if( currRing->OrdSgn != -1 )
2538  // ?? HS: the test above is also true for k[x][[y]], k[[x]][y]
2539  // or should we use:
2540  //if( !ringIsLocal( ) )
2541  {
2542    WerrorS( "only works for local orderings" );
2543    state = spectrumWrongRing;
2544  }
2545  else if( currRing->qideal != NULL )
2546  {
2547    WerrorS( "does not work in quotient rings" );
2548    state = spectrumWrongRing;
2549  }
2550  else
2551  {
2552    lists   L    = (lists)NULL;
2553    int     flag = 2; // symmetric optimization
2554
2555    state = spectrumCompute( (poly)first->Data( ),&L,flag );
2556
2557    if( state==spectrumOK )
2558    {
2559      result->rtyp = LIST_CMD;
2560      result->data = (char*)L;
2561    }
2562    else
2563    {
2564      spectrumPrintError(state);
2565    }
2566  }
2567
2568  return  (state!=spectrumOK);
2569}
2570
2571// ----------------------------------------------------------------------------
2572//  check if a list is a spectrum
2573//  check for:
2574//      list has 6 elements
2575//      1st element is int (mu=Milnor number)
2576//      2nd element is int (pg=geometrical genus)
2577//      3rd element is int (n =number of different spectrum numbers)
2578//      4th element is intvec (num=numerators)
2579//      5th element is intvec (den=denomiantors)
2580//      6th element is intvec (mul=multiplicities)
2581//      exactly n numerators
2582//      exactly n denominators
2583//      exactly n multiplicities
2584//      mu>0
2585//      pg>=0
2586//      n>0
2587//      num>0
2588//      den>0
2589//      mul>0
2590//      symmetriy with respect to numberofvariables/2
2591//      monotony
2592//      mu = sum of all multiplicities
2593//      pg = sum of all multiplicities where num/den<=1
2594// ----------------------------------------------------------------------------
2595
2596semicState  list_is_spectrum( lists l )
2597{
2598    // -------------------
2599    //  check list length
2600    // -------------------
2601
2602    if( l->nr < 5 )
2603    {
2604        return  semicListTooShort;
2605    }
2606    else if( l->nr > 5 )
2607    {
2608        return  semicListTooLong;
2609    }
2610
2611    // -------------
2612    //  check types
2613    // -------------
2614
2615    if( l->m[0].rtyp != INT_CMD )
2616    {
2617        return  semicListFirstElementWrongType;
2618    }
2619    else if( l->m[1].rtyp != INT_CMD )
2620    {
2621        return  semicListSecondElementWrongType;
2622    }
2623    else if( l->m[2].rtyp != INT_CMD )
2624    {
2625        return  semicListThirdElementWrongType;
2626    }
2627    else if( l->m[3].rtyp != INTVEC_CMD )
2628    {
2629        return  semicListFourthElementWrongType;
2630    }
2631    else if( l->m[4].rtyp != INTVEC_CMD )
2632    {
2633        return  semicListFifthElementWrongType;
2634    }
2635    else if( l->m[5].rtyp != INTVEC_CMD )
2636    {
2637        return  semicListSixthElementWrongType;
2638    }
2639
2640    // -------------------------
2641    //  check number of entries
2642    // -------------------------
2643
2644    int     mu = (int)(l->m[0].Data( ));
2645    int     pg = (int)(l->m[1].Data( ));
2646    int     n  = (int)(l->m[2].Data( ));
2647
2648    if( n <= 0 )
2649    {
2650        return  semicListNNegative;
2651    }
2652
2653    intvec  *num = (intvec*)l->m[3].Data( );
2654    intvec  *den = (intvec*)l->m[4].Data( );
2655    intvec  *mul = (intvec*)l->m[5].Data( );
2656
2657    if( n != num->length( ) )
2658    {
2659        return  semicListWrongNumberOfNumerators;
2660    }
2661    else if( n != den->length( ) )
2662    {
2663        return  semicListWrongNumberOfDenominators;
2664    }
2665    else if( n != mul->length( ) )
2666    {
2667        return  semicListWrongNumberOfMultiplicities;
2668    }
2669
2670    // --------
2671    //  values
2672    // --------
2673
2674    if( mu <= 0 )
2675    {
2676        return  semicListMuNegative;
2677    }
2678    if( pg < 0 )
2679    {
2680        return  semicListPgNegative;
2681    }
2682
2683    int i;
2684
2685    for( i=0; i<n; i++ )
2686    {
2687        if( (*num)[i] <= 0 )
2688        {
2689            return  semicListNumNegative;
2690        }
2691        if( (*den)[i] <= 0 )
2692        {
2693            return  semicListDenNegative;
2694        }
2695        if( (*mul)[i] <= 0 )
2696        {
2697            return  semicListMulNegative;
2698        }
2699    }
2700
2701    // ----------------
2702    //  check symmetry
2703    // ----------------
2704
2705    int     j;
2706
2707    for( i=0, j=n-1; i<=j; i++,j-- )
2708    {
2709        if( (*num)[i] != pVariables*((*den)[i]) - (*num)[j] ||
2710            (*den)[i] != (*den)[j] ||
2711            (*mul)[i] != (*mul)[j] )
2712        {
2713            return  semicListNotSymmetric;
2714        }
2715    }
2716
2717    // ----------------
2718    //  check monotony
2719    // ----------------
2720
2721    for( i=0, j=1; i<n/2; i++,j++ )
2722    {
2723        if( (*num)[i]*(*den)[j] >= (*num)[j]*(*den)[i] )
2724        {
2725            return  semicListNotMonotonous;
2726        }
2727    }
2728
2729    // ---------------------
2730    //  check Milnor number
2731    // ---------------------
2732
2733    for( mu=0, i=0; i<n; i++ )
2734    {
2735        mu += (*mul)[i];
2736    }
2737
2738    if( mu != (int)(l->m[0].Data( )) )
2739    {
2740        return  semicListMilnorWrong;
2741    }
2742
2743    // -------------------------
2744    //  check geometrical genus
2745    // -------------------------
2746
2747    for( pg=0, i=0; i<n; i++ )
2748    {
2749        if( (*num)[i]<=(*den)[i] )
2750        {
2751            pg += (*mul)[i];
2752        }
2753    }
2754
2755    if( pg != (int)(l->m[1].Data( )) )
2756    {
2757        return  semicListPGWrong;
2758    }
2759
2760    return  semicOK;
2761}
2762
2763// ----------------------------------------------------------------------------
2764//  this procedure is called from the interpreter
2765// ----------------------------------------------------------------------------
2766//  first  = list of spectrum numbers
2767//  second = list of spectrum numbers
2768//  result = sum of the two lists
2769// ----------------------------------------------------------------------------
2770
2771BOOLEAN spaddProc( leftv result,leftv first,leftv second )
2772{
2773    semicState  state;
2774
2775    // -----------------
2776    //  check arguments
2777    // -----------------
2778
2779    lists l1 = (lists)first->Data( );
2780    lists l2 = (lists)second->Data( );
2781
2782    if( (state=list_is_spectrum( l1 )) != semicOK )
2783    {
2784        WerrorS( "first argument is not a spectrum:" );
2785        list_error( state );
2786    }
2787    else if( (state=list_is_spectrum( l2 )) != semicOK )
2788    {
2789        WerrorS( "second argument is not a spectrum:" );
2790        list_error( state );
2791    }
2792    else
2793    {
2794        spectrum s1( l1 );
2795        spectrum s2( l2 );
2796        spectrum sum( s1+s2 );
2797
2798        result->rtyp = LIST_CMD;
2799        result->data = (char*)(sum.thelist( ));
2800    }
2801
2802    return  (state!=semicOK);
2803}
2804
2805// ----------------------------------------------------------------------------
2806//  this procedure is called from the interpreter
2807// ----------------------------------------------------------------------------
2808//  first  = list of spectrum numbers
2809//  second = integer
2810//  result = the multiple of the first list by the second factor
2811// ----------------------------------------------------------------------------
2812
2813BOOLEAN spmulProc( leftv result,leftv first,leftv second )
2814{
2815    semicState  state;
2816
2817    // -----------------
2818    //  check arguments
2819    // -----------------
2820
2821    lists   l = (lists)first->Data( );
2822    int     k = (int)second->Data( );
2823
2824    if( (state=list_is_spectrum( l ))!=semicOK )
2825    {
2826        WerrorS( "first argument is not a spectrum" );
2827        list_error( state );
2828    }
2829    else if( k < 0 )
2830    {
2831        WerrorS( "second argument should be positive" );
2832        state = semicMulNegative;
2833    }
2834    else
2835    {
2836        spectrum s( l );
2837        spectrum product( k*s );
2838
2839        result->rtyp = LIST_CMD;
2840        result->data = (char*)product.thelist( );
2841    }
2842
2843    return  (state!=semicOK);
2844}
2845
2846// ----------------------------------------------------------------------------
2847//  this procedure is called from the interpreter
2848// ----------------------------------------------------------------------------
2849//  first  = list of spectrum numbers
2850//  second = list of spectrum numbers
2851//  result = semicontinuity index
2852// ----------------------------------------------------------------------------
2853
2854BOOLEAN    semicProc3   ( leftv res,leftv u,leftv v,leftv w )
2855{
2856  semicState  state;
2857  BOOLEAN qh=(((int)w->Data())==1);
2858
2859  // -----------------
2860  //  check arguments
2861  // -----------------
2862
2863  lists l1 = (lists)u->Data( );
2864  lists l2 = (lists)v->Data( );
2865
2866  if( (state=list_is_spectrum( l1 ))!=semicOK )
2867  {
2868    WerrorS( "first argument is not a spectrum" );
2869    list_error( state );
2870  }
2871  else if( (state=list_is_spectrum( l2 ))!=semicOK )
2872  {
2873    WerrorS( "second argument is not a spectrum" );
2874    list_error( state );
2875  }
2876  else
2877  {
2878    spectrum s1( l1 );
2879    spectrum s2( l2 );
2880
2881    res->rtyp = INT_CMD;
2882    if (qh)
2883      res->data = (void*)(s1.mult_spectrumh( s2 ));
2884    else
2885      res->data = (void*)(s1.mult_spectrum( s2 ));
2886  }
2887
2888  // -----------------
2889  //  check status
2890  // -----------------
2891
2892  return  (state!=semicOK);
2893}
2894BOOLEAN    semicProc   ( leftv res,leftv u,leftv v )
2895{
2896  sleftv tmp;
2897  memset(&tmp,0,sizeof(tmp));
2898  tmp.rtyp=INT_CMD;
2899  /* tmp.data = (void *)0;  -- done by memset */
2900
2901  return  semicProc3(res,u,v,&tmp);
2902}
2903// from splist.cc
2904// ----------------------------------------------------------------------------
2905//  Compute the spectrum of a  spectrumPolyList
2906// ----------------------------------------------------------------------------
2907
2908spectrumState   spectrumPolyList::spectrum( lists *L,int fast )
2909{
2910    spectrumPolyNode  **node = &root;
2911    spectrumPolyNode  *search;
2912
2913    poly              f,tmp;
2914    int               found,cmp;
2915
2916    Rational smax( ( fast==0 ? 0 : pVariables ),
2917                   ( fast==2 ? 2 : 1 ) );
2918
2919    Rational weight_prev( 0,1 );
2920
2921    int     mu = 0;          // the milnor number
2922    int     pg = 0;          // the geometrical genus
2923    int     n  = 0;          // number of different spectral numbers
2924    int     z  = 0;          // number of spectral number equal to smax
2925
2926    int     k = 0;
2927
2928    while( (*node)!=(spectrumPolyNode*)NULL &&
2929           ( fast==0 || (*node)->weight<=smax ) )
2930    {
2931        // ---------------------------------------
2932        //  determine the first normal form which
2933        //  contains the monomial  node->mon
2934        // ---------------------------------------
2935
2936        found  = FALSE;
2937        search = *node;
2938
2939        while( search!=(spectrumPolyNode*)NULL && found==FALSE )
2940        {
2941            if( search->nf!=(poly)NULL )
2942            {
2943                f = search->nf;
2944
2945                do
2946                {
2947                    // --------------------------------
2948                    //  look for  (*node)->mon  in   f
2949                    // --------------------------------
2950
2951                    cmp = pCmp( (*node)->mon,f );
2952
2953                    if( cmp<0 )
2954                    {
2955                        f = pNext( f );
2956                    }
2957                    else if( cmp==0 )
2958                    {
2959                        // -----------------------------
2960                        //  we have found a normal form
2961                        // -----------------------------
2962
2963                        found = TRUE;
2964
2965                        //  normalize coefficient
2966
2967                        number inv = nInvers( pGetCoeff( f ) );
2968                        pMult_nn( search->nf,inv );
2969                        nDelete( &inv );
2970
2971                        //  exchange  normal forms
2972
2973                        tmp         = (*node)->nf;
2974                        (*node)->nf = search->nf;
2975                        search->nf  = tmp;
2976                    }
2977                }
2978                while( cmp<0 && f!=(poly)NULL );
2979            }
2980            search = search->next;
2981        }
2982
2983        if( found==FALSE )
2984        {
2985            // ------------------------------------------------
2986            //  the weight of  node->mon  is a spectrum number
2987            // ------------------------------------------------
2988
2989            mu++;
2990
2991            if( (*node)->weight<=(Rational)1 )              pg++;
2992            if( (*node)->weight==smax )           z++;
2993            if( (*node)->weight>weight_prev )     n++;
2994
2995            weight_prev = (*node)->weight;
2996            node = &((*node)->next);
2997        }
2998        else
2999        {
3000            // -----------------------------------------------
3001            //  determine all other normal form which contain
3002            //  the monomial  node->mon
3003            //  replace for  node->mon  its normal form
3004            // -----------------------------------------------
3005
3006            while( search!=(spectrumPolyNode*)NULL )
3007            {
3008                    if( search->nf!=(poly)NULL )
3009                {
3010                    f = search->nf;
3011
3012                    do
3013                    {
3014                        // --------------------------------
3015                        //  look for  (*node)->mon  in   f
3016                        // --------------------------------
3017
3018                        cmp = pCmp( (*node)->mon,f );
3019
3020                        if( cmp<0 )
3021                        {
3022                            f = pNext( f );
3023                        }
3024                        else if( cmp==0 )
3025                        {
3026                            search->nf = pSub( search->nf,
3027                                ppMult_nn( (*node)->nf,pGetCoeff( f ) ) );
3028                            pNorm( search->nf );
3029                        }
3030                    }
3031                    while( cmp<0 && f!=(poly)NULL );
3032                }
3033                search = search->next;
3034            }
3035            delete_node( node );
3036        }
3037
3038    }
3039
3040    // --------------------------------------------------------
3041    //  fast computation exploits the symmetry of the spectrum
3042    // --------------------------------------------------------
3043
3044    if( fast==2 )
3045    {
3046        mu = 2*mu - z;
3047        n  = ( z > 0 ? 2*n - 1 : 2*n );
3048    }
3049
3050    // --------------------------------------------------------
3051    //  compute the spectrum numbers with their multiplicities
3052    // --------------------------------------------------------
3053
3054    intvec            *nom  = new intvec( n );
3055    intvec            *den  = new intvec( n );
3056    intvec            *mult = new intvec( n );
3057
3058    int count         = 0;
3059    int multiplicity  = 1;
3060
3061    for( search=root; search!=(spectrumPolyNode*)NULL &&
3062                     ( fast==0 || search->weight<=smax );
3063                     search=search->next )
3064    {
3065        if( search->next==(spectrumPolyNode*)NULL ||
3066            search->weight<search->next->weight )
3067        {
3068            (*nom) [count] = search->weight.get_num_si( );
3069            (*den) [count] = search->weight.get_den_si( );
3070            (*mult)[count] = multiplicity;
3071
3072            multiplicity=1;
3073            count++;
3074        }
3075        else
3076        {
3077            multiplicity++;
3078        }
3079    }
3080
3081    // --------------------------------------------------------
3082    //  fast computation exploits the symmetry of the spectrum
3083    // --------------------------------------------------------
3084
3085    if( fast==2 )
3086    {
3087        int n1,n2;
3088        for( n1=0, n2=n-1; n1<n2; n1++, n2-- )
3089        {
3090            (*nom) [n2] = pVariables*(*den)[n1]-(*nom)[n1];
3091            (*den) [n2] = (*den)[n1];
3092            (*mult)[n2] = (*mult)[n1];
3093        }
3094    }
3095
3096    // -----------------------------------
3097    //  test if the spectrum is symmetric
3098    // -----------------------------------
3099
3100    if( fast==0 || fast==1 )
3101    {
3102        int symmetric=TRUE;
3103
3104        for( int n1=0, n2=n-1 ; n1<n2 && symmetric==TRUE; n1++, n2-- )
3105        {
3106            if( (*mult)[n1]!=(*mult)[n2] ||
3107                (*den) [n1]!= (*den)[n2] ||
3108                (*nom)[n1]+(*nom)[n2]!=pVariables*(*den) [n1] )
3109            {
3110                symmetric = FALSE;
3111            }
3112        }
3113
3114        if( symmetric==FALSE )
3115        {
3116            // ---------------------------------------------
3117            //  the spectrum is not symmetric => degenerate
3118            //  principal part
3119            // ---------------------------------------------
3120
3121            *L = (lists)omAllocBin( slists_bin);
3122            (*L)->Init( 1 );
3123            (*L)->m[0].rtyp = INT_CMD;    //  milnor number
3124            (*L)->m[0].data = (void*)mu;
3125
3126            return spectrumDegenerate;
3127        }
3128    }
3129
3130    *L = (lists)omAllocBin( slists_bin);
3131
3132    (*L)->Init( 6 );
3133
3134    (*L)->m[0].rtyp = INT_CMD;    //  milnor number
3135    (*L)->m[1].rtyp = INT_CMD;    //  geometrical genus
3136    (*L)->m[2].rtyp = INT_CMD;    //  number of spectrum values
3137    (*L)->m[3].rtyp = INTVEC_CMD; //  nominators
3138    (*L)->m[4].rtyp = INTVEC_CMD; //  denomiantors
3139    (*L)->m[5].rtyp = INTVEC_CMD; //  multiplicities
3140
3141    (*L)->m[0].data = (void*)mu;
3142    (*L)->m[1].data = (void*)pg;
3143    (*L)->m[2].data = (void*)n;
3144    (*L)->m[3].data = (void*)nom;
3145    (*L)->m[4].data = (void*)den;
3146    (*L)->m[5].data = (void*)mult;
3147
3148    return  spectrumOK;
3149}
3150
3151#endif
3152
3153//from mpr_inout.cc
3154extern void nPrint(number n);
3155
3156BOOLEAN loNewtonP( leftv res, leftv arg1 )
3157{
3158  res->data= (void*)loNewtonPolytope( (ideal)arg1->Data() );
3159  return FALSE;
3160}
3161
3162BOOLEAN loSimplex( leftv res, leftv args )
3163{
3164  if ( !(rField_is_long_R()) )
3165  {
3166    WerrorS("Ground field not implemented!");
3167    return TRUE;
3168  }
3169
3170  simplex * LP;
3171  matrix m;
3172
3173  leftv v= args;
3174  if ( v->Typ() != MATRIX_CMD ) // 1: matrix
3175    return TRUE;
3176  else
3177    m= (matrix)(v->CopyD());
3178
3179  LP = new simplex(MATROWS(m),MATCOLS(m));
3180  LP->mapFromMatrix(m);
3181
3182  v= v->next;
3183  if ( v->Typ() != INT_CMD )    // 2: m = number of constraints
3184    return TRUE;
3185  else
3186    LP->m= (int)(v->Data());
3187
3188  v= v->next;
3189  if ( v->Typ() != INT_CMD )    // 3: n = number of variables
3190    return TRUE;
3191  else
3192    LP->n= (int)(v->Data());
3193
3194  v= v->next;
3195  if ( v->Typ() != INT_CMD )    // 4: m1 = number of <= constraints
3196    return TRUE;
3197  else
3198    LP->m1= (int)(v->Data());
3199
3200  v= v->next;
3201  if ( v->Typ() != INT_CMD )    // 5: m2 = number of >= constraints
3202    return TRUE;
3203  else
3204    LP->m2= (int)(v->Data());
3205
3206  v= v->next;
3207  if ( v->Typ() != INT_CMD )    // 6: m3 = number of == constraints
3208    return TRUE;
3209  else
3210    LP->m3= (int)(v->Data());
3211
3212#ifdef mprDEBUG_PROT
3213  Print("m (constraints) %d\n",LP->m);
3214  Print("n (columns) %d\n",LP->n);
3215  Print("m1 (<=) %d\n",LP->m1);
3216  Print("m2 (>=) %d\n",LP->m2);
3217  Print("m3 (==) %d\n",LP->m3);
3218#endif
3219
3220  LP->compute();
3221
3222  lists lres= (lists)omAlloc( sizeof(slists) );
3223  lres->Init( 6 );
3224
3225  lres->m[0].rtyp= MATRIX_CMD; // output matrix
3226  lres->m[0].data=(void*)LP->mapToMatrix(m);
3227
3228  lres->m[1].rtyp= INT_CMD;   // found a solution?
3229  lres->m[1].data=(void*)LP->icase;
3230
3231  lres->m[2].rtyp= INTVEC_CMD;
3232  lres->m[2].data=(void*)LP->posvToIV();
3233
3234  lres->m[3].rtyp= INTVEC_CMD;
3235  lres->m[3].data=(void*)LP->zrovToIV();
3236
3237  lres->m[4].rtyp= INT_CMD;
3238  lres->m[4].data=(void*)LP->m;
3239
3240  lres->m[5].rtyp= INT_CMD;
3241  lres->m[5].data=(void*)LP->n;
3242
3243  res->data= (void*)lres;
3244
3245  return FALSE;
3246}
3247
3248BOOLEAN nuMPResMat( leftv res, leftv arg1, leftv arg2 )
3249{
3250  ideal gls = (ideal)(arg1->Data());
3251  int imtype= (int)arg2->Data();
3252
3253  uResultant::resMatType mtype= determineMType( imtype );
3254
3255  // check input ideal ( = polynomial system )
3256  if ( mprIdealCheck( gls, arg1->Name(), mtype, true ) != mprOk )
3257  {
3258    return TRUE;
3259  }
3260
3261  uResultant *resMat= new uResultant( gls, mtype, false );
3262
3263  res->rtyp = MODUL_CMD;
3264  res->data= (void*)resMat->accessResMat()->getMatrix();
3265
3266  delete resMat;
3267
3268  return FALSE;
3269}
3270
3271BOOLEAN nuLagSolve( leftv res, leftv arg1, leftv arg2, leftv arg3 )
3272{
3273
3274  poly gls;
3275  gls= (poly)(arg1->Data());
3276  int howclean= (int)arg3->Data();
3277
3278  if ( !(rField_is_R() ||
3279         rField_is_Q() ||
3280         rField_is_long_R() ||
3281         rField_is_long_C()) )
3282  {
3283    WerrorS("Ground field not implemented!");
3284    return TRUE;
3285  }
3286
3287  if ( !(rField_is_R()||rField_is_long_R()||rField_is_long_C()) )
3288  {
3289    unsigned long int ii = (unsigned long int)arg2->Data();
3290    setGMPFloatDigits( ii, ii );
3291  }
3292
3293  if ( gls == NULL || pIsConstant( gls ) )
3294  {
3295    WerrorS("Input polynomial is constant!");
3296    return TRUE;
3297  }
3298
3299  int ldummy;
3300  int deg= pLDeg( gls, &ldummy );
3301  //  int deg= pDeg( gls );
3302  int len= pLength( gls );
3303  int i,vpos;
3304  poly piter;
3305  lists elist;
3306  lists rlist;
3307
3308  elist= (lists)omAlloc( sizeof(slists) );
3309  elist->Init( 0 );
3310
3311  if ( pVariables > 1 )
3312  {
3313    piter= gls;
3314    for ( i= 1; i <= pVariables; i++ )
3315      if ( pGetExp( piter, i ) )
3316      {
3317        vpos= i;
3318        break;
3319      }
3320    while ( piter )
3321    {
3322      for ( i= 1; i <= pVariables; i++ )
3323        if ( (vpos != i) && (pGetExp( piter, i ) != 0) )
3324        {
3325          WerrorS("The input polynomial must be univariate!");
3326          return TRUE;
3327        }
3328      pIter( piter );
3329    }
3330  }
3331
3332  rootContainer * roots= new rootContainer();
3333  number * pcoeffs= (number *)omAlloc( (deg+1) * sizeof( number ) );
3334  piter= gls;
3335  for ( i= deg; i >= 0; i-- )
3336  {
3337    //if ( piter ) Print("deg %d, pDeg(piter) %d\n",i,pTotaldegree(piter));
3338    if ( piter && pTotaldegree(piter) == i )
3339    {
3340      pcoeffs[i]= nCopy( pGetCoeff( piter ) );
3341      //nPrint( pcoeffs[i] );PrintS("  ");
3342      pIter( piter );
3343    }
3344    else
3345    {
3346      pcoeffs[i]= nInit(0);
3347    }
3348  }
3349
3350#ifdef mprDEBUG_PROT
3351  for (i=deg; i >= 0; i--)
3352  {
3353    nPrint( pcoeffs[i] );PrintS("  ");
3354  }
3355  PrintLn();
3356#endif
3357
3358  roots->fillContainer( pcoeffs, NULL, 1, deg, rootContainer::onepoly, 1 );
3359  roots->solver( howclean );
3360
3361  int elem= roots->getAnzRoots();
3362  char *out;
3363  char *dummy;
3364  int j;
3365
3366  rlist= (lists)omAlloc( sizeof(slists) );
3367  rlist->Init( elem );
3368
3369  if (rField_is_long_C())
3370  {
3371    for ( j= 0; j < elem; j++ )
3372    {
3373      rlist->m[j].rtyp=NUMBER_CMD;
3374      rlist->m[j].data=(void *)nCopy((number)(roots->getRoot(j)));
3375      //rlist->m[j].data=(void *)(number)(roots->getRoot(j));
3376    }
3377  }
3378  else
3379  {
3380    for ( j= 0; j < elem; j++ )
3381    {
3382      dummy = complexToStr( (*roots)[j], gmp_output_digits );
3383      rlist->m[j].rtyp=STRING_CMD;
3384      rlist->m[j].data=(void *)dummy;
3385    }
3386  }
3387
3388  elist->Clean();
3389  //omFreeSize( (ADDRESS) elist, sizeof(slists) );
3390
3391  // this is (via fillContainer) the same data as in root
3392  //for ( i= deg; i >= 0; i-- ) nDelete( &pcoeffs[i] );
3393  //omFreeSize( (ADDRESS) pcoeffs, (deg+1) * sizeof( number ) );
3394
3395  delete roots;
3396
3397  res->rtyp= LIST_CMD;
3398  res->data= (void*)rlist;
3399
3400  return FALSE;
3401}
3402
3403BOOLEAN nuVanderSys( leftv res, leftv arg1, leftv arg2, leftv arg3)
3404{
3405  int i;
3406  ideal p,w;
3407  p= (ideal)arg1->Data();
3408  w= (ideal)arg2->Data();
3409
3410  // w[0] = f(p^0)
3411  // w[1] = f(p^1)
3412  // ...
3413  // p can be a vector of numbers (multivariate polynom)
3414  //   or one number (univariate polynom)
3415  // tdg = deg(f)
3416
3417  int n= IDELEMS( p );
3418  int m= IDELEMS( w );
3419  int tdg= (int)arg3->Data();
3420
3421  res->data= (void*)NULL;
3422
3423  // check the input
3424  if ( tdg < 1 )
3425  {
3426    WerrorS("Last input parameter must be > 0!");
3427    return TRUE;
3428  }
3429  if ( n != pVariables )
3430  {
3431    Werror("Size of first input ideal must be equal to %d!",pVariables);
3432    return TRUE;
3433  }
3434  if ( m != (int)pow((double)tdg+1,(double)n) )
3435  {
3436    Werror("Size of second input ideal must be equal to %d!",
3437      (int)pow((double)tdg+1,(double)n));
3438    return TRUE;
3439  }
3440  if ( !(rField_is_Q() /* ||
3441         rField_is_R() || rField_is_long_R() ||
3442         rField_is_long_C()*/ ) )
3443         {
3444    WerrorS("Ground field not implemented!");
3445    return TRUE;
3446  }
3447
3448  number tmp;
3449  number *pevpoint= (number *)omAlloc( n * sizeof( number ) );
3450  for ( i= 0; i < n; i++ )
3451  {
3452    pevpoint[i]=nInit(0);
3453    if (  (p->m)[i] )
3454    {
3455      tmp = pGetCoeff( (p->m)[i] );
3456      if ( nIsZero(tmp) || nIsOne(tmp) || nIsMOne(tmp) )
3457      {
3458        omFreeSize( (ADDRESS)pevpoint, n * sizeof( number ) );
3459        WerrorS("Elements of first input ideal must not be equal to -1, 0, 1!");
3460        return TRUE;
3461      }
3462    } else tmp= NULL;
3463    if ( !nIsZero(tmp) )
3464    {
3465      if ( !pIsConstant((p->m)[i]))
3466      {
3467        omFreeSize( (ADDRESS)pevpoint, n * sizeof( number ) );
3468        WerrorS("Elements of first input ideal must be numbers!");
3469        return TRUE;
3470      }
3471      pevpoint[i]= nCopy( tmp );
3472    }
3473  }
3474
3475  number *wresults= (number *)omAlloc( m * sizeof( number ) );
3476  for ( i= 0; i < m; i++ )
3477  {
3478    wresults[i]= nInit(0);
3479    if ( (w->m)[i] && !nIsZero(pGetCoeff((w->m)[i])) )
3480    {
3481      if ( !pIsConstant((w->m)[i]))
3482      {
3483        omFreeSize( (ADDRESS)pevpoint, n * sizeof( number ) );
3484        omFreeSize( (ADDRESS)wresults, m * sizeof( number ) );
3485        WerrorS("Elements of second input ideal must be numbers!");
3486        return TRUE;
3487      }
3488      wresults[i]= nCopy(pGetCoeff((w->m)[i]));
3489    }
3490  }
3491
3492  vandermonde vm( m, n, tdg, pevpoint, FALSE );
3493  number *ncpoly= vm.interpolateDense( wresults );
3494  // do not free ncpoly[]!!
3495  poly rpoly= vm.numvec2poly( ncpoly );
3496
3497  omFreeSize( (ADDRESS)pevpoint, n * sizeof( number ) );
3498  omFreeSize( (ADDRESS)wresults, m * sizeof( number ) );
3499
3500  res->data= (void*)rpoly;
3501  return FALSE;
3502}
3503
3504BOOLEAN nuUResSolve( leftv res, leftv args )
3505{
3506  leftv v= args;
3507
3508  ideal gls;
3509  int imtype;
3510  int howclean;
3511
3512  // get ideal
3513  if ( v->Typ() != IDEAL_CMD )
3514    return TRUE;
3515  else gls= (ideal)(v->Data());
3516  v= v->next;
3517
3518  // get resultant matrix type to use (0,1)
3519  if ( v->Typ() != INT_CMD )
3520    return TRUE;
3521  else imtype= (int)v->Data();
3522  v= v->next;
3523
3524  // get and set precision in digits ( > 0 )
3525  if ( v->Typ() != INT_CMD )
3526    return TRUE;
3527  else if ( !(rField_is_R()||rField_is_long_R()||rField_is_long_C()) )
3528  {
3529    unsigned long int ii=(unsigned long int)v->Data();
3530    setGMPFloatDigits( ii, ii );
3531  }
3532  v= v->next;
3533
3534  // get interpolation steps (0,1,2)
3535  if ( v->Typ() != INT_CMD )
3536    return TRUE;
3537  else howclean= (int)v->Data();
3538
3539  uResultant::resMatType mtype= determineMType( imtype );
3540  int i,c,count;
3541  lists listofroots= NULL;
3542  lists emptylist;
3543  number smv= NULL;
3544  BOOLEAN interpolate_det= (mtype==uResultant::denseResMat)?TRUE:FALSE;
3545
3546  //emptylist= (lists)omAlloc( sizeof(slists) );
3547  //emptylist->Init( 0 );
3548
3549  //res->rtyp = LIST_CMD;
3550  //res->data= (void *)emptylist;
3551
3552  // check input ideal ( = polynomial system )
3553  if ( mprIdealCheck( gls, args->Name(), mtype ) != mprOk )
3554  {
3555    return TRUE;
3556  }
3557
3558  uResultant * ures;
3559  rootContainer ** iproots;
3560  rootContainer ** muiproots;
3561  rootArranger * arranger;
3562
3563  // main task 1: setup of resultant matrix
3564  ures= new uResultant( gls, mtype );
3565  if ( ures->accessResMat()->initState() != resMatrixBase::ready )
3566  {
3567    WerrorS("Error occurred during matrix setup!");
3568    return TRUE;
3569  }
3570
3571  // if dense resultant, check if minor nonsingular
3572  if ( mtype == uResultant::denseResMat )
3573  {
3574    smv= ures->accessResMat()->getSubDet();
3575#ifdef mprDEBUG_PROT
3576    PrintS("// Determinant of submatrix: ");nPrint(smv);PrintLn();
3577#endif
3578    if ( nIsZero(smv) )
3579    {
3580      WerrorS("Unsuitable input ideal: Minor of resultant matrix is singular!");
3581      return TRUE;
3582    }
3583  }
3584
3585  // main task 2: Interpolate specialized resultant polynomials
3586  if ( interpolate_det )
3587    iproots= ures->interpolateDenseSP( false, smv );
3588  else
3589    iproots= ures->specializeInU( false, smv );
3590
3591  // main task 3: Interpolate specialized resultant polynomials
3592  if ( interpolate_det )
3593    muiproots= ures->interpolateDenseSP( true, smv );
3594  else
3595    muiproots= ures->specializeInU( true, smv );
3596
3597#ifdef mprDEBUG_PROT
3598  c= iproots[0]->getAnzElems();
3599  for (i=0; i < c; i++) pWrite(iproots[i]->getPoly());
3600  c= muiproots[0]->getAnzElems();
3601  for (i=0; i < c; i++) pWrite(muiproots[i]->getPoly());
3602#endif
3603
3604  // main task 4: Compute roots of specialized polys and match them up
3605  arranger= new rootArranger( iproots, muiproots, howclean );
3606  arranger->solve_all();
3607
3608  // get list of roots
3609  if ( arranger->success() )
3610  {
3611    arranger->arrange();
3612    listofroots= arranger->listOfRoots( gmp_output_digits );
3613  }
3614  else
3615  {
3616    WerrorS("Solver was unable to find any roots!");
3617    return TRUE;
3618  }
3619
3620  // free everything
3621  count= iproots[0]->getAnzElems();
3622  for (i=0; i < count; i++) delete iproots[i];
3623  omFreeSize( (ADDRESS) iproots, count * sizeof(rootContainer*) );
3624  count= muiproots[0]->getAnzElems();
3625  for (i=0; i < count; i++) delete muiproots[i];
3626  omFreeSize( (ADDRESS) muiproots, count * sizeof(rootContainer*) );
3627
3628  delete ures;
3629  delete arranger;
3630  nDelete( &smv );
3631
3632  res->data= (void *)listofroots;
3633
3634  //emptylist->Clean();
3635  //  omFreeSize( (ADDRESS) emptylist, sizeof(slists) );
3636
3637  return FALSE;
3638}
3639
3640// from mpr_numeric.cc
3641lists rootArranger::listOfRoots( const unsigned int oprec )
3642{
3643  int i,j,tr;
3644  int count= roots[0]->getAnzRoots(); // number of roots
3645  int elem= roots[0]->getAnzElems();  // number of koordinates per root
3646
3647  lists listofroots= (lists)omAlloc( sizeof(slists) ); // must be done this way!
3648
3649  if ( found_roots )
3650  {
3651    listofroots->Init( count );
3652
3653    for (i=0; i < count; i++)
3654    {
3655      lists onepoint= (lists)omAlloc(sizeof(slists)); // must be done this way!
3656      onepoint->Init(elem);
3657      for ( j= 0; j < elem; j++ )
3658      {
3659        if ( !rField_is_long_C() )
3660        {
3661          onepoint->m[j].rtyp=STRING_CMD;
3662          onepoint->m[j].data=(void *)complexToStr((*roots[j])[i],oprec);
3663        }
3664        else
3665        {
3666          onepoint->m[j].rtyp=NUMBER_CMD;
3667          onepoint->m[j].data=(void *)nCopy((number)(roots[j]->getRoot(i)));
3668        }
3669        onepoint->m[j].next= NULL;
3670        onepoint->m[j].name= NULL;
3671      }
3672      listofroots->m[i].rtyp=LIST_CMD;
3673      listofroots->m[i].data=(void *)onepoint;
3674      listofroots->m[j].next= NULL;
3675      listofroots->m[j].name= NULL;
3676    }
3677
3678  }
3679  else
3680  {
3681    listofroots->Init( 0 );
3682  }
3683
3684  return listofroots;
3685}
3686
3687#ifdef PDEBUG
3688
3689#if (OM_TRACK > 2) && defined(OM_TRACK_CUSTOM)
3690
3691void p_SetRingOfPoly(poly p, ring r)
3692{
3693  while (p != NULL)
3694  {
3695    p_SetRingOfLm(p, r);
3696    pIter(p);
3697  }
3698}
3699
3700void p_SetRingOfIdeal(ideal id, ring r)
3701{
3702  if (id == NULL) return;
3703
3704  int i, n = id->ncols*id->nrows;
3705
3706  for (i=0; i<n; i++)
3707  {
3708    p_SetRingOfPoly(id->m[i], r);
3709  }
3710}
3711
3712void p_SetRingOfList(lists L, ring r)
3713{
3714  int i;
3715  for (i=0; i<L->nr; i++)
3716  {
3717    p_SetRingOfLeftv(&(L->m[i]), r);
3718  }
3719}
3720
3721void p_SetRingOfCommand(command cmd, ring r)
3722{
3723  if (cmd->op == PROC_CMD && cmd->argc == 2)
3724    p_SetRingOfLeftv(&(cmd->arg2), r);
3725  else if (cmd->argc > 0)
3726  {
3727    p_SetRingOfLeftv(&(cmd->arg1), r);
3728    if (cmd->argc > 1)
3729    {
3730      p_SetRingOfLeftv(&(cmd->arg2), r);
3731      if (cmd->argc > 2)
3732        p_SetRingOfLeftv(&(cmd->arg3), r);
3733    }
3734  }
3735}
3736
3737void p_SetRingOfLeftv(leftv l, ring r)
3738{
3739  while (l != NULL)
3740  {
3741    switch(l->rtyp)
3742    {
3743        case POLY_CMD:
3744        case VECTOR_CMD:
3745          p_SetRingOfPoly((poly) l->data, r);
3746      break;
3747
3748      case IDEAL_CMD:
3749      case MODUL_CMD:
3750      case MATRIX_CMD:
3751      case MAP_CMD:
3752        p_SetRingOfIdeal((ideal) l->data, r);
3753        break;
3754
3755        case LIST_CMD:
3756          p_SetRingOfList((lists) l->data, r);
3757          break;
3758
3759        case COMMAND:
3760          p_SetRingOfCommand((command)l->data, r);
3761        default:
3762          break;
3763    }
3764    l = l->next;
3765  }
3766}
3767#endif // (OM_TRACK > 2) && defined(OM_TRACK_CUSTOM)
3768
3769#endif // PDEBUG
3770
3771// from ring.cc
3772void rSetHdl(idhdl h)
3773{
3774  int i;
3775  ring rg = NULL;
3776  if (h!=NULL)
3777  {
3778//   Print(" new ring:%s (l:%d)\n",IDID(h),IDLEV(h));
3779    rg = IDRING(h);
3780    omCheckAddrSize((ADDRESS)h,sizeof(idrec));
3781    if (IDID(h))  // OB: ????
3782      omCheckAddr((ADDRESS)IDID(h));
3783    rTest(rg);
3784  }
3785
3786  // clean up history
3787  if (sLastPrinted.RingDependend())
3788  {
3789    sLastPrinted.CleanUp();
3790    memset(&sLastPrinted,0,sizeof(sleftv));
3791  }
3792
3793   /*------------ change the global ring -----------------------*/
3794  rChangeCurrRing(rg);
3795  currRingHdl = h;
3796}
3797
3798BOOLEAN rSleftvOrdering2Ordering(sleftv *ord, ring R)
3799{
3800  int last = 0, o=0, n = 1, i=0, typ = 1, j;
3801  sleftv *sl = ord;
3802
3803  // determine nBlocks
3804  while (sl!=NULL)
3805  {
3806    intvec *iv = (intvec *)(sl->data);
3807    if (((*iv)[1]==ringorder_c)||((*iv)[1]==ringorder_C)) i++;
3808    else if ((*iv)[1]==ringorder_L)
3809    {
3810      R->bitmask=(*iv)[2];
3811      n--;
3812    }
3813    else if ((*iv)[1]!=ringorder_a) o++;
3814    n++;
3815    sl=sl->next;
3816  }
3817  // check whether at least one real ordering
3818  if (o==0)
3819  {
3820    WerrorS("invalid combination of orderings");
3821    return TRUE;
3822  }
3823  // if no c/C ordering is given, increment n
3824  if (i==0) n++;
3825  else if (i != 1)
3826  {
3827    // throw error if more than one is given
3828    WerrorS("more than one ordering c/C specified");
3829    return TRUE;
3830  }
3831
3832  // initialize fields of R
3833  R->order=(int *)omAlloc0(n*sizeof(int));
3834  R->block0=(int *)omAlloc0(n*sizeof(int));
3835  R->block1=(int *)omAlloc0(n*sizeof(int));
3836  R->wvhdl=(int**)omAlloc0(n*sizeof(int_ptr));
3837
3838  // init order, so that rBlocks works correctly
3839  for (j=0; j < n-1; j++)
3840    R->order[j] = (int) ringorder_unspec;
3841  // set last _C order, if no c/C order was given
3842  if (i == 0) R->order[n-2] = ringorder_C;
3843
3844  /* init orders */
3845  sl=ord;
3846  n=-1;
3847  while (sl!=NULL)
3848  {
3849    intvec *iv;
3850    iv = (intvec *)(sl->data);
3851    if ((*iv)[1]!=ringorder_L)
3852    {
3853      n++;
3854
3855      /* the format of an ordering:
3856       *  iv[0]: factor
3857       *  iv[1]: ordering
3858       *  iv[2..end]: weights
3859       */
3860      R->order[n] = (*iv)[1];
3861      switch ((*iv)[1])
3862      {
3863          case ringorder_ws:
3864          case ringorder_Ws:
3865            typ=-1;
3866          case ringorder_wp:
3867          case ringorder_Wp:
3868            R->wvhdl[n]=(int*)omAlloc((iv->length()-1)*sizeof(int));
3869            for (i=2; i<iv->length(); i++)
3870              R->wvhdl[n][i-2] = (*iv)[i];
3871            R->block0[n] = last+1;
3872            last += iv->length()-2;
3873            R->block1[n] = last;
3874            break;
3875          case ringorder_ls:
3876          case ringorder_ds:
3877          case ringorder_Ds:
3878            typ=-1;
3879          case ringorder_lp:
3880          case ringorder_dp:
3881          case ringorder_Dp:
3882          case ringorder_rp:
3883            R->block0[n] = last+1;
3884            if (iv->length() == 3) last+=(*iv)[2];
3885            else last += (*iv)[0];
3886            R->block1[n] = last;
3887            if (rCheckIV(iv)) return TRUE;
3888            break;
3889          case ringorder_S:
3890          case ringorder_c:
3891          case ringorder_C:
3892            if (rCheckIV(iv)) return TRUE;
3893            break;
3894          case ringorder_aa:
3895          case ringorder_a:
3896            R->block0[n] = last+1;
3897            R->block1[n] = si_min(last+iv->length()-2 , R->N);
3898            R->wvhdl[n] = (int*)omAlloc((iv->length()-1)*sizeof(int));
3899            for (i=2; i<iv->length(); i++)
3900            {
3901              R->wvhdl[n][i-2]=(*iv)[i];
3902              if ((*iv)[i]<0) typ=-1;
3903            }
3904            break;
3905          case ringorder_M:
3906          {
3907            int Mtyp=rTypeOfMatrixOrder(iv);
3908            if (Mtyp==0) return TRUE;
3909            if (Mtyp==-1) typ = -1;
3910
3911            R->wvhdl[n] =( int *)omAlloc((iv->length()-1)*sizeof(int));
3912            for (i=2; i<iv->length();i++)
3913              R->wvhdl[n][i-2]=(*iv)[i];
3914
3915            R->block0[n] = last+1;
3916            last += (int)sqrt((double)(iv->length()-2));
3917            R->block1[n] = last;
3918            break;
3919          }
3920
3921          case ringorder_no:
3922            R->order[n] = ringorder_unspec;
3923            return TRUE;
3924
3925          default:
3926            Werror("Internal Error: Unknown ordering %d", (*iv)[1]);
3927            R->order[n] = ringorder_unspec;
3928            return TRUE;
3929      }
3930    }
3931    sl=sl->next;
3932  }
3933
3934  // check for complete coverage
3935  if ((R->order[n]==ringorder_c) ||  (R->order[n]==ringorder_C)) n--;
3936  if (R->block1[n] != R->N)
3937  {
3938    if (((R->order[n]==ringorder_dp) ||
3939         (R->order[n]==ringorder_ds) ||
3940         (R->order[n]==ringorder_Dp) ||
3941         (R->order[n]==ringorder_Ds) ||
3942         (R->order[n]==ringorder_rp) ||
3943         (R->order[n]==ringorder_lp) ||
3944         (R->order[n]==ringorder_ls))
3945        &&
3946        R->block0[n] <= R->N)
3947    {
3948      R->block1[n] = R->N;
3949    }
3950    else
3951    {
3952      Werror("mismatch of number of vars (%d) and ordering (%d vars)",
3953             R->N,R->block1[n]);
3954      return TRUE;
3955    }
3956  }
3957  R->OrdSgn = typ;
3958  return FALSE;
3959}
3960
3961BOOLEAN rSleftvList2StringArray(sleftv* sl, char** p)
3962{
3963
3964  while(sl!=NULL)
3965  {
3966    if (sl->Name() == sNoName)
3967    {
3968      if (sl->Typ()==POLY_CMD)
3969      {
3970        sleftv s_sl;
3971        iiConvert(POLY_CMD,ANY_TYPE,-1,sl,&s_sl);
3972        if (s_sl.Name() != sNoName)
3973          *p = omStrDup(s_sl.Name());
3974        else
3975          *p = NULL;
3976        sl->next = s_sl.next;
3977        s_sl.next = NULL;
3978        s_sl.CleanUp();
3979        if (*p == NULL) return TRUE;
3980      }
3981      else
3982        return TRUE;
3983    }
3984    else
3985      *p = omStrDup(sl->Name());
3986    p++;
3987    sl=sl->next;
3988  }
3989  return FALSE;
3990}
3991
3992////////////////////
3993//
3994// rInit itself:
3995//
3996// INPUT:  s: name, pn: ch & parameter (names), rv: variable (names)
3997//         ord: ordering
3998// RETURN: currRingHdl on success
3999//         NULL        on error
4000// NOTE:   * makes new ring to current ring, on success
4001//         * considers input sleftv's as read-only
4002//idhdl rInit(char *s, sleftv* pn, sleftv* rv, sleftv* ord)
4003ring rInit(sleftv* pn, sleftv* rv, sleftv* ord)
4004{
4005  int ch;
4006  int float_len=0;
4007  int float_len2=0;
4008  ring R = NULL;
4009  idhdl tmp = NULL;
4010  BOOLEAN ffChar=FALSE;
4011  int typ = 1;
4012
4013  /* ch -------------------------------------------------------*/
4014  // get ch of ground field
4015  int numberOfAllocatedBlocks;
4016
4017  if (pn->Typ()==INT_CMD)
4018  {
4019    ch=(int)pn->Data();
4020  }
4021  else if ((pn->name != NULL)
4022  && ((strcmp(pn->name,"real")==0) || (strcmp(pn->name,"complex")==0)))
4023  {
4024    BOOLEAN complex_flag=(strcmp(pn->name,"complex")==0);
4025    ch=-1;
4026    if ((pn->next!=NULL) && (pn->next->Typ()==INT_CMD))
4027    {
4028      float_len=(int)pn->next->Data();
4029      float_len2=float_len;
4030      pn=pn->next;
4031      if ((pn->next!=NULL) && (pn->next->Typ()==INT_CMD))
4032      {
4033        float_len2=(int)pn->next->Data();
4034        pn=pn->next;
4035      }
4036    }
4037    if ((pn->next==NULL) && complex_flag)
4038    {
4039      pn->next=(leftv)omAlloc0Bin(sleftv_bin);
4040      pn->next->name=omStrDup("i");
4041    }
4042  }
4043  else
4044  {
4045    Werror("Wrong ground field specification");
4046    goto rInitError;
4047  }
4048  pn=pn->next;
4049
4050  int l, last;
4051  sleftv * sl;
4052  /*every entry in the new ring is initialized to 0*/
4053
4054  /* characteristic -----------------------------------------------*/
4055  /* input: 0 ch=0 : Q     parameter=NULL    ffChar=FALSE   float_len
4056   *         0    1 : Q(a,...)        *names         FALSE
4057   *         0   -1 : R               NULL           FALSE  0
4058   *         0   -1 : R               NULL           FALSE  prec. >6
4059   *         0   -1 : C               *names         FALSE  prec. 0..?
4060   *         p    p : Fp              NULL           FALSE
4061   *         p   -p : Fp(a)           *names         FALSE
4062   *         q    q : GF(q=p^n)       *names         TRUE
4063  */
4064  if (ch!=-1)
4065  {
4066    int l = 0;
4067
4068    if (ch!=0 && (ch<2)
4069    #ifndef NV_OPS
4070    || (ch > 32003)
4071    #endif
4072    )
4073    {
4074      Warn("%d is invalid characteristic of ground field. 32003 is used.", ch);
4075      ch=32003;
4076    }
4077    // load fftable, if necessary
4078    if (pn!=NULL)
4079    {
4080      while ((ch!=fftable[l]) && (fftable[l])) l++;
4081      if (fftable[l]==0) ch = IsPrime(ch);
4082      else
4083      {
4084        char *m[1]={(char *)sNoName};
4085        nfSetChar(ch,m);
4086        if (errorreported) goto rInitError;
4087        else ffChar=TRUE;
4088      }
4089    }
4090    else
4091      ch = IsPrime(ch);
4092  }
4093  // allocated ring and set ch
4094  R = (ring) omAlloc0Bin(sip_sring_bin);
4095  R->ch = ch;
4096  if (ch == -1)
4097  {
4098    R->float_len= si_min(float_len,32767);
4099    R->float_len2= si_min(float_len2,32767);
4100  }
4101
4102  /* parameter -------------------------------------------------------*/
4103  if (pn!=NULL)
4104  {
4105    R->P=pn->listLength();
4106    //if ((ffChar|| (ch == 1)) && (R->P > 1))
4107    if ((R->P > 1) && (ffChar || (ch == -1)))
4108    {
4109      WerrorS("too many parameters");
4110      goto rInitError;
4111    }
4112    R->parameter=(char**)omAlloc0(R->P*sizeof(char_ptr));
4113    if (rSleftvList2StringArray(pn, R->parameter))
4114    {
4115      WerrorS("parameter expected");
4116      goto rInitError;
4117    }
4118    if (ch>1 && !ffChar) R->ch=-ch;
4119    else if (ch==0) R->ch=1;
4120  }
4121  else if (ffChar)
4122  {
4123    WerrorS("need one parameter");
4124    goto rInitError;
4125  }
4126  /* post-processing of field description */
4127  // we have short reals, but no short complex
4128  if ((R->ch == - 1)
4129  && (R->parameter !=NULL)
4130  && (R->float_len < SHORT_REAL_LENGTH))
4131  {
4132    R->float_len = SHORT_REAL_LENGTH;
4133    R->float_len2 = SHORT_REAL_LENGTH;
4134  }
4135
4136  /* names and number of variables-------------------------------------*/
4137  {
4138    int l=rv->listLength();
4139#if SIZEOF_SHORT == 2
4140#define MAX_SHORT 0x7fff
4141#endif
4142    if (l>MAX_SHORT)
4143    {
4144      Werror("too many ring variables(%d), max is %d",l,MAX_SHORT);
4145       goto rInitError;
4146    }
4147    R->N = l; /*rv->listLength();*/
4148  }
4149  R->names   = (char **)omAlloc0(R->N * sizeof(char_ptr));
4150  if (rSleftvList2StringArray(rv, R->names))
4151  {
4152    WerrorS("name of ring variable expected");
4153    goto rInitError;
4154  }
4155
4156  /* check names and parameters for conflicts ------------------------- */
4157  {
4158    int i,j;
4159    for(i=0;i<R->P; i++)
4160    {
4161      for(j=0;j<R->N;j++)
4162      {
4163        if (strcmp(R->parameter[i],R->names[j])==0)
4164        {
4165          Werror("parameter %d conflicts with variable %d",i+1,j+1);
4166          goto rInitError;
4167        }
4168      }
4169    }
4170  }
4171  /* ordering -------------------------------------------------------------*/
4172  if (rSleftvOrdering2Ordering(ord, R))
4173    goto rInitError;
4174
4175  // Complete the initialization
4176  if (rComplete(R,1))
4177    goto rInitError;
4178
4179  rTest(R);
4180
4181  // try to enter the ring into the name list
4182  // need to clean up sleftv here, before this ring can be set to
4183  // new currRing or currRing can be killed beacuse new ring has
4184  // same name
4185  if (pn != NULL) pn->CleanUp();
4186  if (rv != NULL) rv->CleanUp();
4187  if (ord != NULL) ord->CleanUp();
4188  //if ((tmp = enterid(s, myynest, RING_CMD, &IDROOT))==NULL)
4189  //  goto rInitError;
4190
4191  //memcpy(IDRING(tmp),R,sizeof(*R));
4192  // set current ring
4193  //omFreeBin(R,  ip_sring_bin);
4194  //return tmp;
4195  return R;
4196
4197  // error case:
4198  rInitError:
4199  if  (R != NULL) rDelete(R);
4200  if (pn != NULL) pn->CleanUp();
4201  if (rv != NULL) rv->CleanUp();
4202  if (ord != NULL) ord->CleanUp();
4203  return NULL;
4204}
4205
4206void rKill(ring r)
4207{
4208  if ((r->ref<=0)&&(r->order!=NULL))
4209  {
4210#ifdef RDEBUG
4211    if (traceit &TRACE_SHOW_RINGS) Print("kill ring %x\n",r);
4212#endif
4213    if (r==currRing)
4214    {
4215      if (r->qideal!=NULL)
4216      {
4217        idDelete(&r->qideal);
4218        r->qideal=NULL;
4219        currQuotient=NULL;
4220      }
4221      if (ppNoether!=NULL) pDelete(&ppNoether);
4222      if (sLastPrinted.RingDependend())
4223      {
4224        sLastPrinted.CleanUp();
4225      }
4226      if ((myynest>0) && (iiRETURNEXPR[myynest].RingDependend()))
4227      {
4228        WerrorS("return value depends on local ring variable (export missing ?)");
4229        iiRETURNEXPR[myynest].CleanUp();
4230      }
4231      currRing=NULL;
4232      currRingHdl=NULL;
4233    }
4234    else if (r->qideal!=NULL)
4235    {
4236      id_Delete(&r->qideal, r);
4237      r->qideal = NULL;
4238    }
4239    int i=1;
4240    int j;
4241    int *pi=r->order;
4242#ifdef USE_IILOCALRING
4243    for (j=0;j<iiRETURNEXPR_len;j++)
4244    {
4245      if (iiLocalRing[j]==r)
4246      {
4247        if (j<myynest) Warn("killing the basering for level %d",j);
4248        iiLocalRing[j]=NULL;
4249      }
4250    }
4251#else /* USE_IILOCALRING */
4252//#endif /* USE_IILOCALRING */
4253    {
4254      proclevel * nshdl = procstack;
4255      int lev=myynest-1;
4256
4257      for(; nshdl != NULL; nshdl = nshdl->next)
4258      {
4259        if (nshdl->cRing==r)
4260        {
4261          Warn("killing the basering for level %d",lev);
4262          nshdl->cRing=NULL;
4263          nshdl->cRingHdl=NULL;
4264        }
4265      }
4266    }
4267#endif /* USE_IILOCALRING */
4268
4269    /* nKillChar(r); will be called from inside of rDelete */
4270    rDelete(r);
4271    return;
4272  }
4273  r->ref--;
4274}
4275
4276void rKill(idhdl h)
4277{
4278  ring r = IDRING(h);
4279  int ref=0;
4280  if (r!=NULL)
4281  {
4282    ref=r->ref;
4283    rKill(r);
4284  }
4285  if (h==currRingHdl)
4286  {
4287    if (ref<=0) { currRing=NULL; currRingHdl=NULL;}
4288    else
4289    {
4290      currRingHdl=rFindHdl(r,currRingHdl,NULL);
4291    }
4292  }
4293}
4294
4295idhdl rSimpleFindHdl(ring r, idhdl root, idhdl n=NULL)
4296{
4297  //idhdl next_best=NULL;
4298  idhdl h=root;
4299  while (h!=NULL)
4300  {
4301    if (((IDTYP(h)==RING_CMD)||(IDTYP(h)==QRING_CMD))
4302    && (h!=n)
4303    && (IDRING(h)==r)
4304    )
4305    {
4306   //   if (IDLEV(h)==myynest)
4307   //     return h;
4308   //   if ((IDLEV(h)==0) || (next_best==NULL))
4309   //     next_best=h;
4310   //   else if (IDLEV(next_best)<IDLEV(h))
4311   //     next_best=h;
4312      return h;
4313    }
4314    h=IDNEXT(h);
4315  }
4316  //return next_best;
4317  return NULL;
4318}
4319
Note: See TracBrowser for help on using the repository browser.