source: git/Singular/ipshell.cc @ 28543b

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