source: git/Singular/ipshell.cc @ dee5f7

spielwiese
Last change on this file since dee5f7 was 08db0a, checked in by Hans Schönemann <hannes@…>, 19 years ago
*hannes: gcc-3.4 git-svn-id: file:///usr/local/Singular/svn/trunk@7734 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 105.2 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: ipshell.cc,v 1.97 2005-02-17 09:49:20 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    R->ch=R->algring->ch;
1730    R->parameter=R->algring->names;
1731    R->P=R->algring->N;
1732  }
1733  else
1734  {
1735    WerrorS("coefficient field must be described by `int` or `list`");
1736    goto rCompose_err;
1737  }
1738  // ------------------------- VARS ---------------------------
1739  if (L->m[1].Typ()==LIST_CMD)
1740  {
1741    lists v=(lists)L->m[1].Data();
1742    R->N = v->nr+1;
1743    R->names   = (char **)omAlloc0(R->N * sizeof(char_ptr));
1744    int i;
1745    for(i=0;i<R->N;i++)
1746    {
1747      if (v->m[i].Typ()==STRING_CMD)
1748        R->names[i]=omStrDup((char *)v->m[i].Data());
1749      else if (v->m[i].Typ()==POLY_CMD)
1750      {
1751        poly p=(poly)v->m[i].Data();
1752        int nr=pIsPurePower(p);
1753        if (nr>0)
1754          R->names[i]=omStrDup(currRing->names[nr-1]);
1755        else
1756        {
1757          Werror("var name %d must be a string or a ring variable",i+1);
1758          goto rCompose_err;
1759        }
1760      }
1761      else
1762      {
1763        Werror("var name %d must be `string`",i+1);
1764        goto rCompose_err;
1765      }
1766    }
1767  }
1768  else
1769  {
1770    WerrorS("variable must be given as `list`");
1771    goto rCompose_err;
1772  }
1773  // ------------------------ ORDER ------------------------------
1774  if (L->m[2].Typ()==LIST_CMD)
1775  {
1776    lists v=(lists)L->m[2].Data();
1777    int n= v->nr+2;
1778    int j;
1779    // initialize fields of R
1780    R->order=(int *)omAlloc0(n*sizeof(int));
1781    R->block0=(int *)omAlloc0(n*sizeof(int));
1782    R->block1=(int *)omAlloc0(n*sizeof(int));
1783    R->wvhdl=(int**)omAlloc0(n*sizeof(int_ptr));
1784    // init order, so that rBlocks works correctly
1785    for (j=0; j < n-2; j++)
1786      R->order[j] = (int) ringorder_unspec;
1787    // orderings
1788    R->OrdSgn=1;
1789    for(j=0;j<n-1;j++)
1790    {
1791    // todo: a(..), M
1792      if (v->m[j].Typ()!=LIST_CMD)
1793      {
1794        WerrorS("ordering must be list of lists");
1795        goto rCompose_err;
1796      }
1797      lists vv=(lists)v->m[j].Data();
1798      if ((vv->nr!=1)
1799      || (vv->m[0].Typ()!=STRING_CMD)
1800      || ((vv->m[1].Typ()!=INTVEC_CMD) && (vv->m[1].Typ()!=INT_CMD)))
1801      {
1802        WerrorS("ordering name must be a (string,intvec)");
1803        goto rCompose_err;
1804      }
1805      R->order[j]=rOrderName(omStrDup((char*)vv->m[0].Data())); // assume STRING
1806      if (j==0) R->block0[0]=1;
1807      else      R->block0[j]=R->block1[j-1]+1;
1808      intvec *iv;
1809      if (vv->m[1].Typ()==INT_CMD)
1810        iv=new intvec((int)vv->m[1].Data(),(int)vv->m[1].Data());
1811      else
1812        iv=ivCopy((intvec*)vv->m[1].Data()); //assume INTVEC
1813      R->block1[j]=max(R->block0[j],R->block0[j]+iv->length()-1);
1814      int i;
1815      switch (R->order[j])
1816      {
1817         case ringorder_ws:
1818         case ringorder_Ws:
1819            R->OrdSgn=-1;
1820         case ringorder_wp:
1821         case ringorder_Wp:
1822           R->wvhdl[j] =( int *)omAlloc((iv->length())*sizeof(int));
1823           for (i=0; i<iv->length();i++) R->wvhdl[j][i]=(*iv)[i];
1824           break;
1825         case ringorder_ls:
1826         case ringorder_ds:
1827         case ringorder_Ds:
1828           R->OrdSgn=-1;
1829         case ringorder_lp:
1830         case ringorder_dp:
1831         case ringorder_Dp:
1832         case ringorder_rp:
1833           break;
1834         case ringorder_S:
1835           break;
1836         case ringorder_c:
1837         case ringorder_C:
1838           R->block1[j]=R->block0[j]-1;
1839           break;
1840         case ringorder_aa:
1841         case ringorder_a:
1842           R->wvhdl[j] =( int *)omAlloc((iv->length())*sizeof(int));
1843           for (i=1; i<iv->length();i++) R->wvhdl[n][i-1]=(*iv)[i];
1844         // todo
1845           break;
1846         case ringorder_M:
1847         // todo
1848           break;
1849      }
1850    }
1851    // sanity check
1852    j=n-2;
1853    if ((R->order[j]==ringorder_c)
1854    || (R->order[j]==ringorder_C)) j--;
1855    if (R->block1[j] != R->N)
1856    {
1857      if (((R->order[j]==ringorder_dp) ||
1858           (R->order[j]==ringorder_ds) ||
1859           (R->order[j]==ringorder_Dp) ||
1860           (R->order[j]==ringorder_Ds) ||
1861           (R->order[j]==ringorder_rp) ||
1862           (R->order[j]==ringorder_lp) ||
1863           (R->order[j]==ringorder_ls))
1864          &&
1865            R->block0[j] <= R->N)
1866      {
1867        R->block1[j] = R->N;
1868      }
1869      else
1870      {
1871        Werror("ordering incomplete: size (%d) should be %d",R->block1[j],R->N);
1872        goto rCompose_err;
1873      }
1874    }
1875  }
1876  else
1877  {
1878    WerrorS("ordering must be given as `list`");
1879    goto rCompose_err;
1880  }
1881  // ------------------------ Q-IDEAL ------------------------
1882  if (L->m[3].Typ()==IDEAL_CMD)
1883  {
1884    ideal q=(ideal)L->m[3].Data();
1885    if (q->m[0]!=NULL)
1886      R->qideal=idCopy(q);
1887  }
1888  else
1889  {
1890    WerrorS("q-ideal must be given as `ideal`");
1891    goto rCompose_err;
1892  }
1893
1894  // todo
1895  rComplete(R);
1896
1897  // ---------------------------------------------------------------
1898  #ifdef HAVE_PLURAL
1899  if (L->nr==5)
1900  {
1901    if (nc_CallPlural((matrix)L->m[4].Data(),(matrix)L->m[5].Data(),
1902        NULL,NULL,R))
1903      goto rCompose_err;
1904  }
1905  #endif
1906  return R;
1907
1908rCompose_err:
1909  if (R->N>0)
1910  {
1911    int i;
1912    if (R->names!=NULL)
1913    {
1914      i=R->N;
1915      while (i>=0) { if (R->names[i]!=NULL) omFree(R->names[i]); i--; }
1916      omFree(R->names);
1917    }
1918  }
1919  if (R->order!=NULL) omFree(R->order);
1920  if (R->block0!=NULL) omFree(R->block0);
1921  if (R->block1!=NULL) omFree(R->block1);
1922  if (R->wvhdl!=NULL) omFree(R->wvhdl);
1923  omFree(R);
1924  return NULL;
1925}
1926
1927// from matpol.cc
1928
1929/*2
1930* compute the jacobi matrix of an ideal
1931*/
1932BOOLEAN mpJacobi(leftv res,leftv a)
1933{
1934  int     i,j;
1935  matrix result;
1936  ideal id=(ideal)a->Data();
1937
1938  result =mpNew(IDELEMS(id),pVariables);
1939  for (i=1; i<=IDELEMS(id); i++)
1940  {
1941    for (j=1; j<=pVariables; j++)
1942    {
1943      MATELEM(result,i,j) = pDiff(id->m[i-1],j);
1944    }
1945  }
1946  res->data=(char *)result;
1947  return FALSE;
1948}
1949
1950/*2
1951* returns the Koszul-matrix of degree d of a vectorspace with dimension n
1952* uses the first n entrees of id, if id <> NULL
1953*/
1954BOOLEAN mpKoszul(leftv res,leftv c/*ip*/, leftv b/*in*/, leftv id)
1955{
1956  int n=(int)b->Data();
1957  int d=(int)c->Data();
1958  int     k,l,sign,row,col;
1959  matrix  result;
1960  ideal temp;
1961  BOOLEAN bo;
1962  poly    p;
1963
1964  if ((d>n) || (d<1) || (n<1))
1965  {
1966    res->data=(char *)mpNew(1,1);
1967    return FALSE;
1968  }
1969  int *choise = (int*)omAlloc(d*sizeof(int));
1970  if (id==NULL)
1971    temp=idMaxIdeal(1);
1972  else
1973    temp=(ideal)id->Data();
1974
1975  k = binom(n,d);
1976  l = k*d;
1977  l /= n-d+1;
1978  result =mpNew(l,k);
1979  col = 1;
1980  idInitChoise(d,1,n,&bo,choise);
1981  while (!bo)
1982  {
1983    sign = 1;
1984    for (l=1;l<=d;l++)
1985    {
1986      if (choise[l-1]<=IDELEMS(temp))
1987      {
1988        p = pCopy(temp->m[choise[l-1]-1]);
1989        if (sign == -1) p = pNeg(p);
1990        sign *= -1;
1991        row = idGetNumberOfChoise(l-1,d,1,n,choise);
1992        MATELEM(result,row,col) = p;
1993      }
1994    }
1995    col++;
1996    idGetNextChoise(d,n,&bo,choise);
1997  }
1998  if (id==NULL) idDelete(&temp);
1999
2000  res->data=(char *)result;
2001  return FALSE;
2002}
2003
2004// from syz1.cc
2005/*2
2006* read out the Betti numbers from resolution
2007* (interpreter interface)
2008*/
2009BOOLEAN syBetti2(leftv res, leftv u, leftv w)
2010{
2011  syStrategy syzstr=(syStrategy)u->Data();
2012  BOOLEAN minim=(int)w->Data();
2013  int row_shift=0;
2014  int add_row_shift=0;
2015  intvec *weights=NULL;
2016  intvec *ww=(intvec *)atGet(u,"isHomog",INTVEC_CMD);
2017  if (ww!=NULL)
2018  {
2019     weights=ivCopy(ww);
2020     add_row_shift = ww->min_in();
2021     (*weights) -= add_row_shift;
2022  }
2023  res->data=(void *)syBettiOfComputation(syzstr,minim,&row_shift,weights);
2024  //row_shift += add_row_shift;
2025  //Print("row_shift=%d, add_row_shift=%d\n",row_shift,add_row_shift);
2026  atSet(res,omStrDup("rowShift"),(void*)add_row_shift,INT_CMD);
2027  return FALSE;
2028}
2029BOOLEAN syBetti1(leftv res, leftv u)
2030{
2031  sleftv tmp;
2032  memset(&tmp,0,sizeof(tmp));
2033  tmp.rtyp=INT_CMD;
2034  tmp.data=(void *)1; 
2035  return syBetti2(res,u,&tmp);
2036}
2037
2038/*3
2039* converts a resolution into a list of modules
2040*/
2041lists syConvRes(syStrategy syzstr,BOOLEAN toDel)
2042{
2043  if ((syzstr->fullres==NULL) && (syzstr->minres==NULL))
2044  {
2045    if (syzstr->hilb_coeffs==NULL)
2046    {
2047      syzstr->fullres = syReorder(syzstr->res,syzstr->length,syzstr);
2048    }
2049    else
2050    {
2051      syzstr->minres = syReorder(syzstr->orderedRes,syzstr->length,syzstr);
2052      syKillEmptyEntres(syzstr->minres,syzstr->length);
2053    }
2054  }
2055  resolvente tr;
2056  int typ0=IDEAL_CMD;
2057  if (syzstr->minres!=NULL)
2058    tr = syzstr->minres;
2059  else
2060    tr = syzstr->fullres;
2061  resolvente trueres=NULL;
2062  intvec ** w=NULL;
2063  if (syzstr->length>0)
2064  {
2065    trueres=(resolvente)omAlloc0((syzstr->length)*sizeof(ideal));
2066    for (int i=(syzstr->length)-1;i>=0;i--)
2067    {
2068      if (tr[i]!=NULL)
2069      {
2070        trueres[i] = idCopy(tr[i]);
2071      }
2072    }
2073    if (idRankFreeModule(trueres[0]) > 0)
2074      typ0 = MODUL_CMD;
2075    if (syzstr->weights!=NULL)
2076    {
2077      w = (intvec**)omAlloc0((syzstr->length)*sizeof(intvec*));
2078      for (int i=(syzstr->length)-1;i>=0;i--)
2079      {
2080        if (syzstr->weights[i]!=NULL) w[i] = ivCopy(syzstr->weights[i]);
2081      }
2082    }
2083  }
2084  lists li = liMakeResolv(trueres,syzstr->length,syzstr->list_length,typ0,w);
2085  if (w != NULL) omFreeSize(w, (syzstr->length)*sizeof(intvec*));
2086  if (toDel) syKillComputation(syzstr);
2087  return li;
2088}
2089
2090/*3
2091* converts a list of modules into a resolution
2092*/
2093syStrategy syConvList(lists li,BOOLEAN toDel)
2094{
2095  int typ0;
2096  syStrategy result=(syStrategy)omAlloc0(sizeof(ssyStrategy));
2097
2098  resolvente fr = liFindRes(li,&(result->length),&typ0,&(result->weights));
2099  if (fr != NULL)
2100  {
2101
2102    result->fullres = (resolvente)omAlloc0((result->length+1)*sizeof(ideal));
2103    for (int i=result->length-1;i>=0;i--)
2104    {
2105      if (fr[i]!=NULL)
2106        result->fullres[i] = idCopy(fr[i]);
2107    }
2108    result->list_length=result->length;
2109    omFreeSize((ADDRESS)fr,(result->length)*sizeof(ideal));
2110  }
2111  else
2112  {
2113    omFreeSize(result, sizeof(ssyStrategy));
2114    result = NULL;
2115  }
2116  if (toDel) li->Clean();
2117  return result;
2118}
2119
2120/*3
2121* converts a list of modules into a minimal resolution
2122*/
2123syStrategy syForceMin(lists li)
2124{
2125  int typ0;
2126  syStrategy result=(syStrategy)omAlloc0(sizeof(ssyStrategy));
2127
2128  resolvente fr = liFindRes(li,&(result->length),&typ0);
2129  result->minres = (resolvente)omAlloc0((result->length+1)*sizeof(ideal));
2130  for (int i=result->length-1;i>=0;i--)
2131  {
2132    if (fr[i]!=NULL)
2133      result->minres[i] = idCopy(fr[i]);
2134  }
2135  omFreeSize((ADDRESS)fr,(result->length)*sizeof(ideal));
2136  return result;
2137}
2138// from weight.cc
2139BOOLEAN kWeight(leftv res,leftv id)
2140{
2141  ideal F=(ideal)id->Data();
2142  intvec * iv = new intvec(pVariables);
2143  polyset s;
2144  int  sl, n, i;
2145  int  *x;
2146
2147  res->data=(char *)iv;
2148  s = F->m;
2149  sl = IDELEMS(F) - 1;
2150  n = pVariables;
2151  wNsqr = (double)2.0 / (double)n;
2152  wFunctional = wFunctionalBuch;
2153  x = (int * )omAlloc(2 * (n + 1) * sizeof(int));
2154  wCall(s, sl, x);
2155  for (i = n; i!=0; i--)
2156    (*iv)[i-1] = x[i + n + 1];
2157  omFreeSize((ADDRESS)x, 2 * (n + 1) * sizeof(int));
2158  return FALSE;
2159}
2160
2161BOOLEAN kQHWeight(leftv res,leftv v)
2162{
2163  res->data=(char *)idQHomWeight((ideal)v->Data());
2164  if (res->data==NULL)
2165    res->data=(char *)new intvec(pVariables);
2166  return FALSE;
2167}
2168/*==============================================================*/
2169// from clapsing.cc
2170#if 0
2171BOOLEAN jjIS_SQR_FREE(leftv res, leftv u)
2172{
2173  BOOLEAN b=singclap_factorize((poly)(u->Data()), &v, 0);
2174  res->data=(void *)b;
2175}
2176#endif
2177
2178BOOLEAN jjRESULTANT(leftv res, leftv u, leftv v, leftv w)
2179{
2180  res->data=singclap_resultant((poly)u->Data(),(poly)v->Data(), (poly)w->Data());
2181  return errorreported;
2182}
2183BOOLEAN jjCHARSERIES(leftv res, leftv u)
2184{
2185  res->data=singclap_irrCharSeries((ideal)u->Data());
2186  return (res->data==NULL);
2187}
2188
2189// from semic.cc
2190#ifdef HAVE_SPECTRUM
2191
2192// ----------------------------------------------------------------------------
2193//  Initialize a  spectrum  deep from another  spectrum
2194// ----------------------------------------------------------------------------
2195
2196void spectrum::copy_deep( const spectrum &spec )
2197{
2198    mu = spec.mu;
2199    pg = spec.pg;
2200    n  = spec.n;
2201
2202    copy_new( n );
2203
2204    for( int i=0; i<n; i++ )
2205    {
2206        s[i] = spec.s[i];
2207        w[i] = spec.w[i];
2208    }
2209}
2210
2211// ----------------------------------------------------------------------------
2212//  Initialize a  spectrum  deep from a  singular  lists
2213// ----------------------------------------------------------------------------
2214
2215void spectrum::copy_deep( lists l )
2216{
2217    mu = (int)(l->m[0].Data( ));
2218    pg = (int)(l->m[1].Data( ));
2219    n  = (int)(l->m[2].Data( ));
2220
2221    copy_new( n );
2222
2223    intvec  *num = (intvec*)l->m[3].Data( );
2224    intvec  *den = (intvec*)l->m[4].Data( );
2225    intvec  *mul = (intvec*)l->m[5].Data( );
2226
2227    for( int i=0; i<n; i++ )
2228    {
2229        s[i] = (Rational)((*num)[i])/(Rational)((*den)[i]);
2230        w[i] = (*mul)[i];
2231    }
2232}
2233
2234// ----------------------------------------------------------------------------
2235//  singular lists  constructor for  spectrum
2236// ----------------------------------------------------------------------------
2237
2238spectrum::spectrum( lists l )
2239{
2240    copy_deep( l );
2241}
2242
2243// ----------------------------------------------------------------------------
2244//  generate a Singular  lists  from a spectrum
2245// ----------------------------------------------------------------------------
2246
2247lists   spectrum::thelist( void )
2248{
2249    lists   L  = (lists)omAllocBin( slists_bin);
2250
2251    L->Init( 6 );
2252
2253    intvec            *num  = new intvec( n );
2254    intvec            *den  = new intvec( n );
2255    intvec            *mult = new intvec( n );
2256
2257    for( int i=0; i<n; i++ )
2258    {
2259        (*num) [i] = s[i].get_num_si( );
2260        (*den) [i] = s[i].get_den_si( );
2261        (*mult)[i] = w[i];
2262    }
2263
2264    L->m[0].rtyp = INT_CMD;    //  milnor number
2265    L->m[1].rtyp = INT_CMD;    //  geometrical genus
2266    L->m[2].rtyp = INT_CMD;    //  # of spectrum numbers
2267    L->m[3].rtyp = INTVEC_CMD; //  numerators
2268    L->m[4].rtyp = INTVEC_CMD; //  denomiantors
2269    L->m[5].rtyp = INTVEC_CMD; //  multiplicities
2270
2271    L->m[0].data = (void*)mu;
2272    L->m[1].data = (void*)pg;
2273    L->m[2].data = (void*)n;
2274    L->m[3].data = (void*)num;
2275    L->m[4].data = (void*)den;
2276    L->m[5].data = (void*)mult;
2277
2278    return  L;
2279}
2280// from spectrum.cc
2281// ----------------------------------------------------------------------------
2282//  print out an error message for a spectrum list
2283// ----------------------------------------------------------------------------
2284
2285void    list_error( semicState state )
2286{
2287    switch( state )
2288    {
2289        case semicListTooShort:
2290            WerrorS( "the list is too short" );
2291            break;
2292        case semicListTooLong:
2293            WerrorS( "the list is too long" );
2294            break;
2295
2296        case semicListFirstElementWrongType:
2297            WerrorS( "first element of the list should be int" );
2298            break;
2299        case semicListSecondElementWrongType:
2300            WerrorS( "second element of the list should be int" );
2301            break;
2302        case semicListThirdElementWrongType:
2303            WerrorS( "third element of the list should be int" );
2304            break;
2305        case semicListFourthElementWrongType:
2306            WerrorS( "fourth element of the list should be intvec" );
2307            break;
2308        case semicListFifthElementWrongType:
2309            WerrorS( "fifth element of the list should be intvec" );
2310            break;
2311        case semicListSixthElementWrongType:
2312            WerrorS( "sixth element of the list should be intvec" );
2313            break;
2314
2315        case semicListNNegative:
2316            WerrorS( "first element of the list should be positive" );
2317            break;
2318        case semicListWrongNumberOfNumerators:
2319            WerrorS( "wrong number of numerators" );
2320            break;
2321        case semicListWrongNumberOfDenominators:
2322            WerrorS( "wrong number of denominators" );
2323            break;
2324        case semicListWrongNumberOfMultiplicities:
2325            WerrorS( "wrong number of multiplicities" );
2326            break;
2327
2328        case semicListMuNegative:
2329            WerrorS( "the Milnor number should be positive" );
2330            break;
2331        case semicListPgNegative:
2332            WerrorS( "the geometrical genus should be nonnegative" );
2333            break;
2334        case semicListNumNegative:
2335            WerrorS( "all numerators should be positive" );
2336            break;
2337        case semicListDenNegative:
2338            WerrorS( "all denominators should be positive" );
2339            break;
2340        case semicListMulNegative:
2341            WerrorS( "all multiplicities should be positive" );
2342            break;
2343
2344        case semicListNotSymmetric:
2345            WerrorS( "it is not symmetric" );
2346            break;
2347        case semicListNotMonotonous:
2348            WerrorS( "it is not monotonous" );
2349            break;
2350
2351        case semicListMilnorWrong:
2352            WerrorS( "the Milnor number is wrong" );
2353            break;
2354        case semicListPGWrong:
2355            WerrorS( "the geometrical genus is wrong" );
2356            break;
2357
2358        default:
2359            WerrorS( "unspecific error" );
2360            break;
2361    }
2362}
2363// ----------------------------------------------------------------------------
2364//  this is the main spectrum computation function
2365// ----------------------------------------------------------------------------
2366
2367spectrumState   spectrumCompute( poly h,lists *L,int fast )
2368{
2369  int i,j;
2370
2371  #ifdef SPECTRUM_DEBUG
2372  #ifdef SPECTRUM_PRINT
2373  #ifdef SPECTRUM_IOSTREAM
2374    cout << "spectrumCompute\n";
2375    if( fast==0 ) cout << "    no optimization" << endl;
2376    if( fast==1 ) cout << "    weight optimization" << endl;
2377    if( fast==2 ) cout << "    symmetry optimization" << endl;
2378  #else
2379    fprintf( stdout,"spectrumCompute\n" );
2380    if( fast==0 ) fprintf( stdout,"    no optimization\n" );
2381    if( fast==1 ) fprintf( stdout,"    weight optimization\n" );
2382    if( fast==2 ) fprintf( stdout,"    symmetry optimization\n" );
2383  #endif
2384  #endif
2385  #endif
2386
2387  // ----------------------
2388  //  check if  h  is zero
2389  // ----------------------
2390
2391  if( h==(poly)NULL )
2392  {
2393    return  spectrumZero;
2394  }
2395
2396  // ----------------------------------
2397  //  check if  h  has a constant term
2398  // ----------------------------------
2399
2400  if( hasConstTerm( h ) )
2401  {
2402    return  spectrumBadPoly;
2403  }
2404
2405  // --------------------------------
2406  //  check if  h  has a linear term
2407  // --------------------------------
2408
2409  if( hasLinearTerm( h ) )
2410  {
2411    *L = (lists)omAllocBin( slists_bin);
2412    (*L)->Init( 1 );
2413    (*L)->m[0].rtyp = INT_CMD;    //  milnor number
2414    /* (*L)->m[0].data = (void*)0;a  -- done by Init */
2415
2416    return  spectrumNoSingularity;
2417  }
2418
2419  // ----------------------------------
2420  //  compute the jacobi ideal of  (h)
2421  // ----------------------------------
2422
2423  ideal J = NULL;
2424  J = idInit( pVariables,1 );
2425
2426  #ifdef SPECTRUM_DEBUG
2427  #ifdef SPECTRUM_PRINT
2428  #ifdef SPECTRUM_IOSTREAM
2429    cout << "\n   computing the Jacobi ideal...\n";
2430  #else
2431    fprintf( stdout,"\n   computing the Jacobi ideal...\n" );
2432  #endif
2433  #endif
2434  #endif
2435
2436  for( i=0; i<pVariables; i++ )
2437  {
2438    J->m[i] = pDiff( h,i+1); //j );
2439
2440    #ifdef SPECTRUM_DEBUG
2441    #ifdef SPECTRUM_PRINT
2442    #ifdef SPECTRUM_IOSTREAM
2443      cout << "        ";
2444    #else
2445      fprintf( stdout,"        " );
2446    #endif
2447      pWrite( J->m[i] );
2448    #endif
2449    #endif
2450  }
2451
2452  // --------------------------------------------
2453  //  compute a standard basis  stdJ  of  jac(h)
2454  // --------------------------------------------
2455
2456  #ifdef SPECTRUM_DEBUG
2457  #ifdef SPECTRUM_PRINT
2458  #ifdef SPECTRUM_IOSTREAM
2459    cout << endl;
2460    cout << "    computing a standard basis..." << endl;
2461  #else
2462    fprintf( stdout,"\n" );
2463    fprintf( stdout,"    computing a standard basis...\n" );
2464  #endif
2465  #endif
2466  #endif
2467
2468  ideal stdJ = kStd(J,currQuotient,isNotHomog,NULL);
2469  idSkipZeroes( stdJ );
2470
2471  #ifdef SPECTRUM_DEBUG
2472  #ifdef SPECTRUM_PRINT
2473    for( i=0; i<IDELEMS(stdJ); i++ )
2474    {
2475      #ifdef SPECTRUM_IOSTREAM
2476        cout << "        ";
2477      #else
2478        fprintf( stdout,"        " );
2479      #endif
2480
2481      pWrite( stdJ->m[i] );
2482    }
2483  #endif
2484  #endif
2485
2486  idDelete( &J );
2487
2488  // ------------------------------------------
2489  //  check if the  h  has a singularity
2490  // ------------------------------------------
2491
2492  if( hasOne( stdJ ) )
2493  {
2494    // -------------------------------
2495    //  h is smooth in the origin
2496    //  return only the Milnor number
2497    // -------------------------------
2498
2499    *L = (lists)omAllocBin( slists_bin);
2500    (*L)->Init( 1 );
2501    (*L)->m[0].rtyp = INT_CMD;    //  milnor number
2502    /* (*L)->m[0].data = (void*)0;a  -- done by Init */
2503
2504    return  spectrumNoSingularity;
2505  }
2506
2507  // ------------------------------------------
2508  //  check if the singularity  h  is isolated
2509  // ------------------------------------------
2510
2511  for( i=pVariables; i>0; i-- )
2512  {
2513    if( hasAxis( stdJ,i )==FALSE )
2514    {
2515      return  spectrumNotIsolated;
2516    }
2517  }
2518
2519  // ------------------------------------------
2520  //  compute the highest corner  hc  of  stdJ
2521  // ------------------------------------------
2522
2523  #ifdef SPECTRUM_DEBUG
2524  #ifdef SPECTRUM_PRINT
2525  #ifdef SPECTRUM_IOSTREAM
2526    cout << "\n    computing the highest corner...\n";
2527  #else
2528    fprintf( stdout,"\n    computing the highest corner...\n" );
2529  #endif
2530  #endif
2531  #endif
2532
2533  poly hc = (poly)NULL;
2534
2535  scComputeHC( stdJ,currQuotient, 0,hc );
2536
2537  if( hc!=(poly)NULL )
2538  {
2539    pGetCoeff(hc) = nInit(1);
2540
2541    for( i=pVariables; i>0; i-- )
2542    {
2543      if( pGetExp( hc,i )>0 ) pDecrExp( hc,i );
2544    }
2545    pSetm( hc );
2546  }
2547  else
2548  {
2549    return  spectrumNoHC;
2550  }
2551
2552  #ifdef SPECTRUM_DEBUG
2553  #ifdef SPECTRUM_PRINT
2554  #ifdef SPECTRUM_IOSTREAM
2555    cout << "       ";
2556  #else
2557    fprintf( stdout,"       " );
2558  #endif
2559    pWrite( hc );
2560  #endif
2561  #endif
2562
2563  // ----------------------------------------
2564  //  compute the Newton polygon  nph  of  h
2565  // ----------------------------------------
2566
2567  #ifdef SPECTRUM_DEBUG
2568  #ifdef SPECTRUM_PRINT
2569  #ifdef SPECTRUM_IOSTREAM
2570    cout << "\n    computing the newton polygon...\n";
2571  #else
2572    fprintf( stdout,"\n    computing the newton polygon...\n" );
2573  #endif
2574  #endif
2575  #endif
2576
2577  newtonPolygon nph( h );
2578
2579  #ifdef SPECTRUM_DEBUG
2580  #ifdef SPECTRUM_PRINT
2581    cout << nph;
2582  #endif
2583  #endif
2584
2585  // -----------------------------------------------
2586  //  compute the weight corner  wc  of  (stdj,nph)
2587  // -----------------------------------------------
2588
2589  #ifdef SPECTRUM_DEBUG
2590  #ifdef SPECTRUM_PRINT
2591  #ifdef SPECTRUM_IOSTREAM
2592    cout << "\n    computing the weight corner...\n";
2593  #else
2594    fprintf( stdout,"\n    computing the weight corner...\n" );
2595  #endif
2596  #endif
2597  #endif
2598
2599  poly    wc = ( fast==0 ? pCopy( hc ) :
2600               ( fast==1 ? computeWC( nph,(Rational)pVariables ) :
2601              /* fast==2 */computeWC( nph,((Rational)pVariables)/(Rational)2 ) ) );
2602
2603  #ifdef SPECTRUM_DEBUG
2604  #ifdef SPECTRUM_PRINT
2605  #ifdef SPECTRUM_IOSTREAM
2606    cout << "        ";
2607  #else
2608    fprintf( stdout,"        " );
2609  #endif
2610    pWrite( wc );
2611  #endif
2612  #endif
2613
2614  // -------------
2615  //  compute  NF
2616  // -------------
2617
2618  #ifdef SPECTRUM_DEBUG
2619  #ifdef SPECTRUM_PRINT
2620  #ifdef SPECTRUM_IOSTREAM
2621    cout << "\n    computing NF...\n" << endl;
2622  #else
2623    fprintf( stdout,"\n    computing NF...\n" );
2624  #endif
2625  #endif
2626  #endif
2627
2628  spectrumPolyList NF( &nph );
2629
2630  computeNF( stdJ,hc,wc,&NF );
2631
2632  #ifdef SPECTRUM_DEBUG
2633  #ifdef SPECTRUM_PRINT
2634    cout << NF;
2635  #ifdef SPECTRUM_IOSTREAM
2636    cout << endl;
2637  #else
2638    fprintf( stdout,"\n" );
2639  #endif
2640  #endif
2641  #endif
2642
2643  // ----------------------------
2644  //  compute the spectrum of  h
2645  // ----------------------------
2646
2647  return  NF.spectrum( L,fast );
2648}
2649
2650// ----------------------------------------------------------------------------
2651//  this procedure is called from the interpreter
2652// ----------------------------------------------------------------------------
2653//  first  = polynomial
2654//  result = list of spectrum numbers
2655// ----------------------------------------------------------------------------
2656
2657BOOLEAN spectrumProc( leftv result,leftv first )
2658{
2659  spectrumState state = spectrumOK;
2660
2661  // -------------------
2662  //  check consistency
2663  // -------------------
2664
2665  //  check for a local ring
2666
2667  if( !ringIsLocal( ) )
2668  {
2669    WerrorS( "only works for local orderings" );
2670    state = spectrumWrongRing;
2671  }
2672
2673  //  no quotient rings are allowed
2674
2675  else if( currRing->qideal != NULL )
2676  {
2677    WerrorS( "does not work in quotient rings" );
2678    state = spectrumWrongRing;
2679  }
2680  else
2681  {
2682    lists   L    = (lists)NULL;
2683    int     flag = 1; // weight corner optimization is safe
2684
2685    state = spectrumCompute( (poly)first->Data( ),&L,flag );
2686
2687    if( state==spectrumOK )
2688    {
2689      result->rtyp = LIST_CMD;
2690      result->data = (char*)L;
2691    }
2692    else
2693    {
2694      spectrumPrintError(state);
2695    }
2696  }
2697
2698  return  (state!=spectrumOK);
2699}
2700
2701// ----------------------------------------------------------------------------
2702//  this procedure is called from the interpreter
2703// ----------------------------------------------------------------------------
2704//  first  = polynomial
2705//  result = list of spectrum numbers
2706// ----------------------------------------------------------------------------
2707
2708BOOLEAN spectrumfProc( leftv result,leftv first )
2709{
2710  spectrumState state = spectrumOK;
2711
2712  // -------------------
2713  //  check consistency
2714  // -------------------
2715
2716  //  check for a local polynomial ring
2717
2718  if( currRing->OrdSgn != -1 )
2719  // ?? HS: the test above is also true for k[x][[y]], k[[x]][y]
2720  // or should we use:
2721  //if( !ringIsLocal( ) )
2722  {
2723    WerrorS( "only works for local orderings" );
2724    state = spectrumWrongRing;
2725  }
2726  else if( currRing->qideal != NULL )
2727  {
2728    WerrorS( "does not work in quotient rings" );
2729    state = spectrumWrongRing;
2730  }
2731  else
2732  {
2733    lists   L    = (lists)NULL;
2734    int     flag = 2; // symmetric optimization
2735
2736    state = spectrumCompute( (poly)first->Data( ),&L,flag );
2737
2738    if( state==spectrumOK )
2739    {
2740      result->rtyp = LIST_CMD;
2741      result->data = (char*)L;
2742    }
2743    else
2744    {
2745      spectrumPrintError(state);
2746    }
2747  }
2748
2749  return  (state!=spectrumOK);
2750}
2751
2752// ----------------------------------------------------------------------------
2753//  check if a list is a spectrum
2754//  check for:
2755//      list has 6 elements
2756//      1st element is int (mu=Milnor number)
2757//      2nd element is int (pg=geometrical genus)
2758//      3rd element is int (n =number of different spectrum numbers)
2759//      4th element is intvec (num=numerators)
2760//      5th element is intvec (den=denomiantors)
2761//      6th element is intvec (mul=multiplicities)
2762//      exactly n numerators
2763//      exactly n denominators
2764//      exactly n multiplicities
2765//      mu>0
2766//      pg>=0
2767//      n>0
2768//      num>0
2769//      den>0
2770//      mul>0
2771//      symmetriy with respect to numberofvariables/2
2772//      monotony
2773//      mu = sum of all multiplicities
2774//      pg = sum of all multiplicities where num/den<=1
2775// ----------------------------------------------------------------------------
2776
2777semicState  list_is_spectrum( lists l )
2778{
2779    // -------------------
2780    //  check list length
2781    // -------------------
2782
2783    if( l->nr < 5 )
2784    {
2785        return  semicListTooShort;
2786    }
2787    else if( l->nr > 5 )
2788    {
2789        return  semicListTooLong;
2790    }
2791
2792    // -------------
2793    //  check types
2794    // -------------
2795
2796    if( l->m[0].rtyp != INT_CMD )
2797    {
2798        return  semicListFirstElementWrongType;
2799    }
2800    else if( l->m[1].rtyp != INT_CMD )
2801    {
2802        return  semicListSecondElementWrongType;
2803    }
2804    else if( l->m[2].rtyp != INT_CMD )
2805    {
2806        return  semicListThirdElementWrongType;
2807    }
2808    else if( l->m[3].rtyp != INTVEC_CMD )
2809    {
2810        return  semicListFourthElementWrongType;
2811    }
2812    else if( l->m[4].rtyp != INTVEC_CMD )
2813    {
2814        return  semicListFifthElementWrongType;
2815    }
2816    else if( l->m[5].rtyp != INTVEC_CMD )
2817    {
2818        return  semicListSixthElementWrongType;
2819    }
2820
2821    // -------------------------
2822    //  check number of entries
2823    // -------------------------
2824
2825    int     mu = (int)(l->m[0].Data( ));
2826    int     pg = (int)(l->m[1].Data( ));
2827    int     n  = (int)(l->m[2].Data( ));
2828
2829    if( n <= 0 )
2830    {
2831        return  semicListNNegative;
2832    }
2833
2834    intvec  *num = (intvec*)l->m[3].Data( );
2835    intvec  *den = (intvec*)l->m[4].Data( );
2836    intvec  *mul = (intvec*)l->m[5].Data( );
2837
2838    if( n != num->length( ) )
2839    {
2840        return  semicListWrongNumberOfNumerators;
2841    }
2842    else if( n != den->length( ) )
2843    {
2844        return  semicListWrongNumberOfDenominators;
2845    }
2846    else if( n != mul->length( ) )
2847    {
2848        return  semicListWrongNumberOfMultiplicities;
2849    }
2850
2851    // --------
2852    //  values
2853    // --------
2854
2855    if( mu <= 0 )
2856    {
2857        return  semicListMuNegative;
2858    }
2859    if( pg < 0 )
2860    {
2861        return  semicListPgNegative;
2862    }
2863
2864    int i;
2865
2866    for( i=0; i<n; i++ )
2867    {
2868        if( (*num)[i] <= 0 )
2869        {
2870            return  semicListNumNegative;
2871        }
2872        if( (*den)[i] <= 0 )
2873        {
2874            return  semicListDenNegative;
2875        }
2876        if( (*mul)[i] <= 0 )
2877        {
2878            return  semicListMulNegative;
2879        }
2880    }
2881
2882    // ----------------
2883    //  check symmetry
2884    // ----------------
2885
2886    int     j;
2887
2888    for( i=0, j=n-1; i<=j; i++,j-- )
2889    {
2890        if( (*num)[i] != pVariables*((*den)[i]) - (*num)[j] ||
2891            (*den)[i] != (*den)[j] ||
2892            (*mul)[i] != (*mul)[j] )
2893        {
2894            return  semicListNotSymmetric;
2895        }
2896    }
2897
2898    // ----------------
2899    //  check monotony
2900    // ----------------
2901
2902    for( i=0, j=1; i<n/2; i++,j++ )
2903    {
2904        if( (*num)[i]*(*den)[j] >= (*num)[j]*(*den)[i] )
2905        {
2906            return  semicListNotMonotonous;
2907        }
2908    }
2909
2910    // ---------------------
2911    //  check Milnor number
2912    // ---------------------
2913
2914    for( mu=0, i=0; i<n; i++ )
2915    {
2916        mu += (*mul)[i];
2917    }
2918
2919    if( mu != (int)(l->m[0].Data( )) )
2920    {
2921        return  semicListMilnorWrong;
2922    }
2923
2924    // -------------------------
2925    //  check geometrical genus
2926    // -------------------------
2927
2928    for( pg=0, i=0; i<n; i++ )
2929    {
2930        if( (*num)[i]<=(*den)[i] )
2931        {
2932            pg += (*mul)[i];
2933        }
2934    }
2935
2936    if( pg != (int)(l->m[1].Data( )) )
2937    {
2938        return  semicListPGWrong;
2939    }
2940
2941    return  semicOK;
2942}
2943
2944// ----------------------------------------------------------------------------
2945//  this procedure is called from the interpreter
2946// ----------------------------------------------------------------------------
2947//  first  = list of spectrum numbers
2948//  second = list of spectrum numbers
2949//  result = sum of the two lists
2950// ----------------------------------------------------------------------------
2951
2952BOOLEAN spaddProc( leftv result,leftv first,leftv second )
2953{
2954    semicState  state;
2955
2956    // -----------------
2957    //  check arguments
2958    // -----------------
2959
2960    lists l1 = (lists)first->Data( );
2961    lists l2 = (lists)second->Data( );
2962
2963    if( (state=list_is_spectrum( l1 )) != semicOK )
2964    {
2965        WerrorS( "first argument is not a spectrum:" );
2966        list_error( state );
2967    }
2968    else if( (state=list_is_spectrum( l2 )) != semicOK )
2969    {
2970        WerrorS( "second argument is not a spectrum:" );
2971        list_error( state );
2972    }
2973    else
2974    {
2975        spectrum s1( l1 );
2976        spectrum s2( l2 );
2977        spectrum sum( s1+s2 );
2978
2979        result->rtyp = LIST_CMD;
2980        result->data = (char*)(sum.thelist( ));
2981    }
2982
2983    return  (state!=semicOK);
2984}
2985
2986// ----------------------------------------------------------------------------
2987//  this procedure is called from the interpreter
2988// ----------------------------------------------------------------------------
2989//  first  = list of spectrum numbers
2990//  second = integer
2991//  result = the multiple of the first list by the second factor
2992// ----------------------------------------------------------------------------
2993
2994BOOLEAN spmulProc( leftv result,leftv first,leftv second )
2995{
2996    semicState  state;
2997
2998    // -----------------
2999    //  check arguments
3000    // -----------------
3001
3002    lists   l = (lists)first->Data( );
3003    int     k = (int)second->Data( );
3004
3005    if( (state=list_is_spectrum( l ))!=semicOK )
3006    {
3007        WerrorS( "first argument is not a spectrum" );
3008        list_error( state );
3009    }
3010    else if( k < 0 )
3011    {
3012        WerrorS( "second argument should be positive" );
3013        state = semicMulNegative;
3014    }
3015    else
3016    {
3017        spectrum s( l );
3018        spectrum product( k*s );
3019
3020        result->rtyp = LIST_CMD;
3021        result->data = (char*)product.thelist( );
3022    }
3023
3024    return  (state!=semicOK);
3025}
3026
3027// ----------------------------------------------------------------------------
3028//  this procedure is called from the interpreter
3029// ----------------------------------------------------------------------------
3030//  first  = list of spectrum numbers
3031//  second = list of spectrum numbers
3032//  result = semicontinuity index
3033// ----------------------------------------------------------------------------
3034
3035BOOLEAN    semicProc3   ( leftv res,leftv u,leftv v,leftv w )
3036{
3037  semicState  state;
3038  BOOLEAN qh=(((int)w->Data())==1);
3039
3040  // -----------------
3041  //  check arguments
3042  // -----------------
3043
3044  lists l1 = (lists)u->Data( );
3045  lists l2 = (lists)v->Data( );
3046
3047  if( (state=list_is_spectrum( l1 ))!=semicOK )
3048  {
3049    WerrorS( "first argument is not a spectrum" );
3050    list_error( state );
3051  }
3052  else if( (state=list_is_spectrum( l2 ))!=semicOK )
3053  {
3054    WerrorS( "second argument is not a spectrum" );
3055    list_error( state );
3056  }
3057  else
3058  {
3059    spectrum s1( l1 );
3060    spectrum s2( l2 );
3061
3062    res->rtyp = INT_CMD;
3063    if (qh)
3064      res->data = (void*)(s1.mult_spectrumh( s2 ));
3065    else
3066      res->data = (void*)(s1.mult_spectrum( s2 ));
3067  }
3068
3069  // -----------------
3070  //  check status
3071  // -----------------
3072
3073  return  (state!=semicOK);
3074}
3075BOOLEAN    semicProc   ( leftv res,leftv u,leftv v )
3076{
3077  sleftv tmp;
3078  memset(&tmp,0,sizeof(tmp));
3079  tmp.rtyp=INT_CMD;
3080  /* tmp.data = (void *)0;  -- done by memset */
3081
3082  return  semicProc3(res,u,v,&tmp);
3083}
3084// from splist.cc
3085// ----------------------------------------------------------------------------
3086//  Compute the spectrum of a  spectrumPolyList
3087// ----------------------------------------------------------------------------
3088
3089spectrumState   spectrumPolyList::spectrum( lists *L,int fast )
3090{
3091    spectrumPolyNode  **node = &root;
3092    spectrumPolyNode  *search;
3093
3094    poly              f,tmp;
3095    int               found,cmp;
3096
3097    Rational smax( ( fast==0 ? 0 : pVariables ),
3098                   ( fast==2 ? 2 : 1 ) );
3099
3100    Rational weight_prev( 0,1 );
3101
3102    int     mu = 0;          // the milnor number
3103    int     pg = 0;          // the geometrical genus
3104    int     n  = 0;          // number of different spectral numbers
3105    int     z  = 0;          // number of spectral number equal to smax
3106
3107    int     k = 0;
3108
3109    while( (*node)!=(spectrumPolyNode*)NULL &&
3110           ( fast==0 || (*node)->weight<=smax ) )
3111    {
3112        // ---------------------------------------
3113        //  determine the first normal form which
3114        //  contains the monomial  node->mon
3115        // ---------------------------------------
3116
3117        found  = FALSE;
3118        search = *node;
3119
3120        while( search!=(spectrumPolyNode*)NULL && found==FALSE )
3121        {
3122            if( search->nf!=(poly)NULL )
3123            {
3124                f = search->nf;
3125
3126                do
3127                {
3128                    // --------------------------------
3129                    //  look for  (*node)->mon  in   f
3130                    // --------------------------------
3131
3132                    cmp = pCmp( (*node)->mon,f );
3133
3134                    if( cmp<0 )
3135                    {
3136                        f = pNext( f );
3137                    }
3138                    else if( cmp==0 )
3139                    {
3140                        // -----------------------------
3141                        //  we have found a normal form
3142                        // -----------------------------
3143
3144                        found = TRUE;
3145
3146                        //  normalize coefficient
3147
3148                        number inv = nInvers( pGetCoeff( f ) );
3149                        pMult_nn( search->nf,inv );
3150                        nDelete( &inv );
3151
3152                        //  exchange  normal forms
3153
3154                        tmp         = (*node)->nf;
3155                        (*node)->nf = search->nf;
3156                        search->nf  = tmp;
3157                    }
3158                }
3159                while( cmp<0 && f!=(poly)NULL );
3160            }
3161            search = search->next;
3162        }
3163
3164        if( found==FALSE )
3165        {
3166            // ------------------------------------------------
3167            //  the weight of  node->mon  is a spectrum number
3168            // ------------------------------------------------
3169
3170            mu++;
3171
3172            if( (*node)->weight<=(Rational)1 )              pg++;
3173            if( (*node)->weight==smax )           z++;
3174            if( (*node)->weight>weight_prev )     n++;
3175
3176            weight_prev = (*node)->weight;
3177            node = &((*node)->next);
3178        }
3179        else
3180        {
3181            // -----------------------------------------------
3182            //  determine all other normal form which contain
3183            //  the monomial  node->mon
3184            //  replace for  node->mon  its normal form
3185            // -----------------------------------------------
3186
3187            while( search!=(spectrumPolyNode*)NULL )
3188            {
3189                    if( search->nf!=(poly)NULL )
3190                {
3191                    f = search->nf;
3192
3193                    do
3194                    {
3195                        // --------------------------------
3196                        //  look for  (*node)->mon  in   f
3197                        // --------------------------------
3198
3199                        cmp = pCmp( (*node)->mon,f );
3200
3201                        if( cmp<0 )
3202                        {
3203                            f = pNext( f );
3204                        }
3205                        else if( cmp==0 )
3206                        {
3207                            search->nf = pSub( search->nf,
3208                                ppMult_nn( (*node)->nf,pGetCoeff( f ) ) );
3209                            pNorm( search->nf );
3210                        }
3211                    }
3212                    while( cmp<0 && f!=(poly)NULL );
3213                }
3214                search = search->next;
3215            }
3216            delete_node( node );
3217        }
3218
3219    }
3220
3221    // --------------------------------------------------------
3222    //  fast computation exploits the symmetry of the spectrum
3223    // --------------------------------------------------------
3224
3225    if( fast==2 )
3226    {
3227        mu = 2*mu - z;
3228        n  = ( z > 0 ? 2*n - 1 : 2*n );
3229    }
3230
3231    // --------------------------------------------------------
3232    //  compute the spectrum numbers with their multiplicities
3233    // --------------------------------------------------------
3234
3235    intvec            *nom  = new intvec( n );
3236    intvec            *den  = new intvec( n );
3237    intvec            *mult = new intvec( n );
3238
3239    int count         = 0;
3240    int multiplicity  = 1;
3241
3242    for( search=root; search!=(spectrumPolyNode*)NULL &&
3243                     ( fast==0 || search->weight<=smax );
3244                     search=search->next )
3245    {
3246        if( search->next==(spectrumPolyNode*)NULL ||
3247            search->weight<search->next->weight )
3248        {
3249            (*nom) [count] = search->weight.get_num_si( );
3250            (*den) [count] = search->weight.get_den_si( );
3251            (*mult)[count] = multiplicity;
3252
3253            multiplicity=1;
3254            count++;
3255        }
3256        else
3257        {
3258            multiplicity++;
3259        }
3260    }
3261
3262    // --------------------------------------------------------
3263    //  fast computation exploits the symmetry of the spectrum
3264    // --------------------------------------------------------
3265
3266    if( fast==2 )
3267    {
3268        int n1,n2;
3269        for( n1=0, n2=n-1; n1<n2; n1++, n2-- )
3270        {
3271            (*nom) [n2] = pVariables*(*den)[n1]-(*nom)[n1];
3272            (*den) [n2] = (*den)[n1];
3273            (*mult)[n2] = (*mult)[n1];
3274        }
3275    }
3276
3277    // -----------------------------------
3278    //  test if the spectrum is symmetric
3279    // -----------------------------------
3280
3281    if( fast==0 || fast==1 )
3282    {
3283        int symmetric=TRUE;
3284
3285        for( int n1=0, n2=n-1 ; n1<n2 && symmetric==TRUE; n1++, n2-- )
3286        {
3287            if( (*mult)[n1]!=(*mult)[n2] ||
3288                (*den) [n1]!= (*den)[n2] ||
3289                (*nom)[n1]+(*nom)[n2]!=pVariables*(*den) [n1] )
3290            {
3291                symmetric = FALSE;
3292            }
3293        }
3294
3295        if( symmetric==FALSE )
3296        {
3297            // ---------------------------------------------
3298            //  the spectrum is not symmetric => degenerate
3299            //  principal part
3300            // ---------------------------------------------
3301
3302            *L = (lists)omAllocBin( slists_bin);
3303            (*L)->Init( 1 );
3304            (*L)->m[0].rtyp = INT_CMD;    //  milnor number
3305            (*L)->m[0].data = (void*)mu;
3306
3307            return spectrumDegenerate;
3308        }
3309    }
3310
3311    *L = (lists)omAllocBin( slists_bin);
3312
3313    (*L)->Init( 6 );
3314
3315    (*L)->m[0].rtyp = INT_CMD;    //  milnor number
3316    (*L)->m[1].rtyp = INT_CMD;    //  geometrical genus
3317    (*L)->m[2].rtyp = INT_CMD;    //  number of spectrum values
3318    (*L)->m[3].rtyp = INTVEC_CMD; //  nominators
3319    (*L)->m[4].rtyp = INTVEC_CMD; //  denomiantors
3320    (*L)->m[5].rtyp = INTVEC_CMD; //  multiplicities
3321
3322    (*L)->m[0].data = (void*)mu;
3323    (*L)->m[1].data = (void*)pg;
3324    (*L)->m[2].data = (void*)n;
3325    (*L)->m[3].data = (void*)nom;
3326    (*L)->m[4].data = (void*)den;
3327    (*L)->m[5].data = (void*)mult;
3328
3329    return  spectrumOK;
3330}
3331
3332#endif
3333
3334//from mpr_inout.cc
3335extern void nPrint(number n);
3336
3337BOOLEAN loNewtonP( leftv res, leftv arg1 )
3338{
3339  res->data= (void*)loNewtonPolytope( (ideal)arg1->Data() );
3340  return FALSE;
3341}
3342
3343BOOLEAN loSimplex( leftv res, leftv args )
3344{
3345  if ( !(rField_is_long_R()) )
3346  {
3347    WerrorS("Ground field not implemented!");
3348    return TRUE;
3349  }
3350
3351  simplex * LP;
3352  matrix m;
3353
3354  leftv v= args;
3355  if ( v->Typ() != MATRIX_CMD ) // 1: matrix
3356    return TRUE;
3357  else
3358    m= (matrix)(v->CopyD());
3359
3360  LP = new simplex(MATROWS(m),MATCOLS(m));
3361  LP->mapFromMatrix(m);
3362
3363  v= v->next;
3364  if ( v->Typ() != INT_CMD )    // 2: m = number of constraints
3365    return TRUE;
3366  else
3367    LP->m= (int)(v->Data());
3368
3369  v= v->next;
3370  if ( v->Typ() != INT_CMD )    // 3: n = number of variables
3371    return TRUE;
3372  else
3373    LP->n= (int)(v->Data());
3374
3375  v= v->next;
3376  if ( v->Typ() != INT_CMD )    // 4: m1 = number of <= constraints
3377    return TRUE;
3378  else
3379    LP->m1= (int)(v->Data());
3380
3381  v= v->next;
3382  if ( v->Typ() != INT_CMD )    // 5: m2 = number of >= constraints
3383    return TRUE;
3384  else
3385    LP->m2= (int)(v->Data());
3386
3387  v= v->next;
3388  if ( v->Typ() != INT_CMD )    // 6: m3 = number of == constraints
3389    return TRUE;
3390  else
3391    LP->m3= (int)(v->Data());
3392
3393#ifdef mprDEBUG_PROT
3394  Print("m (constraints) %d\n",LP->m);
3395  Print("n (columns) %d\n",LP->n);
3396  Print("m1 (<=) %d\n",LP->m1);
3397  Print("m2 (>=) %d\n",LP->m2);
3398  Print("m3 (==) %d\n",LP->m3);
3399#endif
3400
3401  LP->compute();
3402
3403  lists lres= (lists)omAlloc( sizeof(slists) );
3404  lres->Init( 6 );
3405
3406  lres->m[0].rtyp= MATRIX_CMD; // output matrix
3407  lres->m[0].data=(void*)LP->mapToMatrix(m);
3408
3409  lres->m[1].rtyp= INT_CMD;   // found a solution?
3410  lres->m[1].data=(void*)LP->icase;
3411
3412  lres->m[2].rtyp= INTVEC_CMD;
3413  lres->m[2].data=(void*)LP->posvToIV();
3414
3415  lres->m[3].rtyp= INTVEC_CMD;
3416  lres->m[3].data=(void*)LP->zrovToIV();
3417
3418  lres->m[4].rtyp= INT_CMD;
3419  lres->m[4].data=(void*)LP->m;
3420
3421  lres->m[5].rtyp= INT_CMD;
3422  lres->m[5].data=(void*)LP->n;
3423
3424  res->data= (void*)lres;
3425
3426  return FALSE;
3427}
3428
3429BOOLEAN nuMPResMat( leftv res, leftv arg1, leftv arg2 )
3430{
3431  ideal gls = (ideal)(arg1->Data());
3432  int imtype= (int)arg2->Data();
3433
3434  uResultant::resMatType mtype= determineMType( imtype );
3435
3436  // check input ideal ( = polynomial system )
3437  if ( mprIdealCheck( gls, arg1->Name(), mtype, true ) != mprOk )
3438  {
3439    return TRUE;
3440  }
3441
3442  uResultant *resMat= new uResultant( gls, mtype, false );
3443
3444  res->rtyp = MODUL_CMD;
3445  res->data= (void*)resMat->accessResMat()->getMatrix();
3446
3447  delete resMat;
3448
3449  return FALSE;
3450}
3451
3452BOOLEAN nuLagSolve( leftv res, leftv arg1, leftv arg2, leftv arg3 )
3453{
3454
3455  poly gls;
3456  gls= (poly)(arg1->Data());
3457  int howclean= (int)arg3->Data();
3458
3459  if ( !(rField_is_R() ||
3460         rField_is_Q() ||
3461         rField_is_long_R() ||
3462         rField_is_long_C()) )
3463  {
3464    WerrorS("Ground field not implemented!");
3465    return TRUE;
3466  }
3467
3468  if ( !(rField_is_R()||rField_is_long_R()||rField_is_long_C()) )
3469  {
3470    unsigned long int ii = (unsigned long int)arg2->Data();
3471    setGMPFloatDigits( ii, ii );
3472  }
3473
3474  if ( gls == NULL || pIsConstant( gls ) )
3475  {
3476    WerrorS("Input polynomial is constant!");
3477    return TRUE;
3478  }
3479
3480  int ldummy;
3481  int deg= pLDeg( gls, &ldummy, currRing );
3482  //  int deg= pDeg( gls );
3483  int len= pLength( gls );
3484  int i,vpos;
3485  poly piter;
3486  lists elist;
3487  lists rlist;
3488
3489  elist= (lists)omAlloc( sizeof(slists) );
3490  elist->Init( 0 );
3491
3492  if ( pVariables > 1 )
3493  {
3494    piter= gls;
3495    for ( i= 1; i <= pVariables; i++ )
3496      if ( pGetExp( piter, i ) )
3497      {
3498        vpos= i;
3499        break;
3500      }
3501    while ( piter )
3502    {
3503      for ( i= 1; i <= pVariables; i++ )
3504        if ( (vpos != i) && (pGetExp( piter, i ) != 0) )
3505        {
3506          WerrorS("The input polynomial must be univariate!");
3507          return TRUE;
3508        }
3509      pIter( piter );
3510    }
3511  }
3512
3513  rootContainer * roots= new rootContainer();
3514  number * pcoeffs= (number *)omAlloc( (deg+1) * sizeof( number ) );
3515  piter= gls;
3516  for ( i= deg; i >= 0; i-- )
3517  {
3518    //if ( piter ) Print("deg %d, pDeg(piter) %d\n",i,pTotaldegree(piter));
3519    if ( piter && pTotaldegree(piter) == i )
3520    {
3521      pcoeffs[i]= nCopy( pGetCoeff( piter ) );
3522      //nPrint( pcoeffs[i] );PrintS("  ");
3523      pIter( piter );
3524    }
3525    else
3526    {
3527      pcoeffs[i]= nInit(0);
3528    }
3529  }
3530
3531#ifdef mprDEBUG_PROT
3532  for (i=deg; i >= 0; i--)
3533  {
3534    nPrint( pcoeffs[i] );PrintS("  ");
3535  }
3536  PrintLn();
3537#endif
3538
3539  roots->fillContainer( pcoeffs, NULL, 1, deg, rootContainer::onepoly, 1 );
3540  roots->solver( howclean );
3541
3542  int elem= roots->getAnzRoots();
3543  char *out;
3544  char *dummy;
3545  int j;
3546
3547  rlist= (lists)omAlloc( sizeof(slists) );
3548  rlist->Init( elem );
3549
3550  if (rField_is_long_C())
3551  {
3552    for ( j= 0; j < elem; j++ )
3553    {
3554      rlist->m[j].rtyp=NUMBER_CMD;
3555      rlist->m[j].data=(void *)nCopy((number)(roots->getRoot(j)));
3556      //rlist->m[j].data=(void *)(number)(roots->getRoot(j));
3557    }
3558  }
3559  else
3560  {
3561    for ( j= 0; j < elem; j++ )
3562    {
3563      dummy = complexToStr( (*roots)[j], gmp_output_digits );
3564      rlist->m[j].rtyp=STRING_CMD;
3565      rlist->m[j].data=(void *)dummy;
3566    }
3567  }
3568
3569  elist->Clean();
3570  //omFreeSize( (ADDRESS) elist, sizeof(slists) );
3571
3572  // this is (via fillContainer) the same data as in root
3573  //for ( i= deg; i >= 0; i-- ) nDelete( &pcoeffs[i] );
3574  //omFreeSize( (ADDRESS) pcoeffs, (deg+1) * sizeof( number ) );
3575
3576  delete roots;
3577
3578  res->rtyp= LIST_CMD;
3579  res->data= (void*)rlist;
3580
3581  return FALSE;
3582}
3583
3584BOOLEAN nuVanderSys( leftv res, leftv arg1, leftv arg2, leftv arg3)
3585{
3586  int i;
3587  ideal p,w;
3588  p= (ideal)arg1->Data();
3589  w= (ideal)arg2->Data();
3590
3591  // w[0] = f(p^0)
3592  // w[1] = f(p^1)
3593  // ...
3594  // p can be a vector of numbers (multivariate polynom)
3595  //   or one number (univariate polynom)
3596  // tdg = deg(f)
3597
3598  int n= IDELEMS( p );
3599  int m= IDELEMS( w );
3600  int tdg= (int)arg3->Data();
3601
3602  res->data= (void*)NULL;
3603
3604  // check the input
3605  if ( tdg < 1 )
3606  {
3607    WerrorS("Last input parameter must be > 0!");
3608    return TRUE;
3609  }
3610  if ( n != pVariables )
3611  {
3612    Werror("Size of first input ideal must be equal to %d!",pVariables);
3613    return TRUE;
3614  }
3615  if ( m != (int)pow((double)tdg+1,(double)n) )
3616  {
3617    Werror("Size of second input ideal must be equal to %d!",
3618      (int)pow((double)tdg+1,(double)n));
3619    return TRUE;
3620  }
3621  if ( !(rField_is_Q() /* ||
3622         rField_is_R() || rField_is_long_R() ||
3623         rField_is_long_C()*/ ) )
3624         {
3625    WerrorS("Ground field not implemented!");
3626    return TRUE;
3627  }
3628
3629  number tmp;
3630  number *pevpoint= (number *)omAlloc( n * sizeof( number ) );
3631  for ( i= 0; i < n; i++ )
3632  {
3633    pevpoint[i]=nInit(0);
3634    if (  (p->m)[i] )
3635    {
3636      tmp = pGetCoeff( (p->m)[i] );
3637      if ( nIsZero(tmp) || nIsOne(tmp) || nIsMOne(tmp) )
3638      {
3639        omFreeSize( (ADDRESS)pevpoint, n * sizeof( number ) );
3640        WerrorS("Elements of first input ideal must not be equal to -1, 0, 1!");
3641        return TRUE;
3642      }
3643    } else tmp= NULL;
3644    if ( !nIsZero(tmp) )
3645    {
3646      if ( !pIsConstant((p->m)[i]))
3647      {
3648        omFreeSize( (ADDRESS)pevpoint, n * sizeof( number ) );
3649        WerrorS("Elements of first input ideal must be numbers!");
3650        return TRUE;
3651      }
3652      pevpoint[i]= nCopy( tmp );
3653    }
3654  }
3655
3656  number *wresults= (number *)omAlloc( m * sizeof( number ) );
3657  for ( i= 0; i < m; i++ )
3658  {
3659    wresults[i]= nInit(0);
3660    if ( (w->m)[i] && !nIsZero(pGetCoeff((w->m)[i])) )
3661    {
3662      if ( !pIsConstant((w->m)[i]))
3663      {
3664        omFreeSize( (ADDRESS)pevpoint, n * sizeof( number ) );
3665        omFreeSize( (ADDRESS)wresults, m * sizeof( number ) );
3666        WerrorS("Elements of second input ideal must be numbers!");
3667        return TRUE;
3668      }
3669      wresults[i]= nCopy(pGetCoeff((w->m)[i]));
3670    }
3671  }
3672
3673  vandermonde vm( m, n, tdg, pevpoint, FALSE );
3674  number *ncpoly= vm.interpolateDense( wresults );
3675  // do not free ncpoly[]!!
3676  poly rpoly= vm.numvec2poly( ncpoly );
3677
3678  omFreeSize( (ADDRESS)pevpoint, n * sizeof( number ) );
3679  omFreeSize( (ADDRESS)wresults, m * sizeof( number ) );
3680
3681  res->data= (void*)rpoly;
3682  return FALSE;
3683}
3684
3685BOOLEAN nuUResSolve( leftv res, leftv args )
3686{
3687  leftv v= args;
3688
3689  ideal gls;
3690  int imtype;
3691  int howclean;
3692
3693  // get ideal
3694  if ( v->Typ() != IDEAL_CMD )
3695    return TRUE;
3696  else gls= (ideal)(v->Data());
3697  v= v->next;
3698
3699  // get resultant matrix type to use (0,1)
3700  if ( v->Typ() != INT_CMD )
3701    return TRUE;
3702  else imtype= (int)v->Data();
3703  v= v->next;
3704
3705  // get and set precision in digits ( > 0 )
3706  if ( v->Typ() != INT_CMD )
3707    return TRUE;
3708  else if ( !(rField_is_R()||rField_is_long_R()||rField_is_long_C()) )
3709  {
3710    unsigned long int ii=(unsigned long int)v->Data();
3711    setGMPFloatDigits( ii, ii );
3712  }
3713  v= v->next;
3714
3715  // get interpolation steps (0,1,2)
3716  if ( v->Typ() != INT_CMD )
3717    return TRUE;
3718  else howclean= (int)v->Data();
3719
3720  uResultant::resMatType mtype= determineMType( imtype );
3721  int i,c,count;
3722  lists listofroots= NULL;
3723  lists emptylist;
3724  number smv= NULL;
3725  BOOLEAN interpolate_det= (mtype==uResultant::denseResMat)?TRUE:FALSE;
3726
3727  //emptylist= (lists)omAlloc( sizeof(slists) );
3728  //emptylist->Init( 0 );
3729
3730  //res->rtyp = LIST_CMD;
3731  //res->data= (void *)emptylist;
3732
3733  // check input ideal ( = polynomial system )
3734  if ( mprIdealCheck( gls, args->Name(), mtype ) != mprOk )
3735  {
3736    return TRUE;
3737  }
3738
3739  uResultant * ures;
3740  rootContainer ** iproots;
3741  rootContainer ** muiproots;
3742  rootArranger * arranger;
3743
3744  // main task 1: setup of resultant matrix
3745  ures= new uResultant( gls, mtype );
3746  if ( ures->accessResMat()->initState() != resMatrixBase::ready )
3747  {
3748    WerrorS("Error occurred during matrix setup!");
3749    return TRUE;
3750  }
3751
3752  // if dense resultant, check if minor nonsingular
3753  if ( mtype == uResultant::denseResMat )
3754  {
3755    smv= ures->accessResMat()->getSubDet();
3756#ifdef mprDEBUG_PROT
3757    PrintS("// Determinant of submatrix: ");nPrint(smv);PrintLn();
3758#endif
3759    if ( nIsZero(smv) )
3760    {
3761      WerrorS("Unsuitable input ideal: Minor of resultant matrix is singular!");
3762      return TRUE;
3763    }
3764  }
3765
3766  // main task 2: Interpolate specialized resultant polynomials
3767  if ( interpolate_det )
3768    iproots= ures->interpolateDenseSP( false, smv );
3769  else
3770    iproots= ures->specializeInU( false, smv );
3771
3772  // main task 3: Interpolate specialized resultant polynomials
3773  if ( interpolate_det )
3774    muiproots= ures->interpolateDenseSP( true, smv );
3775  else
3776    muiproots= ures->specializeInU( true, smv );
3777
3778#ifdef mprDEBUG_PROT
3779  c= iproots[0]->getAnzElems();
3780  for (i=0; i < c; i++) pWrite(iproots[i]->getPoly());
3781  c= muiproots[0]->getAnzElems();
3782  for (i=0; i < c; i++) pWrite(muiproots[i]->getPoly());
3783#endif
3784
3785  // main task 4: Compute roots of specialized polys and match them up
3786  arranger= new rootArranger( iproots, muiproots, howclean );
3787  arranger->solve_all();
3788
3789  // get list of roots
3790  if ( arranger->success() )
3791  {
3792    arranger->arrange();
3793    listofroots= arranger->listOfRoots( gmp_output_digits );
3794  }
3795  else
3796  {
3797    WerrorS("Solver was unable to find any roots!");
3798    return TRUE;
3799  }
3800
3801  // free everything
3802  count= iproots[0]->getAnzElems();
3803  for (i=0; i < count; i++) delete iproots[i];
3804  omFreeSize( (ADDRESS) iproots, count * sizeof(rootContainer*) );
3805  count= muiproots[0]->getAnzElems();
3806  for (i=0; i < count; i++) delete muiproots[i];
3807  omFreeSize( (ADDRESS) muiproots, count * sizeof(rootContainer*) );
3808
3809  delete ures;
3810  delete arranger;
3811  nDelete( &smv );
3812
3813  res->data= (void *)listofroots;
3814
3815  //emptylist->Clean();
3816  //  omFreeSize( (ADDRESS) emptylist, sizeof(slists) );
3817
3818  return FALSE;
3819}
3820
3821// from mpr_numeric.cc
3822lists rootArranger::listOfRoots( const unsigned int oprec )
3823{
3824  int i,j,tr;
3825  int count= roots[0]->getAnzRoots(); // number of roots
3826  int elem= roots[0]->getAnzElems();  // number of koordinates per root
3827
3828  lists listofroots= (lists)omAlloc( sizeof(slists) ); // must be done this way!
3829
3830  if ( found_roots )
3831  {
3832    listofroots->Init( count );
3833
3834    for (i=0; i < count; i++)
3835    {
3836      lists onepoint= (lists)omAlloc(sizeof(slists)); // must be done this way!
3837      onepoint->Init(elem);
3838      for ( j= 0; j < elem; j++ )
3839      {
3840        if ( !rField_is_long_C() )
3841        {
3842          onepoint->m[j].rtyp=STRING_CMD;
3843          onepoint->m[j].data=(void *)complexToStr((*roots[j])[i],oprec);
3844        }
3845        else
3846        {
3847          onepoint->m[j].rtyp=NUMBER_CMD;
3848          onepoint->m[j].data=(void *)nCopy((number)(roots[j]->getRoot(i)));
3849        }
3850        onepoint->m[j].next= NULL;
3851        onepoint->m[j].name= NULL;
3852      }
3853      listofroots->m[i].rtyp=LIST_CMD;
3854      listofroots->m[i].data=(void *)onepoint;
3855      listofroots->m[j].next= NULL;
3856      listofroots->m[j].name= NULL;
3857    }
3858
3859  }
3860  else
3861  {
3862    listofroots->Init( 0 );
3863  }
3864
3865  return listofroots;
3866}
3867
3868#ifdef PDEBUG
3869
3870#if (OM_TRACK > 2) && defined(OM_TRACK_CUSTOM)
3871
3872void p_SetRingOfPoly(poly p, ring r)
3873{
3874  while (p != NULL)
3875  {
3876    p_SetRingOfLm(p, r);
3877    pIter(p);
3878  }
3879}
3880
3881void p_SetRingOfIdeal(ideal id, ring r)
3882{
3883  if (id == NULL) return;
3884
3885  int i, n = id->ncols*id->nrows;
3886
3887  for (i=0; i<n; i++)
3888  {
3889    p_SetRingOfPoly(id->m[i], r);
3890  }
3891}
3892
3893void p_SetRingOfList(lists L, ring r)
3894{
3895  int i;
3896  for (i=0; i<L->nr; i++)
3897  {
3898    p_SetRingOfLeftv(&(L->m[i]), r);
3899  }
3900}
3901
3902void p_SetRingOfCommand(command cmd, ring r)
3903{
3904  if (cmd->op == PROC_CMD && cmd->argc == 2)
3905    p_SetRingOfLeftv(&(cmd->arg2), r);
3906  else if (cmd->argc > 0)
3907  {
3908    p_SetRingOfLeftv(&(cmd->arg1), r);
3909    if (cmd->argc > 1)
3910    {
3911      p_SetRingOfLeftv(&(cmd->arg2), r);
3912      if (cmd->argc > 2)
3913        p_SetRingOfLeftv(&(cmd->arg3), r);
3914    }
3915  }
3916}
3917
3918void p_SetRingOfLeftv(leftv l, ring r)
3919{
3920  while (l != NULL)
3921  {
3922    switch(l->rtyp)
3923    {
3924        case POLY_CMD:
3925        case VECTOR_CMD:
3926          p_SetRingOfPoly((poly) l->data, r);
3927      break;
3928
3929      case IDEAL_CMD:
3930      case MODUL_CMD:
3931      case MATRIX_CMD:
3932      case MAP_CMD:
3933        p_SetRingOfIdeal((ideal) l->data, r);
3934        break;
3935
3936        case LIST_CMD:
3937          p_SetRingOfList((lists) l->data, r);
3938          break;
3939
3940        case COMMAND:
3941          p_SetRingOfCommand((command)l->data, r);
3942        default:
3943          break;
3944    }
3945    l = l->next;
3946  }
3947}
3948#endif // (OM_TRACK > 2) && defined(OM_TRACK_CUSTOM)
3949
3950#endif // PDEBUG
3951
3952// from ring.cc
3953void rSetHdl(idhdl h)
3954{
3955  int i;
3956  ring rg = NULL;
3957  if (h!=NULL)
3958  {
3959//   Print(" new ring:%s (l:%d)\n",IDID(h),IDLEV(h));
3960    rg = IDRING(h);
3961    omCheckAddrSize((ADDRESS)h,sizeof(idrec));
3962    if (IDID(h))  // OB: ????
3963      omCheckAddr((ADDRESS)IDID(h));
3964    rTest(rg);
3965  }
3966
3967  // clean up history
3968  if (sLastPrinted.RingDependend())
3969  {
3970    sLastPrinted.CleanUp();
3971    memset(&sLastPrinted,0,sizeof(sleftv));
3972  }
3973
3974   /*------------ change the global ring -----------------------*/
3975  rChangeCurrRing(rg);
3976  currRingHdl = h;
3977}
3978
3979BOOLEAN rSleftvOrdering2Ordering(sleftv *ord, ring R)
3980{
3981  int last = 0, o=0, n = 1, i=0, typ = 1, j;
3982  sleftv *sl = ord;
3983
3984  // determine nBlocks
3985  while (sl!=NULL)
3986  {
3987    intvec *iv = (intvec *)(sl->data);
3988    if (((*iv)[1]==ringorder_c)||((*iv)[1]==ringorder_C)) i++;
3989    else if ((*iv)[1]==ringorder_L)
3990    {
3991      R->bitmask=(*iv)[2];
3992      n--;
3993    }
3994    else if ((*iv)[1]!=ringorder_a) o++;
3995    n++;
3996    sl=sl->next;
3997  }
3998  // check whether at least one real ordering
3999  if (o==0)
4000  {
4001    WerrorS("invalid combination of orderings");
4002    return TRUE;
4003  }
4004  // if no c/C ordering is given, increment n
4005  if (i==0) n++;
4006  else if (i != 1)
4007  {
4008    // throw error if more than one is given
4009    WerrorS("more than one ordering c/C specified");
4010    return TRUE;
4011  }
4012
4013  // initialize fields of R
4014  R->order=(int *)omAlloc0(n*sizeof(int));
4015  R->block0=(int *)omAlloc0(n*sizeof(int));
4016  R->block1=(int *)omAlloc0(n*sizeof(int));
4017  R->wvhdl=(int**)omAlloc0(n*sizeof(int_ptr));
4018
4019  // init order, so that rBlocks works correctly
4020  for (j=0; j < n-1; j++)
4021    R->order[j] = (int) ringorder_unspec;
4022  // set last _C order, if no c/C order was given
4023  if (i == 0) R->order[n-2] = ringorder_C;
4024
4025  /* init orders */
4026  sl=ord;
4027  n=-1;
4028  while (sl!=NULL)
4029  {
4030    intvec *iv;
4031    iv = (intvec *)(sl->data);
4032    if ((*iv)[1]!=ringorder_L)
4033    {
4034      n++;
4035
4036      /* the format of an ordering:
4037       *  iv[0]: factor
4038       *  iv[1]: ordering
4039       *  iv[2..end]: weights
4040       */
4041      R->order[n] = (*iv)[1];
4042      switch ((*iv)[1])
4043      {
4044          case ringorder_ws:
4045          case ringorder_Ws:
4046            typ=-1;
4047          case ringorder_wp:
4048          case ringorder_Wp:
4049            R->wvhdl[n]=(int*)omAlloc((iv->length()-1)*sizeof(int));
4050            for (i=2; i<iv->length(); i++)
4051              R->wvhdl[n][i-2] = (*iv)[i];
4052            R->block0[n] = last+1;
4053            last += iv->length()-2;
4054            R->block1[n] = last;
4055            break;
4056          case ringorder_ls:
4057          case ringorder_ds:
4058          case ringorder_Ds:
4059            typ=-1;
4060          case ringorder_lp:
4061          case ringorder_dp:
4062          case ringorder_Dp:
4063          case ringorder_rp:
4064            R->block0[n] = last+1;
4065            if (iv->length() == 3) last+=(*iv)[2];
4066            else last += (*iv)[0];
4067            R->block1[n] = last;
4068            if (rCheckIV(iv)) return TRUE;
4069            break;
4070          case ringorder_S:
4071          case ringorder_c:
4072          case ringorder_C:
4073            if (rCheckIV(iv)) return TRUE;
4074            break;
4075          case ringorder_aa:
4076          case ringorder_a:
4077            R->block0[n] = last+1;
4078            R->block1[n] = si_min(last+iv->length()-2 , R->N);
4079            R->wvhdl[n] = (int*)omAlloc((iv->length()-1)*sizeof(int));
4080            for (i=2; i<iv->length(); i++)
4081            {
4082              R->wvhdl[n][i-2]=(*iv)[i];
4083              if ((*iv)[i]<0) typ=-1;
4084            }
4085            break;
4086          case ringorder_M:
4087          {
4088            int Mtyp=rTypeOfMatrixOrder(iv);
4089            if (Mtyp==0) return TRUE;
4090            if (Mtyp==-1) typ = -1;
4091
4092            R->wvhdl[n] =( int *)omAlloc((iv->length()-1)*sizeof(int));
4093            for (i=2; i<iv->length();i++)
4094              R->wvhdl[n][i-2]=(*iv)[i];
4095
4096            R->block0[n] = last+1;
4097            last += (int)sqrt((double)(iv->length()-2));
4098            R->block1[n] = last;
4099            break;
4100          }
4101
4102          case ringorder_no:
4103            R->order[n] = ringorder_unspec;
4104            return TRUE;
4105
4106          default:
4107            Werror("Internal Error: Unknown ordering %d", (*iv)[1]);
4108            R->order[n] = ringorder_unspec;
4109            return TRUE;
4110      }
4111    }
4112    sl=sl->next;
4113  }
4114
4115  // check for complete coverage
4116  if ((R->order[n]==ringorder_c) ||  (R->order[n]==ringorder_C)) n--;
4117  if (R->block1[n] != R->N)
4118  {
4119    if (((R->order[n]==ringorder_dp) ||
4120         (R->order[n]==ringorder_ds) ||
4121         (R->order[n]==ringorder_Dp) ||
4122         (R->order[n]==ringorder_Ds) ||
4123         (R->order[n]==ringorder_rp) ||
4124         (R->order[n]==ringorder_lp) ||
4125         (R->order[n]==ringorder_ls))
4126        &&
4127        R->block0[n] <= R->N)
4128    {
4129      R->block1[n] = R->N;
4130    }
4131    else
4132    {
4133      Werror("mismatch of number of vars (%d) and ordering (%d vars)",
4134             R->N,R->block1[n]);
4135      return TRUE;
4136    }
4137  }
4138  R->OrdSgn = typ;
4139  return FALSE;
4140}
4141
4142BOOLEAN rSleftvList2StringArray(sleftv* sl, char** p)
4143{
4144
4145  while(sl!=NULL)
4146  {
4147    if (sl->Name() == sNoName)
4148    {
4149      if (sl->Typ()==POLY_CMD)
4150      {
4151        sleftv s_sl;
4152        iiConvert(POLY_CMD,ANY_TYPE,-1,sl,&s_sl);
4153        if (s_sl.Name() != sNoName)
4154          *p = omStrDup(s_sl.Name());
4155        else
4156          *p = NULL;
4157        sl->next = s_sl.next;
4158        s_sl.next = NULL;
4159        s_sl.CleanUp();
4160        if (*p == NULL) return TRUE;
4161      }
4162      else
4163        return TRUE;
4164    }
4165    else
4166      *p = omStrDup(sl->Name());
4167    p++;
4168    sl=sl->next;
4169  }
4170  return FALSE;
4171}
4172
4173////////////////////
4174//
4175// rInit itself:
4176//
4177// INPUT:  s: name, pn: ch & parameter (names), rv: variable (names)
4178//         ord: ordering
4179// RETURN: currRingHdl on success
4180//         NULL        on error
4181// NOTE:   * makes new ring to current ring, on success
4182//         * considers input sleftv's as read-only
4183//idhdl rInit(char *s, sleftv* pn, sleftv* rv, sleftv* ord)
4184ring rInit(sleftv* pn, sleftv* rv, sleftv* ord)
4185{
4186  int ch;
4187  int float_len=0;
4188  int float_len2=0;
4189  ring R = NULL;
4190  idhdl tmp = NULL;
4191  BOOLEAN ffChar=FALSE;
4192  int typ = 1;
4193
4194  /* ch -------------------------------------------------------*/
4195  // get ch of ground field
4196  int numberOfAllocatedBlocks;
4197
4198  if (pn->Typ()==INT_CMD)
4199  {
4200    ch=(int)pn->Data();
4201  }
4202  else if ((pn->name != NULL)
4203  && ((strcmp(pn->name,"real")==0) || (strcmp(pn->name,"complex")==0)))
4204  {
4205    BOOLEAN complex_flag=(strcmp(pn->name,"complex")==0);
4206    ch=-1;
4207    if ((pn->next!=NULL) && (pn->next->Typ()==INT_CMD))
4208    {
4209      float_len=(int)pn->next->Data();
4210      float_len2=float_len;
4211      pn=pn->next;
4212      if ((pn->next!=NULL) && (pn->next->Typ()==INT_CMD))
4213      {
4214        float_len2=(int)pn->next->Data();
4215        pn=pn->next;
4216      }
4217    }
4218    if ((pn->next==NULL) && complex_flag)
4219    {
4220      pn->next=(leftv)omAlloc0Bin(sleftv_bin);
4221      pn->next->name=omStrDup("i");
4222    }
4223  }
4224  else
4225  {
4226    Werror("Wrong ground field specification");
4227    goto rInitError;
4228  }
4229  pn=pn->next;
4230
4231  int l, last;
4232  sleftv * sl;
4233  /*every entry in the new ring is initialized to 0*/
4234
4235  /* characteristic -----------------------------------------------*/
4236  /* input: 0 ch=0 : Q     parameter=NULL    ffChar=FALSE   float_len
4237   *         0    1 : Q(a,...)        *names         FALSE
4238   *         0   -1 : R               NULL           FALSE  0
4239   *         0   -1 : R               NULL           FALSE  prec. >6
4240   *         0   -1 : C               *names         FALSE  prec. 0..?
4241   *         p    p : Fp              NULL           FALSE
4242   *         p   -p : Fp(a)           *names         FALSE
4243   *         q    q : GF(q=p^n)       *names         TRUE
4244  */
4245  if (ch!=-1)
4246  {
4247    int l = 0;
4248
4249    if (ch!=0 && (ch<2)
4250    #ifndef NV_OPS
4251    || (ch > 32003)
4252    #endif
4253    )
4254    {
4255      Warn("%d is invalid characteristic of ground field. 32003 is used.", ch);
4256      ch=32003;
4257    }
4258    // load fftable, if necessary
4259    if (pn!=NULL)
4260    {
4261      while ((ch!=fftable[l]) && (fftable[l])) l++;
4262      if (fftable[l]==0) ch = IsPrime(ch);
4263      else
4264      {
4265        char *m[1]={(char *)sNoName};
4266        nfSetChar(ch,m);
4267        if (errorreported) goto rInitError;
4268        else ffChar=TRUE;
4269      }
4270    }
4271    else
4272      ch = IsPrime(ch);
4273  }
4274  // allocated ring and set ch
4275  R = (ring) omAlloc0Bin(sip_sring_bin);
4276  R->ch = ch;
4277  if (ch == -1)
4278  {
4279    R->float_len= si_min(float_len,32767);
4280    R->float_len2= si_min(float_len2,32767);
4281  }
4282
4283  /* parameter -------------------------------------------------------*/
4284  if (pn!=NULL)
4285  {
4286    R->P=pn->listLength();
4287    //if ((ffChar|| (ch == 1)) && (R->P > 1))
4288    if ((R->P > 1) && (ffChar || (ch == -1)))
4289    {
4290      WerrorS("too many parameters");
4291      goto rInitError;
4292    }
4293    R->parameter=(char**)omAlloc0(R->P*sizeof(char_ptr));
4294    if (rSleftvList2StringArray(pn, R->parameter))
4295    {
4296      WerrorS("parameter expected");
4297      goto rInitError;
4298    }
4299    if (ch>1 && !ffChar) R->ch=-ch;
4300    else if (ch==0) R->ch=1;
4301  }
4302  else if (ffChar)
4303  {
4304    WerrorS("need one parameter");
4305    goto rInitError;
4306  }
4307  /* post-processing of field description */
4308  // we have short reals, but no short complex
4309  if ((R->ch == - 1)
4310  && (R->parameter !=NULL)
4311  && (R->float_len < SHORT_REAL_LENGTH))
4312  {
4313    R->float_len = SHORT_REAL_LENGTH;
4314    R->float_len2 = SHORT_REAL_LENGTH;
4315  }
4316
4317  /* names and number of variables-------------------------------------*/
4318  {
4319    int l=rv->listLength();
4320#if SIZEOF_SHORT == 2
4321#define MAX_SHORT 0x7fff
4322#endif
4323    if (l>MAX_SHORT)
4324    {
4325      Werror("too many ring variables(%d), max is %d",l,MAX_SHORT);
4326       goto rInitError;
4327    }
4328    R->N = l; /*rv->listLength();*/
4329  }
4330  R->names   = (char **)omAlloc0(R->N * sizeof(char_ptr));
4331  if (rSleftvList2StringArray(rv, R->names))
4332  {
4333    WerrorS("name of ring variable expected");
4334    goto rInitError;
4335  }
4336
4337  /* check names and parameters for conflicts ------------------------- */
4338  {
4339    int i,j;
4340    for(i=0;i<R->P; i++)
4341    {
4342      for(j=0;j<R->N;j++)
4343      {
4344        if (strcmp(R->parameter[i],R->names[j])==0)
4345        {
4346          Werror("parameter %d conflicts with variable %d",i+1,j+1);
4347          goto rInitError;
4348        }
4349      }
4350    }
4351  }
4352  /* ordering -------------------------------------------------------------*/
4353  if (rSleftvOrdering2Ordering(ord, R))
4354    goto rInitError;
4355
4356  // Complete the initialization
4357  if (rComplete(R,1))
4358    goto rInitError;
4359
4360  rTest(R);
4361
4362  // try to enter the ring into the name list
4363  // need to clean up sleftv here, before this ring can be set to
4364  // new currRing or currRing can be killed beacuse new ring has
4365  // same name
4366  if (pn != NULL) pn->CleanUp();
4367  if (rv != NULL) rv->CleanUp();
4368  if (ord != NULL) ord->CleanUp();
4369  //if ((tmp = enterid(s, myynest, RING_CMD, &IDROOT))==NULL)
4370  //  goto rInitError;
4371
4372  //memcpy(IDRING(tmp),R,sizeof(*R));
4373  // set current ring
4374  //omFreeBin(R,  ip_sring_bin);
4375  //return tmp;
4376  return R;
4377
4378  // error case:
4379  rInitError:
4380  if  (R != NULL) rDelete(R);
4381  if (pn != NULL) pn->CleanUp();
4382  if (rv != NULL) rv->CleanUp();
4383  if (ord != NULL) ord->CleanUp();
4384  return NULL;
4385}
4386
4387void rKill(ring r)
4388{
4389  if ((r->ref<=0)&&(r->order!=NULL))
4390  {
4391#ifdef RDEBUG
4392    if (traceit &TRACE_SHOW_RINGS) Print("kill ring %x\n",r);
4393#endif
4394    if (r==currRing)
4395    {
4396      if (r->qideal!=NULL)
4397      {
4398        idDelete(&r->qideal);
4399        r->qideal=NULL;
4400        currQuotient=NULL;
4401      }
4402      if (ppNoether!=NULL) pDelete(&ppNoether);
4403      if (sLastPrinted.RingDependend())
4404      {
4405        sLastPrinted.CleanUp();
4406      }
4407      if ((myynest>0) && (iiRETURNEXPR[myynest].RingDependend()))
4408      {
4409        WerrorS("return value depends on local ring variable (export missing ?)");
4410        iiRETURNEXPR[myynest].CleanUp();
4411      }
4412      currRing=NULL;
4413      currRingHdl=NULL;
4414    }
4415    else if (r->qideal!=NULL)
4416    {
4417      id_Delete(&r->qideal, r);
4418      r->qideal = NULL;
4419    }
4420    int i=1;
4421    int j;
4422    int *pi=r->order;
4423#ifdef USE_IILOCALRING
4424    for (j=0;j<iiRETURNEXPR_len;j++)
4425    {
4426      if (iiLocalRing[j]==r)
4427      {
4428        if (j<myynest) Warn("killing the basering for level %d",j);
4429        iiLocalRing[j]=NULL;
4430      }
4431    }
4432#else /* USE_IILOCALRING */
4433//#endif /* USE_IILOCALRING */
4434    {
4435      proclevel * nshdl = procstack;
4436      int lev=myynest-1;
4437
4438      for(; nshdl != NULL; nshdl = nshdl->next)
4439      {
4440        if (nshdl->cRing==r)
4441        {
4442          Warn("killing the basering for level %d",lev);
4443          nshdl->cRing=NULL;
4444          nshdl->cRingHdl=NULL;
4445        }
4446      }
4447    }
4448#endif /* USE_IILOCALRING */
4449
4450    /* nKillChar(r); will be called from inside of rDelete */
4451    rDelete(r);
4452    return;
4453  }
4454  r->ref--;
4455}
4456
4457void rKill(idhdl h)
4458{
4459  ring r = IDRING(h);
4460  int ref=0;
4461  if (r!=NULL)
4462  {
4463    ref=r->ref;
4464    rKill(r);
4465  }
4466  if (h==currRingHdl)
4467  {
4468    if (ref<=0) { currRing=NULL; currRingHdl=NULL;}
4469    else
4470    {
4471      currRingHdl=rFindHdl(r,currRingHdl,NULL);
4472    }
4473  }
4474}
4475
4476idhdl rSimpleFindHdl(ring r, idhdl root, idhdl n=NULL)
4477{
4478  //idhdl next_best=NULL;
4479  idhdl h=root;
4480  while (h!=NULL)
4481  {
4482    if (((IDTYP(h)==RING_CMD)||(IDTYP(h)==QRING_CMD))
4483    && (h!=n)
4484    && (IDRING(h)==r)
4485    )
4486    {
4487   //   if (IDLEV(h)==myynest)
4488   //     return h;
4489   //   if ((IDLEV(h)==0) || (next_best==NULL))
4490   //     next_best=h;
4491   //   else if (IDLEV(next_best)<IDLEV(h))
4492   //     next_best=h;
4493      return h;
4494    }
4495    h=IDNEXT(h);
4496  }
4497  //return next_best;
4498  return NULL;
4499}
4500
Note: See TracBrowser for help on using the repository browser.