source: git/kernel/gring.cc @ 3a67ea7

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