source: git/Singular/ipshell.cc @ 4a7c16

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