source: git/kernel/gring.cc @ 528f5b7

spielwiese
Last change on this file since 528f5b7 was e0cb6c, checked in by Oleksandr Motsak <motsak@…>, 14 years ago
*motsak: Fixing the bug (http://www.singular.uni-kl.de:8002/trac/ticket/233) git-svn-id: file:///usr/local/Singular/svn/trunk@13381 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 86.1 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/***************************************************************
5 *  File:    gring.cc
6 *  Purpose: noncommutative kernel procedures
7 *  Author:  levandov (Viktor Levandovsky)
8 *  Created: 8/00 - 11/00
9 *  Version: $Id$
10 *******************************************************************/
11
12#define MYTEST 0
13#define OUTPUT 0
14
15#if MYTEST
16#define OM_CHECK 4
17#define OM_TRACK 5
18#endif
19
20#include <kernel/mod2.h>
21
22#ifdef HAVE_PLURAL
23#define PLURAL_INTERNAL_DECLARATIONS
24
25#include <kernel/febase.h>
26#include <kernel/options.h>
27#include <kernel/ring.h>
28#include <kernel/polys.h>
29#include <kernel/numbers.h>
30#include <kernel/ideals.h>
31#include <kernel/matpol.h>
32#include <kernel/kbuckets.h>
33#include <kernel/kstd1.h>
34#include <kernel/sbuckets.h>
35#include <kernel/prCopy.h>
36#include <kernel/p_Mult_q.h>
37#include <kernel/pInline1.h>
38
39// dirty tricks:
40#include <kernel/p_MemAdd.h>
41
42#include <kernel/gring.h>
43#include <kernel/sca.h>
44#include <summator.h>
45
46#include <kernel/ncSAMult.h> // for CMultiplier etc classes
47#include <kernel/ncSAFormula.h> // for CFormulaPowerMultiplier and enum Enum_ncSAType
48
49#ifdef HAVE_RATGRING
50#include <kernel/ratgring.h>
51#endif
52
53
54
55int  iNCExtensions = 0x00001; // only SCA can be used by default
56
57
58int& getNCExtensions()
59{
60  return (iNCExtensions);
61}
62
63int setNCExtensions(int iMask)
64{
65  const int iOld = getNCExtensions();
66  getNCExtensions() = iMask;
67  return (iOld);
68}
69
70
71bool ncExtensions(int iMask) //  = 0x0FFFF
72{
73  return ((getNCExtensions() & iMask) == iMask);
74}
75
76
77
78
79static const bool bNoPluralMultiplication = false;  // use only formula shortcuts in my OOP Multiplier
80
81// the following make sense only if bNoPluralMultiplication is false:
82static const bool bNoFormula = true;  // don't use any formula shortcuts
83static const bool bNoCache   = false; // only formula whenever possible, only make sanse if bNoFormula is false!
84
85
86// false, true, false == old "good" Plural
87// false, false ==>> Plural + Cache + Direct Formula - not much
88// false, false, true ==>> Plural Mult + Direct Formula (no ~cache)
89// true, *, *  == new OOP multiplication!
90
91
92/* global nc_macros : */
93
94#define freeT(A,v) omFreeSize((ADDRESS)A,(v+1)*sizeof(int))
95#define freeN(A,k) omFreeSize((ADDRESS)A,k*sizeof(number))
96
97
98// some forward declarations:
99
100
101// polynomial multiplication functions for p_Procs :
102poly gnc_pp_Mult_mm(const poly p, const poly m, const ring r, poly &last);
103poly gnc_p_Mult_mm(poly p, const poly m, const ring r);
104poly gnc_mm_Mult_p(const poly m, poly p, const ring r);
105poly gnc_mm_Mult_pp(const poly m, const poly p, const ring r);
106
107
108// set pProcs for r and global variable p_Procs as for general non-commutative algebras.
109void gnc_p_ProcsSet(ring rGR, p_Procs_s* p_Procs);
110
111/* syzygies : */
112poly gnc_CreateSpolyOld(const poly p1, const poly p2/*, poly spNoether*/, const ring r);
113poly gnc_ReduceSpolyOld(const poly p1, poly p2/*, poly spNoether*/, const ring r);
114
115poly gnc_CreateSpolyNew(const poly p1, const poly p2/*, poly spNoether*/, const ring r);
116poly gnc_ReduceSpolyNew(const poly p1, poly p2/*, poly spNoether*/, const ring r);
117
118
119
120void gnc_kBucketPolyRedNew(kBucket_pt b, poly p, number *c);
121void gnc_kBucketPolyRed_ZNew(kBucket_pt b, poly p, number *c);
122
123void gnc_kBucketPolyRedOld(kBucket_pt b, poly p, number *c);
124void gnc_kBucketPolyRed_ZOld(kBucket_pt b, poly p, number *c);
125
126
127// poly gnc_ReduceSpolyNew(poly p1, poly p2, poly spNoether, const ring r);
128// void gnc_ReduceSpolyTail(poly p1, poly q, poly q2, poly spNoether, const ring r);
129
130// void nc_kBucketPolyRed(kBucket_pt b, poly p);
131
132ideal gnc_gr_mora(const ideal, const ideal, const intvec *, const intvec *, kStrategy); // Not yet!
133ideal gnc_gr_bba (const ideal F, const ideal Q, const intvec *, const intvec *, kStrategy strat);
134
135
136void nc_CleanUp(nc_struct* p); // just free memory!
137void nc_rCleanUp(ring r); // smaller than kill: just free mem
138
139
140#if 0
141// deprecated functions:
142//  poly gnc_p_Minus_mm_Mult_qq_ign(poly p, const poly m, poly q, int & d1, poly d2, const ring ri, poly &d3);
143//  poly gnc_p_Minus_mm_Mult_qq(poly p, const poly m, poly q, const ring r);
144//  poly nc_p_Minus_mm_Mult_qq(poly p, const poly m, const poly q, int &lp, int lq, const ring r);
145//  poly nc_p_Plus_mm_Mult_qq (poly p, const poly m, const poly q, int &lp, int lq, const ring r);
146#endif
147
148
149
150/*2
151* returns the LCM of the head terms of a and b
152* without coefficient!!!
153*/
154poly p_Lcm(const poly a, const poly b, const long lCompM, const ring r)
155{
156  poly m = // p_One( r);
157          p_Init(r);
158
159  const int pVariables = r->N;
160
161  for (int i = pVariables; i!=0; i--)
162  {
163    const int lExpA = p_GetExp (a, i, r);
164    const int lExpB = p_GetExp (b, i, r);
165
166    p_SetExp (m, i, si_max(lExpA, lExpB), r);
167  }
168
169  p_SetComp (m, lCompM, r);
170
171  p_Setm(m,r);
172
173#ifdef PDEBUG
174//  p_Test(m,r);
175#endif
176
177  n_New(&(p_GetCoeff(m, r)), r);
178
179  return(m);
180};
181
182poly p_Lcm(const poly a, const poly b, const ring r)
183{
184#ifdef PDEBUG
185  p_Test(a, r);
186  p_Test(b, r);
187#endif
188
189  const long lCompP1 = p_GetComp(a, r);
190  const long lCompP2 = p_GetComp(b, r);
191
192  const poly m = p_Lcm(a, b, si_max(lCompP1, lCompP2), r);
193
194#ifdef PDEBUG
195//  p_Test(m,r);
196#endif
197  return(m);
198};
199
200
201
202///////////////////////////////////////////////////////////////////////////////
203poly nc_p_Minus_mm_Mult_qq(poly p, const poly m, const poly q, int &lp,
204                                    const int, const poly, const ring r)
205{
206  poly mc  = p_Neg( p_Copy(m, r), r );
207  poly mmc = nc_mm_Mult_pp( mc, q, r );
208  p_Delete(&mc, r);
209
210  p = p_Add_q(p, mmc, r);
211
212  lp = pLength(p); // ring independent!
213
214  return(p);
215}
216
217// returns p + m*q destroys p, const: q, m
218poly nc_p_Plus_mm_Mult_qq(poly p, const poly m, const poly q, int &lp,
219                              const int, const ring r)
220{
221  p = p_Add_q(p, nc_mm_Mult_pp( m, q, r ), r);
222
223  lp = pLength(p);
224
225  return(p);
226}
227
228#if 0
229poly gnc_p_Minus_mm_Mult_qq_ign(poly p, const poly m, poly q, int & d1, poly d2, const ring r, poly &d3)
230{
231  poly t;
232  int  i;
233
234  return gnc_p_Minus_mm_Mult_qq(p, m, q, d1, i, t, r);
235}
236#endif
237
238
239//----------- auxiliary routines--------------------------
240poly _gnc_p_Mult_q(poly p, poly q, const int copy, const ring r) // not used anymore!
241  /* destroy p,q unless copy=1 */
242{
243  poly res=NULL;
244  poly ghost=NULL;
245  poly qq,pp;
246  if (copy)
247  {
248    qq=p_Copy(q,r);
249    pp=p_Copy(p,r);
250  }
251  else
252  {
253    qq=q;
254    pp=p;
255  }
256  while (qq!=NULL)
257  {
258    res=p_Add_q(res, pp_Mult_mm(pp, qq, r), r); // p_Head(qq, r)?
259    qq=p_LmDeleteAndNext(qq,r);
260  }
261  p_Delete(&pp,r);
262  return(res);
263}
264
265// return pPolyP * pPolyQ; destroy or reuse pPolyP and pPolyQ
266poly _nc_p_Mult_q(poly pPolyP, poly pPolyQ, const ring rRing)
267{
268  assume( rIsPluralRing(rRing) );
269#ifdef PDEBUG
270  p_Test(pPolyP, rRing);
271  p_Test(pPolyQ, rRing);
272#endif
273#ifdef RDEBUG
274  rTest(rRing);
275#endif
276
277  int lp, lq;
278
279  pqLength(pPolyP, pPolyQ, lp, lq, MIN_LENGTH_BUCKET);
280
281  bool bUsePolynomial = TEST_OPT_NOT_BUCKETS || (si_max(lp, lq) < MIN_LENGTH_BUCKET); // ???
282
283  CPolynomialSummator sum(rRing, bUsePolynomial);
284
285  if (lq <= lp) // ?
286  {
287    // always length(q) times "p * q[j]"
288    for( ; pPolyQ!=NULL; pPolyQ  = p_LmDeleteAndNext( pPolyQ, rRing ) )
289      sum += pp_Mult_mm( pPolyP, pPolyQ, rRing);
290
291    p_Delete( &pPolyP, rRing );
292  } else
293  {
294    // always length(p) times "p[i] * q"
295    for( ; pPolyP!=NULL; pPolyP  = p_LmDeleteAndNext( pPolyP, rRing ) )
296      sum += nc_mm_Mult_pp( pPolyP, pPolyQ, rRing);
297
298    p_Delete( &pPolyQ, rRing );
299  }
300
301  return(sum);
302}
303
304// return pPolyP * pPolyQ; preserve pPolyP and pPolyQ
305poly _nc_pp_Mult_qq(const poly pPolyP, const poly pPolyQ, const ring rRing)
306{
307  assume( rIsPluralRing(rRing) );
308#ifdef PDEBUG
309  p_Test(pPolyP, rRing);
310  p_Test(pPolyQ, rRing);
311#endif
312#ifdef RDEBUG
313  rTest(rRing);
314#endif
315
316  int lp, lq;
317
318  pqLength(pPolyP, pPolyQ, lp, lq, MIN_LENGTH_BUCKET);
319
320  bool bUsePolynomial = TEST_OPT_NOT_BUCKETS || (si_max(lp, lq) < MIN_LENGTH_BUCKET); // ???
321
322  CPolynomialSummator sum(rRing, bUsePolynomial);
323
324  if (lq <= lp) // ?
325  {
326    // always length(q) times "p * q[j]"
327    for( poly q = pPolyQ; q !=NULL; q = pNext(q) )
328      sum += pp_Mult_mm(pPolyP, q, rRing);
329  } else
330  {
331    // always length(p) times "p[i] * q"
332    for( poly p = pPolyP; p !=NULL; p = pNext(p) )
333      sum += nc_mm_Mult_pp( p, pPolyQ, rRing);
334  }
335
336  return(sum);
337}
338
339
340
341poly gnc_mm_Mult_nn (int *F, int *G, const ring r);
342poly gnc_mm_Mult_uu (int *F,int jG,int bG, const ring r);
343
344/* #define nc_uu_Mult_ww nc_uu_Mult_ww_vert */
345poly gnc_uu_Mult_ww (int i, int a, int j, int b, const ring r);
346/* poly nc_uu_Mult_ww_vert (int i, int a, int j, int b, const ring r); */
347/* poly nc_uu_Mult_ww_horvert (int i, int a, int j, int b, const ring r); */
348/* poly nc_uu_Mult_ww_hvdiag (int i, int a, int j, int b, const ring r); */
349/* not written yet */
350
351
352poly gnc_p_Mult_mm_Common(poly p, const poly m, int side, const ring r)
353/* p is poly, m is mono with coeff, destroys p */
354/* if side==1, computes p_Mult_mm; otherwise, mm_Mult_p */
355{
356  if ((p==NULL) || (m==NULL)) return NULL;
357  /*  if (pNext(p)==NULL) return(nc_mm_Mult_nn(p,pCopy(m),r)); */
358  /* excluded  - the cycle will do it anyway - OK. */
359  if (p_IsConstant(m,r)) return(p_Mult_nn(p,p_GetCoeff(m,r),r));
360
361#ifdef PDEBUG
362  p_Test(p,r);
363  p_Test(m,r);
364#endif
365  poly v=NULL;
366  int rN=r->N;
367  int *P=(int *)omAlloc0((rN+1)*sizeof(int));
368  int *M=(int *)omAlloc0((rN+1)*sizeof(int));
369  /* coefficients: */
370  number cP,cM,cOut;
371  p_GetExpV(m, M, r);
372  cM=p_GetCoeff(m,r);
373  /* components:*/
374  const int expM=p_GetComp(m,r);
375  int expP=0;
376  int expOut=0;
377  /* bucket constraints: */
378  int UseBuckets=1;
379  if (pLength(p)< MIN_LENGTH_BUCKET || TEST_OPT_NOT_BUCKETS) UseBuckets=0;
380
381  CPolynomialSummator sum(r, UseBuckets == 0);
382
383  while (p!=NULL)
384  {
385#ifdef PDEBUG
386    p_Test(p,r);
387#endif
388    expP=p_GetComp(p,r);
389    if (expP==0)
390    {
391      expOut=expM;
392    }
393    else
394    {
395      if (expM==0)
396      {
397        expOut=expP;
398#ifdef PDEBUG
399        if (side)
400        {
401          Print("gnc_p_Mult_mm: Multiplication in the left module from the right");
402        }
403#endif
404      }
405      else
406      {
407        /* REPORT_ERROR */
408#ifdef PDEBUG
409        const char* s;
410        if (side==1) s="gnc_p_Mult_mm";
411        else s="gnc_mm_Mult_p";
412        Print("%s: exponent mismatch %d and %d\n",s,expP,expM);
413#endif
414        expOut=0;
415      }
416    }
417    p_GetExpV(p,P,r);
418    cP=p_GetCoeff(p,r);
419    cOut=n_Mult(cP,cM,r);
420    if (side==1)
421    {
422      v = gnc_mm_Mult_nn(P, M, r);
423    }
424    else
425    {
426      v = gnc_mm_Mult_nn(M, P, r);
427    }
428    v = p_Mult_nn(v,cOut,r);
429    n_Delete(&cOut,r);
430    p_SetCompP(v,expOut,r);
431
432    sum += v;
433
434    p_LmDelete(&p,r);
435  }
436  freeT(P,rN);
437  freeT(M,rN);
438
439  return(sum);
440}
441
442/* poly functions defined in p_Procs : */
443poly gnc_pp_Mult_mm(const poly p, const poly m, const ring r, poly &last)
444{
445  return( gnc_p_Mult_mm_Common(p_Copy(p,r), m, 1, r) );
446}
447
448poly gnc_p_Mult_mm(poly p, const poly m, const ring r)
449{
450  return( gnc_p_Mult_mm_Common(p, m, 1, r) );
451}
452
453poly gnc_mm_Mult_p(const poly m, poly p, const ring r)
454{
455  return( gnc_p_Mult_mm_Common(p, m, 0, r) );
456}
457
458poly gnc_mm_Mult_pp(const poly m, const poly p, const ring r)
459{
460  return( gnc_p_Mult_mm_Common(p_Copy(p,r), m, 0, r) );
461}
462
463
464
465poly gnc_mm_Mult_nn(int *F0, int *G0, const ring r)
466/* destroys nothing, no coeffs and exps */
467{
468  poly out=NULL;
469  int i,j;
470  int iF,jG,iG;
471  int rN=r->N;
472  int ExpSize=(((rN+1)*sizeof(int)+sizeof(long)-1)/sizeof(long))*sizeof(long);
473
474  int *F=(int *)omAlloc0((rN+1)*sizeof(int));
475  int *G=(int *)omAlloc0((rN+1)*sizeof(int));
476
477  memcpy(F, F0,(rN+1)*sizeof(int));
478  // pExpVectorCopy(F,F0);
479  memcpy(G, G0,(rN+1)*sizeof(int));
480  //  pExpVectorCopy(G,G0);
481  F[0]=0; /* important for p_MemAdd */
482  G[0]=0;
483
484  iF=rN;
485  while ((F[iF]==0)&&(iF>=1)) iF--; /* last exp_num of F */
486  if (iF==0) /* F0 is zero vector */
487  {
488    out=pOne();
489    p_SetExpV(out,G0,r);
490    p_Setm(out,r);
491    freeT(F,rN);
492    freeT(G,rN);
493    return(out);
494  }
495  jG=1;
496  while ((G[jG]==0)&&(jG<rN)) jG++;  /* first exp_num of G */
497  iG=rN;
498  while ((G[iG]==0)&&(iG>1)) iG--;  /* last exp_num of G */
499
500  out=pOne();
501
502  if (iF<=jG)
503    /* i.e. no mixed exp_num , MERGE case */
504  {
505    p_MemAdd_LengthGeneral(F, G, ExpSize/sizeof(long));
506    p_SetExpV(out,F,r);
507    p_Setm(out,r);
508    //    omFreeSize((ADDRESS)F,ExpSize);
509    freeT(F,rN);
510    freeT(G,rN);
511    return(out);
512  }
513
514  number cff=n_Init(1,r);
515  number tmp_num=NULL;
516  int cpower=0;
517
518  if (ncRingType(r)==nc_skew)
519  {
520    if (r->GetNC()->IsSkewConstant==1)
521    {
522      int tpower=0;
523      for(j=jG; j<=iG; j++)
524      {
525        if (G[j]!=0)
526        {
527          cpower = 0;
528          for(i=j+1; i<=iF; i++)
529          {
530            cpower = cpower + F[i];
531          }
532          cpower = cpower*G[j]; // bug! here may happen an arithmetic overflow!!!
533          tpower = tpower + cpower;
534        }
535      }
536      cff = n_Copy(p_GetCoeff(MATELEM(r->GetNC()->COM,1,2),r),r);
537      nPower(cff,tpower,&tmp_num);
538      n_Delete(&cff,r);
539      cff = tmp_num;
540    }
541    else /* skew commutative with nonequal coeffs */
542    {
543      number totcff=n_Init(1,r);
544      for(j=jG; j<=iG; j++)
545      {
546        if (G[j]!=0)
547        {
548          cpower = 0;
549          for(i=j+1; i<=iF; i++)
550          {
551            if (F[i]!=0)
552            {
553              cpower = F[i]*G[j]; // bug! overflow danger!!!
554              cff = n_Copy(p_GetCoeff(MATELEM(r->GetNC()->COM,j,i),r),r);
555              nPower(cff,cpower,&tmp_num);
556              cff = nMult(totcff,tmp_num);
557              nDelete(&totcff);
558              nDelete(&tmp_num);
559              totcff = n_Copy(cff,r);
560              n_Delete(&cff,r);
561            }
562          } /* end 2nd for */
563        }
564      }
565      cff=totcff;
566    }
567    p_MemAdd_LengthGeneral(F, G, ExpSize/sizeof(long));
568    p_SetExpV(out,F,r);
569    p_Setm(out,r);
570    p_SetCoeff(out,cff,r);
571    //    p_MemAdd_NegWeightAdjust(p, r); ??? do we need this?
572    freeT(F,rN);
573    freeT(G,rN);
574    return(out);
575  } /* end nc_skew */
576
577  /* now we have to destroy out! */
578  p_Delete(&out,r);
579  out = NULL;
580
581  if (iG==jG)
582    /* g is univariate monomial */
583  {
584    /*    if (ri->GetNC()->type==nc_skew) -- postpone to TU */
585    out = gnc_mm_Mult_uu(F,jG,G[jG],r);
586    freeT(F,rN);
587    freeT(G,rN);
588    return(out);
589  }
590
591  number n1=n_Init(1,r);
592  int *Prv=(int *)omAlloc0((rN+1)*sizeof(int));
593  int *Nxt=(int *)omAlloc0((rN+1)*sizeof(int));
594
595  int *log=(int *)omAlloc0((rN+1)*sizeof(int));
596  int cnt=0; int cnf=0;
597
598  /* splitting F wrt jG */
599  for (i=1;i<=jG;i++)
600  {
601    Prv[i]=F[i]; Nxt[i]=0; /* mult at the very end */
602    if (F[i]!=0) cnf++;
603  }
604
605  if (cnf==0) freeT(Prv,rN);
606
607  for (i=jG+1;i<=rN;i++)
608  {
609    Nxt[i]=F[i];
610    /*    if (cnf!=0)  Prv[i]=0; */
611    if (F[i]!=0)
612    {
613      cnt++;
614    }              /* effective part for F */
615  }
616  freeT(F,rN);
617  cnt=0;
618
619  for (i=1;i<=rN;i++)
620  {
621    if (G[i]!=0)
622    {
623     cnt++;
624     log[cnt]=i;
625     }               /* lG for G */
626   }
627
628/* ---------------------- A C T I O N ------------------------ */
629  poly D=NULL;
630  poly Rout=NULL;
631  number *c=(number *)omAlloc0((rN+1)*sizeof(number));
632  c[0]=n_Init(1,r);
633
634  int *Op=Nxt;
635  int *On=G;
636  int *U=(int *)omAlloc0((rN+1)*sizeof(int));
637
638  for (i=jG;i<=rN;i++) U[i]=Nxt[i]+G[i];  /* make leadterm */
639  Nxt=NULL;
640  G=NULL;
641  cnt=1;
642  int t=0;
643  poly w=NULL;
644  poly Pn=pOne();
645  p_SetExpV(Pn,On,r);
646  p_Setm(Pn,r);
647
648  while (On[iG]!=0)
649  {
650     t=log[cnt];
651
652     w=gnc_mm_Mult_uu(Op,t,On[t],r);
653     c[cnt]=n_Mult(c[cnt-1],p_GetCoeff(w,r),r);
654     D = pNext(w);  /* getting coef and rest D */
655     p_LmDelete(&w,r);
656     w=NULL;
657
658     Op[t] += On[t];   /* update exp_vectors */
659     On[t] = 0;
660
661     if (t!=iG)    /* not the last step */
662     {
663       p_SetExpV(Pn,On,r);
664       p_Setm(Pn,r);
665#ifdef PDEBUG
666       p_Test(Pn,r);
667#endif
668
669//       if (pNext(D)==0)
670// is D a monomial? could be postponed higher
671//       {
672//       Rout=nc_mm_Mult_nn(D,Pn,r);
673//       }
674//       else
675//       {
676       Rout=gnc_p_Mult_mm(D,Pn,r);
677//       }
678     }
679     else
680     {
681       Rout=D;
682       D=NULL;
683     }
684
685     if (Rout!=NULL)
686     {
687       Rout=p_Mult_nn(Rout,c[cnt-1],r); /* Rest is ready */
688       out=p_Add_q(out,Rout,r);
689       Rout=NULL;
690     }
691     cnt++;
692  }
693  freeT(On,rN);
694  freeT(Op,rN);
695  p_Delete(&Pn,r);
696  omFreeSize((ADDRESS)log,(rN+1)*sizeof(int));
697
698  /* leadterm and Prv-part */
699
700  Rout=pOne();
701  /* U is lead.monomial */
702  U[0]=0;
703  p_SetExpV(Rout,U,r);
704  p_Setm(Rout,r);  /* use again this name Rout */
705#ifdef PDEBUG
706  p_Test(Rout,r);
707#endif
708  p_SetCoeff(Rout,c[cnt-1],r);
709  out=p_Add_q(out,Rout,r);
710  freeT(U,rN);
711  freeN(c,rN+1);
712  if (cnf!=0)  /* Prv is non-zero vector */
713  {
714    Rout=pOne();
715    Prv[0]=0;
716    p_SetExpV(Rout,Prv,r);
717    p_Setm(Rout,r);
718#ifdef PDEBUG
719    p_Test(Rout,r);
720#endif
721    out=gnc_mm_Mult_p(Rout,out,r); /* getting the final result */
722    freeT(Prv,rN);
723    p_Delete(&Rout,r);
724  }
725  return (out);
726}
727
728
729poly gnc_mm_Mult_uu(int *F,int jG,int bG, const ring r)
730/* f=mono(F),g=(x_iG)^bG */
731{
732  poly out=NULL;
733  int i;
734  number num=NULL;
735
736  int rN=r->N;
737  int iF=r->N;
738  while ((F[iF]==0)&&(iF>0)) iF-- ;   /* last exponent_num of F */
739
740  if (iF==0)  /* F==zero vector in other words */
741  {
742   out=pOne();
743   p_SetExp(out,jG,bG,r);
744   p_Setm(out,r);
745   return(out);
746  }
747
748  int jF=1;
749  while ((F[jF]==0)&&(jF<=rN)) jF++;  /* first exp of F */
750
751  if (iF<=jG)                       /* i.e. no mixed exp_num */
752  {
753    out=pOne();
754    F[jG]=F[jG]+bG;
755    p_SetExpV(out,F,r);
756    p_Setm(out,r);
757    return(out);
758  }
759
760  if (iF==jF)              /* uni times uni */
761  {
762   out=gnc_uu_Mult_ww(iF,F[iF],jG,bG,r);
763   return(out);
764  }
765
766  /* Now: F is mono with >=2 exponents, jG<iF */
767  /* check the quasi-commutative case */
768//   matrix LCOM=r->GetNC()->COM;
769//   number rescoef=n_Init(1,r);
770//   number tmpcoef=n_Init(1,r);
771//   int tmpint;
772//   i=iF;
773//   while (i>=jG+1)
774//     /* all the non-zero exponents */
775//   {
776//     if (MATELEM(LCOM,jG,i)!=NULL)
777//     {
778//       tmpcoef=pGetCoeff(MATELEM(LCOM,jG,i));
779//       tmpint=(int)F[i];
780//       nPower(tmpcoef,F[i],&tmpcoef);
781//       rescoef=nMult(rescoef,tmpcoef);
782//       i--;
783//     }
784//     else
785//     {
786//       if (F[i]!=0) break;
787//     }
788//   }
789//   if (iF==i)
790//   /* no action took place*/
791//   {
792
793//   }
794//   else /* power the result up to bG */
795//   {
796//     nPower(rescoef,bG,&rescoef);
797//     /* + cleanup, post-processing */
798//   }
799
800  int *Prv=(int*)omAlloc0((rN+1)*sizeof(int));
801  int *Nxt=(int*)omAlloc0((rN+1)*sizeof(int));
802  int *lF=(int *)omAlloc0((rN+1)*sizeof(int));
803
804  int cnt=0; int cnf=0;
805  /* splitting F wrt jG */
806  for (i=1;i<=jG;i++) /* mult at the very end */
807  {
808    Prv[i]=F[i]; Nxt[i]=0;
809    if (F[i]!=0) cnf++;
810  }
811
812  if (cnf==0)
813  {
814    freeT(Prv,rN); Prv = NULL;
815  }
816
817  for (i=jG+1;i<=rN;i++)
818  {
819    Nxt[i]=F[i];
820    if (cnf!=0) { Prv[i]=0;}
821    if (F[i]!=0)
822    {
823      cnt++;
824      lF[cnt]=i;
825    }                 /* eff_part,lF_for_F */
826  }
827
828  if (cnt==1) /* Nxt consists of 1 nonzero el-t only */
829  {
830    int q=lF[1];
831    poly Rout=pOne();
832    out=gnc_uu_Mult_ww(q,Nxt[q],jG,bG,r);
833
834    freeT(Nxt,rN);  Nxt = NULL;
835
836    if (cnf!=0)
837    {
838       Prv[0]=0;
839       p_SetExpV(Rout,Prv,r);
840       p_Setm(Rout,r);
841
842#ifdef PDEBUG
843       p_Test(Rout,r);
844#endif
845
846       freeT(Prv,rN);
847       Prv = NULL;
848
849       out=gnc_mm_Mult_p(Rout,out,r); /* getting the final result */
850    }
851
852    freeT(lF,rN);
853    lF = NULL;
854
855    p_Delete(&Rout,r);
856
857    assume(Nxt == NULL);
858    assume(lF == NULL);
859    assume(Prv == NULL);
860
861    return (out);
862  }
863/* -------------------- MAIN ACTION --------------------- */
864
865  poly D=NULL;
866  poly Rout=NULL;
867  number *c=(number *)omAlloc0((cnt+2)*sizeof(number));
868  c[cnt+1]=n_Init(1,r);
869  i=cnt+2;         /* later in freeN */
870  int *Op=Nxt;
871
872  int *On=(int *)omAlloc0((rN+1)*sizeof(int));
873  int *U=(int *)omAlloc0((rN+1)*sizeof(int));
874
875
876  //  pExpVectorCopy(U,Nxt);
877  memcpy(U, Nxt,(rN+1)*sizeof(int));
878  U[jG] = U[jG] + bG;
879
880  /* Op=Nxt and initial On=(0); */
881  Nxt=NULL;
882
883  poly Pp;
884  poly Pn;
885  int t=0;
886  int first=lF[1];
887  int nlast=lF[cnt];
888  int kk=0;
889  /*  cnt--;   */
890  /* now lF[cnt] should be <=iF-1 */
891
892  while (Op[first]!=0)
893  {
894     t=lF[cnt];   /* cnt as it was computed */
895
896     poly w=gnc_uu_Mult_ww(t,Op[t],jG,bG,r);
897     c[cnt]=n_Copy(p_GetCoeff(w,r),r);
898     D = pNext(w);  /* getting coef and rest D */
899     p_LmDelete(&w,r);
900     w=NULL;
901
902     Op[t]= 0;
903     Pp=pOne();
904     p_SetExpV(Pp,Op,r);
905     p_Setm(Pp,r);
906
907     if (t<nlast)
908     {
909       kk=lF[cnt+1];
910       On[kk]=F[kk];
911
912       Pn=pOne();
913       p_SetExpV(Pn,On,r);
914       p_Setm(Pn,r);
915
916       if (t!=first)   /* typical expr */
917       {
918         w=gnc_p_Mult_mm(D,Pn,r);
919         Rout=gnc_mm_Mult_p(Pp,w,r);
920         w=NULL;
921       }
922       else                   /* last step */
923       {
924         On[t]=0;
925         p_SetExpV(Pn,On,r);
926         p_Setm(Pn,r);
927         Rout=gnc_p_Mult_mm(D,Pn,r);
928       }
929#ifdef PDEBUG
930       p_Test(Pp,r);
931#endif
932       p_Delete(&Pn,r);
933     }
934     else                     /* first step */
935     {
936       Rout=gnc_mm_Mult_p(Pp,D,r);
937     }
938#ifdef PDEBUG
939     p_Test(Pp,r);
940#endif
941     p_Delete(&Pp,r);
942     num=n_Mult(c[cnt+1],c[cnt],r);
943     n_Delete(&c[cnt],r);
944     c[cnt]=num;
945     Rout=p_Mult_nn(Rout,c[cnt+1],r); /* Rest is ready */
946     out=p_Add_q(out,Rout,r);
947     Pp=NULL;
948     cnt--;
949  }
950  /* only to feel safe:*/
951  Pn=Pp=NULL;
952  freeT(On,rN);
953  freeT(Op,rN);
954
955/* leadterm and Prv-part with coef 1 */
956/*  U[0]=exp; */
957/*  U[jG]=U[jG]+bG;  */
958/* make leadterm */
959/* ??????????? we have done it already :-0 */
960
961  Rout=pOne();
962  p_SetExpV(Rout,U,r);
963  p_Setm(Rout,r);  /* use again this name */
964  p_SetCoeff(Rout,c[cnt+1],r);  /* last computed coef */
965
966  out=p_Add_q(out,Rout,r);
967
968  Rout=NULL;
969
970  freeT(U, rN);
971  freeN(c, i);
972  freeT(lF, rN);
973
974  if (cnf!=0)
975  {
976    Rout=pOne();
977    p_SetExpV(Rout,Prv,r);
978    p_Setm(Rout,r);
979    freeT(Prv, rN);
980    out=gnc_mm_Mult_p(Rout,out,r); /* getting the final result */
981    p_Delete(&Rout,r);
982  }
983
984  return (out);
985}
986
987poly gnc_uu_Mult_ww_vert (int i, int a, int j, int b, const ring r)
988{
989  int k,m;
990  int rN=r->N;
991  const int cMTindex = UPMATELEM(j,i,rN);
992  matrix cMT=r->GetNC()->MT[cMTindex];         /* cMT=current MT */
993
994  poly x=pOne();p_SetExp(x,j,1,r);p_Setm(x,r);
995/* var(j); */
996  poly y=pOne();p_SetExp(y,i,1,r);p_Setm(y,r);
997/*var(i);  for convenience */
998#ifdef PDEBUG
999  p_Test(x,r);
1000  p_Test(y,r);
1001#endif
1002  poly t=NULL;
1003/* ------------ Main Cycles ----------------------------*/
1004
1005  for (k=2;k<=a;k++)
1006  {
1007     t = MATELEM(cMT,k,1);
1008
1009     if (t==NULL)   /* not computed yet */
1010     {
1011       t = nc_p_CopyGet(MATELEM(cMT,k-1,1),r);
1012       //        t=p_Copy(MATELEM(cMT,k-1,1),r);
1013       t = gnc_mm_Mult_p(y,t,r);
1014       cMT=r->GetNC()->MT[cMTindex]; // since multiplication can change the MT table...
1015       assume( t != NULL );
1016#ifdef PDEBUG
1017       p_Test(t,r);
1018#endif
1019       MATELEM(cMT,k,1) = nc_p_CopyPut(t,r);
1020       //        omCheckAddr(cMT->m);
1021       p_Delete(&t,r);
1022     }
1023     t=NULL;
1024  }
1025
1026  for (m=2;m<=b;m++)
1027  {
1028    t = MATELEM(cMT,a,m);
1029    //     t=MATELEM(cMT,a,m);
1030    if (t==NULL)   //not computed yet
1031    {
1032      t = nc_p_CopyGet(MATELEM(cMT,a,m-1),r);
1033      assume( t != NULL );
1034      //      t=p_Copy(MATELEM(cMT,a,m-1),r);
1035      t = gnc_p_Mult_mm(t,x,r);
1036      cMT=r->GetNC()->MT[cMTindex]; // since multiplication can change the MT table...
1037#ifdef PDEBUG
1038      p_Test(t,r);
1039#endif
1040      MATELEM(cMT,a,m) = nc_p_CopyPut(t,r);
1041      //      MATELEM(cMT,a,m) = t;
1042      //        omCheckAddr(cMT->m);
1043      p_Delete(&t,r);
1044    }
1045    t=NULL;
1046  }
1047  p_Delete(&x,r);
1048  p_Delete(&y,r);
1049  t=MATELEM(cMT,a,b);
1050  assume( t != NULL );
1051 
1052  t= nc_p_CopyGet(t,r);
1053#ifdef PDEBUG
1054  p_Test(t,r);
1055#endif
1056  //  return(p_Copy(t,r));
1057  /* since the last computed element was cMT[a,b] */
1058  return(t);
1059}
1060
1061
1062static inline poly gnc_uu_Mult_ww_formula (int i, int a, int j, int b, const ring r)
1063{
1064  if(bNoFormula)
1065    return gnc_uu_Mult_ww_vert(i, a, j, b, r);
1066
1067  CFormulaPowerMultiplier* FormulaMultiplier = GetFormulaPowerMultiplier(r);
1068  Enum_ncSAType PairType = _ncSA_notImplemented;
1069
1070  if( FormulaMultiplier != NULL )
1071    PairType = FormulaMultiplier->GetPair(j, i);
1072
1073
1074  if( PairType == _ncSA_notImplemented )
1075    return gnc_uu_Mult_ww_vert(i, a, j, b, r);
1076
1077
1078 //    return FormulaMultiplier->Multiply(j, i, b, a);
1079  poly t = CFormulaPowerMultiplier::Multiply( PairType, j, i, b, a, r);
1080
1081  int rN=r->N;
1082  matrix cMT = r->GetNC()->MT[UPMATELEM(j,i,rN)];         /* cMT=current MT */
1083
1084
1085  MATELEM(cMT, a, b) = nc_p_CopyPut(t,r);
1086
1087  //  t=MATELEM(cMT,a,b);
1088//  t= nc_p_CopyGet(MATELEM(cMT,a,b),r);
1089  //  return(p_Copy(t,r));
1090  /* since the last computed element was cMT[a,b] */
1091  return(t);
1092}
1093
1094
1095poly gnc_uu_Mult_ww (int i, int a, int j, int b, const ring r)
1096  /* (x_i)^a times (x_j)^b */
1097  /* x_i = y,  x_j = x ! */
1098{
1099  /* Check zero exceptions, (q-)commutativity and is there something to do? */
1100  assume(a!=0);
1101  assume(b!=0);
1102  poly out=pOne();
1103  if (i<=j)
1104  {
1105    p_SetExp(out,i,a,r);
1106    p_AddExp(out,j,b,r);
1107    p_Setm(out,r);
1108    return(out);
1109  }/* zero exeptions and usual case */
1110  /*  if ((a==0)||(b==0)||(i<=j)) return(out); */
1111
1112  if (MATELEM(r->GetNC()->COM,j,i)!=NULL)
1113    /* commutative or quasicommutative case */
1114  {
1115    p_SetExp(out,i,a,r);
1116    p_AddExp(out,j,b,r);
1117    p_Setm(out,r);
1118    if (r->cf->nIsOne(p_GetCoeff(MATELEM(r->GetNC()->COM,j,i),r))) /* commutative case */
1119    {
1120      return(out);
1121    }
1122    else
1123    {
1124      number tmp_number=p_GetCoeff(MATELEM(r->GetNC()->COM,j,i),r); /* quasicommutative case */
1125      nPower(tmp_number,a*b,&tmp_number); // BUG! ;-(
1126      p_SetCoeff(out,tmp_number,r);
1127      return(out);
1128    }
1129  }/* end_of commutative or quasicommutative case */
1130  p_Delete(&out,r);
1131
1132
1133  if(bNoCache && !bNoFormula) // don't use cache whenever possible!
1134  { // without cache!?
1135    CFormulaPowerMultiplier* FormulaMultiplier = GetFormulaPowerMultiplier(r);
1136    Enum_ncSAType PairType = _ncSA_notImplemented;
1137
1138     if( FormulaMultiplier != NULL )
1139       PairType = FormulaMultiplier->GetPair(j, i);
1140
1141     if( PairType != _ncSA_notImplemented )
1142  // //    return FormulaMultiplier->Multiply(j, i, b, a);
1143       return CFormulaPowerMultiplier::Multiply( PairType, j, i, b, a, r);
1144  }
1145
1146
1147  /* we are here if  i>j and variables do not commute or quasicommute */
1148  /* in fact, now a>=1 and b>=1; and j<i */
1149  /* now check whether the polynomial is already computed */
1150  int rN=r->N;
1151  int vik = UPMATELEM(j,i,rN);
1152  int cMTsize=r->GetNC()->MTsize[vik];
1153  int newcMTsize=0;
1154  newcMTsize=si_max(a,b);
1155
1156  if (newcMTsize<=cMTsize)
1157  {
1158    out =  nc_p_CopyGet(MATELEM(r->GetNC()->MT[vik],a,b),r);
1159    if (out !=NULL) return (out);
1160  }
1161  int k,m;
1162  if (newcMTsize > cMTsize)
1163  {
1164    int inM=(((newcMTsize+6)/7)*7);
1165    assume (inM>=newcMTsize);
1166    newcMTsize = inM;
1167    //    matrix tmp = (matrix)omAlloc0(inM*inM*sizeof(poly));
1168    matrix tmp = mpNew(newcMTsize,newcMTsize);
1169
1170    for (k=1;k<=cMTsize;k++)
1171    {
1172      for (m=1;m<=cMTsize;m++)
1173      {
1174        out = MATELEM(r->GetNC()->MT[UPMATELEM(j,i,rN)],k,m);
1175        if ( out != NULL )
1176        {
1177          MATELEM(tmp,k,m) = out;/*MATELEM(r->GetNC()->MT[UPMATELEM(j,i,rN)],k,m)*/
1178          //           omCheckAddr(tmp->m);
1179          MATELEM(r->GetNC()->MT[UPMATELEM(j,i,rN)],k,m)=NULL;
1180          //           omCheckAddr(r->GetNC()->MT[UPMATELEM(j,i,rN)]->m);
1181          out=NULL;
1182        }
1183      }
1184    }
1185    id_Delete((ideal *)&(r->GetNC()->MT[UPMATELEM(j,i,rN)]),r);
1186    r->GetNC()->MT[UPMATELEM(j,i,rN)] = tmp;
1187    tmp=NULL;
1188    r->GetNC()->MTsize[UPMATELEM(j,i,rN)] = newcMTsize;
1189  }
1190  /* The update of multiplication matrix is finished */
1191
1192
1193  return gnc_uu_Mult_ww_formula(i, a, j, b, r);
1194
1195  out = gnc_uu_Mult_ww_vert(i, a, j, b, r);
1196  //    out = nc_uu_Mult_ww_horvert(i, a, j, b, r);
1197  return(out);
1198}
1199
1200poly gnc_uu_Mult_ww_horvert (int i, int a, int j, int b, const ring r)
1201
1202{
1203  int k,m;
1204  int rN=r->N;
1205  matrix cMT=r->GetNC()->MT[UPMATELEM(j,i,rN)];         /* cMT=current MT */
1206
1207  poly x=pOne();p_SetExp(x,j,1,r);p_Setm(x,r);/* var(j); */
1208  poly y=pOne();p_SetExp(y,i,1,r);p_Setm(y,r); /*var(i);  for convenience */
1209#ifdef PDEBUG
1210  p_Test(x,r);
1211  p_Test(y,r);
1212#endif
1213
1214  poly t=NULL;
1215
1216  int toXY;
1217  int toYX;
1218
1219  if (a==1) /* y*x^b, b>=2 */
1220  {
1221    toXY=b-1;
1222    while ( (MATELEM(cMT,1,toXY)==NULL) && (toXY>=2)) toXY--;
1223    for (m=toXY+1;m<=b;m++)
1224    {
1225      t=MATELEM(cMT,1,m);
1226      if (t==NULL)   /* remove after debug */
1227      {
1228        t = p_Copy(MATELEM(cMT,1,m-1),r);
1229        t = gnc_p_Mult_mm(t,x,r);
1230        MATELEM(cMT,1,m) = t;
1231        /*        omCheckAddr(cMT->m); */
1232      }
1233      else
1234      {
1235        /* Error, should never get there */
1236        WarnS("Error: a=1; MATELEM!=0");
1237      }
1238      t=NULL;
1239    }
1240    return(p_Copy(MATELEM(cMT,1,b),r));
1241  }
1242
1243  if (b==1) /* y^a*x, a>=2 */
1244  {
1245    toYX=a-1;
1246    while ( (MATELEM(cMT,toYX,1)==NULL) && (toYX>=2)) toYX--;
1247    for (m=toYX+1;m<=a;m++)
1248    {
1249      t=MATELEM(cMT,m,1);
1250      if (t==NULL)   /* remove after debug */
1251      {
1252        t = p_Copy(MATELEM(cMT,m-1,1),r);
1253        t = gnc_mm_Mult_p(y,t,r);
1254        MATELEM(cMT,m,1) = t;
1255        /*        omCheckAddr(cMT->m); */
1256      }
1257      else
1258      {
1259        /* Error, should never get there */
1260        WarnS("Error: b=1, MATELEM!=0");
1261      }
1262      t=NULL;
1263    }
1264    return(p_Copy(MATELEM(cMT,a,1),r));
1265  }
1266
1267/* ------------ Main Cycles ----------------------------*/
1268  /*            a>1, b>1              */
1269
1270  int dXY=0; int dYX=0;
1271  /* dXY = distance for computing x-mult, then y-mult */
1272  /* dYX = distance for computing y-mult, then x-mult */
1273  int toX=a-1; int toY=b-1; /* toX = to axe X, toY = to axe Y */
1274  toXY=b-1; toYX=a-1;
1275  /* if toX==0, toXY = dist. to computed y * x^toXY */
1276  /* if toY==0, toYX = dist. to computed y^toYX * x */
1277  while ( (MATELEM(cMT,toX,b)==NULL) && (toX>=1)) toX--;
1278  if (toX==0) /* the whole column is not computed yet */
1279  {
1280    while ( (MATELEM(cMT,1,toXY)==NULL) && (toXY>=1)) toXY--;
1281    /* toXY >=1 */
1282    dXY=b-1-toXY;
1283  }
1284  dXY=dXY+a-toX; /* the distance to nearest computed y^toX x^b */
1285
1286  while ( (MATELEM(cMT,a,toY)==NULL) && (toY>=1)) toY--;
1287  if (toY==0) /* the whole row is not computed yet */
1288  {
1289    while ( (MATELEM(cMT,toYX,1)==NULL) && (toYX>=1)) toYX--;
1290    /* toYX >=1 */
1291    dYX=a-1-toYX;
1292  }
1293  dYX=dYX+b-toY; /* the distance to nearest computed y^a x^toY */
1294
1295  if (dYX>=dXY)
1296  {
1297    /* first x, then y */
1298    if (toX==0) /* start with the row*/
1299    {
1300      for (m=toXY+1;m<=b;m++)
1301      {
1302        t=MATELEM(cMT,1,m);
1303        if (t==NULL)   /* remove after debug */
1304        {
1305          t = p_Copy(MATELEM(cMT,1,m-1),r);
1306          t = gnc_p_Mult_mm(t,x,r);
1307          MATELEM(cMT,1,m) = t;
1308          /*        omCheckAddr(cMT->m); */
1309        }
1310        else
1311        {
1312          /* Error, should never get there */
1313          WarnS("dYX>=dXY,toXY; MATELEM==0");
1314        }
1315        t=NULL;
1316      }
1317      toX=1; /* y*x^b is computed */
1318    }
1319    /* Now toX>=1 */
1320    for (k=toX+1;k<=a;k++)
1321    {
1322      t=MATELEM(cMT,k,b);
1323      if (t==NULL)   /* remove after debug */
1324      {
1325        t = p_Copy(MATELEM(cMT,k-1,b),r);
1326        t = gnc_mm_Mult_p(y,t,r);
1327        MATELEM(cMT,k,b) = t;
1328        /*        omCheckAddr(cMT->m); */
1329      }
1330      else
1331      {
1332        /* Error, should never get there */
1333        WarnS("dYX>=dXY,toX; MATELEM==0");
1334      }
1335      t=NULL;
1336    }
1337  } /* endif (dYX>=dXY) */
1338
1339
1340  if (dYX<dXY)
1341  {
1342    /* first y, then x */
1343    if (toY==0) /* start with the column*/
1344    {
1345      for (m=toYX+1;m<=a;m++)
1346      {
1347        t=MATELEM(cMT,m,1);
1348        if (t==NULL)   /* remove after debug */
1349        {
1350          t = p_Copy(MATELEM(cMT,m-1,1),r);
1351          t = gnc_mm_Mult_p(y,t,r);
1352          MATELEM(cMT,m,1) = t;
1353          /*        omCheckAddr(cMT->m); */
1354        }
1355        else
1356        {
1357          /* Error, should never get there */
1358          WarnS("dYX<dXY,toYX; MATELEM==0");
1359        }
1360        t=NULL;
1361      }
1362      toY=1; /* y^a*x is computed */
1363    }
1364    /* Now toY>=1 */
1365    for (k=toY+1;k<=b;k++)
1366    {
1367      t=MATELEM(cMT,a,k);
1368      if (t==NULL)   /* remove after debug */
1369      {
1370        t = p_Copy(MATELEM(cMT,a,k-1),r);
1371        t = gnc_p_Mult_mm(t,x,r);
1372        MATELEM(cMT,a,k) = t;
1373        /*        omCheckAddr(cMT->m); */
1374      }
1375      else
1376      {
1377        /* Error, should never get there */
1378        WarnS("dYX<dXY,toY; MATELEM==0");
1379      }
1380      t=NULL;
1381    }
1382  } /* endif (dYX<dXY) */
1383
1384  p_Delete(&x,r);
1385  p_Delete(&y,r);
1386  t=p_Copy(MATELEM(cMT,a,b),r);
1387  return(t);  /* since the last computed element was cMT[a,b] */
1388}
1389
1390
1391/* ----------------------------- Syzygies ---------------------- */
1392
1393/*2
1394* reduction of p2 with p1
1395* do not destroy p1, but p2
1396* p1 divides p2 -> for use in NF algorithm
1397*/
1398poly gnc_ReduceSpolyOld(const poly p1, poly p2/*,poly spNoether*/, const ring r)
1399{
1400  assume(p_LmDivisibleBy(p1, p2, r));
1401
1402#ifdef PDEBUG
1403  if (p_GetComp(p1,r)!=p_GetComp(p2,r)
1404  && (p_GetComp(p1,r)!=0)
1405  && (p_GetComp(p2,r)!=0))
1406  {
1407    dReportError("nc_ReduceSpolyOld: different components");
1408    return(NULL);
1409  }
1410#endif
1411  poly m = pOne();
1412  p_ExpVectorDiff(m,p2,p1,r);
1413  //p_Setm(m,r);
1414#ifdef PDEBUG
1415  p_Test(m,r);
1416#endif
1417  /* pSetComp(m,r)=0? */
1418  poly   N  = nc_mm_Mult_p(m, p_Head(p1,r), r);
1419  number C  = n_Copy( p_GetCoeff(N,  r), r);
1420  number cF = n_Copy( p_GetCoeff(p2, r),r);
1421  /* GCD stuff */
1422  number cG = nGcd(C, cF, r);
1423  if ( !nEqual(cG, n_Init(1,r) ) )
1424  {
1425    cF = nDiv(cF, cG);
1426    C  = nDiv(C,  cG);
1427  }
1428  p2 = p_Mult_nn(p2, C, r);
1429  poly out = nc_mm_Mult_pp(m, pNext(p1), r);
1430  N = p_Add_q(N, out, r);
1431  p_Test(p2,r);
1432  p_Test(N,r);
1433  number MinusOne = n_Init(-1,r);
1434  if (!n_Equal(cF,MinusOne,r))
1435  {
1436    cF = n_Neg(cF,r);
1437    N  = p_Mult_nn(N, cF, r);
1438    p_Test(N,r);
1439  }
1440  out = p_Add_q(p2,N,r);
1441  p_Test(out,r);
1442  if ( out!=NULL ) p_Content(out,r);
1443  p_Delete(&m,r);
1444  n_Delete(&cF,r);
1445  n_Delete(&C,r);
1446  n_Delete(&MinusOne,r);
1447  return(out);
1448
1449}
1450
1451poly gnc_ReduceSpolyNew(const poly p1, poly p2, const ring r)
1452{
1453  assume(p_LmDivisibleBy(p1, p2, r));
1454
1455  const long lCompP1 = p_GetComp(p1,r);
1456  const long lCompP2 = p_GetComp(p2,r);
1457
1458  if ((lCompP1!=lCompP2) && (lCompP1!=0) && (lCompP2!=0))
1459  {
1460#ifdef PDEBUG
1461    Werror("gnc_ReduceSpolyNew: different non-zero components!");
1462#endif
1463    return(NULL);
1464  }
1465
1466  poly m = pOne();
1467  p_ExpVectorDiff(m, p2, p1, r);
1468  //p_Setm(m,r);
1469#ifdef PDEBUG
1470  p_Test(m,r);
1471#endif
1472
1473  /* pSetComp(m,r)=0? */
1474  poly   N  = nc_mm_Mult_p(m, p_Head(p1,r), r);
1475
1476  number C  = n_Copy( p_GetCoeff(N,  r), r);
1477  number cF = n_Copy( p_GetCoeff(p2, r), r);
1478
1479  /* GCD stuff */
1480  number cG = nGcd(C, cF, r);
1481
1482  if (!n_IsOne(cG, r))
1483  {
1484    number n_tmp;
1485    n_tmp = n_Div(cF, cG, r); n_Delete(&cF,r); cF=n_tmp;
1486    n_tmp  = n_Div(C,  cG, r); n_Delete(&C,r); C=n_tmp;
1487  }
1488
1489  p2 = p_Mult_nn(p2, C, r); // p2 !!!
1490  p_Test(p2,r);
1491  n_Delete(&C,r);
1492
1493  poly out = nc_mm_Mult_pp(m, pNext(p1), r);
1494  p_Delete(&m,r);
1495
1496  N = p_Add_q(N, out, r);
1497  p_Test(N,r);
1498
1499  if (!n_IsMOne(cF,r)) // ???
1500  {
1501    cF = n_Neg(cF,r);
1502    N  = p_Mult_nn(N, cF, r);
1503    p_Test(N,r);
1504  }
1505  n_Delete(&cF,r);
1506
1507  out = p_Add_q(p2,N,r); // delete N, p2
1508  p_Test(out,r);
1509  if ( out!=NULL ) p_Content(out,r);
1510  return(out);
1511}
1512
1513
1514/*4
1515* creates the S-polynomial of p1 and p2
1516* do not destroy p1 and p2
1517*/
1518poly gnc_CreateSpolyOld(poly p1, poly p2/*,poly spNoether*/, const ring r)
1519{
1520#ifdef PDEBUG
1521  if ((p_GetComp(p1,r)!=p_GetComp(p2,r))
1522  && (p_GetComp(p1,r)!=0)
1523  && (p_GetComp(p2,r)!=0))
1524  {
1525    dReportError("gnc_CreateSpolyOld : different components!");
1526    return(NULL);
1527  }
1528#endif
1529  if ((ncRingType(r)==nc_lie) && pHasNotCF(p1,p2)) /* prod crit */
1530  {
1531    return(nc_p_Bracket_qq(pCopy(p2),p1));
1532  }
1533  poly pL=pOne();
1534  poly m1=pOne();
1535  poly m2=pOne();
1536  pLcm(p1,p2,pL);
1537  p_Setm(pL,r);
1538#ifdef PDEBUG
1539  p_Test(pL,r);
1540#endif
1541  p_ExpVectorDiff(m1,pL,p1,r);
1542  //p_SetComp(m1,0,r);
1543  //p_Setm(m1,r);
1544#ifdef PDEBUG
1545  p_Test(m1,r);
1546#endif
1547  p_ExpVectorDiff(m2,pL,p2,r);
1548  //p_SetComp(m2,0,r);
1549  //p_Setm(m2,r);
1550#ifdef PDEBUG
1551  p_Test(m2,r);
1552#endif
1553  p_Delete(&pL,r);
1554  /* zero exponents ! */
1555  poly M1    = nc_mm_Mult_p(m1,p_Head(p1,r),r);
1556  number C1  = n_Copy(p_GetCoeff(M1,r),r);
1557  poly M2    = nc_mm_Mult_p(m2,p_Head(p2,r),r);
1558  number C2  = n_Copy(p_GetCoeff(M2,r),r);
1559  /* GCD stuff */
1560  number C = nGcd(C1,C2,r);
1561  if (!nEqual(C,n_Init(1,r)))
1562  {
1563    C1=nDiv(C1,C);
1564    C2=nDiv(C2,C);
1565  }
1566  M1=p_Mult_nn(M1,C2,r);
1567  p_SetCoeff(m1,C2,r);
1568  number MinusOne=n_Init(-1,r);
1569  if (n_Equal(C1,MinusOne,r))
1570  {
1571    M2=p_Add_q(M1,M2,r);
1572  }
1573  else
1574  {
1575    C1=n_Neg(C1,r);
1576    M2=p_Mult_nn(M2,C1,r);
1577    M2=p_Add_q(M1,M2,r);
1578    p_SetCoeff(m2,C1,r);
1579  }
1580  /* M1 is killed, M2=res = C2 M1 - C1 M2 */
1581  poly tmp=p_Copy(p1,r);
1582  tmp=p_LmDeleteAndNext(tmp,r);
1583  M1=nc_mm_Mult_p(m1,tmp,r);
1584  tmp=p_Copy(p2,r);
1585  tmp=p_LmDeleteAndNext(tmp,r);
1586  M2=p_Add_q(M2,M1,r);
1587  M1=nc_mm_Mult_p(m2,tmp,r);
1588  M2=p_Add_q(M2,M1,r);
1589  p_Delete(&m1,r);
1590  p_Delete(&m2,r);
1591  //  n_Delete(&C1,r);
1592  //  n_Delete(&C2,r);
1593  n_Delete(&MinusOne,r);
1594#ifdef PDEBUG
1595  p_Test(M2,r);
1596#endif
1597  if (M2!=NULL) M2=p_Cleardenom(M2,r);
1598  //if (M2!=NULL) p_Content(M2); // done by pCleardenom
1599  return(M2);
1600}
1601
1602poly gnc_CreateSpolyNew(poly p1, poly p2/*,poly spNoether*/, const ring r)
1603{
1604  assume(r == currRing);
1605
1606#ifdef PDEBUG
1607  pTest(p1);
1608  pTest(p2);
1609#if MYTEST
1610  Print("p1: "); pWrite(p1);
1611  Print("p2: "); pWrite(p2);
1612#endif
1613#endif
1614
1615  const long lCompP1 = p_GetComp(p1,r);
1616  const long lCompP2 = p_GetComp(p2,r);
1617
1618  if ((lCompP1!=lCompP2) && (lCompP1!=0) && (lCompP2!=0))
1619  {
1620#ifdef PDEBUG
1621    Werror("gnc_CreateSpolyNew: different non-zero components!");
1622#endif
1623    return(NULL);
1624  }
1625
1626#ifdef PDEBUG
1627  if (lCompP1!=lCompP2)
1628  {
1629    WarnS("gnc_CreateSpolyNew: vector & poly in SPoly!");
1630  }
1631#endif
1632
1633
1634//   if ((r->GetNC()->type==nc_lie) && pHasNotCF(p1,p2)) /* prod crit */
1635//   {
1636//     return(nc_p_Bracket_qq(pCopy(p2),p1));
1637//   }
1638
1639//  poly pL=p_One( r);
1640
1641  poly m1=p_One( r);
1642  poly m2=p_One( r);
1643
1644  poly pL = p_Lcm(p1,p2,r);                               // pL = lcm( lm(p1), lm(p2) )
1645
1646
1647#ifdef PDEBUG
1648//  p_Test(pL,r);
1649#endif
1650
1651  p_ExpVectorDiff(m1, pL, p1, r);                  // m1 = pL / lm(p1)
1652  //p_SetComp(m1,0,r);
1653  //p_Setm(m1,r);
1654
1655#ifdef PDEBUG
1656  p_Test(m1,r);
1657#endif
1658//  assume(p_GetComp(m1,r) == 0);
1659
1660  p_ExpVectorDiff(m2, pL, p2, r);                  // m2 = pL / lm(p2)
1661
1662  //p_SetComp(m2,0,r);
1663  //p_Setm(m2,r);
1664#ifdef PDEBUG
1665  p_Test(m2,r);
1666#endif
1667
1668#ifdef PDEBUG
1669#if MYTEST
1670  Print("m1: "); pWrite(m1);
1671  Print("m2: "); pWrite(m2);
1672#endif
1673#endif
1674
1675
1676//  assume(p_GetComp(m2,r) == 0);
1677
1678#ifdef PDEBUG
1679#if 0
1680  if(  (p_GetComp(m2,r) != 0) || (p_GetComp(m1,r) != 0) )
1681  {
1682    WarnS("gnc_CreateSpolyNew: wrong monomials!");
1683
1684
1685#ifdef RDEBUG
1686    PrintS("m1 = "); p_Write(m1, r);
1687    p_DebugPrint(m1, r);
1688
1689    PrintS("m2 = "); p_Write(m2, r);
1690    p_DebugPrint(m2, r);
1691
1692    PrintS("p1 = "); p_Write(p1, r);
1693    p_DebugPrint(p1, r);
1694
1695    PrintS("p2 = "); p_Write(p2, r);
1696    p_DebugPrint(p2, r);
1697
1698    PrintS("pL = "); p_Write(pL, r);
1699    p_DebugPrint(pL, r);
1700#endif
1701
1702  }
1703
1704#endif
1705#endif
1706
1707  p_Delete(&pL,r);
1708
1709  /* zero exponents !? */
1710  poly M1    = nc_mm_Mult_p(m1,p_Head(p1,r),r); // M1 = m1 * lt(p1)
1711  poly M2    = nc_mm_Mult_p(m2,p_Head(p2,r),r); // M2 = m2 * lt(p2)
1712
1713#ifdef PDEBUG
1714  p_Test(M1,r);
1715  p_Test(M2,r);
1716
1717#if MYTEST
1718  Print("M1: "); pWrite(M1);
1719  Print("M2: "); pWrite(M2);
1720#endif
1721#endif
1722
1723  if(M1 == NULL || M2 == NULL)
1724  {
1725#ifdef PDEBUG
1726       Print("\np1 = ");
1727       p_Write(p1, r);
1728
1729       Print("m1 = ");
1730       p_Write(m1, r);
1731
1732       Print("p2 = ");
1733       p_Write(p2, r);
1734
1735       Print("m2 = ");
1736       p_Write(m2, r);
1737
1738       Werror("ERROR in nc_CreateSpoly: result of multiplication is Zero!\n");
1739#endif
1740       return(NULL);
1741  }
1742
1743  number C1  = n_Copy(p_GetCoeff(M1,r),r);      // C1 = lc(M1)
1744  number C2  = n_Copy(p_GetCoeff(M2,r),r);      // C2 = lc(M2)
1745
1746  /* GCD stuff */
1747  number C = nGcd(C1, C2, r);                     // C = gcd(C1, C2)
1748
1749  if (!n_IsOne(C, r))                              // if C != 1
1750  {
1751    C1=n_Div(C1, C, r);                              // C1 = C1 / C
1752    C2=n_Div(C2, C, r);                              // C2 = C2 / C
1753  }
1754
1755  n_Delete(&C,r); // destroy the number C
1756
1757  C1=n_Neg(C1,r);
1758
1759//   number MinusOne=n_Init(-1,r);
1760//   if (n_Equal(C1,MinusOne,r))                   // lc(M1) / gcd( lc(M1), lc(M2)) == -1 ????
1761//   {
1762//     M2=p_Add_q(M1,M2,r);                        // ?????
1763//   }
1764//   else
1765//   {
1766  M1=p_Mult_nn(M1,C2,r);                           // M1 = (C2*lc(p1)) * (lcm(lm(p1),lm(p2)) / lm(p1)) * lm(p1)
1767
1768#ifdef PDEBUG
1769  p_Test(M1,r);
1770#endif
1771
1772  M2=p_Mult_nn(M2,C1,r);                           // M2 =(-C1*lc(p2)) * (lcm(lm(p1),lm(p2)) / lm(p2)) * lm(p2)
1773
1774
1775
1776#ifdef PDEBUG
1777  p_Test(M2,r);
1778
1779#if MYTEST
1780  Print("M1: "); pWrite(M1);
1781  Print("M2: "); pWrite(M2);
1782#endif
1783#endif
1784
1785
1786  M2=p_Add_q(M1,M2,r);                             // M1 is killed, M2 = spoly(lt(p1), lt(p2)) = C2*M1 - C1*M2
1787
1788#ifdef PDEBUG
1789  p_Test(M2,r);
1790
1791#if MYTEST
1792  Print("M2: "); pWrite(M2);
1793#endif
1794
1795#endif
1796
1797// M2 == 0 for supercommutative algebras!
1798//   }
1799//   n_Delete(&MinusOne,r);
1800
1801  p_SetCoeff(m1,C2,r);                           // lc(m1) = C2!!!
1802  p_SetCoeff(m2,C1,r);                           // lc(m2) = C1!!!
1803
1804#ifdef PDEBUG
1805  p_Test(m1,r);
1806  p_Test(m2,r);
1807#endif
1808
1809//  poly tmp = p_Copy(p1,r);                         // tmp = p1
1810//  tmp=p_LmDeleteAndNext(tmp,r);                  // tmp = tail(p1)
1811//#ifdef PDEBUG
1812//  p_Test(tmp,r);
1813//#endif
1814
1815  M1 = nc_mm_Mult_pp(m1, pNext(p1), r);                      // M1 = m1 * tail(p1), delete tmp // ???
1816
1817#ifdef PDEBUG
1818  p_Test(M1,r);
1819
1820#if MYTEST
1821  Print("M1: "); pWrite(M1);
1822#endif
1823
1824#endif
1825
1826  M2=p_Add_q(M2,M1,r);                           // M2 = spoly(lt(p1), lt(p2)) + m1 * tail(p1), delete M1
1827#ifdef PDEBUG
1828  p_Test(M2,r);
1829
1830#if MYTEST
1831  Print("M2: "); pWrite(M2);
1832#endif
1833
1834#endif
1835
1836//  tmp=p_Copy(p2,r);                              // tmp = p2
1837//  tmp=p_LmDeleteAndNext(tmp,r);                  // tmp = tail(p2)
1838
1839//#ifdef PDEBUG
1840//  p_Test(tmp,r);
1841//#endif
1842
1843  M1 = nc_mm_Mult_pp(m2, pNext(p2), r);                      // M1 = m2 * tail(p2), detele tmp
1844
1845#ifdef PDEBUG
1846  p_Test(M1,r);
1847
1848#if MYTEST
1849  Print("M1: "); pWrite(M1);
1850#endif
1851
1852#endif
1853
1854  M2 = p_Add_q(M2,M1,r);                           // M2 = spoly(lt(p1), lt(p2)) + m1 * tail(p1) + m2*tail(p2)
1855
1856#ifdef PDEBUG
1857  p_Test(M2,r);
1858
1859#if MYTEST
1860  Print("M2: "); pWrite(M2);
1861#endif
1862
1863#endif
1864                                                 // delete M1
1865
1866  p_Delete(&m1,r);  //  => n_Delete(&C1,r);
1867  p_Delete(&m2,r);  //  => n_Delete(&C2,r);
1868
1869#ifdef PDEBUG
1870  p_Test(M2,r);
1871#endif
1872
1873  if (M2!=NULL) p_Cleardenom(M2,r);
1874//  if (M2!=NULL) p_Content(M2);
1875
1876  return(M2);
1877}
1878
1879
1880
1881
1882#if 0
1883/*5
1884* reduction of tail(q) with p1
1885* lead(p1) divides lead(pNext(q2)) and pNext(q2) is reduced
1886* do not destroy p1, but tail(q)
1887*/
1888void gnc_ReduceSpolyTail(poly p1, poly q, poly q2, poly spNoether, const ring r)
1889{
1890  poly a1=p_Head(p1,r);
1891  poly Q=pNext(q2);
1892  number cQ=p_GetCoeff(Q,r);
1893  poly m=pOne();
1894  p_ExpVectorDiff(m,Q,p1,r);
1895  //  p_SetComp(m,0,r);
1896  //p_Setm(m,r);
1897#ifdef PDEBUG
1898  p_Test(m,r);
1899#endif
1900  /* pSetComp(m,r)=0? */
1901  poly M = nc_mm_Mult_pp(m, p1,r);
1902  number C=p_GetCoeff(M,r);
1903  M=p_Add_q(M,nc_mm_Mult_p(m,p_LmDeleteAndNext(p_Copy(p1,r),r),r),r); // _pp?
1904  q=p_Mult_nn(q,C,r);
1905  number MinusOne=n_Init(-1,r);
1906  if (!n_Equal(cQ,MinusOne,r))
1907  {
1908    cQ=nNeg(cQ);
1909    M=p_Mult_nn(M,cQ,r);
1910  }
1911  Q=p_Add_q(Q,M,r);
1912  pNext(q2)=Q;
1913
1914  p_Delete(&m,r);
1915  n_Delete(&C,r);
1916  n_Delete(&cQ,r);
1917  n_Delete(&MinusOne,r);
1918  /*  return(q); */
1919}
1920#endif
1921
1922
1923/*6
1924* creates the commutative lcm(lm(p1),lm(p2))
1925* do not destroy p1 and p2
1926*/
1927poly nc_CreateShortSpoly(poly p1, poly p2, const ring r)
1928{
1929#ifdef PDEBUG
1930  p_Test(p1, r);
1931  p_Test(p2, r);
1932#endif
1933
1934  const long lCompP1 = p_GetComp(p1,r);
1935  const long lCompP2 = p_GetComp(p2,r);
1936
1937  if ((lCompP1!=lCompP2) && (lCompP1!=0) && (lCompP2!=0))
1938  {
1939#ifdef PDEBUG
1940    Werror("nc_CreateShortSpoly: wrong module components!"); // !!!!
1941#endif
1942    return(NULL);
1943  }
1944
1945  poly m;
1946  if ( ! rIsRatGRing(currRing))
1947  {
1948    m = p_Lcm(p1, p2, si_max(lCompP1, lCompP2), r);
1949  }
1950#ifdef HAVE_RATGRING
1951  else
1952  {
1953    /* rational version */
1954    m = p_LcmRat(p1, p2, si_max(lCompP1, lCompP2), r);
1955  }
1956#endif
1957
1958//  n_Delete(&p_GetCoeff(m, r), r);
1959//  pSetCoeff0(m, NULL);
1960
1961#ifdef PDEBUG
1962//  p_Test(m,r);
1963#endif
1964
1965  return(m);
1966}
1967
1968void gnc_kBucketPolyRedOld(kBucket_pt b, poly p, number *c)
1969{
1970  // b will not be multiplied by any constant in this impl.
1971  // ==> *c=1
1972  if (c!=NULL) *c=nInit(1);
1973  poly m=pOne();
1974  pExpVectorDiff(m,kBucketGetLm(b),p);
1975  //pSetm(m);
1976#ifdef PDEBUG
1977  pTest(m);
1978#endif
1979  poly pp= nc_mm_Mult_pp(m,p,currRing);
1980  assume(pp!=NULL);
1981  pDelete(&m);
1982  number n=pGetCoeff(pp);
1983  number nn;
1984  if (!n_IsMOne(n,currRing))
1985  {
1986    nn=nNeg(nInvers(n));
1987    n=nMult(nn,pGetCoeff(kBucketGetLm(b)));
1988    nDelete(&nn);
1989    pp=p_Mult_nn(pp,n,currRing);
1990    nDelete(&n);
1991  }
1992  else
1993  {
1994    pp=p_Mult_nn(pp,pGetCoeff(kBucketGetLm(b)),currRing);
1995  }
1996  int l=pLength(pp);
1997  kBucket_Add_q(b,pp,&l);
1998}
1999
2000void gnc_kBucketPolyRedNew(kBucket_pt b, poly p, number *c)
2001{
2002#ifdef PDEBUG
2003//   Print(">*");
2004#endif
2005
2006#ifdef KDEBUG
2007  if( !kbTest(b) )Werror("nc_kBucketPolyRed: broken bucket!");
2008#endif
2009
2010#ifdef PDEBUG
2011  pTest(p);
2012#if MYTEST
2013  Print("p: "); pWrite(p);
2014#endif
2015#endif
2016
2017  // b will not be multiplied by any constant in this impl.
2018  // ==> *c=1
2019  if (c!=NULL) *c=nInit(1);
2020  poly m = pOne();
2021  const poly pLmB = kBucketGetLm(b); // no new copy!
2022
2023  assume( pLmB != NULL );
2024
2025#ifdef PDEBUG
2026  pTest(pLmB);
2027
2028#if MYTEST
2029  Print("pLmB: "); pWrite(pLmB);
2030#endif
2031#endif
2032
2033  pExpVectorDiff(m, pLmB, p);
2034  //pSetm(m);
2035
2036#ifdef PDEBUG
2037  pTest(m);
2038#if MYTEST
2039  Print("m: "); pWrite(m);
2040#endif
2041#endif
2042
2043  poly pp = nc_mm_Mult_pp(m, p, currRing);
2044  pDelete(&m);
2045
2046  assume( pp != NULL );
2047  const number n = pGetCoeff(pp); // bug!
2048
2049  if (!n_IsMOne(n,currRing) ) // does this improve performance??!? also see below... // TODO: check later on.
2050  // if n == -1 => nn = 1 and -1/n
2051  {
2052    number nn=nNeg(nInvers(n));
2053    number t = nMult(nn,pGetCoeff(pLmB));
2054    nDelete(&nn);
2055    pp = p_Mult_nn(pp,t,currRing);
2056    nDelete(&t);
2057  }
2058  else
2059  {
2060    pp = p_Mult_nn(pp,pGetCoeff(pLmB),currRing);
2061  }
2062
2063  int l = pLength(pp);
2064
2065#ifdef PDEBUG
2066  pTest(pp);
2067//   Print("PP: "); pWrite(pp);
2068#endif
2069
2070  kBucket_Add_q(b,pp,&l);
2071
2072
2073#ifdef PDEBUG
2074//   Print("*>");
2075#endif
2076}
2077
2078
2079void gnc_kBucketPolyRed_ZOld(kBucket_pt b, poly p, number *c)
2080{
2081  // b is multiplied by a constant in this impl.
2082  number ctmp;
2083  poly m=pOne();
2084  pExpVectorDiff(m,kBucketGetLm(b),p);
2085  //pSetm(m);
2086#ifdef PDEBUG
2087  pTest(m);
2088#endif
2089  if(p_IsConstant(m,currRing))
2090  {
2091    pDelete(&m);
2092    ctmp = kBucketPolyRed(b,p,pLength(p),NULL);
2093  }
2094  else
2095  {
2096    poly pp = nc_mm_Mult_pp(m,p,currRing);
2097    number c2,cc;
2098    p_Cleardenom_n(pp,currRing,c2);
2099    pDelete(&m);
2100    ctmp = kBucketPolyRed(b,pp,pLength(pp),NULL);
2101    //cc=*c;
2102    //*c=nMult(*c,c2);
2103    nDelete(&c2);
2104    //nDelete(&cc);
2105    pDelete(&pp);
2106  }
2107  if (c!=NULL) *c=ctmp;
2108  else nDelete(&ctmp);
2109}
2110
2111void gnc_kBucketPolyRed_ZNew(kBucket_pt b, poly p, number *c)
2112{
2113  // b is multiplied by a constant in this impl.
2114  number ctmp;
2115  poly m=pOne();
2116  pExpVectorDiff(m,kBucketGetLm(b),p);
2117  //pSetm(m);
2118#ifdef PDEBUG
2119  pTest(m);
2120#endif
2121
2122  if(p_IsConstant(m,currRing))
2123  {
2124    pDelete(&m);
2125    ctmp = kBucketPolyRed(b,p,pLength(p),NULL);
2126  }
2127  else
2128  {
2129    poly pp = nc_mm_Mult_pp(m,p,currRing);
2130    number c2,cc;
2131    p_Cleardenom_n(pp,currRing,c2);
2132    pDelete(&m);
2133    ctmp = kBucketPolyRed(b,pp,pLength(pp),NULL);
2134    //cc=*c;
2135    //*c=nMult(*c,c2);
2136    nDelete(&c2);
2137    //nDelete(&cc);
2138    pDelete(&pp);
2139  }
2140  if (c!=NULL) *c=ctmp;
2141  else nDelete(&ctmp);
2142}
2143
2144
2145inline void nc_PolyPolyRedOld(poly &b, poly p, number *c)
2146  // reduces b with p, do not delete both
2147{
2148  // b will not by multiplied by any constant in this impl.
2149  // ==> *c=1
2150  if (c!=NULL) *c=nInit(1);
2151  poly m=pOne();
2152  pExpVectorDiff(m,pHead(b),p);
2153  //pSetm(m);
2154#ifdef PDEBUG
2155  pTest(m);
2156#endif
2157  poly pp=nc_mm_Mult_pp(m,p,currRing);
2158  assume(pp!=NULL);
2159
2160  pDelete(&m);
2161  number n=pGetCoeff(pp);
2162  number nn;
2163  if (!nIsMOne(n))
2164  {
2165    nn=nNeg(nInvers(n));
2166    n=nMult(nn,pGetCoeff(b));
2167    nDelete(&nn);
2168    pp=p_Mult_nn(pp,n,currRing);
2169    nDelete(&n);
2170  }
2171  else
2172  {
2173    pp=p_Mult_nn(pp,pGetCoeff(b),currRing);
2174  }
2175  b=p_Add_q(b,pp,currRing);
2176}
2177
2178
2179inline void nc_PolyPolyRedNew(poly &b, poly p, number *c)
2180  // reduces b with p, do not delete both
2181{
2182#ifdef PDEBUG
2183  pTest(b);
2184  pTest(p);
2185#endif
2186
2187#if MYTEST
2188  PrintS("nc_PolyPolyRedNew(");
2189  pWrite0(b);
2190  PrintS(", ");
2191  pWrite0(p);
2192  PrintS(", *c): ");
2193#endif
2194
2195  // b will not by multiplied by any constant in this impl.
2196  // ==> *c=1
2197  if (c!=NULL) *c=nInit(1);
2198
2199  poly pp = NULL;
2200
2201  // there is a problem when p is a square(=>0!)
2202
2203  while((b != NULL) && (pp == NULL))
2204  {
2205
2206//    poly pLmB = pHead(b);
2207    poly m = pOne();
2208    pExpVectorDiff(m, b, p);
2209//    pDelete(&pLmB);
2210  //pSetm(m);
2211
2212#ifdef PDEBUG
2213    pTest(m);
2214    pTest(b);
2215#endif
2216
2217    pp = nc_mm_Mult_pp(m, p, currRing);
2218
2219#if MYTEST
2220    PrintS("\n{b': ");
2221    pWrite0(b);
2222    PrintS(", m: ");
2223    pWrite0(m);
2224    PrintS(", pp: ");
2225    pWrite0(pp);
2226    PrintS(" }\n");
2227#endif
2228
2229    pDelete(&m); // one m for all tries!
2230
2231//    assume( pp != NULL );
2232
2233    if( pp == NULL )
2234    {
2235      b = p_LmDeleteAndNext(b, currRing);
2236
2237      if( !p_DivisibleBy(p, b, currRing) )
2238        return;
2239
2240    }
2241  }
2242
2243#if MYTEST
2244  PrintS("{b': ");
2245  pWrite0(b);
2246  PrintS(", pp: ");
2247  pWrite0(pp);
2248  PrintS(" }\n");
2249#endif
2250
2251
2252  if(b == NULL) return;
2253
2254
2255  assume(pp != NULL);
2256
2257  const number n = pGetCoeff(pp); // no new copy
2258
2259  number nn;
2260
2261  if (!n_IsMOne(n, currRing)) // TODO: as above.
2262  {
2263    nn=nNeg(nInvers(n));
2264    number t = nMult(nn, pGetCoeff(b));
2265    nDelete(&nn);
2266    pp=p_Mult_nn(pp, t, currRing);
2267    nDelete(&t);
2268  }
2269  else
2270  {
2271    pp=p_Mult_nn(pp, pGetCoeff(b), currRing);
2272  }
2273
2274
2275  b=p_Add_q(b,pp,currRing);
2276
2277}
2278
2279void nc_PolyPolyRed(poly &b, poly p, number *c)
2280{
2281#if 0
2282  nc_PolyPolyRedOld(b, p, c);
2283#else
2284  nc_PolyPolyRedNew(b, p, c);
2285#endif
2286}
2287
2288
2289poly nc_mm_Bracket_nn(poly m1, poly m2);
2290
2291poly nc_p_Bracket_qq(poly p, const poly q)
2292  /* returns [p,q], destroys p */
2293{
2294  assume(p != NULL && q!= NULL);
2295
2296  if (!rIsPluralRing(currRing)) return(NULL);
2297  if (pComparePolys(p,q)) return(NULL);
2298  /* Components !? */
2299  poly Q=NULL;
2300  number coef=NULL;
2301  poly res=NULL;
2302  poly pres=NULL;
2303  int UseBuckets=1;
2304  if ((pLength(p)< MIN_LENGTH_BUCKET/2) && (pLength(q)< MIN_LENGTH_BUCKET/2) || TEST_OPT_NOT_BUCKETS) UseBuckets=0;
2305
2306
2307  CPolynomialSummator sum(currRing, UseBuckets == 0);
2308
2309  while (p!=NULL)
2310  {
2311    Q=q;
2312    while(Q!=NULL)
2313    {
2314      pres=nc_mm_Bracket_nn(p,Q); /* since no coeffs are taken into account there */
2315      if (pres!=NULL)
2316      {
2317        coef = nMult(pGetCoeff(p),pGetCoeff(Q));
2318        pres = p_Mult_nn(pres,coef,currRing);
2319
2320        sum += pres;
2321        nDelete(&coef);
2322      }
2323      pIter(Q);
2324    }
2325    p=pLmDeleteAndNext(p);
2326  }
2327  return(sum);
2328}
2329
2330poly nc_mm_Bracket_nn(poly m1, poly m2)
2331  /*returns [m1,m2] for two monoms, destroys nothing */
2332  /* without coeffs */
2333{
2334  if (pLmIsConstant(m1) || pLmIsConstant(m1)) return(NULL);
2335  if (pLmCmp(m1,m2)==0) return(NULL);
2336  int rN=currRing->N;
2337  int *M1=(int *)omAlloc0((rN+1)*sizeof(int));
2338  int *M2=(int *)omAlloc0((rN+1)*sizeof(int));
2339  int *PREFIX=(int *)omAlloc0((rN+1)*sizeof(int));
2340  int *SUFFIX=(int *)omAlloc0((rN+1)*sizeof(int));
2341  pGetExpV(m1,M1);
2342  pGetExpV(m2,M2);
2343  poly res=NULL;
2344  poly ares=NULL;
2345  poly bres=NULL;
2346  poly prefix=NULL;
2347  poly suffix=NULL;
2348  int nMin,nMax;
2349  number nTmp=NULL;
2350  int i,j,k;
2351  for (i=1;i<=rN;i++)
2352  {
2353    if (M2[i]!=0)
2354    {
2355      ares=NULL;
2356      for (j=1;j<=rN;j++)
2357      {
2358        if (M1[j]!=0)
2359        {
2360          bres=NULL;
2361          /* compute [ x_j^M1[j],x_i^M2[i] ] */
2362          if (i<j) {nMax=j;  nMin=i;} else {nMax=i;  nMin=j;}
2363          if ( (i==j) || ((MATELEM(currRing->GetNC()->COM,nMin,nMax)!=NULL) && nIsOne(pGetCoeff(MATELEM(currRing->GetNC()->C,nMin,nMax))) )) /* not (the same exp. or commuting exps)*/
2364          { bres=NULL; }
2365          else
2366          {
2367            if (i<j) { bres=gnc_uu_Mult_ww(j,M1[j],i,M2[i],currRing); }
2368            else bres=gnc_uu_Mult_ww(i,M2[i],j,M1[j],currRing);
2369            if (nIsOne(pGetCoeff(bres)))
2370            {
2371              bres=pLmDeleteAndNext(bres);
2372            }
2373            else
2374            {
2375              nTmp=nSub(pGetCoeff(bres),nInit(1));
2376              pSetCoeff(bres,nTmp); /* only lc ! */
2377            }
2378#ifdef PDEBUG
2379            pTest(bres);
2380#endif
2381            if (i>j)  bres=p_Neg(bres, currRing);
2382          }
2383          if (bres!=NULL)
2384          {
2385            /* now mult (prefix, bres, suffix) */
2386            memcpy(SUFFIX, M1,(rN+1)*sizeof(int));
2387            memcpy(PREFIX, M1,(rN+1)*sizeof(int));
2388            for (k=1;k<=j;k++) SUFFIX[k]=0;
2389            for (k=j;k<=rN;k++) PREFIX[k]=0;
2390            SUFFIX[0]=0;
2391            PREFIX[0]=0;
2392            prefix=pOne();
2393            suffix=pOne();
2394            pSetExpV(prefix,PREFIX);
2395            pSetm(prefix);
2396            pSetExpV(suffix,SUFFIX);
2397            pSetm(suffix);
2398            if (!pLmIsConstant(prefix)) bres = gnc_mm_Mult_p(prefix, bres,currRing);
2399            if (!pLmIsConstant(suffix)) bres = gnc_p_Mult_mm(bres, suffix,currRing);
2400            ares=p_Add_q(ares, bres,currRing);
2401            /* What to give free? */
2402        /* Do we have to free PREFIX/SUFFIX? it seems so */
2403            pDelete(&prefix);
2404            pDelete(&suffix);
2405          }
2406        }
2407      }
2408      if (ares!=NULL)
2409      {
2410        /* now mult (prefix, bres, suffix) */
2411        memcpy(SUFFIX, M2,(rN+1)*sizeof(int));
2412        memcpy(PREFIX, M2,(rN+1)*sizeof(int));
2413        for (k=1;k<=i;k++) SUFFIX[k]=0;
2414        for (k=i;k<=rN;k++) PREFIX[k]=0;
2415        SUFFIX[0]=0;
2416        PREFIX[0]=0;
2417        prefix=pOne();
2418        suffix=pOne();
2419        pSetExpV(prefix,PREFIX);
2420        pSetm(prefix);
2421        pSetExpV(suffix,SUFFIX);
2422        pSetm(suffix);
2423        bres=ares;
2424        if (!pLmIsConstant(prefix)) bres = gnc_mm_Mult_p(prefix, bres,currRing);
2425        if (!pLmIsConstant(suffix)) bres = gnc_p_Mult_mm(bres, suffix,currRing);
2426        res=p_Add_q(res, bres,currRing);
2427        pDelete(&prefix);
2428        pDelete(&suffix);
2429      }
2430    }
2431  }
2432  freeT(M1, rN);
2433  freeT(M2, rN);
2434  freeT(PREFIX, rN);
2435  freeT(SUFFIX, rN);
2436  pTest(res);
2437  return(res);
2438}
2439
2440ideal twostd(ideal I) // works in currRing only!
2441{
2442  ideal J = kStd(I, currQuotient, testHomog, NULL, NULL, 0, 0, NULL); // in currRing!!!
2443  idSkipZeroes(J); // ring independent!
2444
2445  const int rN = currRing->N;
2446
2447  loop
2448  {
2449    ideal     K    = NULL;
2450    const int s    = idElem(J); // ring independent
2451
2452    for(int i = 0; i < s; i++)
2453    {
2454      const poly p = J->m[i];
2455
2456#ifdef PDEBUG
2457      p_Test(p, currRing);
2458#if 0
2459      Print("p: "); // !
2460      p_Write(p, currRing);
2461#endif
2462#endif
2463
2464      for (int j = 1; j <= rN; j++) // for all j = 1..N
2465      {
2466        poly varj = p_One( currRing);
2467        p_SetExp(varj, j, 1, currRing);
2468        p_Setm(varj, currRing);
2469
2470        poly q = pp_Mult_mm(p, varj, currRing); // q = J[i] * var(j),
2471
2472#ifdef PDEBUG
2473        p_Test(varj, currRing);
2474        p_Test(p, currRing);
2475        p_Test(q, currRing);
2476#if 0
2477        Print("Reducing p: "); // !
2478        p_Write(p, currRing);
2479        Print("With q: "); // !
2480        p_Write(q, currRing);
2481#endif
2482#endif
2483
2484        p_Delete(&varj, currRing);
2485
2486        if (q != NULL)
2487        {
2488#ifdef PDEBUG
2489#if 0
2490          Print("Reducing q[j = %d]: ", j); // !
2491          p_Write(q, currRing);
2492
2493          Print("With p:");
2494          p_Write(p, currRing);
2495
2496#endif
2497#endif
2498
2499          // bug: lm(p) may not divide lm(p * var(i)) in a SCA!
2500          if( p_LmDivisibleBy(p, q, currRing) )
2501            q = nc_ReduceSpoly(p, q, currRing);
2502
2503
2504#ifdef PDEBUG
2505          p_Test(q, currRing);
2506#if 0
2507          Print("reductum q/p: ");
2508          p_Write(q, currRing);
2509
2510          // Print("With J!\n");
2511#endif
2512#endif
2513
2514//          if( q != NULL)
2515          q = kNF(J, currQuotient, q, 0, KSTD_NF_NONORM); // in currRing!!!
2516
2517#ifdef PDEBUG
2518          p_Test(q, currRing);
2519#if 0
2520          Print("NF(J/currQuotient)=> q: "); // !
2521          p_Write(q, currRing);
2522#endif
2523#endif
2524          if (q!=NULL)
2525          {
2526            if (p_IsConstant(q, currRing)) // => return (1)!
2527            {
2528              p_Delete(&q, currRing);
2529              id_Delete(&J, currRing);
2530
2531              if (K != NULL)
2532                id_Delete(&K, currRing);
2533
2534              ideal Q = idInit(1,1); // ring independent!
2535              Q->m[0] = p_One(currRing);
2536
2537              return(Q);
2538            }
2539
2540//            flag = false;
2541
2542            // K += q:
2543
2544            ideal Q = idInit(1,1); // ring independent
2545            Q->m[0]=q;
2546
2547            if( K == NULL )
2548              K = Q;
2549            else
2550            {
2551              ideal id_tmp = idSimpleAdd(K, Q); // in currRing
2552              id_Delete(&K, currRing);
2553              id_Delete(&Q, currRing);
2554              K = id_tmp; // K += Q
2555            }
2556          }
2557
2558
2559        } // if q != NULL
2560      } // for all variables
2561
2562    }
2563
2564    if (K == NULL) // nothing new: i.e. all elements are two-sided
2565      return(J);
2566    /* now we update GrBasis J with K */
2567    //    iSize=IDELEMS(J);
2568#ifdef PDEBUG
2569    idTest(J); // in currRing!
2570#if 0
2571    Print("J:");
2572    idPrint(J);
2573    PrintLn();
2574#endif // debug
2575#endif
2576
2577
2578
2579#ifdef PDEBUG
2580    idTest(K); // in currRing!
2581#if 0
2582    Print("+K:");
2583    idPrint(K);
2584    PrintLn();
2585#endif // debug
2586#endif
2587
2588
2589    int iSize = idElem(J); // ring independent
2590
2591    // J += K:
2592    ideal id_tmp = idSimpleAdd(J,K); // in currRing
2593    id_Delete(&K, currRing); id_Delete(&J, currRing);
2594
2595#if 1
2596    BITSET save_test=test;
2597    test|=Sy_bit(OPT_SB_1); // ring independent
2598    J = kStd(id_tmp, currQuotient, testHomog, NULL, NULL, 0, iSize); // J = J + K, J - std // in currRing!
2599    test = save_test;
2600#else
2601    J=kStd(id_tmp, currQuotient,testHomog,NULL,NULL,0,0,NULL);
2602#endif
2603
2604    id_Delete(&id_tmp, currRing);
2605    idSkipZeroes(J); // ring independent
2606
2607#ifdef PDEBUG
2608    idTest(J); // in currRing!
2609#if 0
2610    Print("J:");
2611    idPrint(J);
2612    PrintLn();
2613#endif // debug
2614#endif
2615  } // loop
2616}
2617
2618
2619matrix nc_PrintMat(int a, int b, ring r, int metric)
2620  /* returns matrix with the info on noncomm multiplication */
2621{
2622
2623  if ( (a==b) || !rIsPluralRing(r) ) return(NULL);
2624  int i;
2625  int j;
2626  if (a>b) {j=b; i=a;}
2627  else {j=a; i=b;}
2628  /* i<j */
2629  int rN=r->N;
2630  int size=r->GetNC()->MTsize[UPMATELEM(i,j,rN)];
2631  matrix M = r->GetNC()->MT[UPMATELEM(i,j,rN)];
2632  /*  return(M); */
2633  int sizeofres;
2634  if (metric==0)
2635  {
2636    sizeofres=sizeof(int);
2637  }
2638  if (metric==1)
2639  {
2640    sizeofres=sizeof(number);
2641  }
2642  matrix res=mpNew(size,size);
2643  int s;
2644  int t;
2645  int length;
2646  long totdeg;
2647  poly p;
2648  for(s=1;s<=size;s++)
2649  {
2650    for(t=1;t<=size;t++)
2651    {
2652      p=MATELEM(M,s,t);
2653      if (p==NULL)
2654      {
2655        MATELEM(res,s,t)=0;
2656      }
2657      else
2658      {
2659        length = pLength(p);
2660        if (metric==0) /* length */
2661        {
2662          MATELEM(res,s,t)= p_ISet(length,r);
2663        }
2664        else if (metric==1) /* sum of deg divided by the length */
2665        {
2666          totdeg=0;
2667          while (p!=NULL)
2668          {
2669            totdeg=totdeg+pDeg(p,r);
2670            pIter(p);
2671          }
2672          number ntd = nInit(totdeg);
2673          number nln = nInit(length);
2674          number nres=nDiv(ntd,nln);
2675          nDelete(&ntd);
2676          nDelete(&nln);
2677          MATELEM(res,s,t)=p_NSet(nres,r);
2678        }
2679      }
2680    }
2681  }
2682  return(res);
2683}
2684
2685inline void nc_CleanUp(nc_struct* p)
2686{
2687  assume(p != NULL);
2688  omFreeSize((ADDRESS)p,sizeof(nc_struct));
2689}
2690
2691inline void nc_CleanUp(ring r)
2692{
2693  /* small CleanUp of r->GetNC() */
2694  assume(r != NULL);
2695  nc_CleanUp(r->GetNC());
2696  r->GetNC() = NULL;
2697}
2698
2699void nc_rKill(ring r)
2700// kills the nc extension of ring r
2701{
2702  if( r->GetNC()->GetGlobalMultiplier() != NULL )
2703  {
2704    delete r->GetNC()->GetGlobalMultiplier();
2705    r->GetNC()->GetGlobalMultiplier() = NULL;
2706  }
2707
2708  if( r->GetNC()->GetFormulaPowerMultiplier() != NULL )
2709  {
2710    delete r->GetNC()->GetFormulaPowerMultiplier();
2711    r->GetNC()->GetFormulaPowerMultiplier() = NULL;
2712  }
2713
2714
2715  int i,j;
2716  int rN=r->N;
2717  if ( rN > 1 )
2718  {
2719    for(i=1;i<rN;i++)
2720    {
2721      for(j=i+1;j<=rN;j++)
2722      {
2723        id_Delete((ideal *)&(r->GetNC()->MT[UPMATELEM(i,j,rN)]),r);
2724      }
2725    }
2726    omFreeSize((ADDRESS)r->GetNC()->MT,rN*(rN-1)/2*sizeof(matrix));
2727    omFreeSize((ADDRESS)r->GetNC()->MTsize,rN*(rN-1)/2*sizeof(int));
2728    id_Delete((ideal *)&(r->GetNC()->COM),r);
2729  }
2730  id_Delete((ideal *)&(r->GetNC()->C),r);
2731  id_Delete((ideal *)&(r->GetNC()->D),r);
2732
2733  if( rIsSCA(r) && (r->GetNC()->SCAQuotient() != NULL) )
2734  {
2735    id_Delete(&r->GetNC()->SCAQuotient(), r); // Custom SCA destructor!!!
2736  }
2737
2738
2739  nc_CleanUp(r);
2740}
2741
2742
2743////////////////////////////////////////////////////////////////////////////////////////////////
2744
2745
2746poly nc_p_CopyGet(poly a, const ring r)
2747/* for use in getting the mult. matrix elements*/
2748/* ring r must be a currRing! */
2749/* for consistency, copies a poly from the comm. r->GetNC()->basering */
2750/* to its image in NC ring */
2751{
2752  if (r != currRing)
2753  {
2754#ifdef PDEBUF
2755    Werror("nc_p_CopyGet call not in currRing");
2756#endif
2757    return(NULL);
2758  }
2759  return(p_Copy(a,r));
2760}
2761
2762poly nc_p_CopyPut(poly a, const ring r)
2763/* for use in defining the mult. matrix elements*/
2764/* ring r must be a currRing! */
2765/* for consistency, puts a polynomial from the NC ring */
2766/* to its presentation in the comm. r->GetNC()->basering */
2767{
2768  if (r != currRing)
2769  {
2770#ifdef PDEBUF
2771    Werror("nc_p_CopyGet call not in currRing");
2772#endif
2773    return(NULL);
2774  }
2775
2776  return(p_Copy(a,r));
2777}
2778
2779BOOLEAN nc_CheckSubalgebra(poly PolyVar, ring r)
2780  /* returns TRUE if there were errors */
2781  /* checks whether product of vars from PolyVar defines */
2782  /* an admissible subalgebra of r */
2783  /* r is indeed currRing and assumed to be noncomm. */
2784{
2785  ring save = currRing;
2786  int WeChangeRing = 0;
2787  if (currRing != r)
2788  {
2789    rChangeCurrRing(r);
2790    WeChangeRing = 1;
2791  }
2792  int rN=r->N;
2793  int *ExpVar=(int*)omAlloc0((rN+1)*sizeof(int));
2794  int *ExpTmp=(int*)omAlloc0((rN+1)*sizeof(int));
2795  p_GetExpV(PolyVar, ExpVar, r);
2796  int i; int j; int k;
2797  poly test=NULL;
2798  int OK=1;
2799  for (i=1; i<rN; i++)
2800  {
2801    if (ExpVar[i]==0) /* i.e. not in PolyVar */
2802    {
2803      for (j=i+1; j<=rN; j++)
2804      {
2805        if (ExpVar[j]==0)
2806        {
2807          test = MATELEM(r->GetNC()->D,i,j);
2808          while (test!=NULL)
2809          {
2810            p_GetExpV(test, ExpTmp, r);
2811            OK=1;
2812            for (k=1;k<=rN;k++)
2813            {
2814              if (ExpTmp[k]!=0)
2815              {
2816                if (ExpVar[k]!=0) OK=0;
2817              }
2818            }
2819            if (!OK)
2820            {
2821              if ( WeChangeRing )
2822                rChangeCurrRing(save);
2823              return(TRUE);
2824            }
2825            pIter(test);
2826          }
2827        }
2828      }
2829    }
2830  }
2831  freeT(ExpVar,rN);
2832  freeT(ExpTmp,rN);
2833  if ( WeChangeRing )
2834    rChangeCurrRing(save);
2835  return(FALSE);
2836}
2837
2838
2839BOOLEAN gnc_CheckOrdCondition(matrix D, ring r)
2840/* returns TRUE if there were errors */
2841/* checks whether the current ordering */
2842/* is admissible for r and D == r->GetNC()->D */
2843/* to be executed in a currRing */
2844{
2845  /* analyze D: an upper triangular matrix of polys */
2846  /* check the ordering condition for D */
2847  ring save = currRing;
2848  int WeChangeRing = 0;
2849  if (r != currRing)
2850  {
2851    rChangeCurrRing(r);
2852    WeChangeRing = 1;
2853  }
2854  poly p,q;
2855  int i,j;
2856  int report = 0;
2857  for(i=1; i<r->N; i++)
2858  {
2859    for(j=i+1; j<=r->N; j++)
2860    {
2861      p = nc_p_CopyGet(MATELEM(D,i,j),r);
2862      if ( p != NULL)
2863      {
2864    q = p_One(r); // replaces pOne();
2865    p_SetExp(q,i,1,r);
2866    p_SetExp(q,j,1,r);
2867    p_Setm(q,r);
2868    if (p_LmCmp(q,p,r) != 1) /* i.e. lm(p)==xy < lm(q)==D_ij  */
2869    {
2870      Werror("Bad ordering at %d,%d\n",i,j);
2871#if 0 /*Singularg should not differ from Singular except in error case*/
2872      p_Write(p,r);
2873      p_Write(q,r);
2874#endif
2875      report = 1;
2876    }
2877    p_Delete(&q,r);
2878    p_Delete(&p,r);
2879    p = NULL;
2880      }
2881    }
2882  }
2883  if ( WeChangeRing )
2884    rChangeCurrRing(save);
2885  return(report);
2886}
2887
2888
2889BOOLEAN nc_CallPlural(
2890                      matrix CCC, matrix DDD,
2891                      poly CCN, poly DDN,
2892                      ring r,
2893                      bool bSetupQuotient, bool bCopyInput, bool bBeQuiet,
2894                      ring curr, BOOLEAN dummy_ring /*=FALSE*/)
2895  /* returns TRUE if there were errors */
2896  /* analyze inputs, check them for consistency */
2897  /* detects nc_type, DO NOT initialize multiplication but call for it at the end*/
2898  /* checks the ordering condition and evtl. NDC */
2899// NOTE: all the data belong to the curr,
2900// we change r which may be the same ring, and must have the same representation!
2901{
2902  assume( r->qideal == NULL ); // The basering must NOT be a qring!
2903
2904//  assume( curr != r );
2905  assume( rSamePolyRep(r, curr) );
2906
2907
2908  if( r->N == 1 ) // clearly commutative!!!
2909  {
2910    assume(
2911           ( (CCC != NULL) && (MATCOLS(CCC) == 1) && (MATROWS(CCC) == 1) && (MATELEM(CCC,1,1) == NULL) ) ||
2912           ( (CCN == NULL) )
2913          );
2914
2915    assume(
2916           ( (DDD != NULL) && (MATCOLS(DDD) == 1) && (MATROWS(DDD) == 1) && (MATELEM(DDD,1,1) == NULL) ) ||
2917           ( (DDN == NULL) )
2918          );
2919    if(!dummy_ring)
2920    {
2921      WarnS("commutative ring with 1 variable");
2922      return FALSE;
2923    }
2924  }
2925
2926  // there must be:
2927  assume( (CCC != NULL) != (CCN != NULL) ); // exactly one data about coeffs (C).
2928  assume( !((DDD != NULL) && (DDN != NULL)) ); // at most one data about tails (D).
2929
2930  ring save = currRing;
2931
2932  if( save != curr )
2933    rChangeCurrRing(curr);
2934
2935#if OUTPUT
2936  if( CCC != NULL )
2937  {
2938    PrintS("nc_CallPlural(), Input data, CCC: \n");
2939    iiWriteMatrix(CCC, "C", 2, 4);
2940  }
2941  if( DDD != NULL )
2942  {
2943    PrintS("nc_CallPlural(), Input data, DDD: \n");
2944    iiWriteMatrix(DDD, "D", 2, 4);
2945  }
2946#endif
2947
2948
2949#ifndef NDEBUG
2950  idTest((ideal)CCC);
2951  idTest((ideal)DDD);
2952  pTest(CCN);
2953  pTest(DDN);
2954#endif
2955
2956  if( (!bBeQuiet) && (r->GetNC() != NULL) )
2957    WarnS("going to redefine the algebra structure");
2958
2959  if( currRing != r )
2960    rChangeCurrRing(r);
2961
2962#ifndef NDEBUG
2963  idTest((ideal)CCC);
2964  idTest((ideal)DDD);
2965  pTest(CCN);
2966  pTest(DDN);
2967#endif
2968
2969  matrix CC = NULL;
2970  poly CN = NULL;
2971  matrix C; bool bCnew = false;
2972
2973  matrix DD = NULL;
2974  poly DN = NULL;
2975  matrix D; bool bDnew = false;
2976
2977  number nN, pN, qN;
2978
2979  bool IsSkewConstant = false, tmpIsSkewConstant;
2980  int i, j;
2981
2982  nc_type nctype = nc_undef;
2983
2984  //////////////////////////////////////////////////////////////////
2985  // check the correctness of arguments, without any real chagnes!!!
2986
2987
2988
2989  // check C
2990  if ((CCC != NULL) && ( (MATCOLS(CCC)==1) || MATROWS(CCC)==1 ) )
2991  {
2992    CN = MATELEM(CCC,1,1);
2993  }
2994  else
2995  {
2996    if ((CCC != NULL) && ( (MATCOLS(CCC)!=r->N) || (MATROWS(CCC)!=r->N) ))
2997    {
2998      Werror("Square %d x %d  matrix expected", r->N, r->N);
2999
3000      if( currRing != save )
3001        rChangeCurrRing(save);
3002      return TRUE;
3003    }
3004  }
3005  if (( CCC != NULL) && (CC == NULL)) CC = CCC; // mpCopy(CCC); // bug!?
3006  if (( CCN != NULL) && (CN == NULL)) CN = CCN;
3007
3008  // check D
3009  if ((DDD != NULL) && ( (MATCOLS(DDD)==1) || MATROWS(DDD)==1 ) )
3010  {
3011    DN = MATELEM(DDD,1,1);
3012  }
3013  else
3014  {
3015    if ((DDD != NULL) && ( (MATCOLS(DDD)!=r->N) || (MATROWS(DDD)!=r->N) ))
3016    {
3017      Werror("Square %d x %d  matrix expected",r->N,r->N);
3018
3019      if( currRing != save )
3020        rChangeCurrRing(save);
3021      return TRUE;
3022    }
3023  }
3024
3025  if (( DDD != NULL) && (DD == NULL)) DD = DDD; // mpCopy(DDD); // ???
3026  if (( DDN != NULL) && (DN == NULL)) DN = DDN;
3027
3028  // further checks and some analysis:
3029  // all data in 'curr'!
3030  if (CN != NULL)       /* create matrix C = CN * Id */
3031  {
3032    nN = p_GetCoeff(CN, curr);
3033    if (n_IsZero(nN, curr))
3034    {
3035      Werror("Incorrect input : zero coefficients are not allowed");
3036
3037      if( currRing != save )
3038        rChangeCurrRing(save);
3039      return TRUE;
3040    }
3041
3042    if (n_IsOne(nN, curr))
3043      nctype = nc_lie;
3044    else
3045      nctype = nc_general;
3046
3047    IsSkewConstant = true;
3048
3049    C = mpNew(r->N,r->N); // ring independent!
3050    bCnew = true;
3051
3052    for(i=1; i<r->N; i++)
3053      for(j=i+1; j<=r->N; j++)
3054        MATELEM(C,i,j) = prCopyR_NoSort(CN, curr, r); // nc_p_CopyPut(CN, r); // copy CN from curr into r
3055  } else
3056  if ( (CN == NULL) && (CC != NULL) ) /* copy matrix C */
3057  {
3058    /* analyze C */
3059
3060    pN = NULL; /* check the consistency later */
3061
3062    if( r->N > 1 )
3063      if ( MATELEM(CC,1,2) != NULL )
3064        pN = p_GetCoeff(MATELEM(CC,1,2), curr);
3065
3066    tmpIsSkewConstant = true;
3067
3068    for(i=1; i<r->N; i++)
3069      for(j=i+1; j<=r->N; j++)
3070      {
3071        if (MATELEM(CC,i,j) == NULL)
3072          qN = NULL;
3073        else
3074          qN = p_GetCoeff(MATELEM(CC,i,j),curr);
3075
3076        if ( qN == NULL )   /* check the consistency: Cij!=0 */
3077        // find also illegal pN
3078        {
3079          Werror("Incorrect input : matrix of coefficients contains zeros in the upper triangle");
3080
3081        if( currRing != save )
3082            rChangeCurrRing(save);
3083          return TRUE;
3084        }
3085
3086        if (!n_Equal(pN, qN, curr)) tmpIsSkewConstant = false;
3087      }
3088
3089    if( bCopyInput )
3090    {
3091      C = mpCopy(CC, curr, r); // Copy C into r!!!???
3092      bCnew = true;
3093    }
3094    else
3095      C = CC;
3096
3097    IsSkewConstant = tmpIsSkewConstant;
3098
3099    if ( tmpIsSkewConstant && n_IsOne(pN, curr) )
3100      nctype = nc_lie;
3101    else
3102      nctype = nc_general;
3103  }
3104
3105  /* initialition of the matrix D */
3106  if ( DD == NULL ) /* we treat DN only (it could also be NULL) */
3107  {
3108    D = mpNew(r->N,r->N); bDnew = true;
3109
3110    if (DN  == NULL)
3111    {
3112      if ( (nctype == nc_lie) || (nctype == nc_undef) )
3113        nctype = nc_comm; /* it was nc_skew earlier */
3114      else /* nc_general, nc_skew */
3115        nctype = nc_skew;
3116    }
3117    else /* DN  != NULL */
3118      for(i=1; i<r->N; i++)
3119        for(j=i+1; j<=r->N; j++)
3120          MATELEM(D,i,j) = prCopyR_NoSort(DN, curr, r); // project DN into r->GetNC()->basering!
3121  }
3122  else /* DD != NULL */
3123  {
3124    bool b = true; // DD == null ?
3125
3126    for(int i = 1; (i < r->N) && b; i++)
3127    for(int j = i+1; (j <= r->N) && b; j++)
3128      if (MATELEM(DD, i, j) != NULL)
3129      {
3130        b = false;
3131        break;
3132      }
3133
3134    if (b) // D == NULL!!!
3135    {
3136      if ( (nctype == nc_lie) || (nctype == nc_undef) )
3137        nctype = nc_comm; /* it was nc_skew earlier */
3138      else /* nc_general, nc_skew */
3139        nctype = nc_skew;
3140    }
3141
3142    if( bCopyInput )
3143    {
3144      D = mpCopy(DD, curr, r); // Copy DD into r!!!
3145      bDnew = true;
3146    }
3147    else
3148      D = DD;
3149
3150  }
3151
3152  assume( C != NULL );
3153  assume( D != NULL );
3154
3155#if OUTPUT
3156  PrintS("nc_CallPlural(), Computed data, C: \n");
3157  iiWriteMatrix(C, "C", 2, 4);
3158
3159  PrintS("nc_CallPlural(), Computed data, D: \n");
3160  iiWriteMatrix(D, "D", 2, 4);
3161
3162  Print("\nTemporary: type = %d, IsSkewConstant = %d\n", nctype, IsSkewConstant);
3163#endif
3164
3165
3166
3167
3168  // check the ordering condition for D (both matrix and poly cases):
3169  if ( gnc_CheckOrdCondition(D, r) )
3170  {
3171    if( bCnew ) mpDelete( &C, r );
3172    if( bDnew ) mpDelete( &D, r );
3173
3174    Werror("Matrix of polynomials violates the ordering condition");
3175
3176    if( currRing != save )
3177      rChangeCurrRing(save);
3178    return TRUE;
3179  }
3180
3181  // okay now we are ready for this!!!
3182
3183  // create new non-commutative structure
3184  nc_struct *nc_new = (nc_struct *)omAlloc0(sizeof(nc_struct));
3185
3186  ncRingType(nc_new, nctype);
3187
3188  nc_new->C = C; // if C and D were given by matrices at the beginning they are in r
3189  nc_new->D = D; // otherwise they should be in r->GetNC()->basering(polynomial * Id_{N})
3190
3191  nc_new->IsSkewConstant = (IsSkewConstant?1:0);
3192
3193  // Setup new NC structure!!!
3194  if (r->GetNC() != NULL)
3195    nc_rKill(r);
3196
3197  r->GetNC() = nc_new;
3198
3199  if( currRing != save )
3200    rChangeCurrRing(save);
3201
3202  return gnc_InitMultiplication(r, bSetupQuotient);
3203}
3204
3205//////////////////////////////////////////////////////////////////////////////
3206
3207bool nc_rCopy(ring res, const ring r, bool bSetupQuotient)
3208{
3209  if (nc_CallPlural(r->GetNC()->C, r->GetNC()->D, NULL, NULL, res, bSetupQuotient, true, true, r))
3210  {
3211    WarnS("Error occured while coping/setuping the NC structure!"); // No reaction!???
3212    return true; // error
3213  }
3214
3215  return false;
3216}
3217
3218//////////////////////////////////////////////////////////////////////////////
3219BOOLEAN gnc_InitMultiplication(ring r, bool bSetupQuotient)
3220{
3221  /* returns TRUE if there were errors */
3222  /* initialize the multiplication: */
3223  /*  r->GetNC()->MTsize, r->GetNC()->MT, r->GetNC()->COM, */
3224  /* and r->GetNC()->IsSkewConstant for the skew case */
3225  if (rVar(r)==1)
3226  {
3227    ncRingType(r, nc_comm);
3228    r->GetNC()->IsSkewConstant=1;
3229    return FALSE;
3230  }
3231
3232  ring save = currRing;
3233
3234  int WeChangeRing = 0;
3235  if (currRing!=r)
3236  {
3237    rChangeCurrRing(r);
3238    WeChangeRing = 1;
3239  }
3240  assume( (currRing == r)
3241       && (currRing->GetNC()!=NULL) );   // otherwise we cannot work with all these matrices!
3242
3243  int i,j;
3244  r->GetNC()->MT = (matrix *)omAlloc0((r->N*(r->N-1))/2*sizeof(matrix));
3245  r->GetNC()->MTsize = (int *)omAlloc0((r->N*(r->N-1))/2*sizeof(int));
3246  idTest(((ideal)r->GetNC()->C));
3247  matrix COM = mpCopy(r->GetNC()->C);
3248  poly p,q;
3249  short DefMTsize=7;
3250  int IsNonComm=0;
3251  int tmpIsSkewConstant;
3252
3253  for(i=1; i<r->N; i++)
3254  {
3255    for(j=i+1; j<=r->N; j++)
3256    {
3257      if ( MATELEM(r->GetNC()->D,i,j) == NULL ) /* quasicommutative case */
3258      {
3259        /* 1x1 mult.matrix */
3260        r->GetNC()->MTsize[UPMATELEM(i,j,r->N)] = 1;
3261        r->GetNC()->MT[UPMATELEM(i,j,r->N)] = mpNew(1,1);
3262      }
3263      else /* pure noncommutative case */
3264      {
3265        /* TODO check the special multiplication properties */
3266        IsNonComm = 1;
3267        p_Delete(&(MATELEM(COM,i,j)),r);
3268        //MATELEM(COM,i,j) = NULL; // done by p_Delete
3269        r->GetNC()->MTsize[UPMATELEM(i,j,r->N)] = DefMTsize; /* default sizes */
3270        r->GetNC()->MT[UPMATELEM(i,j,r->N)] = mpNew(DefMTsize, DefMTsize);
3271      }
3272      /* set MT[i,j,1,1] to c_i_j*x_i*x_j + D_i_j */
3273      p = p_One(r); /* instead of     p = pOne(); */
3274      if (MATELEM(r->GetNC()->C,i,j)!=NULL)
3275        p_SetCoeff(p,n_Copy(pGetCoeff(MATELEM(r->GetNC()->C,i,j)),r),r);
3276      p_SetExp(p,i,1,r);
3277      p_SetExp(p,j,1,r);
3278      p_Setm(p,r);
3279      p_Test(MATELEM(r->GetNC()->D,i,j),r);
3280      q =  nc_p_CopyGet(MATELEM(r->GetNC()->D,i,j),r);
3281      p = p_Add_q(p,q,r);
3282      MATELEM(r->GetNC()->MT[UPMATELEM(i,j,r->N)],1,1) = nc_p_CopyPut(p,r);
3283      p_Delete(&p,r);
3284      // p = NULL;// done by p_Delete
3285    }
3286  }
3287  if (ncRingType(r)==nc_undef)
3288  {
3289    if (IsNonComm==1)
3290    {
3291      //      assume(pN!=NULL);
3292      //      if ((tmpIsSkewConstant==1) && (nIsOne(pGetCoeff(pN)))) r->GetNC()->type=nc_lie;
3293      //      else r->GetNC()->type=nc_general;
3294    }
3295    if (IsNonComm==0)
3296    {
3297      ncRingType(r, nc_skew); /* TODO: check whether it is commutative */
3298      r->GetNC()->IsSkewConstant=tmpIsSkewConstant;
3299    }
3300  }
3301  r->GetNC()->COM=COM;
3302
3303  nc_p_ProcsSet(r, r->p_Procs);
3304
3305  if(bSetupQuotient) // Test me!!!
3306  {
3307    nc_SetupQuotient(r);
3308  }
3309
3310
3311  // ???
3312  if( bNoPluralMultiplication )
3313    ncInitSpecialPairMultiplication(r);
3314
3315
3316  if(!rIsSCA(r) && !bNoFormula)
3317    ncInitSpecialPowersMultiplication(r);
3318
3319
3320  if (save != currRing)
3321    rChangeCurrRing(save);
3322
3323  return FALSE;
3324}
3325
3326void gnc_p_ProcsSet(ring rGR, p_Procs_s* p_Procs)
3327{
3328  // "commutative"
3329  p_Procs->p_Mult_mm  = rGR->p_Procs->p_Mult_mm  = gnc_p_Mult_mm;
3330  p_Procs->pp_Mult_mm = rGR->p_Procs->pp_Mult_mm = gnc_pp_Mult_mm;
3331  p_Procs->p_Minus_mm_Mult_qq = rGR->p_Procs->p_Minus_mm_Mult_qq = NULL;
3332  // gnc_p_Minus_mm_Mult_qq_ign; // should not be used!!!???
3333
3334
3335
3336  // non-commutaitve multiplication by monomial from the left
3337  rGR->GetNC()->p_Procs.mm_Mult_p   = gnc_mm_Mult_p;
3338  rGR->GetNC()->p_Procs.mm_Mult_pp  = gnc_mm_Mult_pp;
3339
3340  rGR->GetNC()->p_Procs.GB          = gnc_gr_bba; // bba even for local case!
3341
3342//   rGR->GetNC()->p_Procs.GlobalGB    = gnc_gr_bba;
3343//   rGR->GetNC()->p_Procs.LocalGB     = gnc_gr_mora;
3344
3345
3346#if 0
3347  // Previous Plural's implementation...
3348  rGR->GetNC()->p_Procs.SPoly       = gnc_CreateSpolyOld;
3349  rGR->GetNC()->p_Procs.ReduceSPoly = gnc_ReduceSpolyOld;
3350
3351  rGR->GetNC()->p_Procs.BucketPolyRed  = gnc_kBucketPolyRedOld;
3352  rGR->GetNC()->p_Procs.BucketPolyRed_Z= gnc_kBucketPolyRed_ZOld;
3353#else
3354  // A bit cleaned up and somewhat rewritten functions...
3355  rGR->GetNC()->p_Procs.SPoly       = gnc_CreateSpolyNew;
3356  rGR->GetNC()->p_Procs.ReduceSPoly = gnc_ReduceSpolyNew;
3357
3358  rGR->GetNC()->p_Procs.BucketPolyRed  = gnc_kBucketPolyRedNew;
3359  rGR->GetNC()->p_Procs.BucketPolyRed_Z= gnc_kBucketPolyRed_ZNew;
3360#endif
3361
3362
3363
3364
3365#if 0
3366    // Old Stuff
3367    p_Procs->p_Mult_mm   = gnc_p_Mult_mm;
3368    _p_procs->p_Mult_mm  = gnc_p_Mult_mm;
3369
3370    p_Procs->pp_Mult_mm  = gnc_pp_Mult_mm;
3371    _p_procs->pp_Mult_mm = gnc_pp_Mult_mm;
3372
3373    p_Procs->p_Minus_mm_Mult_qq = NULL; // gnc_p_Minus_mm_Mult_qq_ign;
3374    _p_procs->p_Minus_mm_Mult_qq= NULL; // gnc_p_Minus_mm_Mult_qq_ign;
3375
3376    r->GetNC()->mmMultP()       = gnc_mm_Mult_p;
3377    r->GetNC()->mmMultPP()      = gnc_mm_Mult_pp;
3378
3379    r->GetNC()->GB()            = gnc_gr_bba;
3380
3381    r->GetNC()->SPoly()         = gnc_CreateSpoly;
3382    r->GetNC()->ReduceSPoly()   = gnc_ReduceSpoly;
3383
3384#endif
3385}
3386
3387
3388// set pProcs table for rGR and global variable p_Procs
3389void nc_p_ProcsSet(ring rGR, p_Procs_s* p_Procs)
3390{
3391  assume(rIsPluralRing(rGR));
3392  assume(p_Procs!=NULL);
3393
3394  gnc_p_ProcsSet(rGR, p_Procs);
3395
3396  if(rIsSCA(rGR) && ncExtensions(SCAMASK) )
3397  {
3398    sca_p_ProcsSet(rGR, p_Procs);
3399  }
3400}
3401
3402
3403
3404/* substitute the n-th variable by e in p
3405* destroy p
3406* e is not a constant
3407*/
3408poly nc_pSubst(poly p, int n, poly e)
3409{
3410  int rN=currRing->N;
3411  int *PRE = (int *)omAlloc0((rN+1)*sizeof(int));
3412  int *SUF = (int *)omAlloc0((rN+1)*sizeof(int));
3413  int i,j,pow;
3414  number C;
3415  poly suf,pre;
3416  poly res = NULL;
3417  poly out = NULL;
3418  while ( p!= NULL )
3419  {
3420    C =  pGetCoeff(p);
3421    pGetExpV(p, PRE); /* faster splitting? */
3422    pow = PRE[n]; PRE[n]=0;
3423    res = NULL;
3424    if (pow!=0)
3425    {
3426      for (i=n+1; i<=rN; i++)
3427      {
3428    SUF[i] = PRE[i];
3429    PRE[i] = 0;
3430      }
3431      res =  pPower(pCopy(e),pow);
3432      /* multiply with prefix */
3433      pre = pOne();
3434      pSetExpV(pre,PRE);
3435      pSetm(pre);
3436      res = nc_mm_Mult_p(pre,res,currRing);
3437      /* multiply with suffix */
3438      suf = pOne();
3439      pSetExpV(suf,SUF);
3440      pSetm(suf);
3441      res = p_Mult_mm(res,suf,currRing);
3442      res = p_Mult_nn(res,C,currRing);
3443      pSetComp(res,PRE[0]);
3444    }
3445    else /* pow==0 */
3446    {
3447      res = pHead(p);
3448    }
3449    p   = pLmDeleteAndNext(p);
3450    out = pAdd(out,res);
3451  }
3452  freeT(PRE,rN);
3453  freeT(SUF,rN);
3454  return(out);
3455}
3456
3457static ideal idPrepareStd(ideal T, ideal s,  int k)
3458{
3459  /* T is a left SB, without zeros, s is a list with zeros */
3460#ifdef PDEBUG
3461  if (IDELEMS(s)!=IDELEMS(T))
3462  {
3463    Print("ideals of diff. size!!!");
3464  }
3465#endif
3466  ideal t = idCopy(T);
3467  int j,rs=idRankFreeModule(s),rt=idRankFreeModule(t);
3468  poly p,q;
3469
3470  ideal res = idInit(2*idElem(t),1+idElem(t));
3471  if (rs == 0)
3472  {
3473    for (j=0; j<IDELEMS(t); j++)
3474    {
3475      if (s->m[j]!=NULL) pSetCompP(s->m[j],1);
3476      if (t->m[j]!=NULL) pSetCompP(t->m[j],1);
3477    }
3478    k = si_max(k,1);
3479  }
3480  for (j=0; j<IDELEMS(t); j++)
3481  {
3482    if (s->m[j]!=NULL)
3483    {
3484      p = s->m[j];
3485      q = pOne();
3486      pSetComp(q,k+1+j);
3487      pSetmComp(q);
3488#if 0
3489      while (pNext(p)) pIter(p);
3490      pNext(p) = q;
3491#else
3492      p = pAdd(p,q);
3493      s->m[j] = p;
3494#ifdef PDEBUG
3495    pTest(p);
3496#endif
3497#endif
3498    }
3499  }
3500  res = idSimpleAdd(t,s);
3501  idDelete(&t);
3502  res->rank = 1+idElem(T);
3503  return(res);
3504}
3505
3506ideal Approx_Step(ideal L)
3507{
3508  int N=currRing->N;
3509  int i,j; // k=syzcomp
3510  int flag, flagcnt=0, syzcnt=0;
3511  int syzcomp = 0;
3512  int k=1; /* for ideals not modules */
3513  ideal I = kStd(L, currQuotient,testHomog,NULL,NULL,0,0,NULL);
3514  idSkipZeroes(I);
3515  ideal s_I;
3516  int idI = idElem(I);
3517  ideal trickyQuotient,s_trickyQuotient;
3518  if (currQuotient !=NULL)
3519  {
3520    trickyQuotient = idSimpleAdd(currQuotient,I);
3521  }
3522  else
3523    trickyQuotient = I;
3524  idSkipZeroes(trickyQuotient);
3525  poly *var = (poly *)omAlloc0((N+1)*sizeof(poly));
3526  //  poly *W = (poly *)omAlloc0((2*N+1)*sizeof(poly));
3527  resolvente S = (resolvente)omAlloc0((N+1)*sizeof(ideal));
3528  ideal SI, res;
3529  matrix MI;
3530  poly x=pOne();
3531  var[0]=x;
3532  ideal   h2, h3, s_h2, s_h3;
3533  poly    p,q,qq;
3534  /* init vars */
3535  for (i=1; i<=N; i++ )
3536  {
3537    x = pOne();
3538    pSetExp(x,i,1);
3539    pSetm(x);
3540    var[i]=pCopy(x);
3541  }
3542  /* init NF's */
3543  for (i=1; i<=N; i++ )
3544  {
3545    h2 = idInit(idI,1);
3546    flag = 0;
3547    for (j=0; j< idI; j++ )
3548    {
3549      q = pp_Mult_mm(I->m[j],var[i],currRing);
3550      q = kNF(I,currQuotient,q,0,0);
3551      if (q!=0)
3552      {
3553    h2->m[j]=pCopy(q);
3554    //  pShift(&(h2->m[flag]),1);
3555    flag++;
3556    pDelete(&q);
3557      }
3558      else
3559    h2->m[j]=0;
3560    }
3561    /* W[1..idElems(I)] */
3562    if (flag >0)
3563    {
3564      /* compute syzygies with values in I*/
3565      //      idSkipZeroes(h2);
3566      //      h2 = idSimpleAdd(h2,I);
3567      //      h2->rank=flag+idI+1;
3568      idTest(h2);
3569      //idShow(h2);
3570      ring orig_ring=currRing;
3571      ring syz_ring=rCurrRingAssure_SyzComp();
3572      syzcomp = 1;
3573      rSetSyzComp(syzcomp);
3574      if (orig_ring != syz_ring)
3575      {
3576        s_h2=idrCopyR_NoSort(h2,orig_ring);
3577        //  s_trickyQuotient=idrCopyR_NoSort(trickyQuotient,orig_ring);
3578        //  rDebugPrint(syz_ring);
3579        s_I=idrCopyR_NoSort(I,orig_ring);
3580      }
3581      else
3582      {
3583        s_h2 = h2;
3584        s_I  = I;
3585        //  s_trickyQuotient=trickyQuotient;
3586      }
3587      idTest(s_h2);
3588      //      idTest(s_trickyQuotient);
3589      Print(".proceeding with the variable %d\n",i);
3590      s_h3 = idPrepareStd(s_I, s_h2, 1);
3591      BITSET save_test=test;
3592      test|=Sy_bit(OPT_SB_1);
3593      idTest(s_h3);
3594      idDelete(&s_h2);
3595      s_h2=idCopy(s_h3);
3596      idDelete(&s_h3);
3597      Print("...computing Syz");
3598      s_h3 = kStd(s_h2, currQuotient,(tHomog)FALSE,NULL,NULL,syzcomp,idI);
3599      test=save_test;
3600      //idShow(s_h3);
3601      if (orig_ring != syz_ring)
3602      {
3603        idDelete(&s_h2);
3604        for (j=0; j<IDELEMS(s_h3); j++)
3605        {
3606          if (s_h3->m[j] != NULL)
3607          {
3608            if (p_MinComp(s_h3->m[j],syz_ring) > syzcomp) /* i.e. it is a syzygy */
3609              pShift(&s_h3->m[j], -syzcomp);
3610            else
3611              pDelete(&s_h3->m[j]);
3612          }
3613        }
3614        idSkipZeroes(s_h3);
3615        s_h3->rank -= syzcomp;
3616        rChangeCurrRing(orig_ring);
3617        //  s_h3 = idrMoveR_NoSort(s_h3, syz_ring);
3618        s_h3 = idrMoveR_NoSort(s_h3, syz_ring);
3619        rKill(syz_ring);
3620      }
3621      idTest(s_h3);
3622      S[syzcnt]=kStd(s_h3,currQuotient,(tHomog)FALSE,NULL,NULL);
3623      syzcnt++;
3624      idDelete(&s_h3);
3625    } /* end if flag >0 */
3626    else
3627    {
3628      flagcnt++;
3629    }
3630  }
3631  if (flagcnt == N)
3632  {
3633    Print("the input is a two--sided ideal");
3634    return(I);
3635  }
3636  if (syzcnt >0)
3637  {
3638    Print("..computing Intersect of %d modules\n",syzcnt);
3639    if (syzcnt == 1)
3640      SI = S[0];
3641    else
3642      SI = idMultSect(S, syzcnt);
3643    //idShow(SI);
3644    MI = idModule2Matrix(SI);
3645    res= idInit(MATCOLS(MI),1);
3646    for (i=1; i<= MATCOLS(MI); i++)
3647    {
3648      p = NULL;
3649      for (j=0; j< idElem(I); j++)
3650      {
3651        q = pCopy(MATELEM(MI,j+1,i));
3652        if (q!=NULL)
3653        {
3654          q = pMult(q,pCopy(I->m[j]));
3655          p = pAdd(p,q);
3656        }
3657      }
3658      res->m[i-1]=p;
3659    }
3660    Print("final std");
3661    res = kStd(res, currQuotient,testHomog,NULL,NULL,0,0,NULL);
3662    idSkipZeroes(res);
3663    return(res);
3664  }
3665  else
3666  {
3667    Print("No syzygies");
3668    return(I);
3669  }
3670}
3671
3672
3673// creates a commutative nc extension; "converts" comm.ring to a Plural ring
3674ring nc_rCreateNCcomm(ring r)
3675{
3676  if (rIsPluralRing(r)) return r;
3677
3678  matrix C = mpNew(r->N,r->N); // ring-independent!?!
3679  matrix D = mpNew(r->N,r->N);
3680
3681  for(int i=1; i<r->N; i++)
3682    for(int j=i+1; j<=r->N; j++)
3683      MATELEM(C,i,j) = p_One( r);
3684
3685  if (nc_CallPlural(C, D, NULL, NULL, r, false, true, false, currRing, TRUE)) // TODO: what about quotient ideal?
3686    WarnS("Error initializing multiplication!"); // No reaction!???
3687
3688  return r;
3689}
3690
3691poly p_CopyEmbed(poly p, ring srcRing, int shift, int par_shift)
3692  /* NOT USED ANYMORE: replaced by maFindPerm in ring.cc */
3693  /* for use with embeddings: currRing is a sum of smaller rings */
3694  /* and srcRing is one of such smaller rings */
3695  /* shift defines the position of a subring in srcRing */
3696  /* par_shift defines the position of a subfield in basefield of CurrRing */
3697{
3698  if (currRing == srcRing)
3699  {
3700    return(p_Copy(p,currRing));
3701  }
3702  nMapFunc nMap=nSetMap(srcRing);
3703  poly q;
3704  //  if ( nMap == nCopy)
3705  //  {
3706  //    q = prCopyR(p,srcRing);
3707  //  }
3708  //  else
3709  {
3710    int *perm = (int *)omAlloc0((srcRing->N+1)*sizeof(int));
3711    int *par_perm = (int *)omAlloc0((srcRing->P+1)*sizeof(int));
3712    //    int *par_perm = (int *)omAlloc0((srcRing->P+1)*sizeof(int));
3713    int i;
3714    //    if (srcRing->P > 0)
3715    //    {
3716    //      for (i=0; i<srcRing->P; i++)
3717    //  par_perm[i]=-i;
3718    //    }
3719    if ((shift<0) || (shift > currRing->N))
3720    {
3721      Werror("bad shifts in p_CopyEmbed");
3722      return(0);
3723    }
3724    for (i=1; i<= srcRing->N; i++)
3725    {
3726      perm[i] = shift+i;
3727    }
3728    q = pPermPoly(p,perm,srcRing,nMap,par_perm,srcRing->P);
3729  }
3730  return(q);
3731}
3732
3733poly pOppose(ring Rop, poly p)
3734  /* opposes a vector p from Rop to currRing */
3735{
3736  /* the simplest case:*/
3737  if (  Rop == currRing )  return(pCopy(p));
3738  /* check Rop == rOpposite(currRing) */
3739  if ( !rIsLikeOpposite(currRing, Rop) )
3740  {
3741    WarnS("an opposite ring should be used");
3742    return NULL;
3743  }
3744  /* nMapFunc nMap = nSetMap(Rop);*/
3745  /* since we know that basefields coinside! */
3746  int *perm=(int *)omAlloc0((Rop->N+1)*sizeof(int));
3747  if (!p_IsConstantPoly(p, Rop))
3748  {
3749    /* we know perm exactly */
3750    int i;
3751    for(i=1; i<=Rop->N; i++)
3752    {
3753      perm[i] = Rop->N+1-i;
3754    }
3755  }
3756  poly res = pPermPoly(p, perm, Rop, nCopy);
3757  omFreeSize((ADDRESS)perm,(Rop->N+1)*sizeof(int));
3758  return res;
3759}
3760
3761ideal idOppose(ring Rop, ideal I)
3762  /* opposes a module I from Rop to currRing */
3763{
3764  /* the simplest case:*/
3765  if ( Rop == currRing ) return idCopy(I);
3766  /* check Rop == rOpposite(currRing) */
3767  if (!rIsLikeOpposite(currRing, Rop))
3768  {
3769    WarnS("an opposite ring should be used");
3770    return NULL;
3771  }
3772  int i;
3773  ideal idOp = idInit(I->ncols, I->rank);
3774  for (i=0; i< (I->ncols)*(I->nrows); i++)
3775  {
3776    idOp->m[i] = pOppose(Rop,I->m[i]);
3777  }
3778  idTest(idOp);
3779  return idOp;
3780}
3781
3782BOOLEAN rIsLikeOpposite(ring rBase, ring rCandidate)
3783  /* checks whether rings rBase and rCandidate */
3784  /* could be opposite to each other */
3785  /* returns TRUE if it is so */
3786{
3787  /* the same basefield */
3788  int diagnose = TRUE;
3789  ring save = currRing;
3790  rChangeCurrRing(rBase);
3791  nMapFunc nMap = nSetMap(rCandidate);
3792  if (nMap != nCopy) diagnose = FALSE;
3793  rChangeCurrRing(save);
3794  /* same number of variables */
3795  if (rBase->N != rCandidate->N) diagnose = FALSE;
3796  /* nc and comm ring */
3797  if ( rIsPluralRing(rBase) != rIsPluralRing(rCandidate) ) diagnose = FALSE;
3798  /* both are qrings */
3799  /* NO CHECK, since it is used in building opposite qring */
3800  /*  if ( ((rBase->qideal != NULL) && (rCandidate->qideal == NULL)) */
3801  /*       || ((rBase->qideal == NULL) && (rCandidate->qideal != NULL)) ) */
3802  /*  diagnose = FALSE; */
3803  /* TODO: varnames are e->E etc */
3804  return diagnose;
3805}
3806
3807
3808
3809bool nc_SetupQuotient(ring rGR, const ring rG, bool bCopy)
3810{
3811  if( rGR->qideal == NULL )
3812    return false; // no quotient = no work! done!? What about factors of SCA?
3813
3814  bool ret = true;
3815  // currently only super-commutative extension deals with factors.
3816
3817  if( ncExtensions(SCAMASK)  )
3818  {
3819    bool sca_ret = sca_SetupQuotient(rGR, rG, bCopy);
3820
3821    if(sca_ret) // yes it was dealt with!
3822      ret = false;
3823  }
3824
3825  if( bCopy )
3826  {
3827    assume(rIsPluralRing(rGR) == rIsPluralRing(rG));
3828    assume((rGR->qideal==NULL) == (rG->qideal==NULL));
3829    assume(rIsSCA(rGR) == rIsSCA(rG));
3830    assume(ncRingType(rGR) == ncRingType(rG));
3831  }
3832
3833  return ret;
3834}
3835
3836
3837
3838// int Commutative_Context(ring r, leftv expression)
3839//   /* returns 1 if expression consists */
3840//   /*  of commutative elements */
3841// {
3842//   /* crucial: poly -> ideal, module, matrix  */
3843// }
3844
3845// int Comm_Context_Poly(ring r, poly p)
3846// {
3847//   poly COMM=r->GetNC()->COMM;
3848//   poly pp=pOne();
3849//   memset(pp->exp,0,r->ExpL_Size*sizeof(long));
3850//   while (p!=NULL)
3851//   {
3852//     for (i=0;i<=r->ExpL_Size;i++)
3853//     {
3854//       if ((p->exp[i]) && (pp->exp[i]))  return(FALSE);
3855//       /* nonzero exponent of non-comm variable */
3856//     }
3857//     pIter(p);
3858//   }
3859//   return(TRUE);
3860// }
3861#endif
Note: See TracBrowser for help on using the repository browser.