source: git/kernel/gring.cc @ f4b74e2

spielwiese
Last change on this file since f4b74e2 was f4b74e2, checked in by Motsak Oleksandr <motsak@…>, 17 years ago
*motsak: found the twostd's bug in kStd with SB_1 git-svn-id: file:///usr/local/Singular/svn/trunk@9797 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 72.9 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.40 2007-02-01 17:24:03 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  nc_PolyPolyRedOld(b, p, c);
1788}
1789
1790
1791poly nc_mm_Bracket_nn(poly m1, poly m2);
1792
1793poly nc_p_Bracket_qq(poly p, poly q)
1794  /* returns [p,q], destroys p */
1795{
1796  if (!rIsPluralRing(currRing)) return(NULL);
1797  if (pComparePolys(p,q)) return(NULL);
1798  /* Components !? */
1799  poly Q=NULL;
1800  number coef=NULL;
1801  poly res=NULL;
1802  poly pres=NULL;
1803  int UseBuckets=1;
1804  if ((pLength(p)< MIN_LENGTH_BUCKET/2) && (pLength(q)< MIN_LENGTH_BUCKET/2) || TEST_OPT_NOT_BUCKETS) UseBuckets=0;
1805  sBucket_pt bu_out;
1806  if (UseBuckets) bu_out=sBucketCreate(currRing);
1807  while (p!=NULL)
1808  {
1809    Q=q;
1810    while(Q!=NULL)
1811    {
1812      pres=nc_mm_Bracket_nn(p,Q); /* since no coeffs are taken into account there */
1813      if (pres!=NULL)
1814      {
1815        coef = nMult(pGetCoeff(p),pGetCoeff(Q));
1816        pres = p_Mult_nn(pres,coef,currRing);
1817    if (UseBuckets) sBucket_Add_p(bu_out,pres,pLength(pres));
1818    else res=p_Add_q(res,pres,currRing);
1819        nDelete(&coef);
1820      }
1821      pIter(Q);
1822    }
1823    p=pLmDeleteAndNext(p);
1824  }
1825  if (UseBuckets)
1826  {
1827    res = NULL;
1828    int len = pLength(res);
1829    sBucketDestroyAdd(bu_out, &res, &len);
1830  }
1831  return(res);
1832}
1833
1834poly nc_mm_Bracket_nn(poly m1, poly m2)
1835  /*returns [m1,m2] for two monoms, destroys nothing */
1836  /* without coeffs */
1837{
1838  if (pLmIsConstant(m1) || pLmIsConstant(m1)) return(NULL);
1839  if (pLmCmp(m1,m2)==0) return(NULL);
1840  int rN=currRing->N;
1841  int *M1=(int *)omAlloc0((rN+1)*sizeof(int));
1842  int *M2=(int *)omAlloc0((rN+1)*sizeof(int));
1843  int *PREFIX=(int *)omAlloc0((rN+1)*sizeof(int));
1844  int *SUFFIX=(int *)omAlloc0((rN+1)*sizeof(int));
1845  pGetExpV(m1,M1);
1846  pGetExpV(m2,M2);
1847  poly res=NULL;
1848  poly ares=NULL;
1849  poly bres=NULL;
1850  poly prefix=NULL;
1851  poly suffix=NULL;
1852  int nMin,nMax;
1853  number nTmp=NULL;
1854  int i,j,k;
1855  for (i=1;i<=rN;i++)
1856  {
1857    if (M2[i]!=0)
1858    {
1859      ares=NULL;
1860      for (j=1;j<=rN;j++)
1861      {
1862        if (M1[j]!=0)
1863        {
1864          bres=NULL;
1865          /* compute [ x_j^M1[j],x_i^M2[i] ] */
1866          if (i<j) {nMax=j;  nMin=i;} else {nMax=i;  nMin=j;}
1867          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)*/
1868          { bres=NULL; }
1869          else
1870          {
1871            if (i<j) { bres=gnc_uu_Mult_ww(j,M1[j],i,M2[i],currRing); }
1872            else bres=gnc_uu_Mult_ww(i,M2[i],j,M1[j],currRing);
1873            if (nIsOne(pGetCoeff(bres)))
1874            {
1875              bres=pLmDeleteAndNext(bres);
1876            }
1877            else
1878            {
1879              nTmp=nSub(pGetCoeff(bres),nInit(1));
1880              pSetCoeff(bres,nTmp); /* only lc ! */
1881            }
1882#ifdef PDEBUG
1883            pTest(bres);
1884#endif
1885            if (i>j)  bres=p_Neg(bres, currRing);
1886          }
1887          if (bres!=NULL)
1888          {
1889            /* now mult (prefix, bres, suffix) */
1890            memcpy(SUFFIX, M1,(rN+1)*sizeof(int));
1891            memcpy(PREFIX, M1,(rN+1)*sizeof(int));
1892            for (k=1;k<=j;k++) SUFFIX[k]=0;
1893            for (k=j;k<=rN;k++) PREFIX[k]=0;
1894            SUFFIX[0]=0;
1895            PREFIX[0]=0;
1896            prefix=pOne();
1897            suffix=pOne();
1898            pSetExpV(prefix,PREFIX);
1899            pSetm(prefix);
1900            pSetExpV(suffix,SUFFIX);
1901            pSetm(suffix);
1902            if (!pLmIsConstant(prefix)) bres = gnc_mm_Mult_p(prefix, bres,currRing);
1903            if (!pLmIsConstant(suffix)) bres = gnc_p_Mult_mm(bres, suffix,currRing);
1904            ares=p_Add_q(ares, bres,currRing);
1905            /* What to give free? */
1906        /* Do we have to free PREFIX/SUFFIX? it seems so */
1907            pDelete(&prefix);
1908            pDelete(&suffix);
1909          }
1910        }
1911      }
1912      if (ares!=NULL)
1913      {
1914        /* now mult (prefix, bres, suffix) */
1915        memcpy(SUFFIX, M2,(rN+1)*sizeof(int));
1916        memcpy(PREFIX, M2,(rN+1)*sizeof(int));
1917        for (k=1;k<=i;k++) SUFFIX[k]=0;
1918        for (k=i;k<=rN;k++) PREFIX[k]=0;
1919        SUFFIX[0]=0;
1920        PREFIX[0]=0;
1921        prefix=pOne();
1922        suffix=pOne();
1923        pSetExpV(prefix,PREFIX);
1924        pSetm(prefix);
1925        pSetExpV(suffix,SUFFIX);
1926        pSetm(suffix);
1927        bres=ares;
1928        if (!pLmIsConstant(prefix)) bres = gnc_mm_Mult_p(prefix, bres,currRing);
1929        if (!pLmIsConstant(suffix)) bres = gnc_p_Mult_mm(bres, suffix,currRing);
1930        res=p_Add_q(res, bres,currRing);
1931        pDelete(&prefix);
1932        pDelete(&suffix);
1933      }
1934    }
1935  }
1936  freeT(M1, rN);
1937  freeT(M2, rN);
1938  freeT(PREFIX, rN);
1939  freeT(SUFFIX, rN);
1940  pTest(res);
1941  return(res);
1942}
1943
1944ideal twostd(ideal I)
1945{
1946  int i;
1947  int j;
1948  int s;
1949  bool flag;
1950  poly p=NULL;
1951  poly q=NULL;
1952  ideal J=kStd(I, currQuotient,testHomog,NULL,NULL,0,0,NULL);
1953  idSkipZeroes(J);
1954  ideal K=NULL;
1955  poly varj=NULL;
1956  ideal Q=NULL;
1957  ideal id_tmp=NULL;
1958  int rN=currRing->N;
1959  int iSize=0;
1960
1961  loop
1962  {
1963    flag = true; // nothing new!
1964    K    = NULL;
1965    s    = idElem(J);
1966   
1967    for (i=0;i<=s-1;i++)
1968    {
1969      p=J->m[i];
1970
1971        #ifdef PDEBUG
1972          p_Test(p, currRing);
1973        #if 0
1974          Print("p: "); // !
1975          p_Write(p, currRing);
1976        #endif
1977        #endif
1978
1979      for (j=1;j<=rN;j++) // for all j = 1..N
1980      {
1981        varj = pOne();
1982        pSetExp(varj,j,1);
1983        pSetm(varj);
1984        q = pp_Mult_mm(p,varj,currRing); // q = J[i] * var(j),
1985        pDelete(&varj);
1986
1987        #ifdef PDEBUG
1988          p_Test(p, currRing);
1989          p_Test(q, currRing);
1990        #if 0
1991          Print("Reducing p: "); // !
1992          p_Write(p, currRing);
1993
1994          Print("With q: "); // !
1995          p_Write(q, currRing);
1996        #endif
1997        #endif
1998       
1999        q = nc_ReduceSPoly(p,q,currRing);
2000
2001        #ifdef PDEBUG
2002          p_Test(q, currRing);
2003        #if 0
2004          Print("Reducing q: "); // !
2005          p_Write(q, currRing);
2006
2007          Print("With J!\n");
2008        #endif
2009        #endif
2010
2011        q = kNF(J,currQuotient,q,0,0);
2012
2013        #ifdef PDEBUG
2014          p_Test(q, currRing);
2015        #if 0
2016          Print("=> q: "); // !
2017          p_Write(q, currRing);
2018        #endif
2019        #endif
2020
2021        if (q!=NULL)
2022        {
2023          if (pIsConstant(q)) // => return (1)!
2024          {
2025            Q=idInit(1,1);
2026            Q->m[0]=pOne();
2027            idDelete(&J);
2028            pDelete(&q);
2029            if (K!=NULL) idDelete(&K);
2030            return(Q);
2031          }
2032         
2033          flag=false;
2034          Q=idInit(1,1);
2035          Q->m[0]=q;
2036          id_tmp=idSimpleAdd(K,Q);
2037          idDelete(&K);
2038          K=id_tmp;
2039          idDelete(&Q);
2040        }
2041      }
2042    }
2043    if (flag) // nothing new!
2044      /* i.e. all elements are two-sided */
2045    {
2046//      idDelete(&K);
2047      return(J);
2048    }
2049    /* now we update GrBasis J with K */
2050    //    iSize=IDELEMS(J);
2051  #ifdef PDEBUG
2052    idTest(J);
2053  #if 0
2054    Print("J:");
2055    idPrint(J);
2056    PrintLn();
2057  #endif // debug
2058  #endif 
2059
2060
2061
2062  #ifdef PDEBUG
2063    idTest(K);
2064  #if 0
2065    Print("+K:");
2066    idPrint(K);
2067    PrintLn();
2068  #endif // debug
2069  #endif 
2070
2071
2072    iSize=idElem(J);
2073    id_tmp=idSimpleAdd(J,K);
2074    idDelete(&K);
2075    idDelete(&J);
2076   
2077    BITSET save_test=test;
2078
2079    #if 1
2080      test|=Sy_bit(OPT_SB_1);
2081      J = kStd(id_tmp, currQuotient, testHomog, NULL, NULL, 0, iSize); // J = J + K, J - std   
2082    #else
2083      J=kStd(id_tmp, currQuotient,testHomog,NULL,NULL,0,0,NULL);
2084    #endif
2085    test = save_test;
2086   
2087    idDelete(&id_tmp); // !!!
2088
2089    idSkipZeroes(J);
2090       
2091  #ifdef PDEBUG
2092    idTest(J);
2093  #if 0
2094    Print("J:");
2095    idPrint(J);
2096    PrintLn();
2097  #endif // debug
2098  #endif 
2099
2100// bug:
2101
2102//J:Module of rank 1,real rank 0 and 4 generators.
2103//generator 0: f
2104//generator 1: h2+h
2105//generator 2: eh+e
2106//generator 3: e2
2107
2108//+K:Module of rank 1,real rank 0 and 2 generators.
2109//generator 0: h
2110//generator 1: e
2111
2112// =>>>>
2113
2114//J:Module of rank 1,real rank 0 and 6 generators.
2115//generator 0: h
2116//generator 1: f
2117//generator 2: e
2118//generator 3: h2+h
2119//generator 4: eh+e
2120//generator 5: e2
2121
2122
2123
2124   
2125  }
2126}
2127
2128matrix nc_PrintMat(int a, int b, ring r, int metric)
2129  /* returns matrix with the info on noncomm multiplication */
2130{
2131
2132  if ( (a==b) || !rIsPluralRing(r) ) return(NULL);
2133  int i;
2134  int j;
2135  if (a>b) {j=b; i=a;}
2136  else {j=a; i=b;}
2137  /* i<j */
2138  int rN=r->N;
2139  int size=r->nc->MTsize[UPMATELEM(i,j,rN)];
2140  matrix M = r->nc->MT[UPMATELEM(i,j,rN)];
2141  /*  return(M); */
2142  int sizeofres;
2143  if (metric==0)
2144  {
2145    sizeofres=sizeof(int);
2146  }
2147  if (metric==1)
2148  {
2149    sizeofres=sizeof(number);
2150  }
2151  matrix res=mpNew(size,size);
2152  int s;
2153  int t;
2154  int length;
2155  long totdeg;
2156  poly p;
2157  for(s=1;s<=size;s++)
2158  {
2159    for(t=1;t<=size;t++)
2160    {
2161      p=MATELEM(M,s,t);
2162      if (p==NULL)
2163      {
2164        MATELEM(res,s,t)=0;
2165      }
2166      else
2167      {
2168        length = pLength(p);
2169        if (metric==0) /* length */
2170        {
2171          MATELEM(res,s,t)= p_ISet(length,r);
2172        }
2173        else if (metric==1) /* sum of deg divided by the length */
2174        {
2175          totdeg=0;
2176          while (p!=NULL)
2177          {
2178            totdeg=totdeg+pDeg(p,r);
2179            pIter(p);
2180          }
2181          number ntd = nInit(totdeg);
2182          number nln = nInit(length);
2183          number nres=nDiv(ntd,nln);
2184          nDelete(&ntd);
2185          nDelete(&nln);
2186          MATELEM(res,s,t)=p_NSet(nres,r);
2187        }
2188      }
2189    }
2190  }
2191  return(res);
2192}
2193
2194void ncKill(ring r)
2195  /* kills the nc extension of ring r */
2196{
2197  int i,j;
2198  int rN=r->N;
2199  if ( rN > 1 )
2200  {
2201    for(i=1;i<rN;i++)
2202    {
2203      for(j=i+1;j<=rN;j++)
2204      {
2205        id_Delete((ideal *)&(r->nc->MT[UPMATELEM(i,j,rN)]),r->nc->basering);
2206      }
2207    }
2208    omFreeSize((ADDRESS)r->nc->MT,rN*(rN-1)/2*sizeof(matrix));
2209    omFreeSize((ADDRESS)r->nc->MTsize,rN*(rN-1)/2*sizeof(int));
2210    id_Delete((ideal *)&(r->nc->COM),r->nc->basering);
2211  }
2212  id_Delete((ideal *)&(r->nc->C),r->nc->basering);
2213  id_Delete((ideal *)&(r->nc->D),r->nc->basering);
2214 
2215  if( rIsSCA(r) && (r->nc->SCAQuotient() != NULL) )
2216  {
2217    id_Delete(&r->nc->SCAQuotient(), r->nc->basering);   
2218  }
2219
2220  r->nc->basering->ref--;
2221 
2222  if (r->nc->basering->ref<=0)
2223  {
2224    rKill(r->nc->basering);
2225  }
2226 
2227  ncCleanUp(r);
2228}
2229
2230void ncCleanUp(ring r)
2231{
2232  /* small CleanUp of r->nc */
2233  omFreeSize((ADDRESS)r->nc,sizeof(nc_struct));
2234  r->nc = NULL;
2235}
2236
2237poly nc_p_CopyGet(poly a, const ring r)
2238/* for use in getting the mult. matrix elements*/
2239/* ring r must be a currRing! */
2240/* for consistency, copies a poly from the comm. r->nc->basering */
2241/* to its image in NC ring */
2242{
2243  if (r != currRing)
2244  {
2245#ifdef PDEBUF
2246    Werror("nc_p_CopyGet call not in currRing");
2247#endif
2248    return(NULL);
2249  }
2250  if (!rIsPluralRing(r)) return(p_Copy(a,r));
2251  if (r==r->nc->basering) return(p_Copy(a,r));
2252  else
2253  {
2254    return(prCopyR_NoSort(a,r->nc->basering,r));
2255  }
2256}
2257
2258poly nc_p_CopyPut(poly a, const ring r)
2259/* for use in defining the mult. matrix elements*/
2260/* ring r must be a currRing! */
2261/* for consistency, puts a polynomial from the NC ring */
2262/* to its presentation in the comm. r->nc->basering */
2263{
2264  if (r != currRing)
2265  {
2266#ifdef PDEBUF
2267    Werror("nc_p_CopyGet call not in currRing");
2268#endif
2269    return(NULL);
2270  }
2271  if (!rIsPluralRing(r)) return(p_Copy(a,r));
2272  if (r==r->nc->basering) return(p_Copy(a,r));
2273  else
2274  {
2275    return(prCopyR_NoSort(a,r,r->nc->basering));
2276  }
2277}
2278
2279BOOLEAN nc_CheckSubalgebra(poly PolyVar, ring r)
2280  /* returns TRUE if there were errors */
2281  /* checks whether product of vars from PolyVar defines */
2282  /* an admissible subalgebra of r */
2283  /* r is indeed currRing and assumed to be noncomm. */
2284{
2285  ring save = currRing;
2286  int WeChangeRing = 0;
2287  if (currRing != r)
2288  {
2289    rChangeCurrRing(r);
2290    WeChangeRing = 1;
2291  }
2292  int rN=r->N;
2293  int *ExpVar=(int*)omAlloc0((rN+1)*sizeof(int));
2294  int *ExpTmp=(int*)omAlloc0((rN+1)*sizeof(int));
2295  p_GetExpV(PolyVar, ExpVar, r);
2296  int i; int j; int k;
2297  poly test=NULL;
2298  int OK=1;
2299  for (i=1; i<rN; i++)
2300  {
2301    if (ExpVar[i]==0) /* i.e. not in PolyVar */
2302    {
2303      for (j=i+1; j<=rN; j++)
2304      {
2305    if (ExpVar[j]==0)
2306    {
2307      test = nc_p_CopyGet(MATELEM(r->nc->D,i,j),r);
2308      while (test!=NULL)
2309      {
2310            p_GetExpV(test, ExpTmp, r);
2311        OK=1;
2312        for (k=1;k<=rN;k++)
2313            {
2314          if (ExpTmp[k]!=0)
2315          {
2316        if (ExpVar[k]!=0) OK=0;
2317          }
2318            }
2319        if (!OK) return(TRUE);
2320        pIter(test);
2321          }
2322    }
2323      }
2324    }
2325  }
2326  p_Delete(&test,r);
2327  freeT(ExpVar,rN);
2328  freeT(ExpTmp,rN);
2329  if ( WeChangeRing )
2330    rChangeCurrRing(save);
2331  return(FALSE);
2332}
2333
2334BOOLEAN nc_CheckOrdCondition(matrix D, ring r)
2335/* returns TRUE if there were errors */
2336/* checks whether the current ordering */
2337/* is admissible for r and D == r->nc->D */
2338/* to be executed in a currRing */
2339{
2340  /* analyze D: an upper triangular matrix of polys */
2341  /* check the ordering condition for D */
2342  ring save = currRing;
2343  int WeChangeRing = 0;
2344  if (r != currRing)
2345  {
2346    rChangeCurrRing(r);
2347    WeChangeRing = 1;
2348  }
2349  poly p,q;
2350  int i,j;
2351  int report = 0;
2352  for(i=1; i<r->N; i++)
2353  {
2354    for(j=i+1; j<=r->N; j++)
2355    {
2356      p = nc_p_CopyGet(MATELEM(D,i,j),r);
2357      if ( p != NULL)
2358      {
2359    q = p_ISet(1,r); // replaces pOne();
2360    p_SetExp(q,i,1,r);
2361    p_SetExp(q,j,1,r);
2362    p_Setm(q,r);
2363    if (p_LmCmp(q,p,r) != 1) /* i.e. lm(p)==xy < lm(q)==D_ij  */
2364    {
2365      Print("Bad ordering at %d,%d\n",i,j);
2366#ifdef PDEBUG
2367      p_Write(p,r);
2368      p_Write(q,r);
2369#endif
2370      report = 1;
2371    }
2372    p_Delete(&q,r);
2373    p_Delete(&p,r);
2374    p = NULL;
2375      }
2376    }
2377  }
2378  if ( WeChangeRing )
2379    rChangeCurrRing(save);
2380  return(report);
2381}
2382
2383
2384
2385BOOLEAN nc_CallPlural(matrix CCC, matrix DDD, poly CCN, poly DDN, ring r)
2386  /* returns TRUE if there were errors */
2387  /* analyze inputs, check them for consistency */
2388  /* detects nc_type, DO NOT initialize multiplication but call for it at the end*/
2389  /* checks the ordering condition and evtl. NDC */
2390{
2391  matrix CC = NULL;
2392  matrix DD = NULL;
2393  poly CN = NULL;
2394  poly DN = NULL;
2395  matrix C;
2396  matrix D;
2397  number nN,pN,qN;
2398  int tmpIsSkewConstant;
2399  int i,j;
2400  if (r->nc != NULL)
2401  {
2402    WarnS("redefining algebra structure");
2403    if (r->nc->ref>1) /* in use by somebody else */
2404    {
2405      r->nc->ref--;
2406    }
2407    else  /* kill the previous nc data */
2408    {
2409      ncKill(r);
2410    }
2411  }
2412  ring save = currRing;
2413  int WeChangeRing = 0;
2414  if (currRing!=r)
2415  {
2416    rChangeCurrRing(r);
2417    WeChangeRing = 1;
2418  }
2419  r->nc = (nc_struct *)omAlloc0(sizeof(nc_struct));
2420  r->nc->ref = 1;
2421  r->nc->basering = r;
2422  r->ref++;
2423  ncRingType(r, nc_undef);
2424
2425  /* initialition of the matrix C */
2426  /* check the correctness of arguments */
2427
2428  if ((CCC != NULL) && ( (MATCOLS(CCC)==1) || MATROWS(CCC)==1 ) )
2429  {
2430    CN = MATELEM(CCC,1,1);
2431  }
2432  else
2433  {
2434    if ((CCC != NULL) && ( (MATCOLS(CCC)!=r->N) || (MATROWS(CCC)!=r->N) ))
2435    {
2436      Werror("Square %d x %d  matrix expected",r->N,r->N);
2437      ncCleanUp(r);
2438      if (WeChangeRing)
2439    rChangeCurrRing(save);
2440      return TRUE;
2441    }
2442  }
2443  if (( CCC != NULL) && (CC == NULL)) CC = mpCopy(CCC);
2444  if (( CCN != NULL) && (CN == NULL)) CN = CCN;
2445
2446  /* initialition of the matrix D */
2447  /* check the correctness of arguments */
2448
2449  if ((DDD != NULL) && ( (MATCOLS(DDD)==1) || MATROWS(DDD)==1 ) )
2450  {
2451    DN = MATELEM(DDD,1,1);
2452  }
2453  else
2454  {
2455    if ((DDD != NULL) && ( (MATCOLS(DDD)!=r->N) || (MATROWS(DDD)!=r->N) ))
2456    {
2457      Werror("Square %d x %d  matrix expected",r->N,r->N);
2458      ncCleanUp(r);
2459      if (WeChangeRing)
2460    rChangeCurrRing(save);
2461      return TRUE;
2462    }
2463  }
2464  if (( DDD != NULL) && (DD == NULL)) DD = mpCopy(DDD);
2465  if (( DDN != NULL) && (DN == NULL)) DN = DDN;
2466
2467  /* further checks */
2468
2469  if (CN != NULL)       /* create matrix C = CN * Id */
2470  {
2471    nN = p_GetCoeff(CN,r);
2472    if (n_IsZero(nN,r))
2473    {
2474      Werror("Incorrect input : zero coefficients are not allowed");
2475      ncCleanUp(r);
2476      if (WeChangeRing)
2477    rChangeCurrRing(save);
2478      return TRUE;
2479    }
2480    if (nIsOne(nN))
2481    {
2482      ncRingType(r, nc_lie);
2483    }
2484    else
2485    {
2486      ncRingType(r, nc_general);
2487    }
2488    r->nc->IsSkewConstant = 1;
2489    C = mpNew(r->N,r->N);
2490    for(i=1; i<r->N; i++)
2491    {
2492      for(j=i+1; j<=r->N; j++)
2493      {
2494    MATELEM(C,i,j) = nc_p_CopyPut(CN,r);
2495      }
2496    }
2497  }
2498  if ( (CN == NULL) && (CC != NULL) ) /* copy matrix C */
2499  {
2500    C = mpCopy(CC);
2501    /* analyze C */
2502    if ( MATELEM(C,1,2) == NULL )
2503      pN = NULL; /* check the consistency later */
2504    else
2505      pN = p_GetCoeff(MATELEM(C,1,2),r);
2506    tmpIsSkewConstant = 1;
2507    for(i=1; i<r->N; i++)
2508    {
2509      for(j=i+1; j<=r->N; j++)
2510      {
2511    if (MATELEM(C,i,j) == NULL)
2512      qN = NULL;
2513    else
2514      qN = p_GetCoeff(MATELEM(C,i,j),r);
2515    if ( qN == NULL )   /* check the consistency: Cij!=0 */
2516        // find also illegal pN
2517    {
2518      Werror("Incorrect input : matrix of coefficients contains zeros in the upper triangle");
2519      ncCleanUp(r);
2520      if (WeChangeRing)
2521        rChangeCurrRing(save);
2522      return TRUE;
2523    }
2524    if (!nEqual(pN,qN)) tmpIsSkewConstant = 0;
2525      }
2526    }
2527    r->nc->IsSkewConstant=tmpIsSkewConstant;
2528    if ( (tmpIsSkewConstant) && (nIsOne(pN)) )
2529    {
2530      ncRingType(r, nc_lie);
2531    }
2532    else
2533    {
2534      ncRingType(r, nc_general);
2535    }
2536  }
2537
2538  /* initialition of the matrix D */
2539  if ( DD == NULL )
2540    /* we treat DN only (it could also be NULL) */
2541  {
2542    D = mpNew(r->N,r->N);
2543    if (DN  == NULL)
2544    {
2545      if ( (ncRingType(r) == nc_lie) || (ncRingType(r) == nc_undef) )
2546      {
2547        ncRingType(r, nc_comm); /* it was nc_skew earlier */
2548      }
2549      else /* nc_general, nc_skew */
2550      {
2551        ncRingType(r, nc_skew);
2552      }
2553    }
2554    else /* DN  != NULL */
2555    {
2556      for(i=1; i<r->N; i++)
2557      {
2558    for(j=i+1; j<=r->N; j++)
2559    {
2560      MATELEM(D,i,j) = nc_p_CopyPut(DN,r);
2561    }
2562      }
2563    }
2564  }
2565  else /* DD != NULL */
2566  {
2567    D = mpCopy(DD);
2568  }
2569  /* analyze D */
2570  /* check the ordering condition for D (both matrix and poly cases) */
2571
2572  if ( nc_CheckOrdCondition(D, r) )
2573  {
2574    ncCleanUp(r);
2575    if (WeChangeRing)
2576      rChangeCurrRing(save);
2577    Werror("Matrix of polynomials violates the ordering condition");
2578    return TRUE;
2579  }
2580  r->nc->C = C;
2581  r->nc->D = D;
2582  if (WeChangeRing)
2583    rChangeCurrRing(save);
2584  return nc_InitMultiplication(r);
2585}
2586
2587//////////////////////////////////////////////////////////////////////////////
2588BOOLEAN nc_InitMultiplication(ring r)
2589{
2590  /* returns TRUE if there were errors */
2591  /* initialize the multiplication: */
2592  /*  r->nc->MTsize, r->nc->MT, r->nc->COM, */
2593  /* and r->nc->IsSkewConstant for the skew case */
2594  if (rVar(r)==1)
2595  {
2596    ncRingType(r, nc_comm);
2597    r->nc->IsSkewConstant=1;
2598    return FALSE;
2599  }
2600  ring save = currRing;
2601  int WeChangeRing = 0;
2602  if (currRing!=r)
2603  {
2604    rChangeCurrRing(r);
2605    WeChangeRing = 1;
2606  }
2607  assume( currRing == r->nc->basering ); // otherwise we cannot work with all these matrices!
2608
2609  int i,j;
2610  r->nc->MT = (matrix *)omAlloc0((r->N*(r->N-1))/2*sizeof(matrix));
2611  r->nc->MTsize = (int *)omAlloc0((r->N*(r->N-1))/2*sizeof(int));
2612  idTest(((ideal)r->nc->C));
2613  matrix COM = mpCopy(r->nc->C);
2614  poly p,q;
2615  short DefMTsize=7;
2616  int IsNonComm=0;
2617  int tmpIsSkewConstant;
2618
2619  for(i=1; i<r->N; i++)
2620  {
2621    for(j=i+1; j<=r->N; j++)
2622    {
2623      if ( MATELEM(r->nc->D,i,j) == NULL ) /* quasicommutative case */
2624      {
2625        /* 1x1 mult.matrix */
2626        r->nc->MTsize[UPMATELEM(i,j,r->N)] = 1;
2627        r->nc->MT[UPMATELEM(i,j,r->N)] = mpNew(1,1);
2628      }
2629      else /* pure noncommutative case */
2630      {
2631        /* TODO check the special multiplication properties */
2632        IsNonComm = 1;
2633        p_Delete(&(MATELEM(COM,i,j)),r);
2634        //MATELEM(COM,i,j) = NULL; // done by p_Delete
2635        r->nc->MTsize[UPMATELEM(i,j,r->N)] = DefMTsize; /* default sizes */
2636        r->nc->MT[UPMATELEM(i,j,r->N)] = mpNew(DefMTsize, DefMTsize);
2637      }
2638      /* set MT[i,j,1,1] to c_i_j*x_i*x_j + D_i_j */
2639      p = p_ISet(1,r); /* instead of     p = pOne(); */
2640      if (MATELEM(r->nc->C,i,j)!=NULL)
2641        p_SetCoeff(p,n_Copy(pGetCoeff(MATELEM(r->nc->C,i,j)),r),r);
2642      p_SetExp(p,i,1,r);
2643      p_SetExp(p,j,1,r);
2644      p_Setm(p,r);
2645      p_Test(MATELEM(r->nc->D,i,j),r->nc->basering);
2646      q =  nc_p_CopyGet(MATELEM(r->nc->D,i,j),r);
2647      p = p_Add_q(p,q,r);
2648      MATELEM(r->nc->MT[UPMATELEM(i,j,r->N)],1,1) = nc_p_CopyPut(p,r);
2649      p_Delete(&p,r);
2650      // p = NULL;// done by p_Delete
2651    }
2652  }
2653  if (ncRingType(r)==nc_undef)
2654  {
2655    if (IsNonComm==1)
2656    {
2657      //      assume(pN!=NULL);
2658      //      if ((tmpIsSkewConstant==1) && (nIsOne(pGetCoeff(pN)))) r->nc->type=nc_lie;
2659      //      else r->nc->type=nc_general;
2660    }
2661    if (IsNonComm==0)
2662    {
2663      ncRingType(r, nc_skew); /* TODO: check whether it is commutative */
2664      r->nc->IsSkewConstant=tmpIsSkewConstant;
2665    }
2666  }
2667  r->nc->COM=COM;
2668
2669  gnc_p_ProcsSet(r, r->p_Procs);
2670
2671  if (WeChangeRing)
2672  {
2673    rChangeCurrRing(save);
2674  }
2675  return FALSE;
2676}
2677
2678void gnc_p_ProcsSet(ring rGR, p_Procs_s* p_Procs)
2679{
2680  // "commutative"
2681  rGR->p_Procs->p_Mult_mm  = gnc_p_Mult_mm;
2682  rGR->p_Procs->pp_Mult_mm = gnc_pp_Mult_mm;
2683  rGR->p_Procs->p_Minus_mm_Mult_qq = NULL; // gnc_p_Minus_mm_Mult_qq_ign; // should not be used!!!
2684
2685  p_Procs->p_Mult_mm  = gnc_p_Mult_mm;
2686  p_Procs->pp_Mult_mm = gnc_pp_Mult_mm;
2687  p_Procs->p_Minus_mm_Mult_qq = NULL; // gnc_p_Minus_mm_Mult_qq_ign; // should not be used!!!
2688
2689
2690  // non-commutaitve multiplication by monomial from the left
2691  rGR->nc->p_Procs.mm_Mult_p   = gnc_mm_Mult_p;
2692  rGR->nc->p_Procs.mm_Mult_pp  = gnc_mm_Mult_pp;
2693
2694  rGR->nc->p_Procs.GB          = gnc_gr_bba; // bba even for local case!
2695
2696//   rGR->nc->p_Procs.GlobalGB    = gnc_gr_bba;
2697//   rGR->nc->p_Procs.LocalGB     = gnc_gr_mora;
2698
2699
2700#if 0
2701  // Previous Plural's implementation...
2702  rGR->nc->p_Procs.SPoly       = gnc_CreateSpolyOld;
2703  rGR->nc->p_Procs.ReduceSPoly = gnc_ReduceSpolyOld;
2704
2705  rGR->nc->p_Procs.BucketPolyRed  = gnc_kBucketPolyRedOld;
2706  rGR->nc->p_Procs.BucketPolyRed_Z= gnc_kBucketPolyRed_ZOld;
2707#else
2708  // A bit cleaned up and somewhat rewritten functions...
2709  rGR->nc->p_Procs.SPoly       = gnc_CreateSpolyNew;
2710  rGR->nc->p_Procs.ReduceSPoly = gnc_ReduceSpolyNew;
2711
2712  rGR->nc->p_Procs.BucketPolyRed  = gnc_kBucketPolyRedNew;
2713  rGR->nc->p_Procs.BucketPolyRed_Z= gnc_kBucketPolyRed_ZNew;
2714#endif
2715
2716
2717
2718
2719#if 0
2720    // Old Stuff
2721    p_Procs->p_Mult_mm   = gnc_p_Mult_mm;
2722    _p_procs->p_Mult_mm  = gnc_p_Mult_mm;
2723
2724    p_Procs->pp_Mult_mm  = gnc_pp_Mult_mm;
2725    _p_procs->pp_Mult_mm = gnc_pp_Mult_mm;
2726
2727    p_Procs->p_Minus_mm_Mult_qq = NULL; // gnc_p_Minus_mm_Mult_qq_ign;
2728    _p_procs->p_Minus_mm_Mult_qq= NULL; // gnc_p_Minus_mm_Mult_qq_ign;
2729
2730    r->nc->mmMultP()       = gnc_mm_Mult_p;
2731    r->nc->mmMultPP()      = gnc_mm_Mult_pp;
2732
2733    r->nc->GB()            = gnc_gr_bba;
2734
2735    r->nc->SPoly()         = gnc_CreateSpoly;
2736    r->nc->ReduceSPoly()   = gnc_ReduceSpoly;
2737
2738#endif
2739}
2740
2741
2742// set pProcs table for rGR and global variable p_Procs
2743void nc_p_ProcsSet(ring rGR, p_Procs_s* p_Procs)
2744{
2745  assume(rIsPluralRing(rGR));
2746  assume(p_Procs!=NULL);
2747
2748  gnc_p_ProcsSet(rGR, p_Procs);
2749
2750  if(rIsSCA(rGR))
2751  {
2752    sca_p_ProcsSet(rGR, p_Procs);
2753  }
2754}
2755
2756
2757
2758/* substitute the n-th variable by e in p
2759* destroy p
2760* e is not a constant
2761*/
2762poly nc_pSubst(poly p, int n, poly e)
2763{
2764  int rN=currRing->N;
2765  int *PRE = (int *)omAlloc0((rN+1)*sizeof(int));
2766  int *SUF = (int *)omAlloc0((rN+1)*sizeof(int));
2767  int i,j,pow;
2768  number C;
2769  poly suf,pre;
2770  poly res = NULL;
2771  poly out = NULL;
2772  while ( p!= NULL )
2773  {
2774    C =  pGetCoeff(p);
2775    pGetExpV(p, PRE); /* faster splitting? */
2776    pow = PRE[n]; PRE[n]=0;
2777    res = NULL;
2778    if (pow!=0)
2779    {
2780      for (i=n+1; i<=rN; i++)
2781      {
2782    SUF[i] = PRE[i];
2783    PRE[i] = 0;
2784      }
2785      res =  pPower(pCopy(e),pow);
2786      /* multiply with prefix */
2787      pre = pOne();
2788      pSetExpV(pre,PRE);
2789      pSetm(pre);
2790      res = nc_mm_Mult_p(pre,res,currRing);
2791      /* multiply with suffix */
2792      suf = pOne();
2793      pSetExpV(suf,SUF);
2794      pSetm(suf);
2795      res = p_Mult_mm(res,suf,currRing);
2796      res = p_Mult_nn(res,C,currRing);
2797      pSetComp(res,PRE[0]);
2798    }
2799    else /* pow==0 */
2800    {
2801      res = pHead(p);
2802    }
2803    p   = pLmDeleteAndNext(p);
2804    out = pAdd(out,res);
2805  }
2806  freeT(PRE,rN);
2807  freeT(SUF,rN);
2808  return(out);
2809}
2810
2811static ideal idPrepareStd(ideal T, ideal s,  int k)
2812{
2813  /* T is a left SB, without zeros, s is a list with zeros */
2814#ifdef PDEBUG
2815  if (IDELEMS(s)!=IDELEMS(T))
2816  {
2817    Print("ideals of diff. size!!!");
2818  }
2819#endif
2820  ideal t = idCopy(T);
2821  int j,rs=idRankFreeModule(s),rt=idRankFreeModule(t);
2822  poly p,q;
2823
2824  ideal res = idInit(2*idElem(t),1+idElem(t));
2825  if (rs == 0)
2826  {
2827    for (j=0; j<IDELEMS(t); j++)
2828    {
2829      if (s->m[j]!=NULL) pSetCompP(s->m[j],1);
2830      if (t->m[j]!=NULL) pSetCompP(t->m[j],1);
2831    }
2832    k = si_max(k,1);
2833  }
2834  for (j=0; j<IDELEMS(t); j++)
2835  {
2836    if (s->m[j]!=NULL)
2837    {
2838      p = s->m[j];
2839      q = pOne();
2840      pSetComp(q,k+1+j);
2841      pSetmComp(q);
2842#if 0
2843      while (pNext(p)) pIter(p);
2844      pNext(p) = q;
2845#else
2846      p = pAdd(p,q);
2847      s->m[j] = p;
2848#ifdef PDEBUG
2849    pTest(p);
2850#endif
2851#endif
2852    }
2853  }
2854  res = idSimpleAdd(t,s);
2855  idDelete(&t);
2856  res->rank = 1+idElem(T);
2857  return(res);
2858}
2859
2860ideal Approx_Step(ideal L)
2861{
2862  int N=currRing->N;
2863  int i,j; // k=syzcomp
2864  int flag, flagcnt=0, syzcnt=0;
2865  int syzcomp = 0;
2866  int k=1; /* for ideals not modules */
2867  ideal I = kStd(L, currQuotient,testHomog,NULL,NULL,0,0,NULL);
2868  idSkipZeroes(I);
2869  ideal s_I;
2870  int idI = idElem(I);
2871  ideal trickyQuotient,s_trickyQuotient;
2872  if (currQuotient !=NULL)
2873  {
2874    trickyQuotient = idSimpleAdd(currQuotient,I);
2875  }
2876  else
2877    trickyQuotient = I;
2878  idSkipZeroes(trickyQuotient);
2879  poly *var = (poly *)omAlloc0((N+1)*sizeof(poly));
2880  //  poly *W = (poly *)omAlloc0((2*N+1)*sizeof(poly));
2881  resolvente S = (resolvente)omAlloc0((N+1)*sizeof(ideal));
2882  ideal SI, res;
2883  matrix MI;
2884  poly x=pOne();
2885  var[0]=x;
2886  ideal   h2, h3, s_h2, s_h3;
2887  poly    p,q,qq;
2888  /* init vars */
2889  for (i=1; i<=N; i++ )
2890  {
2891    x = pOne();
2892    pSetExp(x,i,1);
2893    pSetm(x);
2894    var[i]=pCopy(x);
2895  }
2896  /* init NF's */
2897  for (i=1; i<=N; i++ )
2898  {
2899    h2 = idInit(idI,1);
2900    flag = 0;
2901    for (j=0; j< idI; j++ )
2902    {
2903      q = pp_Mult_mm(I->m[j],var[i],currRing);
2904      q = kNF(I,currQuotient,q,0,0);
2905      if (q!=0)
2906      {
2907    h2->m[j]=pCopy(q);
2908    //  pShift(&(h2->m[flag]),1);
2909    flag++;
2910    pDelete(&q);
2911      }
2912      else
2913    h2->m[j]=0;
2914    }
2915    /* W[1..idElems(I)] */
2916    if (flag >0)
2917    {
2918      /* compute syzygies with values in I*/
2919      //      idSkipZeroes(h2);
2920      //      h2 = idSimpleAdd(h2,I);
2921      //      h2->rank=flag+idI+1;
2922      idTest(h2);
2923      idShow(h2);
2924      ring orig_ring=currRing;
2925      ring syz_ring=rCurrRingAssure_SyzComp();
2926      syzcomp = 1;
2927      rSetSyzComp(syzcomp);
2928      if (orig_ring != syz_ring)
2929      {
2930    s_h2=idrCopyR_NoSort(h2,orig_ring);
2931    //  s_trickyQuotient=idrCopyR_NoSort(trickyQuotient,orig_ring);
2932    //  rDebugPrint(syz_ring);
2933    s_I=idrCopyR_NoSort(I,orig_ring);
2934      }
2935      else
2936      {
2937    s_h2 = h2;
2938    s_I  = I;
2939    //  s_trickyQuotient=trickyQuotient;
2940      }
2941      idTest(s_h2);
2942      //      idTest(s_trickyQuotient);
2943      Print(".proceeding with the variable %d\n",i);
2944      s_h3 = idPrepareStd(s_I, s_h2, 1);
2945      BITSET save_test=test;
2946      test|=Sy_bit(OPT_SB_1);
2947      idTest(s_h3);
2948      idDelete(&s_h2);
2949      s_h2=idCopy(s_h3);
2950      idDelete(&s_h3);
2951      Print("...computing Syz");
2952      s_h3 = kStd(s_h2, currQuotient,(tHomog)FALSE,NULL,NULL,syzcomp,idI);
2953      test=save_test;
2954      idShow(s_h3);
2955      if (orig_ring != syz_ring)
2956      {
2957    idDelete(&s_h2);
2958    for (j=0; j<IDELEMS(s_h3); j++)
2959    {
2960      if (s_h3->m[j] != NULL)
2961      {
2962        if (p_MinComp(s_h3->m[j],syz_ring) > syzcomp) /* i.e. it is a syzygy */
2963          pShift(&s_h3->m[j], -syzcomp);
2964        else
2965          pDelete(&s_h3->m[j]);
2966      }
2967    }
2968    idSkipZeroes(s_h3);
2969    s_h3->rank -= syzcomp;
2970    rChangeCurrRing(orig_ring);
2971    //  s_h3 = idrMoveR_NoSort(s_h3, syz_ring);
2972    s_h3 = idrMoveR_NoSort(s_h3, syz_ring);
2973    rKill(syz_ring);
2974      }
2975      idTest(s_h3);
2976      S[syzcnt]=kStd(s_h3,currQuotient,(tHomog)FALSE,NULL,NULL);
2977      syzcnt++;
2978      idDelete(&s_h3);
2979    } /* end if flag >0 */
2980    else
2981    {
2982      flagcnt++;
2983    }
2984  }
2985  if (flagcnt == N)
2986  {
2987    Print("the input is a two--sided ideal");
2988    return(I);
2989  }
2990  if (syzcnt >0)
2991  {
2992    Print("..computing Intersect of %d modules\n",syzcnt);
2993    if (syzcnt == 1)
2994      SI = S[0];
2995    else
2996      SI = idMultSect(S, syzcnt);
2997    idShow(SI);
2998    MI = idModule2Matrix(SI);
2999    res= idInit(MATCOLS(MI),1);
3000    for (i=1; i<= MATCOLS(MI); i++)
3001    {
3002      p = NULL;
3003      for (j=0; j< idElem(I); j++)
3004      {
3005    q = pCopy(MATELEM(MI,j+1,i));
3006    if (q!=NULL)
3007    {
3008      q = pMult(q,pCopy(I->m[j]));
3009      p = pAdd(p,q);
3010    }
3011      }
3012      res->m[i-1]=p;
3013    }
3014    Print("final std");
3015    res = kStd(res, currQuotient,testHomog,NULL,NULL,0,0,NULL);
3016    idSkipZeroes(res);
3017    return(res);
3018  }
3019  else
3020  {
3021    Print("No syzygies");
3022    return(I);
3023  }
3024}
3025
3026
3027ring nc_rCreateNCcomm(ring r)
3028  /* creates a commutative nc extension; "converts" comm.ring to a Plural ring */
3029{
3030  if (rIsPluralRing(r)) return r;
3031  ring save = currRing;
3032  int WeChangeRing = 0;
3033  if (currRing!=r)
3034  {
3035    rChangeCurrRing(r);
3036    WeChangeRing = 1;
3037  }
3038  r->nc = (nc_struct *)omAlloc0(sizeof(nc_struct));
3039  r->nc->ref = 1;
3040  r->nc->basering = r;
3041  ncRingType(r, nc_comm);
3042  r->nc->IsSkewConstant = 1;
3043 
3044  // no reference increment to the base commutative ring???
3045 
3046  matrix C = mpNew(r->N,r->N);
3047  matrix D = mpNew(r->N,r->N);
3048  int i,j;
3049  for(i=1; i<r->N; i++)
3050  {
3051    for(j=i+1; j<=r->N; j++)
3052    {
3053      MATELEM(C,i,j) = pOne();
3054    }
3055  }
3056  r->nc->C = C;
3057  r->nc->D = D;
3058  if (nc_InitMultiplication(r))
3059  {
3060    WarnS("Error initializing multiplication!");
3061  }
3062  if (WeChangeRing)
3063  {
3064    rChangeCurrRing(save);
3065  }
3066  return r;
3067}
3068
3069poly p_CopyEmbed(poly p, ring srcRing, int shift, int par_shift)
3070  /* NOT USED ANYMORE: replaced by maFindPerm in ring.cc */
3071  /* for use with embeddings: currRing is a sum of smaller rings */
3072  /* and srcRing is one of such smaller rings */
3073  /* shift defines the position of a subring in srcRing */
3074  /* par_shift defines the position of a subfield in basefield of CurrRing */
3075{
3076  if (currRing == srcRing)
3077  {
3078    return(p_Copy(p,currRing));
3079  }
3080  nMapFunc nMap=nSetMap(srcRing);
3081  poly q;
3082  //  if ( nMap == nCopy)
3083  //  {
3084  //    q = prCopyR(p,srcRing);
3085  //  }
3086  //  else
3087  {
3088    int *perm = (int *)omAlloc0((srcRing->N+1)*sizeof(int));
3089    int *par_perm = (int *)omAlloc0((srcRing->P+1)*sizeof(int));
3090    //    int *par_perm = (int *)omAlloc0((srcRing->P+1)*sizeof(int));
3091    int i;
3092    //    if (srcRing->P > 0)
3093    //    {
3094    //      for (i=0; i<srcRing->P; i++)
3095    //  par_perm[i]=-i;
3096    //    }
3097    if ((shift<0) || (shift > currRing->N))
3098    {
3099      Werror("bad shifts in p_CopyEmbed");
3100      return(0);
3101    }
3102    for (i=1; i<= srcRing->N; i++)
3103    {
3104      perm[i] = shift+i;
3105    }
3106    q = pPermPoly(p,perm,srcRing,nMap,par_perm,srcRing->P);
3107  }
3108  return(q);
3109}
3110
3111poly pOppose(ring Rop, poly p)
3112  /* opposes a vector p from Rop to currRing */
3113{
3114  /* the simplest case:*/
3115  if (  Rop == currRing )  return(pCopy(p));
3116  /* check Rop == rOpposite(currRing) */
3117  if ( !rIsLikeOpposite(currRing, Rop) )
3118  {
3119    WarnS("an opposite ring should be used");
3120    return NULL;
3121  }
3122  /* nMapFunc nMap = nSetMap(Rop);*/
3123  /* since we know that basefields coinside! */
3124  int *perm=(int *)omAlloc0((Rop->N+1)*sizeof(int));
3125  if (!p_IsConstantPoly(p, Rop))
3126  {
3127    /* we know perm exactly */
3128    int i;
3129    for(i=1; i<=Rop->N; i++)
3130    {
3131      perm[i] = Rop->N+1-i;
3132    }
3133  }
3134  poly res = pPermPoly(p, perm, Rop, nCopy);
3135  omFreeSize((ADDRESS)perm,(Rop->N+1)*sizeof(int));
3136  return res;
3137}
3138
3139ideal idOppose(ring Rop, ideal I)
3140  /* opposes a module I from Rop to currRing */
3141{
3142  /* the simplest case:*/
3143  if ( Rop == currRing ) return idCopy(I);
3144  /* check Rop == rOpposite(currRing) */
3145  if (!rIsLikeOpposite(currRing, Rop))
3146  {
3147    WarnS("an opposite ring should be used");
3148    return NULL;
3149  }
3150  int i;
3151  ideal idOp = idInit(I->ncols, I->rank);
3152  for (i=0; i< (I->ncols)*(I->nrows); i++)
3153  {
3154    idOp->m[i] = pOppose(Rop,I->m[i]);
3155  }
3156  idTest(idOp);
3157  return idOp;
3158}
3159
3160BOOLEAN rIsLikeOpposite(ring rBase, ring rCandidate)
3161  /* checks whether rings rBase and rCandidate */
3162  /* could be opposite to each other */
3163  /* returns TRUE if it is so */
3164{
3165  /* the same basefield */
3166  int diagnose = TRUE;
3167  ring save = currRing;
3168  rChangeCurrRing(rBase);
3169  nMapFunc nMap = nSetMap(rCandidate);
3170  if (nMap != nCopy) diagnose = FALSE;
3171  rChangeCurrRing(save);
3172  /* same number of variables */
3173  if (rBase->N != rCandidate->N) diagnose = FALSE;
3174  /* nc and comm ring */
3175  if ( rIsPluralRing(rBase) != rIsPluralRing(rCandidate) ) diagnose = FALSE;
3176  /* both are qrings */
3177  /* NO CHECK, since it is used in building opposite qring */
3178  /*  if ( ((rBase->qideal != NULL) && (rCandidate->qideal == NULL)) */
3179  /*       || ((rBase->qideal == NULL) && (rCandidate->qideal != NULL)) ) */
3180  /*  diagnose = FALSE; */
3181  /* TODO: varnames are e->E etc */
3182  return diagnose;
3183}
3184
3185
3186
3187bool nc_SetupQuotient(ring rGR, const ring rG)
3188{
3189  // currently only super-commutative extension deals with factors.
3190  return sca_SetupQuotient(rGR, rG);
3191}
3192
3193
3194#endif
3195
3196
3197// int Commutative_Context(ring r, leftv expression)
3198//   /* returns 1 if expression consists */
3199//   /*  of commutative elements */
3200// {
3201//   /* crucial: poly -> ideal, module, matrix  */
3202// }
3203
3204// int Comm_Context_Poly(ring r, poly p)
3205// {
3206//   poly COMM=r->nc->COMM;
3207//   poly pp=pOne();
3208//   memset(pp->exp,0,r->ExpL_Size*sizeof(long));
3209//   while (p!=NULL)
3210//   {
3211//     for (i=0;i<=r->ExpL_Size;i++)
3212//     {
3213//       if ((p->exp[i]) && (pp->exp[i]))  return(FALSE);
3214//       /* nonzero exponent of non-comm variable */
3215//     }
3216//     pIter(p);
3217//   }
3218//   return(TRUE);
3219// }
Note: See TracBrowser for help on using the repository browser.