source: git/Singular/ipshell.cc @ 117518

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