source: git/Singular/ipshell.cc @ 091b0e8

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