source: git/Singular/ipshell.cc @ 666c90

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