source: git/Singular/ipshell.cc @ 2ef280

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