source: git/kernel/ring.cc @ 9e2fdf

spielwiese
Last change on this file since 9e2fdf was 9e2fdf, checked in by Hans Schönemann <hannes@…>, 16 years ago
*hannes: Buch/Example_1_9_29, better error messages git-svn-id: file:///usr/local/Singular/svn/trunk@10744 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 111.2 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: ring.cc,v 1.83 2008-06-10 11:50:07 Singular Exp $ */
5
6/*
7* ABSTRACT - the interpreter related ring operations
8*/
9
10/* includes */
11#include <math.h>
12#include "mod2.h"
13#include "structs.h"
14#include "omalloc.h"
15#include "polys.h"
16#include "numbers.h"
17#include "febase.h"
18#include "intvec.h"
19#include "longalg.h"
20#include "ffields.h"
21#include "ideals.h"
22#include "ring.h"
23#include "prCopy.h"
24#include "../Singular/ipshell.h"
25#include "p_Procs.h"
26#ifdef HAVE_PLURAL
27#include "gring.h"
28#include "sca.h"
29#endif
30#include "maps.h"
31#include "matpol.h"
32#ifdef HAVE_FACTORY
33#include "factory.h"
34#endif
35
36#define BITS_PER_LONG 8*SIZEOF_LONG
37
38static const char * const ringorder_name[] =
39{
40  " ?", //ringorder_no = 0,
41  "a", //ringorder_a,
42  "A", //ringorder_a64,
43  "c", //ringorder_c,
44  "C", //ringorder_C,
45  "M", //ringorder_M,
46  "S", //ringorder_S,
47  "s", //ringorder_s,
48  "lp", //ringorder_lp,
49  "dp", //ringorder_dp,
50  "rp", //ringorder_rp,
51  "Dp", //ringorder_Dp,
52  "wp", //ringorder_wp,
53  "Wp", //ringorder_Wp,
54  "ls", //ringorder_ls,
55  "ds", //ringorder_ds,
56  "Ds", //ringorder_Ds,
57  "ws", //ringorder_ws,
58  "Ws", //ringorder_Ws,
59  "L", //ringorder_L,
60  "aa", //ringorder_aa
61  "rs", //ringorder_rs,
62  " _" //ringorder_unspec
63};
64
65const char * rSimpleOrdStr(int ord)
66{
67  return ringorder_name[ord];
68}
69
70// unconditionally deletes fields in r
71void rDelete(ring r);
72// set r->VarL_Size, r->VarL_Offset, r->VarL_LowIndex
73static void rSetVarL(ring r);
74// get r->divmask depending on bits per exponent
75static unsigned long rGetDivMask(int bits);
76// right-adjust r->VarOffset
77static void rRightAdjustVarOffset(ring r);
78static void rOptimizeLDeg(ring r);
79
80/*0 implementation*/
81//BOOLEAN rField_is_R(ring r=currRing)
82//{
83//  if (r->ch== -1)
84//  {
85//    if (r->float_len==(short)0) return TRUE;
86//  }
87//  return FALSE;
88//}
89
90// internally changes the gloabl ring and resets the relevant
91// global variables:
92void rChangeCurrRing(ring r)
93{
94 // if ((currRing!=NULL) && (currRing->minpoly!=NULL))
95 // {
96 //   omCheckAddr(currRing->minpoly);
97 // }
98  /*------------ set global ring vars --------------------------------*/
99  currRing = r;
100  currQuotient=NULL;
101  if (r != NULL)
102  {
103    rTest(r);
104    /*------------ set global ring vars --------------------------------*/
105    currQuotient=r->qideal;
106
107    /*------------ global variables related to coefficients ------------*/
108    nSetChar(r);
109
110    /*------------ global variables related to polys -------------------*/
111    pSetGlobals(r);
112    /*------------ global variables related to factory -------------------*/
113#ifdef HAVE_FACTORY
114    int c=ABS(nGetChar());
115    if (c==1) c=0;
116    setCharacteristic( c );
117#endif
118  }
119}
120
121void rNameCheck(ring R)
122{
123  int i,j;
124  for(i=0;i<R->N-1;i++)
125  {
126    for(j=i+1;j<R->N;j++)
127    {
128      if (strcmp(R->names[i],R->names[j])==0)
129      {
130        Warn("name conflict var(%d) and var(%d): `%s`",i+1,j+1,R->names[i]);
131        omFree(R->names[j]);
132        R->names[j]=(char *)omAlloc(10);
133        sprintf(R->names[j],"@(%d)",j+1);
134      }
135    }
136  }
137}
138
139ring rDefault(int ch, int N, char **n)
140{
141  ring r=(ring) omAlloc0Bin(sip_sring_bin);
142  r->ch    = ch;
143  r->N     = N;
144  /*r->P     = 0; Alloc0 */
145  /*names*/
146  r->names = (char **) omAlloc0(N * sizeof(char_ptr));
147  int i;
148  for(i=0;i<N;i++)
149  {
150    r->names[i]  = omStrDup(n[i]);
151  }
152  /*weights: entries for 2 blocks: NULL*/
153  r->wvhdl = (int **)omAlloc0(2 * sizeof(int_ptr));
154  /*order: lp,0*/
155  r->order = (int *) omAlloc(2* sizeof(int *));
156  r->block0 = (int *)omAlloc0(2 * sizeof(int *));
157  r->block1 = (int *)omAlloc0(2 * sizeof(int *));
158  /* ringorder dp for the first block: var 1..N */
159  r->order[0]  = ringorder_lp;
160  r->block0[0] = 1;
161  r->block1[0] = N;
162  /* the last block: everything is 0 */
163  r->order[1]  = 0;
164  /*polynomial ring*/
165  r->OrdSgn    = 1;
166
167  /* complete ring intializations */
168  rComplete(r);
169  return r;
170}
171
172///////////////////////////////////////////////////////////////////////////
173//
174// rInit: define a new ring from sleftv's
175//
176//-> ipshell.cc
177
178/////////////////////////////
179// Auxillary functions
180//
181
182// check intvec, describing the ordering
183BOOLEAN rCheckIV(intvec *iv)
184{
185  if ((iv->length()!=2)&&(iv->length()!=3))
186  {
187    WerrorS("weights only for orderings wp,ws,Wp,Ws,a,M");
188    return TRUE;
189  }
190  return FALSE;
191}
192
193int rTypeOfMatrixOrder(intvec * order)
194{
195  int i=0,j,typ=1;
196  int sz = (int)sqrt((double)(order->length()-2));
197  if ((sz*sz)!=(order->length()-2))
198  {
199    WerrorS("Matrix order is not a square matrix");
200    typ=0;
201  }
202  while ((i<sz) && (typ==1))
203  {
204    j=0;
205    while ((j<sz) && ((*order)[j*sz+i+2]==0)) j++;
206    if (j>=sz)
207    {
208      typ = 0;
209      WerrorS("Matrix order not complete");
210    }
211    else if ((*order)[j*sz+i+2]<0)
212      typ = -1;
213    else
214      i++;
215  }
216  return typ;
217}
218
219// set R->order, R->block, R->wvhdl, r->OrdSgn from sleftv
220BOOLEAN rSleftvOrdering2Ordering(sleftv *ord, ring R);
221
222// get array of strings from list of sleftv's
223BOOLEAN rSleftvList2StringArray(sleftv* sl, char** p);
224
225
226/*2
227 * set a new ring from the data:
228 s: name, chr: ch, varnames: rv, ordering: ord, typ: typ
229 */
230
231int r_IsRingVar(const char *n, ring r)
232{
233  if ((r!=NULL) && (r->names!=NULL))
234  {
235    for (int i=0; i<r->N; i++)
236    {
237      if (r->names[i]==NULL) return -1;
238      if (strcmp(n,r->names[i]) == 0) return (int)i;
239    }
240  }
241  return -1;
242}
243
244
245void rWrite(ring r)
246{
247  if ((r==NULL)||(r->order==NULL))
248    return; /*to avoid printing after errors....*/
249
250  int nblocks=rBlocks(r);
251
252  // omCheckAddrSize(r,sizeof(ip_sring));
253  omCheckAddrSize(r->order,nblocks*sizeof(int));
254  omCheckAddrSize(r->block0,nblocks*sizeof(int));
255  omCheckAddrSize(r->block1,nblocks*sizeof(int));
256  omCheckAddrSize(r->wvhdl,nblocks*sizeof(int_ptr));
257  omCheckAddrSize(r->names,r->N*sizeof(char_ptr));
258
259  nblocks--;
260
261
262  if (rField_is_GF(r))
263  {
264    Print("//   # ground field : %d\n",rInternalChar(r));
265    Print("//   primitive element : %s\n", r->parameter[0]);
266    if (r==currRing)
267    {
268      StringSetS("//   minpoly        : ");
269      nfShowMipo();PrintS(StringAppendS("\n"));
270    }
271  }
272#ifdef HAVE_RINGS
273  else if (rField_is_Ring(r))
274  {
275    PrintS("//   coeff. ring is : ");
276#ifdef HAVE_RINGZ
277    if (rField_is_Ring_Z(r)) PrintS("Integers\n");
278#endif
279#ifdef HAVE_RINGMODN
280    if (rField_is_Ring_ModN(r)) Print("Z/%llu\n", r->ringflaga);
281#endif
282#ifdef HAVE_RING2TOM
283    if (rField_is_Ring_2toM(r)) Print("Z/2^%llu\n", r->ringflagb);
284#endif
285#ifdef HAVE_RINGMODN
286    if (rField_is_Ring_PtoM(r)) Print("Z/%llu^%u\n", r->ringflaga, r->ringflagb);
287#endif
288  }
289#endif
290  else
291  {
292    PrintS("//   characteristic : ");
293    if ( rField_is_R(r) )             PrintS("0 (real)\n");  /* R */
294    else if ( rField_is_long_R(r) )
295      Print("0 (real:%d digits, additional %d digits)\n",
296             r->float_len,r->float_len2);  /* long R */
297    else if ( rField_is_long_C(r) )
298      Print("0 (complex:%d digits, additional %d digits)\n",
299             r->float_len, r->float_len2);  /* long C */
300    else
301      Print ("%d\n",rChar(r)); /* Fp(a) */
302    if (r->parameter!=NULL)
303    {
304      Print ("//   %d parameter    : ",rPar(r));
305      char **sp=r->parameter;
306      int nop=0;
307      while (nop<rPar(r))
308      {
309        PrintS(*sp);
310        PrintS(" ");
311        sp++; nop++;
312      }
313      PrintS("\n//   minpoly        : ");
314      if ( rField_is_long_C(r) )
315      {
316        // i^2+1:
317        Print("(%s^2+1)\n",r->parameter[0]);
318      }
319      else if (r->minpoly==NULL)
320      {
321        PrintS("0\n");
322      }
323      else if (r==currRing)
324      {
325        StringSetS(""); nWrite(r->minpoly); PrintS(StringAppendS("\n"));
326      }
327      else
328      {
329        PrintS("...\n");
330      }
331      if (r->minideal!=NULL)
332      {
333        if (r==currRing) iiWriteMatrix((matrix)r->minideal,"//   minpolys",1,0);
334        else PrintS("//   minpolys=...");
335        PrintLn();
336      }
337    }
338  }
339  Print("//   number of vars : %d",r->N);
340
341  //for (nblocks=0; r->order[nblocks]; nblocks++);
342  nblocks=rBlocks(r)-1;
343
344  for (int l=0, nlen=0 ; l<nblocks; l++)
345  {
346    int i;
347    Print("\n//        block %3d : ",l+1);
348
349    Print("ordering %s", rSimpleOrdStr(r->order[l]));
350
351    if ((r->order[l] >= ringorder_lp)
352    ||(r->order[l] == ringorder_M)
353    ||(r->order[l] == ringorder_a)
354    ||(r->order[l] == ringorder_a64)
355    ||(r->order[l] == ringorder_aa))
356    {
357      PrintS("\n//                  : names    ");
358      for (i = r->block0[l]-1; i<r->block1[l]; i++)
359      {
360        nlen = strlen(r->names[i]);
361        Print("%s ",r->names[i]);
362      }
363    }
364#ifndef NDEBUG
365    else if (r->order[l] == ringorder_s)
366    {
367      Print("  syzcomp at %d",r->typ[l].data.syz.limit);
368    }
369#endif
370
371    if (r->wvhdl[l]!=NULL)
372    {
373      for (int j= 0;
374           j<(r->block1[l]-r->block0[l]+1)*(r->block1[l]-r->block0[l]+1);
375           j+=i)
376      {
377        PrintS("\n//                  : weights  ");
378        for (i = 0; i<=r->block1[l]-r->block0[l]; i++)
379        {
380          if (r->order[l] == ringorder_a64)
381          { int64 *w=(int64 *)r->wvhdl[l];
382            Print("%*lld " ,nlen,w[i+j],i+j);
383          }
384          else
385            Print("%*d " ,nlen,r->wvhdl[l][i+j],i+j);
386        }
387        if (r->order[l]!=ringorder_M) break;
388      }
389    }
390  }
391#ifdef HAVE_PLURAL
392  if(rIsPluralRing(r))
393  {
394    PrintS("\n//   noncommutative relations:");
395    if (r==currRing)
396    {
397      poly pl=NULL;
398      int nl;
399      int i,j;
400      for (i = 1; i<r->N; i++)
401      {
402        for (j = i+1; j<=r->N; j++)
403        {
404          nl = nIsOne(p_GetCoeff(MATELEM(r->GetNC()->C,i,j),r));
405          if ( (MATELEM(r->GetNC()->D,i,j)!=NULL) || (!nl) )
406          {
407            Print("\n//    %s%s=",r->names[j-1],r->names[i-1]);
408            pl = MATELEM(r->GetNC()->MT[UPMATELEM(i,j,r->N)],1,1);
409            pWrite0(pl);
410          }
411        }
412      }
413    }
414    else PrintS(" ...");
415#ifdef PDEBUG
416    Print("\n//   noncommutative type:%d", (int)ncRingType(r));
417    Print("\n//   is skew constant:%d",r->GetNC()->IsSkewConstant);
418    if( rIsSCA(r) )
419    {
420      Print("\n//   alternating variables: [%d, %d]", scaFirstAltVar(r), scaLastAltVar(r));
421      const ideal Q = SCAQuotient(r); // resides within r!
422      if (Q!=NULL)
423      {
424        PrintS("\n//   quotient of sca by ideal");
425        if (r==currRing)
426        {
427          PrintLn();
428          iiWriteMatrix((matrix)Q,"__",1);
429        }
430        else PrintS(" ...");
431      }
432    }
433    Print("\n//   ref:%d",r->GetNC()->ref);
434#endif
435  }
436#endif
437  if (r->qideal!=NULL)
438  {
439    PrintS("\n// quotient ring from ideal");
440    if (r==currRing)
441    {
442      PrintLn();
443      iiWriteMatrix((matrix)r->qideal,"_",1);
444    }
445    else PrintS(" ...");
446  }
447}
448
449void rDelete(ring r)
450{
451  int i, j;
452
453  if (r == NULL) return;
454
455#ifdef HAVE_PLURAL
456  if (rIsPluralRing(r))
457    ncKill(r);
458#endif
459
460  nKillChar(r);
461  rUnComplete(r);
462  // delete order stuff
463  if (r->order != NULL)
464  {
465    i=rBlocks(r);
466    assume(r->block0 != NULL && r->block1 != NULL && r->wvhdl != NULL);
467    // delete order
468    omFreeSize((ADDRESS)r->order,i*sizeof(int));
469    omFreeSize((ADDRESS)r->block0,i*sizeof(int));
470    omFreeSize((ADDRESS)r->block1,i*sizeof(int));
471    // delete weights
472    for (j=0; j<i; j++)
473    {
474      if (r->wvhdl[j]!=NULL)
475        omFree(r->wvhdl[j]);
476    }
477    omFreeSize((ADDRESS)r->wvhdl,i*sizeof(int *));
478  }
479  else
480  {
481    assume(r->block0 == NULL && r->block1 == NULL && r->wvhdl == NULL);
482  }
483
484  // delete varnames
485  if(r->names!=NULL)
486  {
487    for (i=0; i<r->N; i++)
488    {
489      if (r->names[i] != NULL) omFree((ADDRESS)r->names[i]);
490    }
491    omFreeSize((ADDRESS)r->names,r->N*sizeof(char_ptr));
492  }
493
494  // delete parameter
495  if (r->parameter!=NULL)
496  {
497    char **s=r->parameter;
498    j = 0;
499    while (j < rPar(r))
500    {
501      if (*s != NULL) omFree((ADDRESS)*s);
502      s++;
503      j++;
504    }
505    omFreeSize((ADDRESS)r->parameter,rPar(r)*sizeof(char_ptr));
506  }
507  omFreeBin(r, ip_sring_bin);
508}
509
510int rOrderName(char * ordername)
511{
512  int order=ringorder_unspec;
513  while (order!= 0)
514  {
515    if (strcmp(ordername,rSimpleOrdStr(order))==0)
516      break;
517    order--;
518  }
519  if (order==0) Werror("wrong ring order `%s`",ordername);
520  omFree((ADDRESS)ordername);
521  return order;
522}
523
524char * rOrdStr(ring r)
525{
526  if ((r==NULL)||(r->order==NULL)) return omStrDup("");
527  int nblocks,l,i;
528
529  for (nblocks=0; r->order[nblocks]; nblocks++);
530  nblocks--;
531
532  StringSetS("");
533  for (l=0; ; l++)
534  {
535    StringAppend((char *)rSimpleOrdStr(r->order[l]));
536    if ((r->order[l] != ringorder_c) && (r->order[l] != ringorder_C))
537    {
538      if (r->wvhdl[l]!=NULL)
539      {
540        StringAppendS("(");
541        for (int j= 0;
542             j<(r->block1[l]-r->block0[l]+1)*(r->block1[l]-r->block0[l]+1);
543             j+=i+1)
544        {
545          char c=',';
546          if(r->order[l]==ringorder_a64)
547          {
548            int64 * w=(int64 *)r->wvhdl[l];
549            for (i = 0; i<r->block1[l]-r->block0[l]; i++)
550            {
551              StringAppend("%lld," ,w[i]);
552            }
553            StringAppend("%lld)" ,w[i]);
554            break;
555          }
556          else
557          {
558            for (i = 0; i<r->block1[l]-r->block0[l]; i++)
559            {
560              StringAppend("%d," ,r->wvhdl[l][i+j]);
561            }
562          }
563          if (r->order[l]!=ringorder_M)
564          {
565            StringAppend("%d)" ,r->wvhdl[l][i+j]);
566            break;
567          }
568          if (j+i+1==(r->block1[l]-r->block0[l]+1)*(r->block1[l]-r->block0[l]+1))
569            c=')';
570          StringAppend("%d%c" ,r->wvhdl[l][i+j],c);
571        }
572      }
573      else
574        StringAppend("(%d)",r->block1[l]-r->block0[l]+1);
575    }
576    if (l==nblocks) return omStrDup(StringAppendS(""));
577    StringAppendS(",");
578  }
579}
580
581char * rVarStr(ring r)
582{
583  if ((r==NULL)||(r->names==NULL)) return omStrDup("");
584  int i;
585  int l=2;
586  char *s;
587
588  for (i=0; i<r->N; i++)
589  {
590    l+=strlen(r->names[i])+1;
591  }
592  s=(char *)omAlloc(l);
593  s[0]='\0';
594  for (i=0; i<r->N-1; i++)
595  {
596    strcat(s,r->names[i]);
597    strcat(s,",");
598  }
599  strcat(s,r->names[i]);
600  return s;
601}
602
603char * rCharStr(ring r)
604{
605  char *s;
606  int i;
607
608  if (r->parameter==NULL)
609  {
610    i=r->ch;
611    if(i==-1)
612      s=omStrDup("real");                    /* R */
613    else
614    {
615      s=(char *)omAlloc(MAX_INT_LEN+1);
616      sprintf(s,"%d",i);                   /* Q, Z/p */
617    }
618    return s;
619  }
620  if (rField_is_long_C(r))
621  {
622    s=(char *)omAlloc(21+strlen(r->parameter[0]));
623    sprintf(s,"complex,%d,%s",r->float_len,r->parameter[0]);   /* C */
624    return s;
625  }
626  int l=0;
627  for(i=0; i<rPar(r);i++)
628  {
629    l+=(strlen(r->parameter[i])+1);
630  }
631  s=(char *)omAlloc(l+MAX_INT_LEN+1);
632  s[0]='\0';
633  if (r->ch<0)       sprintf(s,"%d",-r->ch); /* Fp(a) */
634  else if (r->ch==1) sprintf(s,"0");         /* Q(a)  */
635  else
636  {
637    sprintf(s,"%d,%s",r->ch,r->parameter[0]); /* GF(q)  */
638    return s;
639  }
640  char tt[2];
641  tt[0]=',';
642  tt[1]='\0';
643  for(i=0; i<rPar(r);i++)
644  {
645    strcat(s,tt);
646    strcat(s,r->parameter[i]);
647  }
648  return s;
649}
650
651char * rParStr(ring r)
652{
653  if ((r==NULL)||(r->parameter==NULL)) return omStrDup("");
654
655  int i;
656  int l=2;
657
658  for (i=0; i<rPar(r); i++)
659  {
660    l+=strlen(r->parameter[i])+1;
661  }
662  char *s=(char *)omAlloc(l);
663  s[0]='\0';
664  for (i=0; i<rPar(r)-1; i++)
665  {
666    strcat(s,r->parameter[i]);
667    strcat(s,",");
668  }
669  strcat(s,r->parameter[i]);
670  return s;
671}
672
673char * rString(ring r)
674{
675  char *ch=rCharStr(r);
676  char *var=rVarStr(r);
677  char *ord=rOrdStr(r);
678  char *res=(char *)omAlloc(strlen(ch)+strlen(var)+strlen(ord)+9);
679  sprintf(res,"(%s),(%s),(%s)",ch,var,ord);
680  omFree((ADDRESS)ch);
681  omFree((ADDRESS)var);
682  omFree((ADDRESS)ord);
683  return res;
684}
685
686int  rIsExtension(ring r)
687{
688  return (r->parameter!=NULL); /* R, Q, Fp: FALSE */
689}
690
691int  rIsExtension()
692{
693  return rIsExtension( currRing );
694}
695
696int rChar(ring r)
697{
698  if (rField_is_numeric(r))
699    return 0;
700  if (!rIsExtension(r)) /* Q, Fp */
701    return r->ch;
702  if (rField_is_Zp_a(r))  /* Fp(a)  */
703    return -r->ch;
704  if (rField_is_Q_a(r))   /* Q(a)  */
705    return 0;
706  /*else*/               /* GF(p,n) */
707  {
708    if ((r->ch & 1)==0) return 2;
709    int i=3;
710    while ((r->ch % i)!=0) i+=2;
711    return i;
712  }
713}
714
715/*2
716 *returns -1 for not compatible, (sum is undefined)
717 *         1 for compatible (and sum)
718 */
719/* vartest: test for variable/paramter names
720* dp_dp: for comm. rings: use block order dp + dp/ds/wp
721*/
722int rTensor(ring r1, ring r2, ring &sum, BOOLEAN vartest, BOOLEAN dp_dp)
723{
724  ring save=currRing;
725  ip_sring tmpR;
726  memset(&tmpR,0,sizeof(tmpR));
727  /* check coeff. field =====================================================*/
728  if (rInternalChar(r1)==rInternalChar(r2))
729  {
730    tmpR.ch=rInternalChar(r1);
731    if (rField_is_Q(r1)||rField_is_Zp(r1)||rField_is_GF(r1)) /*Q, Z/p, GF(p,n)*/
732    {
733      if (r1->parameter!=NULL)
734      {
735        if (!vartest || (strcmp(r1->parameter[0],r2->parameter[0])==0)) /* 1 par */
736        {
737          tmpR.parameter=(char **)omAllocBin(char_ptr_bin);
738          tmpR.parameter[0]=omStrDup(r1->parameter[0]);
739          tmpR.P=1;
740        }
741        else
742        {
743          WerrorS("GF(p,n)+GF(p,n)");
744          return -1;
745        }
746      }
747    }
748    else if ((r1->ch==1)||(r1->ch<-1)) /* Q(a),Z/p(a) */
749    {
750      if (r1->minpoly!=NULL)
751      {
752        if (r2->minpoly!=NULL)
753        {
754          // HANNES: TODO: delete nSetChar
755          rChangeCurrRing(r1);
756          if ((!vartest || (strcmp(r1->parameter[0],r2->parameter[0])==0)) /* 1 par */
757              && n_Equal(r1->minpoly,r2->minpoly, r1))
758          {
759            tmpR.parameter=(char **)omAllocBin(char_ptr_bin);
760            tmpR.parameter[0]=omStrDup(r1->parameter[0]);
761            tmpR.minpoly=n_Copy(r1->minpoly, r1);
762            tmpR.P=1;
763            // HANNES: TODO: delete nSetChar
764            rChangeCurrRing(save);
765          }
766          else
767          {
768            // HANNES: TODO: delete nSetChar
769            rChangeCurrRing(save);
770            WerrorS("different minpolys");
771            return -1;
772          }
773        }
774        else
775        {
776          if ((!vartest || (strcmp(r1->parameter[0],r2->parameter[0])==0)) /* 1 par */
777              && (rPar(r2)==1))
778          {
779            tmpR.parameter=(char **)omAllocBin(char_ptr_bin);
780            tmpR.parameter[0]=omStrDup(r1->parameter[0]);
781            tmpR.P=1;
782            tmpR.minpoly=n_Copy(r1->minpoly, r1);
783          }
784          else
785          {
786            WerrorS("different parameters and minpoly!=0");
787            return -1;
788          }
789        }
790      }
791      else /* r1->minpoly==NULL */
792      {
793        if (r2->minpoly!=NULL)
794        {
795          if ((!vartest || (strcmp(r1->parameter[0],r2->parameter[0])==0)) /* 1 par */
796              && (rPar(r1)==1))
797          {
798            tmpR.parameter=(char **)omAllocBin(char_ptr_bin);
799            tmpR.parameter[0]=omStrDup(r1->parameter[0]);
800            tmpR.P=1;
801            tmpR.minpoly=n_Copy(r2->minpoly, r2);
802          }
803          else
804          {
805            WerrorS("different parameters and minpoly!=0");
806            return -1;
807          }
808        }
809        else
810        {
811          int len=rPar(r1)+rPar(r2);
812          tmpR.parameter=(char **)omAlloc0(len*sizeof(char_ptr));
813          int i;
814          for (i=0;i<rPar(r1);i++)
815          {
816            tmpR.parameter[i]=omStrDup(r1->parameter[i]);
817          }
818          int j,l;
819          for(j=0;j<rPar(r2);j++)
820          {
821            if (vartest)
822            {
823              for(l=0;l<i;l++)
824              {
825                if(strcmp(tmpR.parameter[l],r2->parameter[j])==0)
826                  break;
827              }
828            }
829            else
830              l=i;
831            if (l==i)
832            {
833              tmpR.parameter[i]=omStrDup(r2->parameter[j]);
834              i++;
835            }
836          }
837          if (i!=len)
838          {
839            tmpR.parameter=(char**)omReallocSize(tmpR.parameter,len*sizeof(char_ptr),i*sizeof(char_ptr));
840          }
841          tmpR.P=i;
842        }
843      }
844    }
845  }
846  else /* r1->ch!=r2->ch */
847  {
848    if (r1->ch<-1) /* Z/p(a) */
849    {
850      if ((r2->ch==0) /* Q */
851          || (r2->ch==-r1->ch)) /* Z/p */
852      {
853        tmpR.ch=rInternalChar(r1);
854        tmpR.P=rPar(r1);
855        tmpR.parameter=(char **)omAlloc(rPar(r1)*sizeof(char_ptr));
856        int i;
857        for (i=0;i<rPar(r1);i++)
858        {
859          tmpR.parameter[i]=omStrDup(r1->parameter[i]);
860        }
861        if (r1->minpoly!=NULL)
862        {
863          tmpR.minpoly=n_Copy(r1->minpoly, r1);
864        }
865      }
866      else  /* R, Q(a),Z/q,Z/p(a),GF(p,n) */
867      {
868        WerrorS("Z/p(a)+(R,Q(a),Z/q(a),GF(q,n))");
869        return -1;
870      }
871    }
872    else if (r1->ch==-1) /* R */
873    {
874      WerrorS("R+..");
875      return -1;
876    }
877    else if (r1->ch==0) /* Q */
878    {
879      if ((r2->ch<-1)||(r2->ch==1)) /* Z/p(a),Q(a) */
880      {
881        tmpR.ch=rInternalChar(r2);
882        tmpR.P=rPar(r2);
883        tmpR.parameter=(char **)omAlloc(rPar(r2)*sizeof(char_ptr));
884        int i;
885        for (i=0;i<rPar(r2);i++)
886        {
887          tmpR.parameter[i]=omStrDup(r2->parameter[i]);
888        }
889        if (r2->minpoly!=NULL)
890        {
891          tmpR.minpoly=n_Copy(r2->minpoly, r2);
892        }
893      }
894      else if (r2->ch>1) /* Z/p,GF(p,n) */
895      {
896        tmpR.ch=r2->ch;
897        if (r2->parameter!=NULL)
898        {
899          tmpR.parameter=(char **)omAllocBin(char_ptr_bin);
900          tmpR.P=1;
901          tmpR.parameter[0]=omStrDup(r2->parameter[0]);
902        }
903      }
904      else
905      {
906        WerrorS("Q+R");
907        return -1; /* R */
908      }
909    }
910    else if (r1->ch==1) /* Q(a) */
911    {
912      if (r2->ch==0) /* Q */
913      {
914        tmpR.ch=rInternalChar(r1);
915        tmpR.P=rPar(r1);
916        tmpR.parameter=(char **)omAlloc(rPar(r1)*sizeof(char_ptr));
917        int i;
918        for(i=0;i<rPar(r1);i++)
919        {
920          tmpR.parameter[i]=omStrDup(r1->parameter[i]);
921        }
922        if (r1->minpoly!=NULL)
923        {
924          tmpR.minpoly=n_Copy(r1->minpoly, r1);
925        }
926      }
927      else  /* R, Z/p,GF(p,n) */
928      {
929        WerrorS("Q(a)+(R,Z/p,GF(p,n))");
930        return -1;
931      }
932    }
933    else /* r1->ch >=2 , Z/p */
934    {
935      if (r2->ch==0) /* Q */
936      {
937        tmpR.ch=r1->ch;
938      }
939      else if (r2->ch==-r1->ch) /* Z/p(a) */
940      {
941        tmpR.ch=rInternalChar(r2);
942        tmpR.P=rPar(r2);
943        tmpR.parameter=(char **)omAlloc(rPar(r2)*sizeof(char_ptr));
944        int i;
945        for(i=0;i<rPar(r2);i++)
946        {
947          tmpR.parameter[i]=omStrDup(r2->parameter[i]);
948        }
949        if (r2->minpoly!=NULL)
950        {
951          tmpR.minpoly=n_Copy(r2->minpoly, r2);
952        }
953      }
954      else
955      {
956        WerrorS("Z/p+(GF(q,n),Z/q(a),R,Q(a))");
957        return -1; /* GF(p,n),Z/q(a),R,Q(a) */
958      }
959    }
960  }
961  /* variable names ========================================================*/
962  int i,j,k;
963  int l=r1->N+r2->N;
964  char **names=(char **)omAlloc0(l*sizeof(char_ptr));
965  k=0;
966
967  // collect all varnames from r1, except those which are parameters
968  // of r2, or those which are the empty string
969  for (i=0;i<r1->N;i++)
970  {
971    BOOLEAN b=TRUE;
972
973    if (*(r1->names[i]) == '\0')
974      b = FALSE;
975    else if ((r2->parameter!=NULL) && (strlen(r1->names[i])==1))
976    {
977      if (vartest)
978      {
979        for(j=0;j<rPar(r2);j++)
980        {
981          if (strcmp(r1->names[i],r2->parameter[j])==0)
982          {
983            b=FALSE;
984            break;
985          }
986        }
987      }
988    }
989
990    if (b)
991    {
992      //Print("name : %d: %s\n",k,r1->names[i]);
993      names[k]=omStrDup(r1->names[i]);
994      k++;
995    }
996    //else
997    //  Print("no name (par1) %s\n",r1->names[i]);
998  }
999  // Add variables from r2, except those which are parameters of r1
1000  // those which are empty strings, and those which equal a var of r1
1001  for(i=0;i<r2->N;i++)
1002  {
1003    BOOLEAN b=TRUE;
1004
1005    if (*(r2->names[i]) == '\0')
1006      b = FALSE;
1007    else if ((r1->parameter!=NULL) && (strlen(r2->names[i])==1))
1008    {
1009      if (vartest)
1010      {
1011        for(j=0;j<rPar(r1);j++)
1012        {
1013          if (strcmp(r2->names[i],r1->parameter[j])==0)
1014          {
1015            b=FALSE;
1016            break;
1017          }
1018        }
1019      }
1020    }
1021
1022    if (b)
1023    {
1024      if (vartest)
1025      {
1026        for(j=0;j<r1->N;j++)
1027        {
1028          if (strcmp(r1->names[j],r2->names[i])==0)
1029          {
1030            b=FALSE;
1031            break;
1032          }
1033        }
1034      }
1035      if (b)
1036      {
1037        //Print("name : %d : %s\n",k,r2->names[i]);
1038        names[k]=omStrDup(r2->names[i]);
1039        k++;
1040      }
1041      //else
1042      //  Print("no name (var): %s\n",r2->names[i]);
1043    }
1044    //else
1045    //  Print("no name (par): %s\n",r2->names[i]);
1046  }
1047  // check whether we found any vars at all
1048  if (k == 0)
1049  {
1050    names[k]=omStrDup("");
1051    k=1;
1052  }
1053  tmpR.N=k;
1054  tmpR.names=names;
1055  /* ordering *======================================================== */
1056  tmpR.OrdSgn=1;
1057  if (dp_dp
1058#ifdef HAVE_PLURAL
1059      && !rIsPluralRing(r1) && !rIsPluralRing(r2)
1060#endif     
1061     )
1062  {
1063    tmpR.order=(int*)omAlloc(4*sizeof(int));
1064    tmpR.block0=(int*)omAlloc0(4*sizeof(int));
1065    tmpR.block1=(int*)omAlloc0(4*sizeof(int));
1066    tmpR.wvhdl=(int**)omAlloc0(4*sizeof(int_ptr));
1067    tmpR.order[0]=ringorder_dp;
1068    tmpR.block0[0]=1;
1069    tmpR.block1[0]=rVar(r1);
1070    if (r2->OrdSgn==1)
1071    {
1072      if ((r2->block0[0]==1)
1073      && (r2->block1[0]==rVar(r2))
1074      && ((r2->order[0]==ringorder_wp)
1075        || (r2->order[0]==ringorder_Wp)
1076        || (r2->order[0]==ringorder_Dp))
1077     )
1078     {
1079       tmpR.order[1]=r2->order[0];
1080       if (r2->wvhdl[0]!=NULL)
1081         tmpR.wvhdl[1]=(int *)omMemDup(r2->wvhdl[0]);
1082     }
1083     else
1084        tmpR.order[1]=ringorder_dp;
1085    }
1086    else
1087    {
1088      tmpR.order[1]=ringorder_ds;
1089      tmpR.OrdSgn=-1;
1090    }
1091    tmpR.block0[1]=rVar(r1)+1;
1092    tmpR.block1[1]=rVar(r1)+rVar(r2);
1093    tmpR.order[2]=ringorder_C;
1094    tmpR.order[3]=0;
1095  }
1096  else
1097  {
1098    if ((r1->order[0]==ringorder_unspec)
1099        && (r2->order[0]==ringorder_unspec))
1100    {
1101      tmpR.order=(int*)omAlloc(3*sizeof(int));
1102      tmpR.block0=(int*)omAlloc(3*sizeof(int));
1103      tmpR.block1=(int*)omAlloc(3*sizeof(int));
1104      tmpR.wvhdl=(int**)omAlloc0(3*sizeof(int_ptr));
1105      tmpR.order[0]=ringorder_unspec;
1106      tmpR.order[1]=ringorder_C;
1107      tmpR.order[2]=0;
1108      tmpR.block0[0]=1;
1109      tmpR.block1[0]=tmpR.N;
1110    }
1111    else if (l==k) /* r3=r1+r2 */
1112    {
1113      int b;
1114      ring rb;
1115      if (r1->order[0]==ringorder_unspec)
1116      {
1117        /* extend order of r2 to r3 */
1118        b=rBlocks(r2);
1119        rb=r2;
1120        tmpR.OrdSgn=r2->OrdSgn;
1121      }
1122      else if (r2->order[0]==ringorder_unspec)
1123      {
1124        /* extend order of r1 to r3 */
1125        b=rBlocks(r1);
1126        rb=r1;
1127        tmpR.OrdSgn=r1->OrdSgn;
1128      }
1129      else
1130      {
1131        b=rBlocks(r1)+rBlocks(r2)-2; /* for only one order C, only one 0 */
1132        rb=NULL;
1133      }
1134      tmpR.order=(int*)omAlloc0(b*sizeof(int));
1135      tmpR.block0=(int*)omAlloc0(b*sizeof(int));
1136      tmpR.block1=(int*)omAlloc0(b*sizeof(int));
1137      tmpR.wvhdl=(int**)omAlloc0(b*sizeof(int_ptr));
1138      /* weights not implemented yet ...*/
1139      if (rb!=NULL)
1140      {
1141        for (i=0;i<b;i++)
1142        {
1143          tmpR.order[i]=rb->order[i];
1144          tmpR.block0[i]=rb->block0[i];
1145          tmpR.block1[i]=rb->block1[i];
1146          if (rb->wvhdl[i]!=NULL)
1147            WarnS("rSum: weights not implemented");
1148        }
1149        tmpR.block0[0]=1;
1150      }
1151      else /* ring sum for complete rings */
1152      {
1153        for (i=0;r1->order[i]!=0;i++)
1154        {
1155          tmpR.order[i]=r1->order[i];
1156          tmpR.block0[i]=r1->block0[i];
1157          tmpR.block1[i]=r1->block1[i];
1158          if (r1->wvhdl[i]!=NULL)
1159            tmpR.wvhdl[i] = (int*) omMemDup(r1->wvhdl[i]);
1160        }
1161        j=i;
1162        i--;
1163        if ((r1->order[i]==ringorder_c)
1164            ||(r1->order[i]==ringorder_C))
1165        {
1166          j--;
1167          tmpR.order[b-2]=r1->order[i];
1168        }
1169        for (i=0;r2->order[i]!=0;i++)
1170        {
1171          if ((r2->order[i]!=ringorder_c)
1172              &&(r2->order[i]!=ringorder_C))
1173          {
1174            tmpR.order[j]=r2->order[i];
1175            tmpR.block0[j]=r2->block0[i]+rVar(r1);
1176            tmpR.block1[j]=r2->block1[i]+rVar(r1);
1177            if (r2->wvhdl[i]!=NULL)
1178            {
1179              tmpR.wvhdl[j] = (int*) omMemDup(r2->wvhdl[i]);
1180            }
1181            j++;
1182          }
1183        }
1184        if((r1->OrdSgn==-1)||(r2->OrdSgn==-1))
1185          tmpR.OrdSgn=-1;
1186      }
1187    }
1188    else if ((k==rVar(r1)) && (k==rVar(r2))) /* r1 and r2 are "quite" the same ring */
1189      /* copy r1, because we have the variables from r1 */
1190    {
1191      int b=rBlocks(r1);
1192 
1193      tmpR.order=(int*)omAlloc0(b*sizeof(int));
1194      tmpR.block0=(int*)omAlloc0(b*sizeof(int));
1195      tmpR.block1=(int*)omAlloc0(b*sizeof(int));
1196      tmpR.wvhdl=(int**)omAlloc0(b*sizeof(int_ptr));
1197      /* weights not implemented yet ...*/
1198      for (i=0;i<b;i++)
1199      {
1200        tmpR.order[i]=r1->order[i];
1201        tmpR.block0[i]=r1->block0[i];
1202        tmpR.block1[i]=r1->block1[i];
1203        if (r1->wvhdl[i]!=NULL)
1204        {
1205          tmpR.wvhdl[i] = (int*) omMemDup(r1->wvhdl[i]);
1206        }
1207      }
1208      tmpR.OrdSgn=r1->OrdSgn;
1209    }
1210    else
1211    {
1212      for(i=0;i<k;i++) omFree((ADDRESS)tmpR.names[i]);
1213      omFreeSize((ADDRESS)names,tmpR.N*sizeof(char_ptr));
1214      Werror("difficulties with variables: %d,%d -> %d",rVar(r1),rVar(r2),k);
1215      return -1;
1216    }
1217  }
1218  sum=(ring)omAllocBin(ip_sring_bin);
1219  memcpy(sum,&tmpR,sizeof(ip_sring));
1220  rComplete(sum);
1221
1222//#ifdef RDEBUG
1223//  rDebugPrint(sum);
1224//#endif
1225
1226#ifdef HAVE_PLURAL
1227  if(1)
1228  {
1229    ring old_ring = currRing;
1230
1231    BOOLEAN R1_is_nc = rIsPluralRing(r1);
1232    BOOLEAN R2_is_nc = rIsPluralRing(r2);
1233
1234    if ( (R1_is_nc) || (R2_is_nc))
1235    {
1236      rChangeCurrRing(r1); /* since rCopy works well only in currRing */
1237      ring R1 = rCopy(r1);
1238      if ( !R1_is_nc ) nc_rCreateNCcomm(R1);
1239
1240#if 0
1241#ifdef RDEBUG
1242      rWrite(R1);
1243      rDebugPrint(R1);
1244#endif
1245#endif
1246      rChangeCurrRing(r2);
1247      ring R2 = rCopy(r2);
1248      if ( !R2_is_nc ) nc_rCreateNCcomm(R2);
1249
1250#if 0
1251#ifdef RDEBUG
1252      rWrite(R2);
1253      rDebugPrint(R2);
1254#endif
1255#endif
1256
1257      rChangeCurrRing(sum); // ?
1258
1259      // Projections from R_i into Sum:
1260      /* multiplication matrices business: */
1261      /* find permutations of vars and pars */
1262      int *perm1 = (int *)omAlloc0((rVar(R1)+1)*sizeof(int));
1263      int *par_perm1 = NULL;
1264      if (rPar(R1)!=0) par_perm1=(int *)omAlloc0((rPar(R1)+1)*sizeof(int));
1265
1266      int *perm2 = (int *)omAlloc0((rVar(R2)+1)*sizeof(int));
1267      int *par_perm2 = NULL;
1268      if (rPar(R2)!=0) par_perm2=(int *)omAlloc0((rPar(R2)+1)*sizeof(int));
1269
1270      maFindPerm(R1->names,  rVar(R1),  R1->parameter,  rPar(R1),
1271                 sum->names, rVar(sum), sum->parameter, rPar(sum),
1272                 perm1, par_perm1, sum->ch);
1273
1274      maFindPerm(R2->names,  rVar(R2),  R2->parameter,  rPar(R2),
1275                 sum->names, rVar(sum), sum->parameter, rPar(sum),
1276                 perm2, par_perm2, sum->ch);
1277
1278      nMapFunc nMap1 = nSetMap(R1);
1279      nMapFunc nMap2 = nSetMap(R2);
1280
1281      matrix C1 = R1->GetNC()->C, C2 = R2->GetNC()->C;
1282      matrix D1 = R1->GetNC()->D, D2 = R2->GetNC()->D;
1283
1284      // !!!! BUG? C1 and C2 might live in different baserings!!!
1285      // it cannot be both the currRing! :)
1286      // the currRing is sum!
1287
1288      int l = rVar(R1) + rVar(R2);
1289
1290      matrix C  = mpNew(l,l);
1291      matrix D  = mpNew(l,l);
1292
1293      int param_shift = 0;
1294
1295      for (i = 1; i <= rVar(R1); i++)
1296        for (j= rVar(R1)+1; j <= l; j++)
1297          MATELEM(C,i,j) = p_ISet(1, sum); // in 'sum'
1298
1299      idTest((ideal)C);
1300
1301      // Create blocked C and D matrices:
1302      for (i=1; i<= rVar(R1); i++)
1303        for (j=i+1; j<=rVar(R1); j++)
1304        {
1305          assume(MATELEM(C1,i,j) != NULL);
1306          MATELEM(C,i,j) = pPermPoly(MATELEM(C1,i,j), perm1, R1, nMap1, par_perm1, rPar(R1)); // need ADD + CMP ops.
1307
1308          if (MATELEM(D1,i,j) != NULL)
1309            MATELEM(D,i,j) = pPermPoly(MATELEM(D1,i,j),perm1,R1,nMap1,par_perm1,rPar(R1));
1310        }
1311
1312      idTest((ideal)C);
1313      idTest((ideal)D);
1314
1315 
1316      for (i=1; i<= rVar(R2); i++)
1317        for (j=i+1; j<=rVar(R2); j++)
1318        {
1319          assume(MATELEM(C2,i,j) != NULL);
1320          MATELEM(C,rVar(R1)+i,rVar(R1)+j) = pPermPoly(MATELEM(C2,i,j),perm2,R2,nMap2,par_perm2,rPar(R2));
1321
1322          if (MATELEM(D2,i,j) != NULL)
1323            MATELEM(D,rVar(R1)+i,rVar(R1)+j) = pPermPoly(MATELEM(D2,i,j),perm2,R2,nMap2,par_perm2,rPar(R2));
1324        }
1325
1326      idTest((ideal)C);
1327      idTest((ideal)D);
1328
1329      // Now sum is non-commutative with blocked structure constants!
1330      if (nc_CallPlural(C, D, NULL, NULL, sum, false, false, true, sum))
1331        WarnS("Error initializing non-commutative multiplication!");
1332
1333      /* delete R1, R2*/
1334
1335#if 0
1336#ifdef RDEBUG
1337      rWrite(sum);
1338      rDebugPrint(sum);
1339
1340      Print("\nRefs: R1: %d, R2: %d\n", R1->GetNC()->ref, R2->GetNC()->ref);
1341     
1342#endif
1343#endif
1344
1345         
1346      rDelete(R1);
1347      rDelete(R2);
1348
1349      /* delete perm arrays */
1350      if (perm1!=NULL) omFree((ADDRESS)perm1);
1351      if (perm2!=NULL) omFree((ADDRESS)perm2);
1352      if (par_perm1!=NULL) omFree((ADDRESS)par_perm1);
1353      if (par_perm2!=NULL) omFree((ADDRESS)par_perm2);
1354
1355      rChangeCurrRing(old_ring);
1356    }
1357 
1358  }
1359#endif
1360
1361  ideal Q=NULL;
1362  ideal Q1=NULL, Q2=NULL;
1363  ring old_ring2 = currRing;
1364  if (r1->qideal!=NULL)
1365  {
1366    rChangeCurrRing(sum);
1367//     if (r2->qideal!=NULL)
1368//     {
1369//       WerrorS("todo: qring+qring");
1370//       return -1;
1371//     }
1372//     else
1373//     {}
1374    /* these were defined in the Plural Part above... */
1375    int *perm1 = (int *)omAlloc0((rVar(r1)+1)*sizeof(int));
1376    int *par_perm1 = NULL;
1377    if (rPar(r1)!=0) par_perm1=(int *)omAlloc0((rPar(r1)+1)*sizeof(int));
1378    maFindPerm(r1->names,  rVar(r1),  r1->parameter,  rPar(r1),
1379               sum->names, rVar(sum), sum->parameter, rPar(sum),
1380               perm1, par_perm1, sum->ch);
1381    nMapFunc nMap1 = nSetMap(r1);
1382    Q1 = idInit(IDELEMS(r1->qideal),1);
1383    for (int for_i=0;for_i<IDELEMS(r1->qideal);for_i++)
1384      Q1->m[for_i] = pPermPoly(r1->qideal->m[for_i],perm1,r1,nMap1,par_perm1,rPar(r1));
1385    omFree((ADDRESS)perm1);
1386  }
1387
1388  if (r2->qideal!=NULL)
1389  {
1390    if (currRing!=sum)
1391      rChangeCurrRing(sum);
1392    int *perm2 = (int *)omAlloc0((rVar(r2)+1)*sizeof(int));
1393    int *par_perm2 = NULL;
1394    if (rPar(r2)!=0) par_perm2=(int *)omAlloc0((rPar(r2)+1)*sizeof(int));
1395    maFindPerm(r2->names,  rVar(r2),  r2->parameter,  rPar(r2),
1396               sum->names, rVar(sum), sum->parameter, rPar(sum),
1397               perm2, par_perm2, sum->ch);
1398    nMapFunc nMap2 = nSetMap(r2);
1399    Q2 = idInit(IDELEMS(r2->qideal),1);
1400    for (int for_i=0;for_i<IDELEMS(r2->qideal);for_i++)
1401      Q2->m[for_i] = pPermPoly(r2->qideal->m[for_i],perm2,r2,nMap2,par_perm2,rPar(r2));
1402    omFree((ADDRESS)perm2);
1403  }
1404  if ( (Q1!=NULL) || ( Q2!=NULL))
1405  {
1406    Q = idSimpleAdd(Q1,Q2);
1407    rChangeCurrRing(old_ring2);
1408  }
1409  sum->qideal = Q;
1410
1411#ifdef HAVE_PLURAL
1412  if( rIsPluralRing(sum) )
1413    nc_SetupQuotient( sum );
1414#endif
1415  return 1;
1416}
1417
1418/*2
1419 *returns -1 for not compatible, (sum is undefined)
1420 *         0 for equal, (and sum)
1421 *         1 for compatible (and sum)
1422 */
1423int rSum(ring r1, ring r2, ring &sum)
1424{
1425  if (r1==r2)
1426  {
1427    sum=r1;
1428    r1->ref++;
1429    return 0;
1430  }
1431  return rTensor(r1,r2,sum,TRUE,FALSE);
1432}
1433
1434/*2
1435 * create a copy of the ring r, which must be equivalent to currRing
1436 * used for qring definition,..
1437 * (i.e.: normal rings: same nCopy as currRing;
1438 *        qring:        same nCopy, same idCopy as currRing)
1439 * DOES NOT CALL rComplete
1440 */
1441ring rCopy0(ring r, BOOLEAN copy_qideal, BOOLEAN copy_ordering)
1442{
1443  if (r == NULL) return NULL;
1444  int i,j;
1445  ring res=(ring)omAllocBin(ip_sring_bin);
1446
1447  memcpy4(res,r,sizeof(ip_sring));
1448  res->VarOffset = NULL;
1449  res->ref=0;
1450  if (r->algring!=NULL)
1451    r->algring->ref++;
1452  if (r->parameter!=NULL)
1453  {
1454    res->minpoly=nCopy(r->minpoly);
1455    int l=rPar(r);
1456    res->parameter=(char **)omAlloc(l*sizeof(char_ptr));
1457    int i;
1458    for(i=0;i<rPar(r);i++)
1459    {
1460      res->parameter[i]=omStrDup(r->parameter[i]);
1461    }
1462    if (r->minideal!=NULL)
1463    {
1464      res->minideal=id_Copy(r->minideal,r->algring);
1465    }
1466  }
1467  if (copy_ordering == TRUE)
1468  {
1469    i=rBlocks(r);
1470    res->wvhdl   = (int **)omAlloc(i * sizeof(int_ptr));
1471    res->order   = (int *) omAlloc(i * sizeof(int));
1472    res->block0  = (int *) omAlloc(i * sizeof(int));
1473    res->block1  = (int *) omAlloc(i * sizeof(int));
1474    for (j=0; j<i; j++)
1475    {
1476      if (r->wvhdl[j]!=NULL)
1477      {
1478        res->wvhdl[j] = (int*) omMemDup(r->wvhdl[j]);
1479      }
1480      else
1481        res->wvhdl[j]=NULL;
1482    }
1483    memcpy4(res->order,r->order,i * sizeof(int));
1484    memcpy4(res->block0,r->block0,i * sizeof(int));
1485    memcpy4(res->block1,r->block1,i * sizeof(int));
1486  }
1487  else
1488  {
1489    res->wvhdl = NULL;
1490    res->order = NULL;
1491    res->block0 = NULL;
1492    res->block1 = NULL;
1493  }
1494
1495  res->names   = (char **)omAlloc0(rVar(r) * sizeof(char_ptr));
1496  for (i=0; i<rVar(res); i++)
1497  {
1498    res->names[i] = omStrDup(r->names[i]);
1499  }
1500  res->idroot = NULL;
1501  if (r->qideal!=NULL)
1502  {
1503    if (copy_qideal) res->qideal= idrCopyR_NoSort(r->qideal, r);
1504    else res->qideal = NULL;
1505  }
1506#ifdef HAVE_PLURAL
1507  res->GetNC() = NULL; // copy is purely commutative!!!
1508//  if (rIsPluralRing(r))
1509//    nc_rCopy0(res, r); // is this correct??? imho: no!
1510#endif
1511  return res;
1512}
1513
1514/*2
1515 * create a copy of the ring r, which must be equivalent to currRing
1516 * used for qring definition,..
1517 * (i.e.: normal rings: same nCopy as currRing;
1518 *        qring:        same nCopy, same idCopy as currRing)
1519 */
1520ring rCopy(ring r)
1521{
1522  if (r == NULL) return NULL;
1523  ring res=rCopy0(r);
1524  rComplete(res, 1); // res is purely commutative so far
1525
1526#ifdef HAVE_PLURAL
1527  // update nc structure on res: share NC structure of r with res since they are the same!!!
1528  // i.e. no data copy!!! Multiplications will be setuped as well!
1529  if (rIsPluralRing(r))
1530    nc_rCopy0(res, r);
1531#endif
1532
1533  return res;
1534}
1535
1536// returns TRUE, if r1 equals r2 FALSE, otherwise Equality is
1537// determined componentwise, if qr == 1, then qrideal equality is
1538// tested, as well
1539BOOLEAN rEqual(ring r1, ring r2, BOOLEAN qr)
1540{
1541  int i, j;
1542
1543  if (r1 == r2) return TRUE;
1544
1545  if (r1 == NULL || r2 == NULL) return FALSE;
1546
1547  if ((rInternalChar(r1) != rInternalChar(r2))
1548  || (r1->float_len != r2->float_len)
1549  || (r1->float_len2 != r2->float_len2)
1550  || (rVar(r1) != rVar(r2))
1551  || (r1->OrdSgn != r2->OrdSgn)
1552  || (rPar(r1) != rPar(r2)))
1553    return FALSE;
1554
1555  for (i=0; i<rVar(r1); i++)
1556  {
1557    if (r1->names[i] != NULL && r2->names[i] != NULL)
1558    {
1559      if (strcmp(r1->names[i], r2->names[i])) return FALSE;
1560    }
1561    else if ((r1->names[i] != NULL) ^ (r2->names[i] != NULL))
1562    {
1563      return FALSE;
1564    }
1565  }
1566
1567  i=0;
1568  while (r1->order[i] != 0)
1569  {
1570    if (r2->order[i] == 0) return FALSE;
1571    if ((r1->order[i] != r2->order[i])
1572    || (r1->block0[i] != r2->block0[i])
1573    || (r1->block1[i] != r2->block1[i]))
1574      return FALSE;
1575    if (r1->wvhdl[i] != NULL)
1576    {
1577      if (r2->wvhdl[i] == NULL)
1578        return FALSE;
1579      for (j=0; j<r1->block1[i]-r1->block0[i]+1; j++)
1580        if (r2->wvhdl[i][j] != r1->wvhdl[i][j])
1581          return FALSE;
1582    }
1583    else if (r2->wvhdl[i] != NULL) return FALSE;
1584    i++;
1585  }
1586  if (r2->order[i] != 0) return FALSE;
1587
1588  for (i=0; i<rPar(r1);i++)
1589  {
1590      if (strcmp(r1->parameter[i], r2->parameter[i])!=0)
1591        return FALSE;
1592  }
1593
1594  if (r1->minpoly != NULL)
1595  {
1596    if (r2->minpoly == NULL) return FALSE;
1597    if (currRing == r1 || currRing == r2)
1598    {
1599      if (! nEqual(r1->minpoly, r2->minpoly)) return FALSE;
1600    }
1601  }
1602  else if (r2->minpoly != NULL) return FALSE;
1603
1604  if (qr)
1605  {
1606    if (r1->qideal != NULL)
1607    {
1608      ideal id1 = r1->qideal, id2 = r2->qideal;
1609      int i, n;
1610      poly *m1, *m2;
1611
1612      if (id2 == NULL) return FALSE;
1613      if ((n = IDELEMS(id1)) != IDELEMS(id2)) return FALSE;
1614
1615      if (currRing == r1 || currRing == r2)
1616      {
1617        m1 = id1->m;
1618        m2 = id2->m;
1619        for (i=0; i<n; i++)
1620          if (! pEqualPolys(m1[i],m2[i])) return FALSE;
1621      }
1622    }
1623    else if (r2->qideal != NULL) return FALSE;
1624  }
1625
1626  return TRUE;
1627}
1628
1629// returns TRUE, if r1 and r2 represents the monomials in the same way
1630// FALSE, otherwise
1631// this is an analogue to rEqual but not so strict
1632BOOLEAN rSamePolyRep(ring r1, ring r2)
1633{
1634  int i, j;
1635
1636  if (r1 == r2) return TRUE;
1637
1638  if (r1 == NULL || r2 == NULL) return FALSE;
1639
1640  if ((rInternalChar(r1) != rInternalChar(r2))
1641  || (r1->float_len != r2->float_len)
1642  || (r1->float_len2 != r2->float_len2)
1643  || (rVar(r1) != rVar(r2))
1644  || (r1->OrdSgn != r2->OrdSgn)
1645  || (rPar(r1) != rPar(r2)))
1646    return FALSE;
1647
1648  if (rVar(r1)!=rVar(r2)) return FALSE;
1649  if (rPar(r1)!=rPar(r2)) return FALSE;
1650
1651  i=0;
1652  while (r1->order[i] != 0)
1653  {
1654    if (r2->order[i] == 0) return FALSE;
1655    if ((r1->order[i] != r2->order[i])
1656    || (r1->block0[i] != r2->block0[i])
1657    || (r1->block1[i] != r2->block1[i]))
1658      return FALSE;
1659    if (r1->wvhdl[i] != NULL)
1660    {
1661      if (r2->wvhdl[i] == NULL)
1662        return FALSE;
1663      for (j=0; j<r1->block1[i]-r1->block0[i]+1; j++)
1664        if (r2->wvhdl[i][j] != r1->wvhdl[i][j])
1665          return FALSE;
1666    }
1667    else if (r2->wvhdl[i] != NULL) return FALSE;
1668    i++;
1669  }
1670  if (r2->order[i] != 0) return FALSE;
1671
1672  // we do not check minpoly
1673  // we do not check qideal
1674
1675  return TRUE;
1676}
1677
1678rOrderType_t rGetOrderType(ring r)
1679{
1680  // check for simple ordering
1681  if (rHasSimpleOrder(r))
1682  {
1683    if ((r->order[1] == ringorder_c)
1684    || (r->order[1] == ringorder_C))
1685    {
1686      switch(r->order[0])
1687      {
1688          case ringorder_dp:
1689          case ringorder_wp:
1690          case ringorder_ds:
1691          case ringorder_ws:
1692          case ringorder_ls:
1693          case ringorder_unspec:
1694            if (r->order[1] == ringorder_C
1695            ||  r->order[0] == ringorder_unspec)
1696              return rOrderType_ExpComp;
1697            return rOrderType_Exp;
1698
1699          default:
1700            assume(r->order[0] == ringorder_lp ||
1701                   r->order[0] == ringorder_rs ||
1702                   r->order[0] == ringorder_Dp ||
1703                   r->order[0] == ringorder_Wp ||
1704                   r->order[0] == ringorder_Ds ||
1705                   r->order[0] == ringorder_Ws);
1706
1707            if (r->order[1] == ringorder_c) return rOrderType_ExpComp;
1708            return rOrderType_Exp;
1709      }
1710    }
1711    else
1712    {
1713      assume((r->order[0]==ringorder_c)||(r->order[0]==ringorder_C));
1714      return rOrderType_CompExp;
1715    }
1716  }
1717  else
1718    return rOrderType_General;
1719}
1720
1721BOOLEAN rHasSimpleOrder(ring r)
1722{
1723  if (r->order[0] == ringorder_unspec) return TRUE;
1724  int blocks = rBlocks(r) - 1;
1725  assume(blocks >= 1);
1726  if (blocks == 1) return TRUE;
1727  if (blocks > 2)  return FALSE;
1728  if ((r->order[0] != ringorder_c)
1729  && (r->order[0] != ringorder_C)
1730  && (r->order[1] != ringorder_c)
1731  && (r->order[1] != ringorder_C))
1732    return FALSE;
1733  if ((r->order[1] == ringorder_M)
1734  || (r->order[0] == ringorder_M))
1735    return FALSE;
1736  return TRUE;
1737}
1738
1739// returns TRUE, if simple lp or ls ordering
1740BOOLEAN rHasSimpleLexOrder(ring r)
1741{
1742  return rHasSimpleOrder(r) &&
1743    (r->order[0] == ringorder_ls ||
1744     r->order[0] == ringorder_lp ||
1745     r->order[1] == ringorder_ls ||
1746     r->order[1] == ringorder_lp);
1747}
1748
1749BOOLEAN rOrder_is_DegOrdering(rRingOrder_t order)
1750{
1751  switch(order)
1752  {
1753      case ringorder_dp:
1754      case ringorder_Dp:
1755      case ringorder_ds:
1756      case ringorder_Ds:
1757      case ringorder_Ws:
1758      case ringorder_Wp:
1759      case ringorder_ws:
1760      case ringorder_wp:
1761        return TRUE;
1762
1763      default:
1764        return FALSE;
1765  }
1766}
1767
1768BOOLEAN rOrder_is_WeightedOrdering(rRingOrder_t order)
1769{
1770  switch(order)
1771  {
1772      case ringorder_Ws:
1773      case ringorder_Wp:
1774      case ringorder_ws:
1775      case ringorder_wp:
1776        return TRUE;
1777
1778      default:
1779        return FALSE;
1780  }
1781}
1782
1783BOOLEAN rHasSimpleOrderAA(ring r)
1784{
1785  int blocks = rBlocks(r) - 1;
1786  if ((blocks > 3) || (blocks < 2)) return FALSE;
1787  if (blocks == 3)
1788  {
1789    return (((r->order[0] == ringorder_aa) && (r->order[1] != ringorder_M) &&
1790             ((r->order[2] == ringorder_c) || (r->order[2] == ringorder_C))) ||
1791            (((r->order[0] == ringorder_c) || (r->order[0] == ringorder_C)) &&
1792             (r->order[1] == ringorder_aa) && (r->order[2] != ringorder_M)));
1793  }
1794  else
1795  {
1796    return ((r->order[0] == ringorder_aa) && (r->order[1] != ringorder_M));
1797  }
1798}
1799
1800// return TRUE if p_SetComp requires p_Setm
1801BOOLEAN rOrd_SetCompRequiresSetm(ring r)
1802{
1803  if (r->typ != NULL)
1804  {
1805    int pos;
1806    for (pos=0;pos<r->OrdSize;pos++)
1807    {
1808      sro_ord* o=&(r->typ[pos]);
1809      if ((o->ord_typ == ro_syzcomp) || (o->ord_typ == ro_syz)) return TRUE;
1810    }
1811  }
1812  return FALSE;
1813}
1814
1815// return TRUE if p->exp[r->pOrdIndex] holds total degree of p */
1816BOOLEAN rOrd_is_Totaldegree_Ordering(ring r)
1817{
1818  // Hmm.... what about Syz orderings?
1819  return (rVar(r) > 1 &&
1820          ((rHasSimpleOrder(r) &&
1821           (rOrder_is_DegOrdering((rRingOrder_t)r->order[0]) ||
1822            rOrder_is_DegOrdering(( rRingOrder_t)r->order[1]))) ||
1823           (rHasSimpleOrderAA(r) &&
1824            (rOrder_is_DegOrdering((rRingOrder_t)r->order[1]) ||
1825             rOrder_is_DegOrdering((rRingOrder_t)r->order[2])))));
1826}
1827
1828// return TRUE if p->exp[r->pOrdIndex] holds a weighted degree of p */
1829BOOLEAN rOrd_is_WeightedDegree_Ordering(ring r =currRing)
1830{
1831  // Hmm.... what about Syz orderings?
1832  return ((rVar(r) > 1) &&
1833          rHasSimpleOrder(r) &&
1834          (rOrder_is_WeightedOrdering((rRingOrder_t)r->order[0]) ||
1835           rOrder_is_WeightedOrdering(( rRingOrder_t)r->order[1])));
1836}
1837
1838BOOLEAN rIsPolyVar(int v, ring r)
1839{
1840  int  i=0;
1841  while(r->order[i]!=0)
1842  {
1843    if((r->block0[i]<=v)
1844    && (r->block1[i]>=v))
1845    {
1846      switch(r->order[i])
1847      {
1848        case ringorder_a:
1849          return (r->wvhdl[i][v-r->block0[i]]>0);
1850        case ringorder_M:
1851          return 2; /*don't know*/
1852        case ringorder_a64: /* assume: all weight are non-negative!*/
1853        case ringorder_lp:
1854        case ringorder_rs:
1855        case ringorder_dp:
1856        case ringorder_Dp:
1857        case ringorder_wp:
1858        case ringorder_Wp:
1859          return TRUE;
1860        case ringorder_ls:
1861        case ringorder_ds:
1862        case ringorder_Ds:
1863        case ringorder_ws:
1864        case ringorder_Ws:
1865          return FALSE;
1866        default:
1867          break;
1868      }
1869    }
1870    i++;
1871  }
1872  return 3; /* could not find var v*/
1873}
1874
1875#ifdef RDEBUG
1876// This should eventually become a full-fledge ring check, like pTest
1877BOOLEAN rDBTest(ring r, const char* fn, const int l)
1878{
1879  int i,j;
1880
1881  if (r == NULL)
1882  {
1883    dReportError("Null ring in %s:%d", fn, l);
1884    return FALSE;
1885  }
1886
1887
1888  if (r->N == 0) return TRUE;
1889
1890//  omCheckAddrSize(r,sizeof(ip_sring));
1891#if OM_CHECK > 0
1892  i=rBlocks(r);
1893  omCheckAddrSize(r->order,i*sizeof(int));
1894  omCheckAddrSize(r->block0,i*sizeof(int));
1895  omCheckAddrSize(r->block1,i*sizeof(int));
1896  omCheckAddrSize(r->wvhdl,i*sizeof(int *));
1897  for (j=0;j<i; j++)
1898  {
1899    if (r->wvhdl[j] != NULL) omCheckAddr(r->wvhdl[j]);
1900  }
1901#endif
1902  if (r->VarOffset == NULL)
1903  {
1904    dReportError("Null ring VarOffset -- no rComplete (?) in n %s:%d", fn, l);
1905    return FALSE;
1906  }
1907  omCheckAddrSize(r->VarOffset,(r->N+1)*sizeof(int));
1908
1909  if ((r->OrdSize==0)!=(r->typ==NULL))
1910  {
1911    dReportError("mismatch OrdSize and typ-pointer in %s:%d");
1912    return FALSE;
1913  }
1914  omcheckAddrSize(r->typ,r->OrdSize*sizeof(*(r->typ)));
1915  omCheckAddrSize(r->VarOffset,(r->N+1)*sizeof(*(r->VarOffset)));
1916  // test assumptions:
1917  for(i=0;i<=r->N;i++)
1918  {
1919    if(r->typ!=NULL)
1920    {
1921      for(j=0;j<r->OrdSize;j++)
1922      {
1923        if (r->typ[j].ord_typ==ro_cp)
1924        {
1925          if(((short)r->VarOffset[i]) == r->typ[j].data.cp.place)
1926            dReportError("ordrec %d conflicts with var %d",j,i);
1927        }
1928        else
1929          if ((r->typ[j].ord_typ!=ro_syzcomp)
1930          && (r->VarOffset[i] == r->typ[j].data.dp.place))
1931            dReportError("ordrec %d conflicts with var %d",j,i);
1932      }
1933    }
1934    int tmp;
1935      tmp=r->VarOffset[i] & 0xffffff;
1936      #if SIZEOF_LONG == 8
1937        if ((r->VarOffset[i] >> 24) >63)
1938      #else
1939        if ((r->VarOffset[i] >> 24) >31)
1940      #endif
1941          dReportError("bit_start out of range:%d",r->VarOffset[i] >> 24);
1942      if (i > 0 && ((tmp<0) ||(tmp>r->ExpL_Size-1)))
1943      {
1944        dReportError("varoffset out of range for var %d: %d",i,tmp);
1945      }
1946  }
1947  if(r->typ!=NULL)
1948  {
1949    for(j=0;j<r->OrdSize;j++)
1950    {
1951      if ((r->typ[j].ord_typ==ro_dp)
1952      || (r->typ[j].ord_typ==ro_wp)
1953      || (r->typ[j].ord_typ==ro_wp_neg))
1954      {
1955        if (r->typ[j].data.dp.start > r->typ[j].data.dp.end)
1956          dReportError("in ordrec %d: start(%d) > end(%d)",j,
1957            r->typ[j].data.dp.start, r->typ[j].data.dp.end);
1958        if ((r->typ[j].data.dp.start < 1)
1959        || (r->typ[j].data.dp.end > r->N))
1960          dReportError("in ordrec %d: start(%d)<1 or end(%d)>vars(%d)",j,
1961            r->typ[j].data.dp.start, r->typ[j].data.dp.end,r->N);
1962      }
1963    }
1964  }
1965  if (r->minpoly!=NULL)
1966  {
1967    omCheckAddr(r->minpoly);
1968  }
1969  //assume(r->cf!=NULL);
1970
1971  return TRUE;
1972}
1973#endif
1974
1975static void rO_Align(int &place, int &bitplace)
1976{
1977  // increment place to the next aligned one
1978  // (count as Exponent_t,align as longs)
1979  if (bitplace!=BITS_PER_LONG)
1980  {
1981    place++;
1982    bitplace=BITS_PER_LONG;
1983  }
1984}
1985
1986static void rO_TDegree(int &place, int &bitplace, int start, int end,
1987    long *o, sro_ord &ord_struct)
1988{
1989  // degree (aligned) of variables v_start..v_end, ordsgn 1
1990  rO_Align(place,bitplace);
1991  ord_struct.ord_typ=ro_dp;
1992  ord_struct.data.dp.start=start;
1993  ord_struct.data.dp.end=end;
1994  ord_struct.data.dp.place=place;
1995  o[place]=1;
1996  place++;
1997  rO_Align(place,bitplace);
1998}
1999
2000static void rO_TDegree_neg(int &place, int &bitplace, int start, int end,
2001    long *o, sro_ord &ord_struct)
2002{
2003  // degree (aligned) of variables v_start..v_end, ordsgn -1
2004  rO_Align(place,bitplace);
2005  ord_struct.ord_typ=ro_dp;
2006  ord_struct.data.dp.start=start;
2007  ord_struct.data.dp.end=end;
2008  ord_struct.data.dp.place=place;
2009  o[place]=-1;
2010  place++;
2011  rO_Align(place,bitplace);
2012}
2013
2014static void rO_WDegree(int &place, int &bitplace, int start, int end,
2015    long *o, sro_ord &ord_struct, int *weights)
2016{
2017  // weighted degree (aligned) of variables v_start..v_end, ordsgn 1
2018  while((start<end) && (weights[0]==0)) { start++; weights++; }
2019  while((start<end) && (weights[end-start]==0)) { end--; }
2020  int i;
2021  int pure_tdeg=1;
2022  for(i=start;i<=end;i++)
2023  {
2024    if(weights[i-start]!=1)
2025    {
2026      pure_tdeg=0;
2027      break;
2028    }
2029  }
2030  if (pure_tdeg)
2031  {
2032    rO_TDegree(place,bitplace,start,end,o,ord_struct);
2033    return;
2034  }
2035  rO_Align(place,bitplace);
2036  ord_struct.ord_typ=ro_wp;
2037  ord_struct.data.wp.start=start;
2038  ord_struct.data.wp.end=end;
2039  ord_struct.data.wp.place=place;
2040  ord_struct.data.wp.weights=weights;
2041  o[place]=1;
2042  place++;
2043  rO_Align(place,bitplace);
2044  for(i=start;i<=end;i++)
2045  {
2046    if(weights[i-start]<0)
2047    {
2048      ord_struct.ord_typ=ro_wp_neg;
2049      break;
2050    }
2051  }
2052}
2053
2054static void rO_WDegree64(int &place, int &bitplace, int start, int end,
2055    long *o, sro_ord &ord_struct, int64 *weights)
2056{
2057  // weighted degree (aligned) of variables v_start..v_end, ordsgn 1,
2058  // reserved 2 places
2059  rO_Align(place,bitplace);
2060  ord_struct.ord_typ=ro_wp64;
2061  ord_struct.data.wp64.start=start;
2062  ord_struct.data.wp64.end=end;
2063  ord_struct.data.wp64.place=place;
2064  ord_struct.data.wp64.weights64=weights;
2065  o[place]=1;
2066  place++;
2067  o[place]=1;
2068  place++;
2069  rO_Align(place,bitplace);
2070  int i;
2071}
2072
2073static void rO_WDegree_neg(int &place, int &bitplace, int start, int end,
2074    long *o, sro_ord &ord_struct, int *weights)
2075{
2076  // weighted degree (aligned) of variables v_start..v_end, ordsgn -1
2077  while((start<end) && (weights[0]==0)) { start++; weights++; }
2078  while((start<end) && (weights[end-start]==0)) { end--; }
2079  rO_Align(place,bitplace);
2080  ord_struct.ord_typ=ro_wp;
2081  ord_struct.data.wp.start=start;
2082  ord_struct.data.wp.end=end;
2083  ord_struct.data.wp.place=place;
2084  ord_struct.data.wp.weights=weights;
2085  o[place]=-1;
2086  place++;
2087  rO_Align(place,bitplace);
2088  int i;
2089  for(i=start;i<=end;i++)
2090  {
2091    if(weights[i-start]<0)
2092    {
2093      ord_struct.ord_typ=ro_wp_neg;
2094      break;
2095    }
2096  }
2097}
2098
2099static void rO_LexVars(int &place, int &bitplace, int start, int end,
2100  int &prev_ord, long *o,int *v, int bits, int opt_var)
2101{
2102  // a block of variables v_start..v_end with lex order, ordsgn 1
2103  int k;
2104  int incr=1;
2105  if(prev_ord==-1) rO_Align(place,bitplace);
2106
2107  if (start>end)
2108  {
2109    incr=-1;
2110  }
2111  for(k=start;;k+=incr)
2112  {
2113    bitplace-=bits;
2114    if (bitplace < 0) { bitplace=BITS_PER_LONG-bits; place++; }
2115    o[place]=1;
2116    v[k]= place | (bitplace << 24);
2117    if (k==end) break;
2118  }
2119  prev_ord=1;
2120  if (opt_var!= -1)
2121  {
2122    assume((opt_var == end+1) ||(opt_var == end-1));
2123    if((opt_var != end+1) &&(opt_var != end-1)) WarnS("hier-2");
2124    int save_bitplace=bitplace;
2125    bitplace-=bits;
2126    if (bitplace < 0)
2127    {
2128      bitplace=save_bitplace;
2129      return;
2130    }
2131    // there is enough space for the optional var
2132    v[opt_var]=place | (bitplace << 24);
2133  }
2134}
2135
2136static void rO_LexVars_neg(int &place, int &bitplace, int start, int end,
2137  int &prev_ord, long *o,int *v, int bits, int opt_var)
2138{
2139  // a block of variables v_start..v_end with lex order, ordsgn -1
2140  int k;
2141  int incr=1;
2142  if(prev_ord==1) rO_Align(place,bitplace);
2143
2144  if (start>end)
2145  {
2146    incr=-1;
2147  }
2148  for(k=start;;k+=incr)
2149  {
2150    bitplace-=bits;
2151    if (bitplace < 0) { bitplace=BITS_PER_LONG-bits; place++; }
2152    o[place]=-1;
2153    v[k]=place | (bitplace << 24);
2154    if (k==end) break;
2155  }
2156  prev_ord=-1;
2157//  #if 0
2158  if (opt_var!= -1)
2159  {
2160    assume((opt_var == end+1) ||(opt_var == end-1));
2161    if((opt_var != end+1) &&(opt_var != end-1)) WarnS("hier-1");
2162    int save_bitplace=bitplace;
2163    bitplace-=bits;
2164    if (bitplace < 0)
2165    {
2166      bitplace=save_bitplace;
2167      return;
2168    }
2169    // there is enough space for the optional var
2170    v[opt_var]=place | (bitplace << 24);
2171  }
2172//  #endif
2173}
2174
2175static void rO_Syzcomp(int &place, int &bitplace, int &prev_ord,
2176    long *o, sro_ord &ord_struct)
2177{
2178  // ordering is derived from component number
2179  rO_Align(place,bitplace);
2180  ord_struct.ord_typ=ro_syzcomp;
2181  ord_struct.data.syzcomp.place=place;
2182  ord_struct.data.syzcomp.Components=NULL;
2183  ord_struct.data.syzcomp.ShiftedComponents=NULL;
2184  o[place]=1;
2185  prev_ord=1;
2186  place++;
2187  rO_Align(place,bitplace);
2188}
2189
2190static void rO_Syz(int &place, int &bitplace, int &prev_ord,
2191    long *o, sro_ord &ord_struct)
2192{
2193  // ordering is derived from component number
2194  // let's reserve one Exponent_t for it
2195  if ((prev_ord== 1) || (bitplace!=BITS_PER_LONG))
2196    rO_Align(place,bitplace);
2197  ord_struct.ord_typ=ro_syz;
2198  ord_struct.data.syz.place=place;
2199  ord_struct.data.syz.limit=0;
2200  ord_struct.data.syz.syz_index = NULL;
2201  ord_struct.data.syz.curr_index = 1;
2202  o[place]= -1;
2203  prev_ord=-1;
2204  place++;
2205}
2206
2207static unsigned long rGetExpSize(unsigned long bitmask, int & bits)
2208{
2209  if (bitmask == 0)
2210  {
2211    bits=16; bitmask=0xffff;
2212  }
2213  else if (bitmask <= 1)
2214  {
2215    bits=1; bitmask = 1;
2216  }
2217  else if (bitmask <= 3)
2218  {
2219    bits=2; bitmask = 3;
2220  }
2221  else if (bitmask <= 7)
2222  {
2223    bits=3; bitmask=7;
2224  }
2225  else if (bitmask <= 0xf)
2226  {
2227    bits=4; bitmask=0xf;
2228  }
2229  else if (bitmask <= 0x1f)
2230  {
2231    bits=5; bitmask=0x1f;
2232  }
2233  else if (bitmask <= 0x3f)
2234  {
2235    bits=6; bitmask=0x3f;
2236  }
2237#if SIZEOF_LONG == 8
2238  else if (bitmask <= 0x7f)
2239  {
2240    bits=7; bitmask=0x7f; /* 64 bit longs only */
2241  }
2242#endif
2243  else if (bitmask <= 0xff)
2244  {
2245    bits=8; bitmask=0xff;
2246  }
2247#if SIZEOF_LONG == 8
2248  else if (bitmask <= 0x1ff)
2249  {
2250    bits=9; bitmask=0x1ff; /* 64 bit longs only */
2251  }
2252#endif
2253  else if (bitmask <= 0x3ff)
2254  {
2255    bits=10; bitmask=0x3ff;
2256  }
2257#if SIZEOF_LONG == 8
2258  else if (bitmask <= 0xfff)
2259  {
2260    bits=12; bitmask=0xfff; /* 64 bit longs only */
2261  }
2262#endif
2263  else if (bitmask <= 0xffff)
2264  {
2265    bits=16; bitmask=0xffff;
2266  }
2267#if SIZEOF_LONG == 8
2268  else if (bitmask <= 0xfffff)
2269  {
2270    bits=20; bitmask=0xfffff; /* 64 bit longs only */
2271  }
2272  else if (bitmask <= 0xffffffff)
2273  {
2274    bits=32; bitmask=0xffffffff;
2275  }
2276  else
2277  {
2278    bits=64; bitmask=0xffffffffffffffff;
2279  }
2280#else
2281  else
2282  {
2283    bits=32; bitmask=0xffffffff;
2284  }
2285#endif
2286  return bitmask;
2287}
2288
2289/*2
2290* optimize rGetExpSize for a block of N variables, exp <=bitmask
2291*/
2292static unsigned long rGetExpSize(unsigned long bitmask, int & bits, int N)
2293{
2294  bitmask =rGetExpSize(bitmask, bits);
2295  int vars_per_long=BIT_SIZEOF_LONG/bits;
2296  int bits1;
2297  loop
2298  {
2299    if (bits == BIT_SIZEOF_LONG)
2300    {
2301      bits =  BIT_SIZEOF_LONG - 1;
2302      return LONG_MAX;
2303    }
2304    unsigned long bitmask1 =rGetExpSize(bitmask+1, bits1);
2305    int vars_per_long1=BIT_SIZEOF_LONG/bits1;
2306    if ((((N+vars_per_long-1)/vars_per_long) ==
2307         ((N+vars_per_long1-1)/vars_per_long1)))
2308    {
2309      vars_per_long=vars_per_long1;
2310      bits=bits1;
2311      bitmask=bitmask1;
2312    }
2313    else
2314    {
2315      return bitmask; /* and bits */
2316    }
2317  }
2318}
2319
2320/*2
2321 * create a copy of the ring r, which must be equivalent to currRing
2322 * used for std computations
2323 * may share data structures with currRing
2324 * DOES CALL rComplete
2325 */
2326ring rModifyRing(ring r, BOOLEAN omit_degree,
2327                         BOOLEAN omit_comp,
2328                         unsigned long exp_limit)
2329{
2330  assume (r != NULL );
2331  assume (exp_limit > 1);
2332  BOOLEAN need_other_ring;
2333  BOOLEAN omitted_degree = FALSE;
2334  int bits;
2335
2336  exp_limit=rGetExpSize(exp_limit, bits, r->N);
2337  need_other_ring = (exp_limit != r->bitmask);
2338
2339  int nblocks=rBlocks(r);
2340  int *order=(int*)omAlloc0((nblocks+1)*sizeof(int));
2341  int *block0=(int*)omAlloc0((nblocks+1)*sizeof(int));
2342  int *block1=(int*)omAlloc0((nblocks+1)*sizeof(int));
2343  int **wvhdl=(int**)omAlloc0((nblocks+1)*sizeof(int_ptr));
2344
2345  int i=0;
2346  int j=0; /*  i index in r, j index in res */
2347  loop
2348  {
2349    BOOLEAN copy_block_index=TRUE;
2350    int r_ord=r->order[i];
2351    if (r->block0[i]==r->block1[i])
2352    {
2353      switch(r_ord)
2354      {
2355        case ringorder_wp:
2356        case ringorder_dp:
2357        case ringorder_Wp:
2358        case ringorder_Dp:
2359          r_ord=ringorder_lp;
2360          break;
2361        case ringorder_Ws:
2362        case ringorder_Ds:
2363        case ringorder_ws:
2364        case ringorder_ds:
2365          r_ord=ringorder_ls;
2366          break;
2367        default:
2368          break;
2369      }
2370    }
2371    switch(r_ord)
2372    {
2373      case ringorder_C:
2374      case ringorder_c:
2375        if (!omit_comp)
2376        {
2377          order[j]=r_ord; /*r->order[i]*/;
2378        }
2379        else
2380        {
2381          j--;
2382          need_other_ring=TRUE;
2383          omit_comp=FALSE;
2384          copy_block_index=FALSE;
2385        }
2386        break;
2387      case ringorder_wp:
2388      case ringorder_dp:
2389      case ringorder_ws:
2390      case ringorder_ds:
2391        if(!omit_degree)
2392        {
2393          order[j]=r_ord; /*r->order[i]*/;
2394        }
2395        else
2396        {
2397          order[j]=ringorder_rs;
2398          need_other_ring=TRUE;
2399          omit_degree=FALSE;
2400          omitted_degree = TRUE;
2401        }
2402        break;
2403      case ringorder_Wp:
2404      case ringorder_Dp:
2405      case ringorder_Ws:
2406      case ringorder_Ds:
2407        if(!omit_degree)
2408        {
2409          order[j]=r_ord; /*r->order[i];*/
2410        }
2411        else
2412        {
2413          order[j]=ringorder_lp;
2414          need_other_ring=TRUE;
2415          omit_degree=FALSE;
2416          omitted_degree = TRUE;
2417        }
2418        break;
2419      default:
2420        order[j]=r_ord; /*r->order[i];*/
2421        break;
2422    }
2423    if (copy_block_index)
2424    {
2425      block0[j]=r->block0[i];
2426      block1[j]=r->block1[i];
2427      wvhdl[j]=r->wvhdl[i];
2428    }
2429    i++;j++;
2430    // order[j]=ringorder_no; //  done by omAlloc0
2431    if (i==nblocks) break;
2432  }
2433  if(!need_other_ring)
2434  {
2435    omFreeSize(order,(nblocks+1)*sizeof(int));
2436    omFreeSize(block0,(nblocks+1)*sizeof(int));
2437    omFreeSize(block1,(nblocks+1)*sizeof(int));
2438    omFreeSize(wvhdl,(nblocks+1)*sizeof(int_ptr));
2439    return r;
2440  }
2441  ring res=(ring)omAlloc0Bin(ip_sring_bin);
2442  *res = *r;
2443
2444#ifdef HAVE_PLURAL
2445  res->GetNC() = NULL;
2446#endif
2447 
2448  // res->qideal, res->idroot ???
2449  res->wvhdl=wvhdl;
2450  res->order=order;
2451  res->block0=block0;
2452  res->block1=block1;
2453  res->bitmask=exp_limit;
2454  int tmpref=r->cf->ref;
2455  rComplete(res, 1);
2456  r->cf->ref=tmpref;
2457
2458  // adjust res->pFDeg: if it was changed globally, then
2459  // it must also be changed for new ring
2460  if (r->pFDegOrig != res->pFDegOrig &&
2461           rOrd_is_WeightedDegree_Ordering(r))
2462  {
2463    // still might need adjustment for weighted orderings
2464    // and omit_degree
2465    res->firstwv = r->firstwv;
2466    res->firstBlockEnds = r->firstBlockEnds;
2467    res->pFDeg = res->pFDegOrig = pWFirstTotalDegree;
2468  }
2469  if (omitted_degree)
2470    res->pLDeg = res->pLDegOrig = r->pLDegOrig;
2471
2472  rOptimizeLDeg(res);
2473
2474  // set syzcomp
2475  if (res->typ != NULL && res->typ[0].ord_typ == ro_syz)
2476  {
2477    res->typ[0] = r->typ[0];
2478    if (r->typ[0].data.syz.limit > 0)
2479    {
2480      res->typ[0].data.syz.syz_index
2481        = (int*) omAlloc((r->typ[0].data.syz.limit +1)*sizeof(int));
2482      memcpy(res->typ[0].data.syz.syz_index, r->typ[0].data.syz.syz_index,
2483             (r->typ[0].data.syz.limit +1)*sizeof(int));
2484    }
2485  }
2486  // the special case: homog (omit_degree) and 1 block rs: that is global:
2487  // it comes from dp
2488  res->OrdSgn=r->OrdSgn;
2489
2490
2491#ifdef HAVE_PLURAL
2492  if (rIsPluralRing(r))
2493  {
2494    if ( nc_rComplete(r, res, false) ) // no qideal!
2495    {
2496      WarnS("error in nc_rComplete");
2497      // cleanup?
2498     
2499//      rDelete(res);
2500//      return r;
2501
2502      // just go on..
2503    }
2504  }
2505#endif
2506 
2507  return res;
2508}
2509
2510// construct Wp,C ring
2511ring rModifyRing_Wp(ring r, int* weights)
2512{
2513  ring res=(ring)omAlloc0Bin(ip_sring_bin);
2514  *res = *r;
2515#ifdef HAVE_PLURAL
2516  res->GetNC() = NULL;
2517#endif
2518 
2519  /*weights: entries for 3 blocks: NULL*/
2520  res->wvhdl = (int **)omAlloc0(3 * sizeof(int_ptr));
2521  /*order: Wp,C,0*/
2522  res->order = (int *) omAlloc(3 * sizeof(int *));
2523  res->block0 = (int *)omAlloc0(3 * sizeof(int *));
2524  res->block1 = (int *)omAlloc0(3 * sizeof(int *));
2525  /* ringorder Wp for the first block: var 1..r->N */
2526  res->order[0]  = ringorder_Wp;
2527  res->block0[0] = 1;
2528  res->block1[0] = r->N;
2529  res->wvhdl[0] = weights;
2530  /* ringorder C for the second block: no vars */
2531  res->order[1]  = ringorder_C;
2532  /* the last block: everything is 0 */
2533  res->order[2]  = 0;
2534  /*polynomial ring*/
2535  res->OrdSgn    = 1;
2536
2537  int tmpref=r->cf->ref;
2538  rComplete(res, 1);
2539  r->cf->ref=tmpref;
2540#ifdef HAVE_PLURAL
2541  if (rIsPluralRing(r))
2542  {
2543    if ( nc_rComplete(r, res, false) ) // no qideal!
2544    {
2545      WarnS("error in nc_rComplete");
2546      // cleanup?
2547
2548//      rDelete(res);
2549//      return r;
2550
2551      // just go on..
2552    }
2553  }
2554#endif
2555  return res;
2556}
2557
2558// construct lp ring with r->N variables, r->names vars....
2559ring rModifyRing_Simple(ring r, BOOLEAN ommit_degree, BOOLEAN ommit_comp, unsigned long exp_limit, BOOLEAN &simple)
2560{
2561  simple=TRUE;
2562  if (!rHasSimpleOrder(r))
2563  {
2564    simple=FALSE; // sorting needed
2565    assume (r != NULL );
2566    assume (exp_limit > 1);
2567    BOOLEAN omitted_degree = FALSE;
2568    int bits;
2569
2570    exp_limit=rGetExpSize(exp_limit, bits, r->N);
2571
2572    int nblocks=1+(ommit_comp!=0);
2573    int *order=(int*)omAlloc0((nblocks+1)*sizeof(int));
2574    int *block0=(int*)omAlloc0((nblocks+1)*sizeof(int));
2575    int *block1=(int*)omAlloc0((nblocks+1)*sizeof(int));
2576    int **wvhdl=(int**)omAlloc0((nblocks+1)*sizeof(int_ptr));
2577
2578    order[0]=ringorder_lp;
2579    block0[0]=1;
2580    block1[0]=r->N;
2581    if (!ommit_comp)
2582    {
2583      order[1]=ringorder_C;
2584    }
2585    ring res=(ring)omAlloc0Bin(ip_sring_bin);
2586    *res = *r;
2587#ifdef HAVE_PLURAL
2588    res->GetNC() = NULL;
2589#endif   
2590    // res->qideal, res->idroot ???
2591    res->wvhdl=wvhdl;
2592    res->order=order;
2593    res->block0=block0;
2594    res->block1=block1;
2595    res->bitmask=exp_limit;
2596    int tmpref=r->cf->ref;
2597    rComplete(res, 1);
2598    r->cf->ref=tmpref;
2599
2600#ifdef HAVE_PLURAL
2601    if (rIsPluralRing(r))
2602    {
2603      if ( nc_rComplete(r, res, false) ) // no qideal!
2604      {
2605        WarnS("error in nc_rComplete");
2606      // cleanup?
2607
2608//      rDelete(res);
2609//      return r;
2610
2611      // just go on..
2612      }
2613    }
2614#endif
2615
2616    rOptimizeLDeg(res);
2617
2618    return res;
2619  }
2620  return rModifyRing(r, ommit_degree, ommit_comp, exp_limit);
2621}
2622
2623void rKillModifiedRing_Simple(ring r)
2624{
2625  rKillModifiedRing(r);
2626}
2627
2628
2629void rKillModifiedRing(ring r)
2630{
2631  rUnComplete(r);
2632  omFree(r->order);
2633  omFree(r->block0);
2634  omFree(r->block1);
2635  omFree(r->wvhdl);
2636  omFreeBin(r,ip_sring_bin);
2637}
2638
2639void rKillModified_Wp_Ring(ring r)
2640{
2641  rUnComplete(r);
2642  omFree(r->order);
2643  omFree(r->block0);
2644  omFree(r->block1);
2645  omFree(r->wvhdl[0]);
2646  omFree(r->wvhdl);
2647  omFreeBin(r,ip_sring_bin);
2648}
2649
2650static void rSetOutParams(ring r)
2651{
2652  r->VectorOut = (r->order[0] == ringorder_c);
2653  r->ShortOut = TRUE;
2654#ifdef HAVE_TCL
2655  if (tcllmode)
2656  {
2657    r->ShortOut = FALSE;
2658  }
2659  else
2660#endif
2661  {
2662    int i;
2663    if ((r->parameter!=NULL) && (r->ch<2))
2664    {
2665      for (i=0;i<rPar(r);i++)
2666      {
2667        if(strlen(r->parameter[i])>1)
2668        {
2669          r->ShortOut=FALSE;
2670          break;
2671        }
2672      }
2673    }
2674    if (r->ShortOut)
2675    {
2676      // Hmm... sometimes (e.g., from maGetPreimage) new variables
2677      // are intorduced, but their names are never set
2678      // hence, we do the following awkward trick
2679      int N = omSizeWOfAddr(r->names);
2680      if (r->N < N) N = r->N;
2681
2682      for (i=(N-1);i>=0;i--)
2683      {
2684        if(r->names[i] != NULL && strlen(r->names[i])>1)
2685        {
2686          r->ShortOut=FALSE;
2687          break;
2688        }
2689      }
2690    }
2691  }
2692  r->CanShortOut = r->ShortOut;
2693}
2694
2695/*2
2696* sets pMixedOrder and pComponentOrder for orderings with more than one block
2697* block of variables (ip is the block number, o_r the number of the ordering)
2698* o is the position of the orderingering in r
2699*/
2700static void rHighSet(ring r, int o_r, int o)
2701{
2702  switch(o_r)
2703  {
2704    case ringorder_lp:
2705    case ringorder_dp:
2706    case ringorder_Dp:
2707    case ringorder_wp:
2708    case ringorder_Wp:
2709    case ringorder_rp:
2710    case ringorder_a:
2711    case ringorder_aa:
2712    case ringorder_a64:
2713      if (r->OrdSgn==-1) r->MixedOrder=TRUE;
2714      break;
2715    case ringorder_ls:
2716    case ringorder_rs:
2717    case ringorder_ds:
2718    case ringorder_Ds:
2719    case ringorder_s:
2720      break;
2721    case ringorder_ws:
2722    case ringorder_Ws:
2723      if (r->wvhdl[o]!=NULL)
2724      {
2725        int i;
2726        for(i=r->block1[o]-r->block0[o];i>=0;i--)
2727          if (r->wvhdl[o][i]<0) { r->MixedOrder=TRUE; break; }
2728      }
2729      break;
2730    case ringorder_c:
2731      r->ComponentOrder=1;
2732      break;
2733    case ringorder_C:
2734    case ringorder_S:
2735      r->ComponentOrder=-1;
2736      break;
2737    case ringorder_M:
2738      r->MixedOrder=TRUE;
2739      break;
2740    default:
2741      dReportError("wrong internal ordering:%d at %s, l:%d\n",o_r,__FILE__,__LINE__);
2742  }
2743}
2744
2745static void rSetFirstWv(ring r, int i, int* order, int* block1, int** wvhdl)
2746{
2747  // cheat for ringorder_aa
2748  if (order[i] == ringorder_aa)
2749    i++;
2750  if(block1[i]!=r->N) r->LexOrder=TRUE;
2751  r->firstBlockEnds=block1[i];
2752  r->firstwv = wvhdl[i];
2753  if ((order[i]== ringorder_ws)
2754  || (order[i]==ringorder_Ws)
2755  || (order[i]== ringorder_wp)
2756  || (order[i]==ringorder_Wp)
2757  || (order[i]== ringorder_a)
2758   /*|| (order[i]==ringorder_A)*/)
2759  {
2760    int j;
2761    for(j=block1[i]-r->block0[i];j>=0;j--)
2762    {
2763      if (r->firstwv[j]<0) r->MixedOrder=TRUE;
2764      if (r->firstwv[j]==0) r->LexOrder=TRUE;
2765    }
2766  }
2767  else if (order[i]==ringorder_a64)
2768  {
2769    int j;
2770    int64 *w=rGetWeightVec(r);
2771    for(j=block1[i]-r->block0[i];j>=0;j--)
2772    {
2773      if (w[j]==0) r->LexOrder=TRUE;
2774    }
2775  }
2776}
2777
2778static void rOptimizeLDeg(ring r)
2779{
2780  if (r->pFDeg == pDeg)
2781  {
2782    if (r->pLDeg == pLDeg1)
2783      r->pLDeg = pLDeg1_Deg;
2784    if (r->pLDeg == pLDeg1c)
2785      r->pLDeg = pLDeg1c_Deg;
2786  }
2787  else if (r->pFDeg == pTotaldegree)
2788  {
2789    if (r->pLDeg == pLDeg1)
2790      r->pLDeg = pLDeg1_Totaldegree;
2791    if (r->pLDeg == pLDeg1c)
2792      r->pLDeg = pLDeg1c_Totaldegree;
2793  }
2794  else if (r->pFDeg == pWFirstTotalDegree)
2795  {
2796    if (r->pLDeg == pLDeg1)
2797      r->pLDeg = pLDeg1_WFirstTotalDegree;
2798    if (r->pLDeg == pLDeg1c)
2799      r->pLDeg = pLDeg1c_WFirstTotalDegree;
2800  }
2801}
2802
2803// set pFDeg, pLDeg, MixOrder, ComponentOrder, etc
2804static void rSetDegStuff(ring r)
2805{
2806  int* order = r->order;
2807  int* block0 = r->block0;
2808  int* block1 = r->block1;
2809  int** wvhdl = r->wvhdl;
2810
2811  if (order[0]==ringorder_S ||order[0]==ringorder_s)
2812  {
2813    order++;
2814    block0++;
2815    block1++;
2816    wvhdl++;
2817  }
2818  r->LexOrder = FALSE;
2819  r->MixedOrder = FALSE;
2820  r->ComponentOrder = 1;
2821  r->pFDeg = pTotaldegree;
2822  r->pLDeg = (r->OrdSgn == 1 ? pLDegb : pLDeg0);
2823
2824  /*======== ordering type is (_,c) =========================*/
2825  if ((order[0]==ringorder_unspec) || (order[1] == 0)
2826      ||(
2827    ((order[1]==ringorder_c)||(order[1]==ringorder_C)
2828     ||(order[1]==ringorder_S)
2829     ||(order[1]==ringorder_s))
2830    && (order[0]!=ringorder_M)
2831    && (order[2]==0))
2832    )
2833  {
2834    if ((order[0]!=ringorder_unspec)
2835    && ((order[1]==ringorder_C)||(order[1]==ringorder_S)||
2836        (order[1]==ringorder_s)))
2837      r->ComponentOrder=-1;
2838    if (r->OrdSgn == -1) r->pLDeg = pLDeg0c;
2839    if ((order[0] == ringorder_lp)
2840    || (order[0] == ringorder_ls)
2841    || (order[0] == ringorder_rp)
2842    || (order[0] == ringorder_rs))
2843    {
2844      r->LexOrder=TRUE;
2845      r->pLDeg = pLDeg1c;
2846      r->pFDeg = pTotaldegree;
2847    }
2848    if ((order[0] == ringorder_a)
2849    || (order[0] == ringorder_wp)
2850    || (order[0] == ringorder_Wp)
2851    || (order[0] == ringorder_ws)
2852    || (order[0] == ringorder_Ws))
2853      r->pFDeg = pWFirstTotalDegree;
2854    r->firstBlockEnds=block1[0];
2855    r->firstwv = wvhdl[0];
2856  }
2857  /*======== ordering type is (c,_) =========================*/
2858  else if (((order[0]==ringorder_c)
2859            ||(order[0]==ringorder_C)
2860            ||(order[0]==ringorder_S)
2861            ||(order[0]==ringorder_s))
2862  && (order[1]!=ringorder_M)
2863  &&  (order[2]==0))
2864  {
2865    if ((order[0]==ringorder_C)||(order[0]==ringorder_S)||
2866        order[0]==ringorder_s)
2867      r->ComponentOrder=-1;
2868    if ((order[1] == ringorder_lp)
2869    || (order[1] == ringorder_ls)
2870    || (order[1] == ringorder_rp)
2871    || order[1] == ringorder_rs)
2872    {
2873      r->LexOrder=TRUE;
2874      r->pLDeg = pLDeg1c;
2875      r->pFDeg = pTotaldegree;
2876    }
2877    r->firstBlockEnds=block1[1];
2878    r->firstwv = wvhdl[1];
2879    if ((order[1] == ringorder_a)
2880    || (order[1] == ringorder_wp)
2881    || (order[1] == ringorder_Wp)
2882    || (order[1] == ringorder_ws)
2883    || (order[1] == ringorder_Ws))
2884      r->pFDeg = pWFirstTotalDegree;
2885  }
2886  /*------- more than one block ----------------------*/
2887  else
2888  {
2889    if ((r->VectorOut)||(order[0]==ringorder_C)||(order[0]==ringorder_S)||(order[0]==ringorder_s))
2890    {
2891      rSetFirstWv(r, 1, order, block1, wvhdl);
2892    }
2893    else
2894      rSetFirstWv(r, 0, order, block1, wvhdl);
2895
2896    /*the number of orderings:*/
2897    int i = 0;
2898    while (order[++i] != 0);
2899    do
2900    {
2901      i--;
2902      rHighSet(r, order[i],i);
2903    }
2904    while (i != 0);
2905
2906    if ((order[0]!=ringorder_c)
2907        && (order[0]!=ringorder_C)
2908        && (order[0]!=ringorder_S)
2909        && (order[0]!=ringorder_s))
2910    {
2911      r->pLDeg = pLDeg1c;
2912    }
2913    else
2914    {
2915      r->pLDeg = pLDeg1;
2916    }
2917    r->pFDeg = pWTotaldegree; // may be improved: pTotaldegree for lp/dp/ls/.. blocks
2918  }
2919  if (rOrd_is_Totaldegree_Ordering(r) || rOrd_is_WeightedDegree_Ordering(r))
2920    r->pFDeg = pDeg;
2921
2922  r->pFDegOrig = r->pFDeg;
2923  r->pLDegOrig = r->pLDeg;
2924  rOptimizeLDeg(r);
2925}
2926
2927/*2
2928* set NegWeightL_Size, NegWeightL_Offset
2929*/
2930static void rSetNegWeight(ring r)
2931{
2932  int i,l;
2933  if (r->typ!=NULL)
2934  {
2935    l=0;
2936    for(i=0;i<r->OrdSize;i++)
2937    {
2938      if(r->typ[i].ord_typ==ro_wp_neg) l++;
2939    }
2940    if (l>0)
2941    {
2942      r->NegWeightL_Size=l;
2943      r->NegWeightL_Offset=(int *) omAlloc(l*sizeof(int));
2944      l=0;
2945      for(i=0;i<r->OrdSize;i++)
2946      {
2947        if(r->typ[i].ord_typ==ro_wp_neg)
2948        {
2949          r->NegWeightL_Offset[l]=r->typ[i].data.wp.place;
2950          l++;
2951        }
2952      }
2953      return;
2954    }
2955  }
2956  r->NegWeightL_Size = 0;
2957  r->NegWeightL_Offset = NULL;
2958}
2959
2960static void rSetOption(ring r)
2961{
2962  // set redthrough
2963  if (!TEST_OPT_OLDSTD && r->OrdSgn == 1 && ! r->LexOrder)
2964    r->options |= Sy_bit(OPT_REDTHROUGH);
2965  else
2966    r->options &= ~Sy_bit(OPT_REDTHROUGH);
2967
2968  // set intStrategy
2969#ifdef HAVE_RINGS
2970  if (rField_is_Extension(r) || rField_is_Q(r) || rField_is_Ring(r))
2971#else
2972  if (rField_is_Extension(r) || rField_is_Q(r))
2973#endif
2974    r->options |= Sy_bit(OPT_INTSTRATEGY);
2975  else
2976    r->options &= ~Sy_bit(OPT_INTSTRATEGY);
2977
2978  // set redTail
2979  if (r->LexOrder || r->OrdSgn == -1 || rField_is_Extension(r))
2980    r->options &= ~Sy_bit(OPT_REDTAIL);
2981  else
2982    r->options |= Sy_bit(OPT_REDTAIL);
2983}
2984
2985BOOLEAN rComplete(ring r, int force)
2986{
2987  if (r->VarOffset!=NULL && force == 0) return FALSE;
2988  nInitChar(r);
2989  rSetOutParams(r);
2990  int n=rBlocks(r)-1;
2991  int i;
2992  int bits;
2993  r->bitmask=rGetExpSize(r->bitmask,bits,r->N);
2994  r->BitsPerExp = bits;
2995  r->ExpPerLong = BIT_SIZEOF_LONG / bits;
2996  r->divmask=rGetDivMask(bits);
2997
2998  // will be used for ordsgn:
2999  long *tmp_ordsgn=(long *)omAlloc0(3*(n+r->N)*sizeof(long));
3000  // will be used for VarOffset:
3001  int *v=(int *)omAlloc((r->N+1)*sizeof(int));
3002  for(i=r->N; i>=0 ; i--)
3003  {
3004    v[i]=-1;
3005  }
3006  sro_ord *tmp_typ=(sro_ord *)omAlloc0(3*(n+r->N)*sizeof(sro_ord));
3007  int typ_i=0;
3008  int prev_ordsgn=0;
3009
3010  // fill in v, tmp_typ, tmp_ordsgn, determine typ_i (== ordSize)
3011  int j=0;
3012  int j_bits=BITS_PER_LONG;
3013  BOOLEAN need_to_add_comp=FALSE;
3014  for(i=0;i<n;i++)
3015  {
3016    tmp_typ[typ_i].order_index=i;
3017    switch (r->order[i])
3018    {
3019      case ringorder_a:
3020      case ringorder_aa:
3021        rO_WDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,tmp_typ[typ_i],
3022                   r->wvhdl[i]);
3023        typ_i++;
3024        break;
3025
3026      case ringorder_a64:
3027        rO_WDegree64(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3028                     tmp_typ[typ_i], (int64 *)(r->wvhdl[i]));
3029        typ_i++;
3030        break;
3031
3032      case ringorder_c:
3033        rO_Align(j, j_bits);
3034        rO_LexVars_neg(j, j_bits, 0,0, prev_ordsgn,tmp_ordsgn,v,BITS_PER_LONG, -1);
3035        break;
3036
3037      case ringorder_C:
3038        rO_Align(j, j_bits);
3039        rO_LexVars(j, j_bits, 0,0, prev_ordsgn,tmp_ordsgn,v,BITS_PER_LONG, -1);
3040        break;
3041
3042      case ringorder_M:
3043        {
3044          int k,l;
3045          k=r->block1[i]-r->block0[i]+1; // number of vars
3046          for(l=0;l<k;l++)
3047          {
3048            rO_WDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3049                       tmp_typ[typ_i],
3050                       r->wvhdl[i]+(r->block1[i]-r->block0[i]+1)*l);
3051            typ_i++;
3052          }
3053          break;
3054        }
3055
3056      case ringorder_lp:
3057        rO_LexVars(j, j_bits, r->block0[i],r->block1[i], prev_ordsgn,
3058                   tmp_ordsgn,v,bits, -1);
3059        break;
3060
3061      case ringorder_ls:
3062        rO_LexVars_neg(j, j_bits, r->block0[i],r->block1[i], prev_ordsgn,
3063                       tmp_ordsgn,v, bits, -1);
3064        break;
3065
3066      case ringorder_rs:
3067        rO_LexVars_neg(j, j_bits, r->block1[i],r->block0[i], prev_ordsgn,
3068                       tmp_ordsgn,v, bits, -1);
3069        break;
3070
3071      case ringorder_rp:
3072        rO_LexVars(j, j_bits, r->block1[i],r->block0[i], prev_ordsgn,
3073                       tmp_ordsgn,v, bits, -1);
3074        break;
3075
3076      case ringorder_dp:
3077        if (r->block0[i]==r->block1[i])
3078        {
3079          rO_LexVars(j, j_bits, r->block0[i],r->block0[i], prev_ordsgn,
3080                     tmp_ordsgn,v, bits, -1);
3081        }
3082        else
3083        {
3084          rO_TDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3085                     tmp_typ[typ_i]);
3086          typ_i++;
3087          rO_LexVars_neg(j, j_bits, r->block1[i],r->block0[i]+1,
3088                         prev_ordsgn,tmp_ordsgn,v,bits, r->block0[i]);
3089        }
3090        break;
3091
3092      case ringorder_Dp:
3093        if (r->block0[i]==r->block1[i])
3094        {
3095          rO_LexVars(j, j_bits, r->block0[i],r->block0[i], prev_ordsgn,
3096                     tmp_ordsgn,v, bits, -1);
3097        }
3098        else
3099        {
3100          rO_TDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3101                     tmp_typ[typ_i]);
3102          typ_i++;
3103          rO_LexVars(j, j_bits, r->block0[i],r->block1[i]-1, prev_ordsgn,
3104                     tmp_ordsgn,v, bits, r->block1[i]);
3105        }
3106        break;
3107
3108      case ringorder_ds:
3109        if (r->block0[i]==r->block1[i])
3110        {
3111          rO_LexVars_neg(j, j_bits,r->block0[i],r->block1[i],prev_ordsgn,
3112                         tmp_ordsgn,v,bits, -1);
3113        }
3114        else
3115        {
3116          rO_TDegree_neg(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3117                         tmp_typ[typ_i]);
3118          typ_i++;
3119          rO_LexVars_neg(j, j_bits, r->block1[i],r->block0[i]+1,
3120                         prev_ordsgn,tmp_ordsgn,v,bits, r->block0[i]);
3121        }
3122        break;
3123
3124      case ringorder_Ds:
3125        if (r->block0[i]==r->block1[i])
3126        {
3127          rO_LexVars_neg(j, j_bits, r->block0[i],r->block0[i],prev_ordsgn,
3128                         tmp_ordsgn,v, bits, -1);
3129        }
3130        else
3131        {
3132          rO_TDegree_neg(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3133                         tmp_typ[typ_i]);
3134          typ_i++;
3135          rO_LexVars(j, j_bits, r->block0[i],r->block1[i]-1, prev_ordsgn,
3136                     tmp_ordsgn,v, bits, r->block1[i]);
3137        }
3138        break;
3139
3140      case ringorder_wp:
3141        rO_WDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3142                   tmp_typ[typ_i], r->wvhdl[i]);
3143        typ_i++;
3144        { // check for weights <=0
3145          int jj;
3146          BOOLEAN have_bad_weights=FALSE;
3147          for(jj=r->block1[i]-r->block0[i];jj>=0; jj--)
3148          {
3149            if (r->wvhdl[i][jj]<=0) have_bad_weights=TRUE;
3150          }
3151          if (have_bad_weights)
3152          {
3153             rO_TDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3154                                     tmp_typ[typ_i]);
3155             typ_i++;
3156          }
3157        }
3158        if (r->block1[i]!=r->block0[i])
3159        {
3160          rO_LexVars_neg(j, j_bits,r->block1[i],r->block0[i]+1, prev_ordsgn,
3161                         tmp_ordsgn, v,bits, r->block0[i]);
3162        }
3163        break;
3164
3165      case ringorder_Wp:
3166        rO_WDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3167                   tmp_typ[typ_i], r->wvhdl[i]);
3168        typ_i++;
3169        { // check for weights <=0
3170          int j;
3171          BOOLEAN have_bad_weights=FALSE;
3172          for(j=r->block1[i]-r->block0[i];j>=0; j--)
3173          {
3174            if (r->wvhdl[i][j]<=0) have_bad_weights=TRUE;
3175          }
3176          if (have_bad_weights)
3177          {
3178             rO_TDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3179                                     tmp_typ[typ_i]);
3180             typ_i++;
3181          }
3182        }
3183        if (r->block1[i]!=r->block0[i])
3184        {
3185          rO_LexVars(j, j_bits,r->block0[i],r->block1[i]-1, prev_ordsgn,
3186                     tmp_ordsgn,v, bits, r->block1[i]);
3187        }
3188        break;
3189
3190      case ringorder_ws:
3191        rO_WDegree_neg(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3192                       tmp_typ[typ_i], r->wvhdl[i]);
3193        typ_i++;
3194        if (r->block1[i]!=r->block0[i])
3195        {
3196          rO_LexVars_neg(j, j_bits,r->block1[i],r->block0[i]+1, prev_ordsgn,
3197                         tmp_ordsgn, v,bits, r->block0[i]);
3198        }
3199        break;
3200
3201      case ringorder_Ws:
3202        rO_WDegree_neg(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3203                       tmp_typ[typ_i], r->wvhdl[i]);
3204        typ_i++;
3205        if (r->block1[i]!=r->block0[i])
3206        {
3207          rO_LexVars(j, j_bits,r->block0[i],r->block1[i]-1, prev_ordsgn,
3208                     tmp_ordsgn,v, bits, r->block1[i]);
3209        }
3210        break;
3211
3212      case ringorder_S:
3213        rO_Syzcomp(j, j_bits,prev_ordsgn, tmp_ordsgn,tmp_typ[typ_i]);
3214        need_to_add_comp=TRUE;
3215        typ_i++;
3216        break;
3217
3218      case ringorder_s:
3219        rO_Syz(j, j_bits,prev_ordsgn, tmp_ordsgn,tmp_typ[typ_i]);
3220        need_to_add_comp=TRUE;
3221        typ_i++;
3222        break;
3223
3224      case ringorder_unspec:
3225      case ringorder_no:
3226      default:
3227        dReportError("undef. ringorder used\n");
3228        break;
3229    }
3230  }
3231
3232  int j0=j; // save j
3233  int j_bits0=j_bits; // save jbits
3234  rO_Align(j,j_bits);
3235  r->CmpL_Size = j;
3236
3237  j_bits=j_bits0; j=j0;
3238
3239  // fill in some empty slots with variables not already covered
3240  // v0 is special, is therefore normally already covered
3241  // now we do have rings without comp...
3242  if((need_to_add_comp) && (v[0]== -1))
3243  {
3244    if (prev_ordsgn==1)
3245    {
3246      rO_Align(j, j_bits);
3247      rO_LexVars(j, j_bits, 0,0, prev_ordsgn,tmp_ordsgn,v,BITS_PER_LONG, -1);
3248    }
3249    else
3250    {
3251      rO_Align(j, j_bits);
3252      rO_LexVars_neg(j, j_bits, 0,0, prev_ordsgn,tmp_ordsgn,v,BITS_PER_LONG, -1);
3253    }
3254  }
3255  // the variables
3256  for(i=1 ; i<=r->N ; i++)
3257  {
3258    if(v[i]==(-1))
3259    {
3260      if (prev_ordsgn==1)
3261      {
3262        rO_LexVars(j, j_bits, i,i, prev_ordsgn,tmp_ordsgn,v,bits, -1);
3263      }
3264      else
3265      {
3266        rO_LexVars_neg(j,j_bits,i,i, prev_ordsgn,tmp_ordsgn,v,bits, -1);
3267      }
3268    }
3269  }
3270
3271  rO_Align(j,j_bits);
3272  // ----------------------------
3273  // finished with constructing the monomial, computing sizes:
3274
3275  r->ExpL_Size=j;
3276  r->PolyBin = omGetSpecBin(POLYSIZE + (r->ExpL_Size)*sizeof(long));
3277  assume(r->PolyBin != NULL);
3278
3279  // ----------------------------
3280  // indices and ordsgn vector for comparison
3281  //
3282  // r->pCompHighIndex already set
3283  r->ordsgn=(long *)omAlloc0(r->ExpL_Size*sizeof(long));
3284
3285  for(j=0;j<r->CmpL_Size;j++)
3286  {
3287    r->ordsgn[j] = tmp_ordsgn[j];
3288  }
3289
3290  omFreeSize((ADDRESS)tmp_ordsgn,(3*(n+r->N)*sizeof(long)));
3291
3292  // ----------------------------
3293  // description of orderings for setm:
3294  //
3295  r->OrdSize=typ_i;
3296  if (typ_i==0) r->typ=NULL;
3297  else
3298  {
3299    r->typ=(sro_ord*)omAlloc(typ_i*sizeof(sro_ord));
3300    memcpy(r->typ,tmp_typ,typ_i*sizeof(sro_ord));
3301  }
3302  omFreeSize((ADDRESS)tmp_typ,(3*(n+r->N)*sizeof(sro_ord)));
3303
3304  // ----------------------------
3305  // indices for (first copy of ) variable entries in exp.e vector (VarOffset):
3306  r->VarOffset=v;
3307
3308  // ----------------------------
3309  // other indicies
3310  r->pCompIndex=(r->VarOffset[0] & 0xffff); //r->VarOffset[0];
3311  i=0; // position
3312  j=0; // index in r->typ
3313  if (i==r->pCompIndex) i++;
3314  while ((j < r->OrdSize)
3315         && ((r->typ[j].ord_typ==ro_syzcomp) ||
3316             (r->typ[j].ord_typ==ro_syz) ||
3317             (r->order[r->typ[j].order_index] == ringorder_aa)))
3318  {
3319    i++; j++;
3320  }
3321  if (i==r->pCompIndex) i++;
3322  r->pOrdIndex=i;
3323
3324  // ----------------------------
3325  rSetDegStuff(r);
3326  rSetOption(r);
3327  // ----------------------------
3328  // r->p_Setm
3329  r->p_Setm = p_GetSetmProc(r);
3330
3331  // ----------------------------
3332  // set VarL_*
3333  rSetVarL(r);
3334
3335  //  ----------------------------
3336  // right-adjust VarOffset
3337  rRightAdjustVarOffset(r);
3338
3339  // ----------------------------
3340  // set NegWeightL*
3341  rSetNegWeight(r);
3342
3343  // ----------------------------
3344  // p_Procs: call AFTER NegWeightL
3345  r->p_Procs = (p_Procs_s*)omAlloc(sizeof(p_Procs_s));
3346  p_ProcsSet(r, r->p_Procs);
3347  return FALSE;
3348}
3349
3350void rUnComplete(ring r)
3351{
3352  if (r == NULL) return;
3353  if (r->VarOffset != NULL)
3354  {
3355    if (r->PolyBin != NULL)
3356      omUnGetSpecBin(&(r->PolyBin));
3357
3358    omFreeSize((ADDRESS)r->VarOffset, (r->N +1)*sizeof(int));
3359    if (r->order != NULL)
3360    {
3361      if (r->order[0] == ringorder_s && r->typ[0].data.syz.limit > 0)
3362      {
3363        omFreeSize(r->typ[0].data.syz.syz_index,
3364             (r->typ[0].data.syz.limit +1)*sizeof(int));
3365      }
3366    }
3367    if (r->OrdSize!=0 && r->typ != NULL)
3368    {
3369      omFreeSize((ADDRESS)r->typ,r->OrdSize*sizeof(sro_ord));
3370    }
3371    if (r->ordsgn != NULL && r->CmpL_Size != 0)
3372      omFreeSize((ADDRESS)r->ordsgn,r->ExpL_Size*sizeof(long));
3373    if (r->p_Procs != NULL)
3374      omFreeSize(r->p_Procs, sizeof(p_Procs_s));
3375    omfreeSize(r->VarL_Offset, r->VarL_Size*sizeof(int));
3376  }
3377  if (r->NegWeightL_Offset!=NULL)
3378  {
3379    omFreeSize(r->NegWeightL_Offset, r->NegWeightL_Size*sizeof(int));
3380    r->NegWeightL_Offset=NULL;
3381  }
3382}
3383
3384// set r->VarL_Size, r->VarL_Offset, r->VarL_LowIndex
3385static void rSetVarL(ring r)
3386{
3387  int  min = INT_MAX, min_j = -1;
3388  int* VarL_Number = (int*) omAlloc0(r->ExpL_Size*sizeof(int));
3389
3390  int i,j;
3391
3392  // count how often a var long is occupied by an exponent
3393  for (i=1; i<=r->N; i++)
3394  {
3395    VarL_Number[r->VarOffset[i] & 0xffffff]++;
3396  }
3397
3398  // determine how many and min
3399  for (i=0, j=0; i<r->ExpL_Size; i++)
3400  {
3401    if (VarL_Number[i] != 0)
3402    {
3403      if (min > VarL_Number[i])
3404      {
3405        min = VarL_Number[i];
3406        min_j = j;
3407      }
3408      j++;
3409    }
3410  }
3411
3412  r->VarL_Size = j;
3413  r->VarL_Offset = (int*) omAlloc(r->VarL_Size*sizeof(int));
3414  r->VarL_LowIndex = 0;
3415
3416  // set VarL_Offset
3417  for (i=0, j=0; i<r->ExpL_Size; i++)
3418  {
3419    if (VarL_Number[i] != 0)
3420    {
3421      r->VarL_Offset[j] = i;
3422      if (j > 0 && r->VarL_Offset[j-1] != r->VarL_Offset[j] - 1)
3423        r->VarL_LowIndex = -1;
3424      j++;
3425    }
3426  }
3427  if (r->VarL_LowIndex >= 0)
3428    r->VarL_LowIndex = r->VarL_Offset[0];
3429
3430  r->MinExpPerLong = min;
3431  if (min_j != 0)
3432  {
3433    j = r->VarL_Offset[min_j];
3434    r->VarL_Offset[min_j] = r->VarL_Offset[0];
3435    r->VarL_Offset[0] = j;
3436  }
3437  omFree(VarL_Number);
3438}
3439
3440static void rRightAdjustVarOffset(ring r)
3441{
3442  int* shifts = (int*) omAlloc(r->ExpL_Size*sizeof(int));
3443  int i;
3444  // initialize shifts
3445  for (i=0;i<r->ExpL_Size;i++)
3446    shifts[i] = BIT_SIZEOF_LONG;
3447
3448  // find minimal bit shift in each long exp entry
3449  for (i=1;i<=r->N;i++)
3450  {
3451    if (shifts[r->VarOffset[i] & 0xffffff] > r->VarOffset[i] >> 24)
3452      shifts[r->VarOffset[i] & 0xffffff] = r->VarOffset[i] >> 24;
3453  }
3454  // reset r->VarOffset: set the minimal shift to 0
3455  for (i=1;i<=r->N;i++)
3456  {
3457    if (shifts[r->VarOffset[i] & 0xffffff] != 0)
3458      r->VarOffset[i]
3459        = (r->VarOffset[i] & 0xffffff) |
3460        (((r->VarOffset[i] >> 24) - shifts[r->VarOffset[i] & 0xffffff]) << 24);
3461  }
3462  omFree(shifts);
3463}
3464
3465// get r->divmask depending on bits per exponent
3466static unsigned long rGetDivMask(int bits)
3467{
3468  unsigned long divmask = 1;
3469  int i = bits;
3470
3471  while (i < BIT_SIZEOF_LONG)
3472  {
3473    divmask |= (((unsigned long) 1) << (unsigned long) i);
3474    i += bits;
3475  }
3476  return divmask;
3477}
3478
3479#ifdef RDEBUG
3480void rDebugPrint(ring r)
3481{
3482  if (r==NULL)
3483  {
3484    PrintS("NULL ?\n");
3485    return;
3486  }
3487  // corresponds to ro_typ from ring.h:
3488  const char *TYP[]={"ro_dp","ro_wp","ro_wp64","ro_wp_neg","ro_cp",
3489                     "ro_syzcomp", "ro_syz", "ro_none"};
3490  int i,j;
3491
3492  Print("ExpL_Size:%d ",r->ExpL_Size);
3493  Print("CmpL_Size:%d ",r->CmpL_Size);
3494  Print("VarL_Size:%d\n",r->VarL_Size);
3495  Print("bitmask=0x%x (expbound=%d) \n",r->bitmask, r->bitmask);
3496  Print("BitsPerExp=%d ExpPerLong=%d MinExpPerLong=%d at L[%d]\n", r->BitsPerExp, r->ExpPerLong, r->MinExpPerLong, r->VarL_Offset[0]);
3497  PrintS("varoffset:\n");
3498  if (r->VarOffset==NULL) PrintS(" NULL\n");
3499  else
3500    for(j=0;j<=r->N;j++)
3501      Print("  v%d at e-pos %d, bit %d\n",
3502            j,r->VarOffset[j] & 0xffffff, r->VarOffset[j] >>24);
3503  Print("divmask=%p\n", r->divmask);
3504  PrintS("ordsgn:\n");
3505  for(j=0;j<r->CmpL_Size;j++)
3506    Print("  ordsgn %d at pos %d\n",r->ordsgn[j],j);
3507  Print("OrdSgn:%d\n",r->OrdSgn);
3508  PrintS("ordrec:\n");
3509  for(j=0;j<r->OrdSize;j++)
3510  {
3511    Print("  typ %s",TYP[r->typ[j].ord_typ]);
3512    Print("  place %d",r->typ[j].data.dp.place);
3513    if (r->typ[j].ord_typ!=ro_syzcomp)
3514    {
3515      Print("  start %d",r->typ[j].data.dp.start);
3516      Print("  end %d",r->typ[j].data.dp.end);
3517      if ((r->typ[j].ord_typ==ro_wp)
3518      || (r->typ[j].ord_typ==ro_wp_neg))
3519      {
3520        Print(" w:");
3521        int l;
3522        for(l=r->typ[j].data.wp.start;l<=r->typ[j].data.wp.end;l++)
3523          Print(" %d",r->typ[j].data.wp.weights[l-r->typ[j].data.wp.start]);
3524      }
3525      else if (r->typ[j].ord_typ==ro_wp64)
3526      {
3527        Print(" w64:");
3528        int l;
3529        for(l=r->typ[j].data.wp64.start;l<=r->typ[j].data.wp64.end;l++)
3530          Print(" %l",(long)(((int64*)r->typ[j].data.wp64.weights64)+l-r->typ[j].data.wp64.start));
3531      }
3532    }
3533    PrintLn();
3534  }
3535  Print("pOrdIndex:%d pCompIndex:%d\n", r->pOrdIndex, r->pCompIndex);
3536  Print("OrdSize:%d\n",r->OrdSize);
3537  PrintS("--------------------\n");
3538  for(j=0;j<r->ExpL_Size;j++)
3539  {
3540    Print("L[%d]: ",j);
3541    if (j< r->CmpL_Size)
3542      Print("ordsgn %d ", r->ordsgn[j]);
3543    else
3544      PrintS("no comp ");
3545    i=1;
3546    for(;i<=r->N;i++)
3547    {
3548      if( (r->VarOffset[i] & 0xffffff) == j )
3549      {  Print("v%d at e[%d], bit %d; ", i,r->VarOffset[i] & 0xffffff,
3550                                         r->VarOffset[i] >>24 ); }
3551    }
3552    if( r->pCompIndex==j ) PrintS("v0; ");
3553    for(i=0;i<r->OrdSize;i++)
3554    {
3555      if (r->typ[i].data.dp.place == j)
3556      {
3557        Print("ordrec:%s (start:%d, end:%d) ",TYP[r->typ[i].ord_typ],
3558          r->typ[i].data.dp.start, r->typ[i].data.dp.end);
3559      }
3560    }
3561
3562    if (j==r->pOrdIndex)
3563      PrintS("pOrdIndex\n");
3564    else
3565      PrintLn();
3566  }
3567
3568  // p_Procs stuff
3569  p_Procs_s proc_names;
3570  const char* field;
3571  const char* length;
3572  const char* ord;
3573  p_Debug_GetProcNames(r, &proc_names); // changes p_Procs!!!
3574  p_Debug_GetSpecNames(r, field, length, ord);
3575
3576  Print("p_Spec  : %s, %s, %s\n", field, length, ord);
3577  PrintS("p_Procs :\n");
3578  for (i=0; i<(int) (sizeof(p_Procs_s)/sizeof(void*)); i++)
3579  {
3580    Print(" %s,\n", ((char**) &proc_names)[i]);
3581  }
3582}
3583
3584void pDebugPrintR(poly p, const ring r)
3585{
3586  int i,j;
3587  p_Write(p,r);
3588  j=2;
3589  while(p!=NULL)
3590  {
3591    Print("\nexp[0..%d]\n",r->ExpL_Size-1);
3592    for(i=0;i<r->ExpL_Size;i++)
3593      Print("%ld ",p->exp[i]);
3594    PrintLn();
3595    Print("v0:%d ",p_GetComp(p, r));
3596    for(i=1;i<=r->N;i++) Print(" v%d:%d",i,p_GetExp(p,i, r));
3597    PrintLn();
3598    pIter(p);
3599    j--;
3600    if (j==0) { PrintS("...\n"); break; }
3601  }
3602}
3603
3604void pDebugPrint(poly p)
3605{
3606  pDebugPrintR(p, currRing);
3607}
3608#endif // RDEBUG
3609
3610
3611/*2
3612* asssume that rComplete was called with r
3613* assume that the first block ist ringorder_S
3614* change the block to reflect the sequence given by appending v
3615*/
3616
3617#ifdef PDEBUG
3618void rDBChangeSComps(int* currComponents,
3619                     long* currShiftedComponents,
3620                     int length,
3621                     ring r)
3622{
3623  r->typ[1].data.syzcomp.length = length;
3624  rNChangeSComps( currComponents, currShiftedComponents, r);
3625}
3626void rDBGetSComps(int** currComponents,
3627                 long** currShiftedComponents,
3628                 int *length,
3629                 ring r)
3630{
3631  *length = r->typ[1].data.syzcomp.length;
3632  rNGetSComps( currComponents, currShiftedComponents, r);
3633}
3634#endif
3635
3636void rNChangeSComps(int* currComponents, long* currShiftedComponents, ring r)
3637{
3638  assume(r->order[1]==ringorder_S);
3639
3640  r->typ[1].data.syzcomp.ShiftedComponents = currShiftedComponents;
3641  r->typ[1].data.syzcomp.Components = currComponents;
3642}
3643
3644void rNGetSComps(int** currComponents, long** currShiftedComponents, ring r)
3645{
3646  assume(r->order[1]==ringorder_S);
3647
3648  *currShiftedComponents = r->typ[1].data.syzcomp.ShiftedComponents;
3649  *currComponents =   r->typ[1].data.syzcomp.Components;
3650}
3651
3652/////////////////////////////////////////////////////////////////////////////
3653//
3654// The following routines all take as input a ring r, and return R
3655// where R has a certain property. R might be equal r in which case r
3656// had already this property
3657//
3658// Without argument, these functions work on currRing and change it,
3659// if necessary
3660
3661// for the time being, this is still here
3662static ring rAssure_SyzComp(ring r, BOOLEAN complete = TRUE);
3663
3664#define MYTEST 0
3665
3666ring rCurrRingAssure_SyzComp()
3667{
3668#ifdef HAVE_PLURAL
3669#if MYTEST
3670  PrintS("rCurrRingAssure_SyzComp(), currRing:  \n");
3671  rWrite(currRing);
3672#ifdef RDEBUG
3673  rDebugPrint(currRing);
3674#endif
3675#endif
3676#endif
3677
3678  ring r = rAssure_SyzComp(currRing);
3679
3680  if (r != currRing)
3681  {
3682    ring old_ring = currRing;
3683    rChangeCurrRing(r);
3684    assume(currRing == r);
3685
3686#ifdef HAVE_PLURAL
3687#if MYTEST
3688    PrintS("rCurrRingAssure_SyzComp(): currRing': ");
3689    rWrite(currRing);
3690#ifdef RDEBUG
3691    rDebugPrint(currRing);
3692#endif
3693#endif
3694#endif
3695   
3696
3697    if (old_ring->qideal != NULL)
3698    {
3699      r->qideal = idrCopyR_NoSort(old_ring->qideal, old_ring);
3700      assume(idRankFreeModule(r->qideal) == 0);
3701      currQuotient = r->qideal;
3702
3703#ifdef HAVE_PLURAL
3704      if( rIsPluralRing(r) )
3705        nc_SetupQuotient(r);
3706#endif     
3707    }
3708  }
3709
3710  assume(currRing == r);
3711#ifdef HAVE_PLURAL
3712#if MYTEST
3713  PrintS("\nrCurrRingAssure_SyzComp(): new currRing: \n");
3714  rWrite(currRing);
3715#ifdef RDEBUG
3716  rDebugPrint(currRing);
3717#endif
3718#endif
3719#endif
3720     
3721  return r;
3722}
3723
3724static ring rAssure_SyzComp(ring r, BOOLEAN complete)
3725{
3726  if (r->order[0] == ringorder_s) return r;
3727  ring res=rCopy0(r, FALSE, FALSE);
3728  int i=rBlocks(r);
3729  int j;
3730
3731  res->order=(int *)omAlloc0((i+1)*sizeof(int));
3732  for(j=i;j>0;j--) res->order[j]=r->order[j-1];
3733  res->order[0]=ringorder_s;
3734
3735  res->block0=(int *)omAlloc0((i+1)*sizeof(int));
3736  for(j=i;j>0;j--) res->block0[j]=r->block0[j-1];
3737
3738  res->block1=(int *)omAlloc0((i+1)*sizeof(int));
3739  for(j=i;j>0;j--) res->block1[j]=r->block1[j-1];
3740
3741  int ** wvhdl =(int **)omAlloc0((i+1)*sizeof(int**));
3742  for(j=i;j>0;j--)
3743  {
3744    if (r->wvhdl[j-1] != NULL)
3745    {
3746      wvhdl[j] = (int*) omMemDup(r->wvhdl[j-1]);
3747    }
3748  }
3749  res->wvhdl = wvhdl;
3750
3751  if (complete)
3752  {
3753    rComplete(res, 1);
3754
3755#ifdef HAVE_PLURAL
3756    if (rIsPluralRing(r))
3757    {
3758      if ( nc_rComplete(r, res, false) ) // no qideal!
3759      {
3760        WarnS("error in nc_rComplete");
3761      // cleanup?
3762
3763//      rDelete(res);
3764//      return r;
3765
3766      // just go on..
3767      }
3768    }
3769#endif
3770   
3771  }
3772  return res;
3773}
3774
3775ring rAssure_HasComp(ring r)
3776{
3777  int last_block;
3778  int i=0;
3779  do
3780  { 
3781     if (r->order[i] == ringorder_c ||
3782        r->order[i] == ringorder_C) return r;
3783     if (r->order[i] == 0)
3784        break;
3785     i++;
3786  } while (1);
3787  //WarnS("re-creating ring with comps");
3788  last_block=i-1;
3789 
3790  ring new_r = rCopy0(r, FALSE, FALSE);
3791  i+=2;
3792  new_r->wvhdl=(int **)omAlloc0(i * sizeof(int_ptr));
3793  new_r->order   = (int *) omAlloc0(i * sizeof(int));
3794  new_r->block0   = (int *) omAlloc0(i * sizeof(int));
3795  new_r->block1   = (int *) omAlloc0(i * sizeof(int));
3796  memcpy4(new_r->order,r->order,(i-1) * sizeof(int));
3797  memcpy4(new_r->block0,r->block0,(i-1) * sizeof(int));
3798  memcpy4(new_r->block1,r->block1,(i-1) * sizeof(int));
3799  for (int j=0; j<=last_block; j++)
3800  {
3801    if (r->wvhdl[j]!=NULL)
3802    {
3803      new_r->wvhdl[j] = (int*) omMemDup(r->wvhdl[j]);
3804    }
3805  }
3806  last_block++;
3807  new_r->order[last_block]=ringorder_C;
3808  new_r->block0[last_block]=0;
3809  new_r->block1[last_block]=0;
3810  //new_r->wvhdl[last_block]=NULL;
3811
3812  rComplete(new_r, 1);
3813
3814#ifdef HAVE_PLURAL
3815  if (rIsPluralRing(r))
3816  {
3817    if ( nc_rComplete(r, new_r, false) ) // no qideal!
3818    {
3819      WarnS("error in nc_rComplete");
3820      // cleanup?
3821
3822//      rDelete(res);
3823//      return r;
3824
3825      // just go on..
3826    }
3827  }
3828#endif
3829 
3830  return new_r;
3831}
3832
3833static ring rAssure_CompLastBlock(ring r, BOOLEAN complete = TRUE)
3834{
3835  int last_block = rBlocks(r) - 2;
3836  if (r->order[last_block] != ringorder_c &&
3837      r->order[last_block] != ringorder_C)
3838  {
3839    int c_pos = 0;
3840    int i;
3841
3842    for (i=0; i< last_block; i++)
3843    {
3844      if (r->order[i] == ringorder_c || r->order[i] == ringorder_C)
3845      {
3846        c_pos = i;
3847        break;
3848      }
3849    }
3850    if (c_pos != -1)
3851    {
3852      ring new_r = rCopy0(r, FALSE, TRUE);
3853      for (i=c_pos+1; i<=last_block; i++)
3854      {
3855        new_r->order[i-1] = new_r->order[i];
3856        new_r->block0[i-1] = new_r->block0[i];
3857        new_r->block1[i-1] = new_r->block1[i];
3858        new_r->wvhdl[i-1] = new_r->wvhdl[i];
3859      }
3860      new_r->order[last_block] = r->order[c_pos];
3861      new_r->block0[last_block] = r->block0[c_pos];
3862      new_r->block1[last_block] = r->block1[c_pos];
3863      new_r->wvhdl[last_block] = r->wvhdl[c_pos];
3864      if (complete)
3865      {
3866        rComplete(new_r, 1);
3867
3868#ifdef HAVE_PLURAL
3869        if (rIsPluralRing(r))
3870        {
3871          if ( nc_rComplete(r, new_r, false) ) // no qideal!
3872          {
3873            WarnS("error in nc_rComplete");
3874      // cleanup?
3875
3876//      rDelete(res);
3877//      return r;
3878
3879      // just go on..
3880          }
3881        }
3882#endif
3883       
3884      }
3885      return new_r;
3886    }
3887  }
3888  return r;
3889}
3890
3891ring rCurrRingAssure_CompLastBlock()
3892{
3893  ring new_r = rAssure_CompLastBlock(currRing);
3894  if (currRing != new_r)
3895  {
3896    ring old_r = currRing;
3897    rChangeCurrRing(new_r);
3898    if (old_r->qideal != NULL)
3899    {
3900      new_r->qideal = idrCopyR(old_r->qideal, old_r);
3901      currQuotient = new_r->qideal;
3902    }
3903  }
3904  return new_r;
3905}
3906
3907ring rCurrRingAssure_SyzComp_CompLastBlock()
3908{
3909  ring new_r_1 = rAssure_CompLastBlock(currRing, FALSE);
3910  ring new_r = rAssure_SyzComp(new_r_1, FALSE);
3911
3912  if (new_r != currRing)
3913  {
3914    ring old_r = currRing;
3915    if (new_r_1 != new_r && new_r_1 != old_r) rDelete(new_r_1);
3916    rComplete(new_r, 1);
3917#ifdef HAVE_PLURAL
3918    if (rIsPluralRing(old_r))
3919    {
3920      if ( nc_rComplete(old_r, new_r, false) ) // no qideal!
3921      {
3922        WarnS("error in nc_rComplete");
3923      // cleanup?
3924
3925//      rDelete(res);
3926//      return r;
3927
3928      // just go on..
3929      }
3930    }
3931#endif
3932   
3933    rChangeCurrRing(new_r);
3934    if (old_r->qideal != NULL)
3935    {
3936      new_r->qideal = idrCopyR(old_r->qideal, old_r);
3937      currQuotient = new_r->qideal;
3938
3939#ifdef HAVE_PLURAL
3940      if( rIsPluralRing(old_r) )
3941        nc_SetupQuotient( new_r );
3942#endif
3943     
3944    }
3945    rTest(new_r);
3946    rTest(old_r);
3947  }
3948  return new_r;
3949}
3950
3951// use this for global orderings consisting of two blocks
3952static ring rCurrRingAssure_Global(rRingOrder_t b1, rRingOrder_t b2)
3953{
3954  int r_blocks = rBlocks(currRing);
3955  int i;
3956
3957  assume(b1 == ringorder_c || b1 == ringorder_C ||
3958         b2 == ringorder_c || b2 == ringorder_C ||
3959         b2 == ringorder_S);
3960  if ((r_blocks == 3) &&
3961      (currRing->order[0] == b1) &&
3962      (currRing->order[1] == b2) &&
3963      (currRing->order[2] == 0))
3964    return currRing;
3965  ring res = rCopy0(currRing, TRUE, FALSE);
3966  res->order = (int*)omAlloc0(3*sizeof(int));
3967  res->block0 = (int*)omAlloc0(3*sizeof(int));
3968  res->block1 = (int*)omAlloc0(3*sizeof(int));
3969  res->wvhdl = (int**)omAlloc0(3*sizeof(int*));
3970  res->order[0] = b1;
3971  res->order[1] = b2;
3972  if (b1 == ringorder_c || b1 == ringorder_C)
3973  {
3974    res->block0[1] = 1;
3975    res->block1[1] = currRing->N;
3976  }
3977  else
3978  {
3979    res->block0[0] = 1;
3980    res->block1[0] = currRing->N;
3981  }
3982  // HANNES: This sould be set in rComplete
3983  res->OrdSgn = 1;
3984  rComplete(res, 1);
3985  rChangeCurrRing(res);
3986  return res;
3987}
3988
3989
3990ring rCurrRingAssure_dp_S()
3991{
3992  return rCurrRingAssure_Global(ringorder_dp, ringorder_S);
3993}
3994
3995ring rCurrRingAssure_dp_C()
3996{
3997  return rCurrRingAssure_Global(ringorder_dp, ringorder_C);
3998}
3999
4000ring rCurrRingAssure_C_dp()
4001{
4002  return rCurrRingAssure_Global(ringorder_C, ringorder_dp);
4003}
4004
4005
4006void rSetSyzComp(int k)
4007{
4008  if (TEST_OPT_PROT) Print("{%d}", k);
4009  if ((currRing->typ!=NULL) && (currRing->typ[0].ord_typ==ro_syz))
4010  {
4011    assume(k > currRing->typ[0].data.syz.limit);
4012    int i;
4013    if (currRing->typ[0].data.syz.limit == 0)
4014    {
4015      currRing->typ[0].data.syz.syz_index = (int*) omAlloc0((k+1)*sizeof(int));
4016      currRing->typ[0].data.syz.syz_index[0] = 0;
4017      currRing->typ[0].data.syz.curr_index = 1;
4018    }
4019    else
4020    {
4021      currRing->typ[0].data.syz.syz_index = (int*)
4022        omReallocSize(currRing->typ[0].data.syz.syz_index,
4023                (currRing->typ[0].data.syz.limit+1)*sizeof(int),
4024                (k+1)*sizeof(int));
4025    }
4026    for (i=currRing->typ[0].data.syz.limit + 1; i<= k; i++)
4027    {
4028      currRing->typ[0].data.syz.syz_index[i] =
4029        currRing->typ[0].data.syz.curr_index;
4030    }
4031    currRing->typ[0].data.syz.limit = k;
4032    currRing->typ[0].data.syz.curr_index++;
4033  }
4034  else if ((currRing->order[0]!=ringorder_c) && (k!=0))
4035  {
4036    dReportError("syzcomp in incompatible ring");
4037  }
4038#ifdef PDEBUG
4039  extern int pDBsyzComp;
4040  pDBsyzComp=k;
4041#endif
4042}
4043
4044// return the max-comonent wchich has syzIndex i
4045int rGetMaxSyzComp(int i)
4046{
4047  if ((currRing->typ!=NULL) && (currRing->typ[0].ord_typ==ro_syz) &&
4048      currRing->typ[0].data.syz.limit > 0 && i > 0)
4049  {
4050    assume(i <= currRing->typ[0].data.syz.limit);
4051    int j;
4052    for (j=0; j<currRing->typ[0].data.syz.limit; j++)
4053    {
4054      if (currRing->typ[0].data.syz.syz_index[j] == i  &&
4055          currRing->typ[0].data.syz.syz_index[j+1] != i)
4056      {
4057        assume(currRing->typ[0].data.syz.syz_index[j+1] == i+1);
4058        return j;
4059      }
4060    }
4061    return currRing->typ[0].data.syz.limit;
4062  }
4063  else
4064  {
4065    return 0;
4066  }
4067}
4068
4069BOOLEAN rRing_is_Homog(ring r)
4070{
4071  if (r == NULL) return FALSE;
4072  int i, j, nb = rBlocks(r);
4073  for (i=0; i<nb; i++)
4074  {
4075    if (r->wvhdl[i] != NULL)
4076    {
4077      int length = r->block1[i] - r->block0[i];
4078      int* wvhdl = r->wvhdl[i];
4079      if (r->order[i] == ringorder_M) length *= length;
4080      assume(omSizeOfAddr(wvhdl) >= length*sizeof(int));
4081
4082      for (j=0; j< length; j++)
4083      {
4084        if (wvhdl[j] != 0 && wvhdl[j] != 1) return FALSE;
4085      }
4086    }
4087  }
4088  return TRUE;
4089}
4090
4091BOOLEAN rRing_has_CompLastBlock(ring r)
4092{
4093  assume(r != NULL);
4094  int lb = rBlocks(r) - 2;
4095  return (r->order[lb] == ringorder_c || r->order[lb] == ringorder_C);
4096}
4097
4098n_coeffType rFieldType(ring r)
4099{
4100  if (rField_is_Zp(r))     return n_Zp;
4101  if (rField_is_Q(r))      return n_Q;
4102  if (rField_is_R(r))      return n_R;
4103  if (rField_is_GF(r))     return n_GF;
4104  if (rField_is_long_R(r)) return n_long_R;
4105  if (rField_is_Zp_a(r))   return n_Zp_a;
4106  if (rField_is_Q_a(r))    return n_Q_a;
4107  if (rField_is_long_C(r)) return n_long_C;
4108  return n_unknown;
4109}
4110
4111int64 * rGetWeightVec(ring r)
4112{
4113  assume(r!=NULL);
4114  assume(r->OrdSize>0);
4115  int i=0;
4116  while((r->typ[i].ord_typ!=ro_wp64) && (r->typ[i].ord_typ>0)) i++;
4117  assume(r->typ[i].ord_typ==ro_wp64);
4118  return (int64*)(r->typ[i].data.wp64.weights64);
4119}
4120
4121void rSetWeightVec(ring r, int64 *wv)
4122{
4123  assume(r!=NULL);
4124  assume(r->OrdSize>0);
4125  assume(r->typ[0].ord_typ==ro_wp64);
4126  memcpy(r->typ[0].data.wp64.weights64,wv,r->N*sizeof(int64));
4127}
4128
4129#include <ctype.h>
4130
4131static int rRealloc1(ring r, ring src, int size, int pos)
4132{
4133  r->order=(int*)omReallocSize(r->order, size*sizeof(int), (size+1)*sizeof(int));
4134  r->block0=(int*)omReallocSize(r->block0, size*sizeof(int), (size+1)*sizeof(int));
4135  r->block1=(int*)omReallocSize(r->block1, size*sizeof(int), (size+1)*sizeof(int));
4136  r->wvhdl=(int_ptr*)omReallocSize(r->wvhdl,size*sizeof(int_ptr), (size+1)*sizeof(int_ptr));
4137  for(int k=size; k>pos; k--) r->wvhdl[k]=r->wvhdl[k-1];
4138  r->order[size]=0;
4139  size++;
4140  return size;
4141}
4142static int rReallocM1(ring r, ring src, int size, int pos)
4143{
4144  r->order=(int*)omReallocSize(r->order, size*sizeof(int), (size-1)*sizeof(int));
4145  r->block0=(int*)omReallocSize(r->block0, size*sizeof(int), (size-1)*sizeof(int));
4146  r->block1=(int*)omReallocSize(r->block1, size*sizeof(int), (size-1)*sizeof(int));
4147  r->wvhdl=(int_ptr*)omReallocSize(r->wvhdl,size*sizeof(int_ptr), (size-1)*sizeof(int_ptr));
4148  for(int k=pos+1; k<size; k++) r->wvhdl[k]=r->wvhdl[k+1];
4149  size--;
4150  return size;
4151}
4152static void rOppWeight(int *w, int l)
4153{
4154  int i2=(l+1)/2;
4155  for(int j=0; j<=i2; j++)
4156  {
4157    int t=w[j];
4158    w[j]=w[l-j];
4159    w[l-j]=t;
4160  }
4161}
4162
4163#define rOppVar(R,I) (rVar(R)+1-I)
4164
4165ring rOpposite(ring src)
4166  /* creates an opposite algebra of R */
4167  /* that is R^opp, where f (*^opp) g = g*f  */
4168  /* treats the case of qring */
4169{
4170  if (src == NULL) return(NULL);
4171  ring save = currRing;
4172  rChangeCurrRing(src);
4173 
4174  ring r = rCopy0(src,TRUE); /* TRUE for copy the qideal */
4175  /*  rChangeCurrRing(r); */
4176  // change vars v1..vN -> vN..v1
4177  int i;
4178  int i2 = (rVar(r)-1)/2;
4179  for(i=i2; i>=0; i--)
4180  {
4181    // index: 0..N-1
4182    //Print("ex var names: %d <-> %d\n",i,rOppVar(r,i));
4183    // exchange names
4184    char *p;
4185    p = r->names[rVar(r)-1-i];
4186    r->names[rVar(r)-1-i] = r->names[i];
4187    r->names[i] = p;
4188  }
4189//  i2=(rVar(r)+1)/2;
4190//  for(int i=i2; i>0; i--)
4191//  {
4192//    // index: 1..N
4193//    //Print("ex var places: %d <-> %d\n",i,rVar(r)+1-i);
4194//    // exchange VarOffset
4195//    int t;
4196//    t=r->VarOffset[i];
4197//    r->VarOffset[i]=r->VarOffset[rOppVar(r,i)];
4198//    r->VarOffset[rOppVar(r,i)]=t;
4199//  }
4200  // change names:
4201  for (i=rVar(r)-1; i>=0; i--)
4202  {
4203    char *p=r->names[i];
4204    if(isupper(*p)) *p = tolower(*p);
4205    else            *p = toupper(*p);
4206  }
4207  // change ordering: listing
4208  // change ordering: compare
4209//  for(i=0; i<r->OrdSize; i++)
4210//  {
4211//    int t,tt;
4212//    switch(r->typ[i].ord_typ)
4213//    {
4214//      case ro_dp:
4215//      //
4216//        t=r->typ[i].data.dp.start;
4217//        r->typ[i].data.dp.start=rOppVar(r,r->typ[i].data.dp.end);
4218//        r->typ[i].data.dp.end=rOppVar(r,t);
4219//        break;
4220//      case ro_wp:
4221//      case ro_wp_neg:
4222//      {
4223//        t=r->typ[i].data.wp.start;
4224//        r->typ[i].data.wp.start=rOppVar(r,r->typ[i].data.wp.end);
4225//        r->typ[i].data.wp.end=rOppVar(r,t);
4226//        // invert r->typ[i].data.wp.weights
4227//        rOppWeight(r->typ[i].data.wp.weights,
4228//                   r->typ[i].data.wp.end-r->typ[i].data.wp.start);
4229//        break;
4230//      }
4231//      //case ro_wp64:
4232//      case ro_syzcomp:
4233//      case ro_syz:
4234//         WerrorS("not implemented in rOpposite");
4235//         // should not happen
4236//         break;
4237//
4238//      case ro_cp:
4239//        t=r->typ[i].data.cp.start;
4240//        r->typ[i].data.cp.start=rOppVar(r,r->typ[i].data.cp.end);
4241//        r->typ[i].data.cp.end=rOppVar(r,t);
4242//        break;
4243//      case ro_none:
4244//      default:
4245//       Werror("unknown type in rOpposite(%d)",r->typ[i].ord_typ);
4246//       break;
4247//    }
4248//  }
4249  // Change order/block structures (needed for rPrint, rAdd etc.)
4250  int j=0;
4251  int l=rBlocks(src);
4252  for(i=0; src->order[i]!=0; i++)
4253  {
4254    switch (src->order[i])
4255    {
4256      case ringorder_c: /* c-> c */
4257      case ringorder_C: /* C-> C */
4258      case ringorder_no /*=0*/: /* end-of-block */
4259        r->order[j]=src->order[i];
4260        j++; break;
4261      case ringorder_lp: /* lp -> rp */
4262        r->order[j]=ringorder_rp;
4263        r->block0[j]=rOppVar(r, src->block1[i]);
4264        r->block1[j]=rOppVar(r, src->block0[i]);
4265        break;
4266      case ringorder_rp: /* rp -> lp */
4267        r->order[j]=ringorder_lp;
4268        r->block0[j]=rOppVar(r, src->block1[i]);
4269        r->block1[j]=rOppVar(r, src->block0[i]);
4270        break;
4271      case ringorder_dp: /* dp -> a(1..1),ls */
4272      {
4273        l=rRealloc1(r,src,l,j);
4274        r->order[j]=ringorder_a;
4275        r->block0[j]=rOppVar(r, src->block1[i]);
4276        r->block1[j]=rOppVar(r, src->block0[i]);
4277        r->wvhdl[j]=(int*)omAlloc((r->block1[j]-r->block0[j]+1)*sizeof(int));
4278        for(int k=r->block0[j]; k<=r->block1[j]; k++)
4279          r->wvhdl[j][k-r->block0[j]]=1;
4280        j++;
4281        r->order[j]=ringorder_ls;
4282        r->block0[j]=rOppVar(r, src->block1[i]);
4283        r->block1[j]=rOppVar(r, src->block0[i]);
4284        j++;
4285        break;
4286      }
4287      case ringorder_Dp: /* Dp -> a(1..1),rp */
4288      {
4289        l=rRealloc1(r,src,l,j);
4290        r->order[j]=ringorder_a;
4291        r->block0[j]=rOppVar(r, src->block1[i]);
4292        r->block1[j]=rOppVar(r, src->block0[i]);
4293        r->wvhdl[j]=(int*)omAlloc((r->block1[j]-r->block0[j]+1)*sizeof(int));
4294        for(int k=r->block0[j]; k<=r->block1[j]; k++)
4295          r->wvhdl[j][k-r->block0[j]]=1;
4296        j++;
4297        r->order[j]=ringorder_rp;
4298        r->block0[j]=rOppVar(r, src->block1[i]);
4299        r->block1[j]=rOppVar(r, src->block0[i]);
4300        j++;
4301        break;
4302      }
4303      case ringorder_wp: /* wp -> a(...),ls */
4304      {
4305        l=rRealloc1(r,src,l,j);
4306        r->order[j]=ringorder_a;
4307        r->block0[j]=rOppVar(r, src->block1[i]);
4308        r->block1[j]=rOppVar(r, src->block0[i]);
4309        r->wvhdl[j]=r->wvhdl[j+1]; r->wvhdl[j+1]=r->wvhdl[j+1]=NULL;
4310        rOppWeight(r->wvhdl[j], r->block1[j]-r->block0[j]);
4311        j++;
4312        r->order[j]=ringorder_ls;
4313        r->block0[j]=rOppVar(r, src->block1[i]);
4314        r->block1[j]=rOppVar(r, src->block0[i]);
4315        j++;
4316        break;
4317      }
4318      case ringorder_Wp: /* Wp -> a(...),rp */
4319      {
4320        l=rRealloc1(r,src,l,j);
4321        r->order[j]=ringorder_a;
4322        r->block0[j]=rOppVar(r, src->block1[i]);
4323        r->block1[j]=rOppVar(r, src->block0[i]);
4324        r->wvhdl[j]=r->wvhdl[j+1]; r->wvhdl[j+1]=r->wvhdl[j+1]=NULL;
4325        rOppWeight(r->wvhdl[j], r->block1[j]-r->block0[j]);
4326        j++;
4327        r->order[j]=ringorder_rp;
4328        r->block0[j]=rOppVar(r, src->block1[i]);
4329        r->block1[j]=rOppVar(r, src->block0[i]);
4330        j++;
4331        break;
4332      }
4333      case ringorder_M: /* M -> M */
4334      {
4335        r->order[j]=ringorder_M;
4336        r->block0[j]=rOppVar(r, src->block1[i]);
4337        r->block1[j]=rOppVar(r, src->block0[i]);
4338        int n=r->block1[j]-r->block0[j];
4339        /* M is a (n+1)x(n+1) matrix */
4340        for (int nn=0; nn<=n; nn++)
4341        {
4342          rOppWeight(&(r->wvhdl[j][nn*(n+1)]), n /*r->block1[j]-r->block0[j]*/);
4343        }
4344        j++;
4345        break;
4346      }
4347      case ringorder_a: /*  a(...),ls -> wp/dp */
4348      {
4349        r->block0[j]=rOppVar(r, src->block1[i]);
4350        r->block1[j]=rOppVar(r, src->block0[i]);
4351        rOppWeight(r->wvhdl[j], r->block1[j]-r->block0[j]);
4352        if (src->order[i+1]==ringorder_ls)
4353        {
4354          r->order[j]=ringorder_wp;
4355          i++;
4356          //l=rReallocM1(r,src,l,j);
4357        }
4358        else
4359        {
4360          r->order[j]=ringorder_a;
4361        }
4362        j++;
4363        break;
4364      }
4365      // not yet done:
4366      case ringorder_ls:
4367      case ringorder_rs:
4368      case ringorder_ds:
4369      case ringorder_Ds:
4370      case ringorder_ws:
4371      case ringorder_Ws:
4372      // should not occur:
4373      case ringorder_S:
4374      case ringorder_s:
4375      case ringorder_aa:
4376      case ringorder_L:
4377      case ringorder_unspec:
4378        Werror("order %s not (yet) supported", rSimpleOrdStr(src->order[i]));
4379        break;
4380    }
4381  }
4382  rComplete(r);
4383
4384 
4385#ifdef RDEBUG
4386  //   rDebugPrint(r);
4387  rTest(r);
4388#endif
4389
4390  rChangeCurrRing(r); 
4391
4392#ifdef HAVE_PLURAL
4393  // now, we initialize a non-comm structure on r
4394  if (rIsPluralRing(src))
4395  {
4396    assume( currRing == r);
4397   
4398    int *perm       = (int *)omAlloc0((rVar(r)+1)*sizeof(int));
4399    int *par_perm   = NULL;
4400    nMapFunc nMap   = nSetMap(src);
4401    int ni,nj;
4402    for(i=1; i<=r->N; i++)
4403    {
4404      perm[i] = rOppVar(r,i);
4405    }
4406
4407    matrix C = mpNew(rVar(r),rVar(r));
4408    matrix D = mpNew(rVar(r),rVar(r));
4409
4410    for (i=1; i< rVar(r); i++)
4411    {
4412      for (j=i+1; j<=rVar(r); j++)
4413      {
4414        ni = r->N +1 - i;
4415        nj = r->N +1 - j; /* i<j ==>   nj < ni */
4416
4417        assume(MATELEM(src->GetNC()->C,i,j) != NULL);
4418        MATELEM(C,nj,ni) = pPermPoly(MATELEM(src->GetNC()->C,i,j),perm,src,nMap,par_perm,src->P);
4419
4420        if(MATELEM(src->GetNC()->D,i,j) != NULL)
4421          MATELEM(D,nj,ni) = pPermPoly(MATELEM(src->GetNC()->D,i,j),perm,src,nMap,par_perm,src->P);
4422      }
4423    }
4424
4425    idTest((ideal)C);
4426    idTest((ideal)D);
4427
4428    if (nc_CallPlural(C, D, NULL, NULL, r, false, false, true, r))
4429      WarnS("Error initializing non-commutative multiplication!");
4430
4431    assume( r->GetNC()->IsSkewConstant == src->GetNC()->IsSkewConstant);
4432    assume( ncRingType(r) == ncRingType(src) );
4433
4434    omFreeSize((ADDRESS)perm,(rVar(r)+1)*sizeof(int));
4435
4436    rChangeCurrRing(save); 
4437 
4438  }
4439#endif /* HAVE_PLURAL */
4440
4441
4442  /* now oppose the qideal for qrings */
4443  if (src->qideal != NULL)
4444  {
4445    idDelete(&(r->qideal));
4446    r->qideal = idOppose(src, src->qideal);
4447
4448#ifdef HAVE_PLURAL
4449    if( rIsPluralRing(r) )
4450      nc_SetupQuotient(r);
4451#endif
4452  }
4453
4454  rTest(r);
4455
4456  rChangeCurrRing(save);
4457  return r;
4458}
4459
4460ring rEnvelope(ring R)
4461  /* creates an enveloping algebra of R */
4462  /* that is R^e = R \tensor_K R^opp */
4463{
4464  ring Ropp = rOpposite(R);
4465  ring Renv = NULL;
4466  int stat = rSum(R, Ropp, Renv); /* takes care of qideals */
4467  if ( stat <=0 )
4468    WarnS("Error in rEnvelope at rSum");
4469  rTest(Renv);
4470  return Renv;
4471}
4472
4473#ifdef HAVE_PLURAL
4474BOOLEAN nc_rComplete(const ring src, ring dest, bool bSetupQuotient) 
4475/* returns TRUE is there were errors */
4476/* dest is actualy equals src with the different ordering */
4477/* we map src->nc correctly to dest->src */
4478/* to be executed after rComplete, before rChangeCurrRing */
4479{
4480// NOTE: Originally used only by idElimination to transfer NC structure to dest
4481// ring created by dirty hack (without nc_CallPlural)
4482
4483  assume(!rIsPluralRing(dest)); // destination must be a newly constructed commutative ring
4484
4485  if (!rIsPluralRing(src))
4486  {
4487    return FALSE;
4488  }
4489
4490  const int N = dest->N;
4491
4492  assume(src->N == N);
4493
4494  ring save = currRing;
4495
4496  if (dest != save)
4497    rChangeCurrRing(dest);
4498
4499  const ring srcBase = src->GetNC()->basering;
4500 
4501  assume( nSetMap(srcBase) == nSetMap(currRing) ); // currRing is important here!
4502 
4503  matrix C = mpNew(N,N); // ring independent
4504  matrix D = mpNew(N,N);
4505
4506  matrix C0 = src->GetNC()->C;
4507  matrix D0 = src->GetNC()->D;
4508
4509
4510  poly p = NULL;
4511  number n = NULL;
4512
4513  // map C and D into dest
4514  for (int i = 1; i < N; i++)
4515  {
4516    for (int j = i + 1; j <= N; j++)
4517    {
4518      const number n = n_Copy(p_GetCoeff(MATELEM(C0,i,j), srcBase), srcBase); // src, mapping for coeffs into currRing = dest!
4519      const poly   p = p_NSet(n, dest);
4520      MATELEM(C,i,j) = p;
4521      if (MATELEM(D0,i,j) != NULL)
4522        MATELEM(D,i,j) = prCopyR(MATELEM(D0,i,j), srcBase, dest); // ?
4523    }
4524  }
4525  /* One must test C and D _only_ in r->GetNC()->basering!!! not in r!!! */
4526 
4527  idTest((ideal)C); // in dest!
4528  idTest((ideal)D);
4529
4530  if (nc_CallPlural(C, D, NULL, NULL, dest, bSetupQuotient, false, true, dest)) // also takes care about quotient ideal
4531  {
4532    //WarnS("Error transferring non-commutative structure");
4533    // error message should be in the interpreter interface
4534
4535    mpDelete(&C, dest);
4536    mpDelete(&D, dest);
4537
4538    if (currRing != save)
4539       rChangeCurrRing(save);
4540   
4541    return TRUE;
4542  }
4543 
4544//  mpDelete(&C, dest); // used by nc_CallPlural!
4545//  mpDelete(&D, dest);
4546
4547  if (dest != save)
4548    rChangeCurrRing(save);
4549 
4550  return FALSE;
4551}
4552#endif
4553
4554void rModify_a_to_A(ring r)
4555// to be called BEFORE rComplete:
4556// changes every Block with a(...) to A(...)
4557{
4558   int i=0;
4559   int j;
4560   while(r->order[i]!=0)
4561   {
4562      if (r->order[i]==ringorder_a)
4563      {
4564        r->order[i]=ringorder_a64;
4565        int *w=r->wvhdl[i];
4566        int64 *w64=(int64 *)omAlloc((r->block1[i]-r->block0[i]+1)*sizeof(int64));
4567        for(j=r->block1[i]-r->block0[i];j>=0;j--)
4568                w64[j]=(int64)w[j];
4569        r->wvhdl[i]=(int*)w64;
4570        omFreeSize(w,(r->block1[i]-r->block0[i]+1)*sizeof(int));
4571      }
4572      i++;
4573   }
4574}
Note: See TracBrowser for help on using the repository browser.