source: git/kernel/ring.cc @ 88efd2

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