source: git/kernel/gring.cc @ 8fbec5

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