source: git/kernel/ring.cc @ 8cd64e

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