source: git/Singular/ipshell.cc @ 350a6d

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