source: git/libpolys/polys/monomials/ring.cc @ a3aa3f

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