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

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