source: git/kernel/ring.cc @ 645a19

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