source: git/Singular/ipshell.cc @ aaa49c

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