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