source: git/Singular/ipshell.cc @ cfa930

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