source: git/kernel/gring.cc @ b147507

spielwiese
Last change on this file since b147507 was b147507, checked in by Viktor Levandovskyy <levandov@…>, 20 years ago
*levandov: final and cosmetic changes to Opposite stuff git-svn-id: file:///usr/local/Singular/svn/trunk@7570 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 52.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.15 2004-10-13 10:50:37 levandov Exp $
10 *******************************************************************/
11#include "mod2.h"
12#ifdef HAVE_PLURAL
13#include "gring.h"
14#include "febase.h"
15#include "ring.h"
16#include "polys.h"
17#include "numbers.h"
18#include "ideals.h"
19#include "matpol.h"
20#include "kbuckets.h"
21#include "kstd1.h"
22#include "sbuckets.h"
23#include "prCopy.h"
24#include "p_Mult_q.h"
25
26/* global nc_macros : */
27#define freeT(A,v) omFreeSize((ADDRESS)A,(v+1)*sizeof(int))
28#define freeN(A,k) omFreeSize((ADDRESS)A,k*sizeof(number))
29
30/* poly functions defined in p_Procs : */
31poly nc_pp_Mult_mm(poly p, poly m, const ring r, poly &last)
32{
33  return( nc_p_Mult_mm_Common(p_Copy(p,r), m, 1, r) );
34}
35
36poly nc_p_Mult_mm(poly p, const poly m, const ring r)
37{
38  return( nc_p_Mult_mm_Common(p, m, 1, r) );
39}
40
41poly nc_mm_Mult_p(const poly m, poly p, const ring r)
42{
43  return( nc_p_Mult_mm_Common(p, m, 0, r) );
44}
45
46/* poly nc_p_Mult_mm(poly p, poly m, const ring r); defined below */
47poly nc_p_Minus_mm_Mult_qq(poly p, const poly m, poly q, const ring r)
48{
49  poly mc=p_Neg(p_Copy(m,r),r);
50  poly mmc=nc_mm_Mult_p(mc,p_Copy(q,r),r);
51  p_Delete(&mc,r);
52  p=p_Add_q(p,mmc,r);
53  return(p);
54}
55
56//----------- auxiliary routines--------------------------
57poly _nc_p_Mult_q(poly p, poly q, const int copy, const ring r)
58  /* destroy p,q unless copy=1 */
59{
60  poly res=NULL;
61  poly ghost=NULL;
62  poly qq,pp;
63  if (copy)
64  {
65    qq=p_Copy(q,r);
66    pp=p_Copy(p,r);
67  }
68  else
69  {
70    qq=q;
71    pp=p;
72  }
73  while (qq!=NULL)
74  {
75    res=p_Add_q(res, nc_pp_Mult_mm(pp, p_Head(qq,r), r, ghost), r);
76    qq=p_LmDeleteAndNext(qq,r);
77  }
78  p_Delete(&pp,r);
79  return(res);
80}
81
82poly nc_p_Mult_mm_Common(poly p, const poly m, int side, const ring r)
83/* p is poly, m is mono with coeff, destroys p */
84/* if side==1, computes p_Mult_mm; otherwise, mm_Mult_p */
85{
86  if ((p==NULL) || (m==NULL)) return NULL;
87  /*  if (pNext(p)==NULL) return(nc_mm_Mult_nn(p,pCopy(m),r)); */
88  /* excluded  - the cycle will do it anyway - OK. */
89  if (p_IsConstant(m,r)) return(p_Mult_nn(p,p_GetCoeff(m,r),r));
90
91#ifdef PDEBUG
92  p_Test(p,r);
93  p_Test(m,r);
94#endif
95  poly v=NULL;
96  int rN=r->N;
97  int *P=(int *)omAlloc0((rN+1)*sizeof(int));
98  int *M=(int *)omAlloc0((rN+1)*sizeof(int));
99  /* coefficients: */
100  number cP,cM,cOut;
101  p_GetExpV(m, M, r);
102  cM=p_GetCoeff(m,r);
103  /* components:*/
104  const int expM=p_GetComp(m,r);
105  int expP=0;
106  int expOut=0;
107  /* bucket constraints: */
108  int UseBuckets=1;
109  if (pLength(p)< MIN_LENGTH_BUCKET || TEST_OPT_NOT_BUCKETS) UseBuckets=0;
110  sBucket_pt bu_out;
111  poly out=NULL;
112  if (UseBuckets) bu_out=sBucketCreate(r);
113
114  while (p!=NULL)
115  {
116#ifdef PDEBUG
117    p_Test(p,r);
118#endif
119    expP=p_GetComp(p,r);
120    if (expP==0)
121    {
122      expOut=expM;
123    }
124    else
125    {
126      if (expM==0)
127      {
128        expOut=expP;
129#ifdef PDEBUG
130        if (side) 
131        {
132          Print("Multiplication in the left module from the right");
133        }
134#endif         
135      }
136      else
137      {
138        /* REPORT_ERROR */
139        const char* s;
140        if (side==1) s="nc_p_Mult_mm";
141        else s="nc_mm_Mult_p";
142        Print("%s: exponent mismatch %d and %d\n",s,expP,expM);
143        expOut=0;
144      }
145    }
146    p_GetExpV(p,P,r);
147    cP=p_GetCoeff(p,r);
148    cOut=n_Mult(cP,cM,r);
149    if (side==1)
150    {
151      v = nc_mm_Mult_nn(P, M, r);     
152    }
153    else
154    {
155      v = nc_mm_Mult_nn(M, P, r);
156    }
157    v = p_Mult_nn(v,cOut,r);
158    p_SetCompP(v,expOut,r);
159    if (UseBuckets) sBucket_Add_p(bu_out,v,pLength(v));
160    else out = p_Add_q(out,v,r);
161    p_DeleteLm(&p,r);
162  }
163  freeT(P,rN);
164  freeT(M,rN);
165  if (UseBuckets)
166  {
167    out = NULL;
168    int len = pLength(out);
169    sBucketDestroyAdd(bu_out, &out, &len);
170  }
171#ifdef PDEBUG
172  p_Test(out,r);
173#endif
174  return(out);
175}
176
177poly nc_mm_Mult_nn(int *F0, int *G0, const ring r)
178/* destroys nothing, no coeffs and exps */
179{
180  poly out=NULL;
181  int i,j;
182  int iF,jG,iG;
183  int rN=r->N;
184  int ExpSize=(((rN+1)*sizeof(int)+sizeof(long)-1)/sizeof(long))*sizeof(long);
185
186  int *F=(int *)omAlloc0((rN+1)*sizeof(int));
187  int *G=(int *)omAlloc0((rN+1)*sizeof(int));
188
189  memcpy(F, F0,(rN+1)*sizeof(int));
190  // pExpVectorCopy(F,F0);
191  memcpy(G, G0,(rN+1)*sizeof(int));
192  //  pExpVectorCopy(G,G0);
193  F[0]=0; /* important for p_MemAdd */
194  G[0]=0;
195
196  iF=rN;
197  while ((F[iF]==0)&&(iF>=1)) iF--; /* last exp_num of F */
198  if (iF==0) /* F0 is zero vector */
199  {
200    out=pOne();
201    p_SetExpV(out,G0,r);
202    p_Setm(out,r);
203    freeT(F,rN);
204    freeT(G,rN);
205    return(out);
206  }
207  jG=1;
208  while ((G[jG]==0)&&(jG<rN)) jG++;  /* first exp_num of G */
209  iG=rN;
210  while ((G[iG]==0)&&(iG>1)) iG--;  /* last exp_num of G */
211
212  out=pOne();
213
214  if (iF<=jG)
215    /* i.e. no mixed exp_num , MERGE case */
216  {
217    p_MemAdd_LengthGeneral(F, G, ExpSize/sizeof(long));
218    p_SetExpV(out,F,r);
219    p_Setm(out,r);
220    //    omFreeSize((ADDRESS)F,ExpSize);
221    freeT(F,rN);
222    freeT(G,rN);
223    return(out);
224  }
225
226  number cff=n_Init(1,r);
227  number tmp_num=NULL;
228  int cpower=0;
229
230  if (r->nc->type==nc_skew)
231  {
232    if (r->nc->IsSkewConstant==1)
233    {
234      int tpower=0;
235      for(j=jG; j<=iG; j++)
236      {
237        if (G[j]!=0)
238        {
239          cpower = 0;
240          for(i=j+1; i<=iF; i++)
241          {
242            cpower = cpower + F[i];
243          }
244          cpower = cpower*G[j];
245          tpower = tpower + cpower;
246        }
247      }
248      cff = n_Copy(p_GetCoeff(MATELEM(r->nc->COM,1,2),r),r);
249      nPower(cff,tpower,&tmp_num);
250      n_Delete(&cff,r);
251      cff = tmp_num;
252    }
253    else /* skew commutative with nonequal coeffs */
254    {
255      number totcff=n_Init(1,r);
256      for(j=jG; j<=iG; j++)
257      {
258        if (G[j]!=0)
259        {
260          cpower = 0;
261          for(i=j+1; i<=iF; i++)
262          {
263            if (F[i]!=0)
264            {
265              cpower = F[i]*G[j];
266              cff = n_Copy(p_GetCoeff(MATELEM(r->nc->COM,j,i),r),r);
267              nPower(cff,cpower,&tmp_num);
268              cff = nMult(totcff,tmp_num);
269              nDelete(&totcff);
270              nDelete(&tmp_num);
271              totcff = n_Copy(cff,r);
272              n_Delete(&cff,r);
273            }
274          } /* end 2nd for */
275        }
276      }
277      cff=totcff;
278    }
279    p_MemAdd_LengthGeneral(F, G, ExpSize/sizeof(long));
280    p_SetExpV(out,F,r);
281    p_Setm(out,r);
282    p_SetCoeff(out,cff,r);
283    //    p_MemAdd_NegWeightAdjust(p, r); ??? do we need this?
284    freeT(F,rN);
285    freeT(G,rN);
286    return(out);
287  } /* end nc_skew */
288   
289  /* now we have to destroy out! */
290  p_Delete(&out,r); 
291  out = NULL; 
292
293  if (iG==jG)
294    /* g is univariate monomial */
295  {
296    /*    if (ri->nc->type==nc_skew) -- postpone to TU */
297    out = nc_mm_Mult_uu(F,jG,G[jG],r);
298    freeT(F,rN);
299    freeT(G,rN);
300    return(out);
301  }
302
303  number n1=n_Init(1,r);
304  int *Prv=(int *)omAlloc0((rN+1)*sizeof(int));
305  int *Nxt=(int *)omAlloc0((rN+1)*sizeof(int));
306
307  int *log=(int *)omAlloc0((rN+1)*sizeof(int));
308  int cnt=0; int cnf=0;
309
310  /* splitting F wrt jG */
311  for (i=1;i<=jG;i++)
312  {
313    Prv[i]=F[i]; Nxt[i]=0; /* mult at the very end */
314    if (F[i]!=0) cnf++;
315  }
316
317  if (cnf==0) freeT(Prv,rN);
318
319  for (i=jG+1;i<=rN;i++)
320  {
321    Nxt[i]=F[i];
322    /*    if (cnf!=0)  Prv[i]=0; */
323    if (F[i]!=0)
324    {
325      cnt++;
326    }              /* effective part for F */
327  }
328  freeT(F,rN);
329  cnt=0;
330
331  for (i=1;i<=rN;i++)
332  {
333    if (G[i]!=0)
334    {
335     cnt++;
336     log[cnt]=i;
337     }               /* lG for G */
338   }
339
340/* ---------------------- A C T I O N ------------------------ */
341  poly D=NULL;
342  poly Rout=NULL;
343  number *c=(number *)omAlloc0((rN+1)*sizeof(number));
344  c[0]=n_Init(1,r);
345
346  int *Op=Nxt;
347  int *On=G;
348  int *U=(int *)omAlloc0((rN+1)*sizeof(int));
349
350  for (i=jG;i<=rN;i++) U[i]=Nxt[i]+G[i];  /* make leadterm */
351  Nxt=NULL;
352  G=NULL;
353  cnt=1;
354  int t=0;
355  poly w=NULL;
356  poly Pn=pOne();
357  p_SetExpV(Pn,On,r);
358  p_Setm(Pn,r);
359
360  while (On[iG]!=0)
361  {
362     t=log[cnt];
363
364     w=nc_mm_Mult_uu(Op,t,On[t],r);
365     c[cnt]=n_Mult(c[cnt-1],p_GetCoeff(w,r),r);
366     D = pNext(w);  /* getting coef and rest D */
367     p_DeleteLm(&w,r);
368     w=NULL;
369
370     Op[t] += On[t];   /* update exp_vectors */
371     On[t] = 0;
372
373     if (t!=iG)    /* not the last step */
374     {
375       p_SetExpV(Pn,On,r);
376       p_Setm(Pn,r);
377#ifdef PDEBUG
378       p_Test(Pn,r);
379#endif
380
381//       if (pNext(D)==0)
382// is D a monomial? could be postponed higher
383//       {
384//       Rout=nc_mm_Mult_nn(D,Pn,r);
385//       }
386//       else
387//       {
388       Rout=nc_p_Mult_mm(D,Pn,r);
389//       }
390     }
391     else
392     {
393       Rout=D;
394       D=NULL;
395     }
396
397     if (Rout!=NULL)
398     {
399       Rout=p_Mult_nn(Rout,c[cnt-1],r); /* Rest is ready */
400       out=p_Add_q(out,Rout,r);
401       Rout=NULL;
402     }
403     cnt++;
404  }
405  freeT(On,rN);
406  freeT(Op,rN);
407  p_Delete(&Pn,r);
408  omFreeSize((ADDRESS)log,(rN+1)*sizeof(int));
409
410  /* leadterm and Prv-part */
411
412  Rout=pOne();
413  /* U is lead.monomial */
414  U[0]=0;
415  p_SetExpV(Rout,U,r);
416  p_Setm(Rout,r);  /* use again this name Rout */
417#ifdef PDEBUG
418  p_Test(Rout,r);
419#endif
420  p_SetCoeff(Rout,c[cnt-1],r);
421  out=p_Add_q(out,Rout,r);
422  freeT(U,rN);
423  freeN(c,rN+1);
424  if (cnf!=0)  /* Prv is non-zero vector */
425  {
426    Rout=pOne();
427    Prv[0]=0;
428    p_SetExpV(Rout,Prv,r);
429    p_Setm(Rout,r);
430#ifdef PDEBUG
431    p_Test(Rout,r);
432#endif
433    out=nc_mm_Mult_p(Rout,out,r); /* getting the final result */
434    freeT(Prv,rN);
435    p_Delete(&Rout,r);
436  }
437  return (out);
438}
439
440
441poly nc_mm_Mult_uu(int *F,int jG,int bG, const ring r)
442/* f=mono(F),g=(x_iG)^bG */
443{
444  poly out=NULL;
445  int i;
446  number num=NULL;
447
448  int rN=r->N;
449  int iF=r->N;
450  while ((F[iF]==0)&&(iF>0)) iF-- ;   /* last exponent_num of F */
451
452  if (iF==0)  /* F==zero vector in other words */
453  {
454   out=pOne();
455   p_SetExp(out,jG,bG,r);
456   p_Setm(out,r);
457   return(out);
458  }
459
460  int jF=1;
461  while ((F[jF]==0)&&(jF<=rN)) jF++;  /* first exp of F */
462
463  if (iF<=jG)                       /* i.e. no mixed exp_num */
464  {
465    out=pOne();
466    F[jG]=F[jG]+bG;
467    p_SetExpV(out,F,r);
468    p_Setm(out,r);
469    return(out);
470  }
471
472  if (iF==jF)              /* uni times uni */
473  {
474   out=nc_uu_Mult_ww(iF,F[iF],jG,bG,r);
475   return(out);
476  }
477
478  /* Now: F is mono with >=2 exponents, jG<iF */
479  /* check the quasi-commutative case */
480//   matrix LCOM=r->nc->COM;
481//   number rescoef=n_Init(1,r);
482//   number tmpcoef=n_Init(1,r);
483//   int tmpint;
484//   i=iF;
485//   while (i>=jG+1)
486//     /* all the non-zero exponents */
487//   {
488//     if (MATELEM(LCOM,jG,i)!=NULL)
489//     {
490//       tmpcoef=pGetCoeff(MATELEM(LCOM,jG,i));
491//       tmpint=(int)F[i];
492//       nPower(tmpcoef,F[i],&tmpcoef);
493//       rescoef=nMult(rescoef,tmpcoef);
494//       i--;
495//     }
496//     else
497//     {
498//       if (F[i]!=0) break;
499//     }
500//   }
501//   if (iF==i)
502//   /* no action took place*/
503//   {
504
505//   }
506//   else /* power the result up to bG */
507//   {
508//     nPower(rescoef,bG,&rescoef);
509//     /* + cleanup, post-processing */
510//   }
511
512  int *Prv=(int*)omAlloc0((rN+1)*sizeof(int));
513  int *Nxt=(int*)omAlloc0((rN+1)*sizeof(int));
514  int *lF=(int *)omAlloc0((rN+1)*sizeof(int));
515  int cnt=0; int cnf=0;
516  /* splitting F wrt jG */
517  for (i=1;i<=jG;i++) /* mult at the very end */
518  {
519    Prv[i]=F[i]; Nxt[i]=0;
520    if (F[i]!=0) cnf++;
521  }
522  if (cnf==0)  freeT(Prv,rN);
523  for (i=jG+1;i<=rN;i++)
524  {
525    Nxt[i]=F[i];
526    if (cnf!=0) { Prv[i]=0;}
527    if (F[i]!=0)
528    {
529      cnt++;
530      lF[cnt]=i;
531    }                 /* eff_part,lF_for_F */
532  }
533
534  if (cnt==1) /* Nxt consists of 1 nonzero el-t only */
535  {
536    int q=lF[1];
537    poly Rout=pOne();
538    out=nc_uu_Mult_ww(q,Nxt[q],jG,bG,r);
539    freeT(Nxt,rN);
540
541    if (cnf!=0)
542    {
543       Prv[0]=0;
544       p_SetExpV(Rout,Prv,r);
545       p_Setm(Rout,r);
546#ifdef PDEBUG
547       p_Test(Rout,r);
548#endif
549       freeT(Prv,rN);
550       out=nc_mm_Mult_p(Rout,out,r); /* getting the final result */
551    }
552
553    omFreeSize((ADDRESS)lF,(rN+1)*sizeof(int));
554    p_Delete(&Rout,r);
555    return (out);
556  }
557/* -------------------- MAIN ACTION --------------------- */
558
559  poly D=NULL;
560  poly Rout=NULL;
561  number *c=(number *)omAlloc0((cnt+2)*sizeof(number));
562  c[cnt+1]=n_Init(1,r);
563  i=cnt+2;         /* later in freeN */
564  int *Op=Nxt;
565  int *On=(int *)omAlloc0((rN+1)*sizeof(int));
566  int *U=(int *)omAlloc0((rN+1)*sizeof(int));
567
568
569  //  pExpVectorCopy(U,Nxt);
570  memcpy(U, Nxt,(rN+1)*sizeof(int));
571  U[jG] = U[jG] + bG;
572
573  /* Op=Nxt and initial On=(0); */
574  Nxt=NULL;
575
576  poly Pp;
577  poly Pn;
578  int t=0;
579  int first=lF[1];
580  int nlast=lF[cnt];
581  int kk=0;
582  /*  cnt--;   */
583  /* now lF[cnt] should be <=iF-1 */
584
585  while (Op[first]!=0)
586  {
587     t=lF[cnt];   /* cnt as it was computed */
588
589     poly w=nc_uu_Mult_ww(t,Op[t],jG,bG,r);
590     c[cnt]=n_Copy(p_GetCoeff(w,r),r);
591     D = pNext(w);  /* getting coef and rest D */
592     p_DeleteLm(&w,r);
593     w=NULL;
594
595     Op[t]= 0;
596     Pp=pOne();
597     p_SetExpV(Pp,Op,r);
598     p_Setm(Pp,r);
599
600     if (t<nlast)
601     {
602       kk=lF[cnt+1];
603       On[kk]=F[kk];
604
605       Pn=pOne();
606       p_SetExpV(Pn,On,r);
607       p_Setm(Pn,r);
608
609       if (t!=first)   /* typical expr */
610       {
611         w=nc_p_Mult_mm(D,Pn,r);
612         Rout=nc_mm_Mult_p(Pp,w,r);
613         w=NULL;
614       }
615       else                   /* last step */
616       {
617         On[t]=0;
618         p_SetExpV(Pn,On,r);
619         p_Setm(Pn,r);
620         Rout=nc_p_Mult_mm(D,Pn,r);
621       }
622#ifdef PDEBUG
623       p_Test(Pp,r);
624#endif
625       p_Delete(&Pn,r);
626     }
627     else                     /* first step */
628     {
629       Rout=nc_mm_Mult_p(Pp,D,r);
630     }
631#ifdef PDEBUG
632     p_Test(Pp,r);
633#endif
634     p_Delete(&Pp,r);
635     num=n_Mult(c[cnt+1],c[cnt],r);
636     n_Delete(&c[cnt],r);
637     c[cnt]=num;
638     Rout=p_Mult_nn(Rout,c[cnt+1],r); /* Rest is ready */
639     out=p_Add_q(out,Rout,r);
640     Pp=NULL;
641     cnt--;
642  }
643  /* only to feel safe:*/
644  Pn=Pp=NULL;
645  freeT(On,rN);
646  freeT(Op,rN);
647
648/* leadterm and Prv-part with coef 1 */
649/*  U[0]=exp; */
650/*  U[jG]=U[jG]+bG;  */
651/* make leadterm */
652/* ??????????? we have done it already :-0 */
653  Rout=pOne();
654  p_SetExpV(Rout,U,r);
655  p_Setm(Rout,r);  /* use again this name */
656  p_SetCoeff(Rout,c[cnt+1],r);  /* last computed coef */
657  out=p_Add_q(out,Rout,r);
658  Rout=NULL;
659  freeT(U,rN);
660  freeN(c,i);
661  omFreeSize((ADDRESS)lF,(rN+1)*sizeof(int));
662
663  if (cnf!=0)
664  {
665    Rout=pOne();
666    p_SetExpV(Rout,Prv,r);
667    p_Setm(Rout,r);
668    freeT(Prv,rN);
669    out=nc_mm_Mult_p(Rout,out,r); /* getting the final result */
670    p_Delete(&Rout,r);
671  }
672  return (out);
673}
674
675poly nc_uu_Mult_ww_vert (int i, int a, int j, int b, const ring r)
676{
677  int k,m;
678  int rN=r->N;
679  matrix cMT=r->nc->MT[UPMATELEM(j,i,rN)];         /* cMT=current MT */
680
681  poly x=pOne();p_SetExp(x,j,1,r);p_Setm(x,r);
682/* var(j); */
683  poly y=pOne();p_SetExp(y,i,1,r);p_Setm(y,r);
684/*var(i);  for convenience */
685#ifdef PDEBUG
686  p_Test(x,r);
687  p_Test(y,r);
688#endif
689  poly t=NULL;
690/* ------------ Main Cycles ----------------------------*/
691
692  for (k=2;k<=a;k++)
693  {
694     t = nc_p_CopyGet(MATELEM(cMT,k,1),r);
695
696     if (t==NULL)   /* not computed yet */
697     {
698       t = nc_p_CopyGet(MATELEM(cMT,k-1,1),r);
699       //        t=p_Copy(MATELEM(cMT,k-1,1),r);
700       t = nc_mm_Mult_p(y,t,r);
701       MATELEM(cMT,k,1) = nc_p_CopyPut(t,r);
702       //        omCheckAddr(cMT->m);
703       p_Delete(&t,r);
704     }
705     t=NULL;
706  }
707
708  for (m=2;m<=b;m++)
709  {
710    t = nc_p_CopyGet(MATELEM(cMT,a,m),r);
711    //     t=MATELEM(cMT,a,m);
712    if (t==NULL)   //not computed yet
713    {
714      t = nc_p_CopyGet(MATELEM(cMT,a,m-1),r);
715      //      t=p_Copy(MATELEM(cMT,a,m-1),r);
716      t = nc_p_Mult_mm(t,x,r);
717      MATELEM(cMT,a,m) = nc_p_CopyPut(t,r);
718      //      MATELEM(cMT,a,m) = t;
719      //        omCheckAddr(cMT->m);
720      p_Delete(&t,r);
721    }
722    t=NULL;
723  }
724  p_Delete(&x,r);
725  p_Delete(&y,r);
726  //  t=MATELEM(cMT,a,b);
727  t= nc_p_CopyGet(MATELEM(cMT,a,b),r);
728  //  return(p_Copy(t,r));
729  /* since the last computed element was cMT[a,b] */
730  return(t);
731}
732
733poly nc_uu_Mult_ww (int i, int a, int j, int b, const ring r)
734  /* (x_i)^a times (x_j)^b */
735  /* x_i = y,  x_j = x ! */
736{
737  /* Check zero exceptions, (q-)commutativity and is there something to do? */
738  assume(a!=0);
739  assume(b!=0);
740  poly out=pOne();
741  if (i<=j)
742  {
743    p_SetExp(out,i,a,r);
744    p_AddExp(out,j,b,r);
745    p_Setm(out,r);
746    return(out);
747  }/* zero exeptions and usual case */
748  /*  if ((a==0)||(b==0)||(i<=j)) return(out); */
749
750  if (MATELEM(r->nc->COM,j,i)!=NULL)
751    /* commutative or quasicommutative case */
752  {
753    p_SetExp(out,i,a,r);
754    p_AddExp(out,j,b,r);
755    p_Setm(out,r);
756    if (r->cf->nIsOne(p_GetCoeff(MATELEM(r->nc->COM,j,i),r))) /* commutative case */
757    {
758      return(out);
759    }
760    else
761    {
762      number tmp_number=p_GetCoeff(MATELEM(r->nc->COM,j,i),r); /* quasicommutative case */
763      nPower(tmp_number,a*b,&tmp_number);
764      p_SetCoeff(out,tmp_number,r);
765      return(out);
766    }
767  }/* end_of commutative or quasicommutative case */
768  p_Delete(&out,r);
769
770  /* we are here if  i>j and variables do not commute or quasicommute */
771  /* in fact, now a>=1 and b>=1; and j<i */
772  /* now check whether the polynomial is already computed */
773  int rN=r->N;
774  int vik = UPMATELEM(j,i,rN);
775  int cMTsize=r->nc->MTsize[vik];
776  int newcMTsize=0;
777  newcMTsize=si_max(a,b);
778
779  if (newcMTsize<=cMTsize)
780  {
781    out =  nc_p_CopyGet(MATELEM(r->nc->MT[vik],a,b),r);
782    if (out !=NULL) return (out);
783  }
784  int k,m;
785  if (newcMTsize > cMTsize)
786  {
787    int inM=(((newcMTsize+6)/7)*7);
788    assume (inM>=newcMTsize);
789    newcMTsize = inM;
790    //    matrix tmp = (matrix)omAlloc0(inM*inM*sizeof(poly));
791    matrix tmp = mpNew(newcMTsize,newcMTsize);
792
793    for (k=1;k<=cMTsize;k++)
794    {
795      for (m=1;m<=cMTsize;m++)
796      {
797        out = MATELEM(r->nc->MT[UPMATELEM(j,i,rN)],k,m);
798        if ( out != NULL )
799        {
800          MATELEM(tmp,k,m) = out;/*MATELEM(r->nc->MT[UPMATELEM(j,i,rN)],k,m)*/
801          //           omCheckAddr(tmp->m);
802          MATELEM(r->nc->MT[UPMATELEM(j,i,rN)],k,m)=NULL;
803          //           omCheckAddr(r->nc->MT[UPMATELEM(j,i,rN)]->m);
804        }
805      }
806    }
807    id_Delete((ideal *)&(r->nc->MT[UPMATELEM(j,i,rN)]),r);
808    r->nc->MT[UPMATELEM(j,i,rN)] = tmp;
809    tmp=NULL;
810    r->nc->MTsize[UPMATELEM(j,i,rN)] = newcMTsize;
811  }
812  /* The update of multiplication matrix is finished */
813    pDelete(&out);
814    out = nc_uu_Mult_ww_vert(i, a, j, b, r);
815    //    out = nc_uu_Mult_ww_horvert(i, a, j, b, r);
816    return(out);
817}
818
819poly nc_uu_Mult_ww_horvert (int i, int a, int j, int b, const ring r)
820
821{
822  int k,m;
823  int rN=r->N;
824  matrix cMT=r->nc->MT[UPMATELEM(j,i,rN)];         /* cMT=current MT */
825
826  poly x=pOne();p_SetExp(x,j,1,r);p_Setm(x,r);/* var(j); */
827  poly y=pOne();p_SetExp(y,i,1,r);p_Setm(y,r); /*var(i);  for convenience */
828#ifdef PDEBUG
829  p_Test(x,r);
830  p_Test(y,r);
831#endif
832
833  poly t=NULL;
834
835  int toXY;
836  int toYX;
837
838  if (a==1) /* y*x^b, b>=2 */
839  {
840    toXY=b-1;
841    while ( (MATELEM(cMT,1,toXY)==NULL) && (toXY>=2)) toXY--;
842    for (m=toXY+1;m<=b;m++)
843    {
844      t=MATELEM(cMT,1,m);
845      if (t==NULL)   /* remove after debug */
846      {
847        t = p_Copy(MATELEM(cMT,1,m-1),r);
848        t = nc_p_Mult_mm(t,x,r);
849        MATELEM(cMT,1,m) = t;
850        /*        omCheckAddr(cMT->m); */
851      }
852      else
853      {
854        /* Error, should never get there */
855        WarnS("Error: a=1; MATELEM!=0");
856      }
857      t=NULL;
858    }
859    return(p_Copy(MATELEM(cMT,1,b),r));
860  }
861
862  if (b==1) /* y^a*x, a>=2 */
863  {
864    toYX=a-1;
865    while ( (MATELEM(cMT,toYX,1)==NULL) && (toYX>=2)) toYX--;
866    for (m=toYX+1;m<=a;m++)
867    {
868      t=MATELEM(cMT,m,1);
869      if (t==NULL)   /* remove after debug */
870      {
871        t = p_Copy(MATELEM(cMT,m-1,1),r);
872        t = nc_mm_Mult_p(y,t,r);
873        MATELEM(cMT,m,1) = t;
874        /*        omCheckAddr(cMT->m); */
875      }
876      else
877      {
878        /* Error, should never get there */
879        WarnS("Error: b=1, MATELEM!=0");
880      }
881      t=NULL;
882    }
883    return(p_Copy(MATELEM(cMT,a,1),r));
884  }
885
886/* ------------ Main Cycles ----------------------------*/
887  /*            a>1, b>1              */
888
889  int dXY=0; int dYX=0;
890  /* dXY = distance for computing x-mult, then y-mult */
891  /* dYX = distance for computing y-mult, then x-mult */
892  int toX=a-1; int toY=b-1; /* toX = to axe X, toY = to axe Y */
893  toXY=b-1; toYX=a-1;
894  /* if toX==0, toXY = dist. to computed y * x^toXY */
895  /* if toY==0, toYX = dist. to computed y^toYX * x */
896  while ( (MATELEM(cMT,toX,b)==NULL) && (toX>=1)) toX--;
897  if (toX==0) /* the whole column is not computed yet */
898  {
899    while ( (MATELEM(cMT,1,toXY)==NULL) && (toXY>=1)) toXY--;
900    /* toXY >=1 */
901    dXY=b-1-toXY;
902  }
903  dXY=dXY+a-toX; /* the distance to nearest computed y^toX x^b */
904
905  while ( (MATELEM(cMT,a,toY)==NULL) && (toY>=1)) toY--;
906  if (toY==0) /* the whole row is not computed yet */
907  {
908    while ( (MATELEM(cMT,toYX,1)==NULL) && (toYX>=1)) toYX--;
909    /* toYX >=1 */
910    dYX=a-1-toYX;
911  }
912  dYX=dYX+b-toY; /* the distance to nearest computed y^a x^toY */
913
914  if (dYX>=dXY)
915  {
916    /* first x, then y */
917    if (toX==0) /* start with the row*/
918    {
919      for (m=toXY+1;m<=b;m++)
920      {
921        t=MATELEM(cMT,1,m);
922        if (t==NULL)   /* remove after debug */
923        {
924          t = p_Copy(MATELEM(cMT,1,m-1),r);
925          t = nc_p_Mult_mm(t,x,r);
926          MATELEM(cMT,1,m) = t;
927          /*        omCheckAddr(cMT->m); */
928        }
929        else
930        {
931          /* Error, should never get there */
932          WarnS("dYX>=dXY,toXY; MATELEM==0");
933        }
934        t=NULL;
935      }
936      toX=1; /* y*x^b is computed */
937    }
938    /* Now toX>=1 */
939    for (k=toX+1;k<=a;k++)
940    {
941      t=MATELEM(cMT,k,b);
942      if (t==NULL)   /* remove after debug */
943      {
944        t = p_Copy(MATELEM(cMT,k-1,b),r);
945        t = nc_mm_Mult_p(y,t,r);
946        MATELEM(cMT,k,b) = t;
947        /*        omCheckAddr(cMT->m); */
948      }
949      else
950      {
951        /* Error, should never get there */
952        WarnS("dYX>=dXY,toX; MATELEM==0");
953      }
954      t=NULL;
955    }
956  } /* endif (dYX>=dXY) */
957
958
959  if (dYX<dXY)
960  {
961    /* first y, then x */
962    if (toY==0) /* start with the column*/
963    {
964      for (m=toYX+1;m<=a;m++)
965      {
966        t=MATELEM(cMT,m,1);
967        if (t==NULL)   /* remove after debug */
968        {
969          t = p_Copy(MATELEM(cMT,m-1,1),r);
970          t = nc_mm_Mult_p(y,t,r);
971          MATELEM(cMT,m,1) = t;
972          /*        omCheckAddr(cMT->m); */
973        }
974        else
975        {
976          /* Error, should never get there */
977          WarnS("dYX<dXY,toYX; MATELEM==0");
978        }
979        t=NULL;
980      }
981      toY=1; /* y^a*x is computed */
982    }
983    /* Now toY>=1 */
984    for (k=toY+1;k<=b;k++)
985    {
986      t=MATELEM(cMT,a,k);
987      if (t==NULL)   /* remove after debug */
988      {
989        t = p_Copy(MATELEM(cMT,a,k-1),r);
990        t = nc_p_Mult_mm(t,x,r);
991        MATELEM(cMT,a,k) = t;
992        /*        omCheckAddr(cMT->m); */
993      }
994      else
995      {
996        /* Error, should never get there */
997        WarnS("dYX<dXY,toY; MATELEM==0");
998      }
999      t=NULL;
1000    }
1001  } /* endif (dYX<dXY) */
1002
1003  p_Delete(&x,r);
1004  p_Delete(&y,r);
1005  t=p_Copy(MATELEM(cMT,a,b),r);
1006  return(t);  /* since the last computed element was cMT[a,b] */
1007}
1008
1009
1010/* ----------------------------- Syzygies ---------------------- */
1011
1012/*2
1013* reduction of p2 with p1
1014* do not destroy p1, but p2
1015* p1 divides p2 -> for use in NF algorithm
1016*/
1017
1018poly nc_ReduceSpoly(poly p1, poly p2,poly spNoether, const ring r)
1019{
1020  if (p_GetComp(p1,r)!=p_GetComp(p2,r)
1021  && (p_GetComp(p1,r)!=0)
1022  && (p_GetComp(p2,r)!=0))
1023  {
1024#ifdef PDEBUG
1025    Print("nc_ReduceSpoly: different components");
1026#endif
1027    return(NULL);
1028  }
1029  poly m = pOne();
1030  p_ExpVectorDiff(m,p2,p1,r);
1031  //p_Setm(m,r);
1032#ifdef PDEBUG
1033  p_Test(m,r);
1034#endif
1035  /* pSetComp(m,r)=0? */
1036  poly   N  = nc_mm_Mult_p(m, p_Head(p1,r), r);
1037  number C  = n_Copy( p_GetCoeff(N,  r), r);
1038  number cF = n_Copy( p_GetCoeff(p2, r),r);
1039  /* GCD stuff */
1040  number cG = nGcd(C, cF, r);
1041  if ( !nEqual(cG, n_Init(1,r) ) )
1042  {
1043    cF = nDiv(cF, cG);
1044    C  = nDiv(C,  cG);
1045  }
1046  p2 = p_Mult_nn(p2, C, r);
1047  poly out = nc_mm_Mult_p(m, p_Copy(pNext(p1),r), r);
1048  N = p_Add_q(N, out, r);
1049  p_Test(p2,r);
1050  p_Test(N,r);
1051  number MinusOne = n_Init(-1,r);
1052  if (!n_Equal(cF,MinusOne,r))
1053  {
1054    cF = n_Neg(cF,r);
1055    N  = p_Mult_nn(N, cF, r);
1056    p_Test(N,r);
1057  }
1058  out = p_Add_q(p2,N,r);
1059  p_Test(out,r);
1060  if ( out!=NULL ) pContent(out);
1061  p_Delete(&m,r);
1062  n_Delete(&cF,r);
1063  n_Delete(&C,r);
1064  n_Delete(&MinusOne,r);
1065  return(out);
1066}
1067
1068
1069/*3
1070* reduction of p2 with p1
1071* do not destroy p1 and p2
1072* p1 divides p2 -> for use in NF algorithm
1073*/
1074poly nc_ReduceSpolyNew(poly p1, poly p2,poly spNoether, const ring r)
1075{
1076  return(nc_ReduceSpoly(p1,p_Copy(p2,r),spNoether,r));
1077}
1078
1079/*4
1080* creates the S-polynomial of p1 and p2
1081* do not destroy p1 and p2
1082*/
1083poly nc_CreateSpoly(poly p1, poly p2,poly spNoether, const ring r)
1084{
1085  if ((p_GetComp(p1,r)!=p_GetComp(p2,r))
1086  && (p_GetComp(p1,r)!=0)
1087  && (p_GetComp(p2,r)!=0))
1088  {
1089#ifdef PDEBUG
1090    Print("nc_CreateSpoly : different components!");
1091#endif
1092    return(NULL);
1093  }
1094  if ((r->nc->type==nc_lie) && pHasNotCF(p1,p2)) /* prod crit */
1095  {
1096    return(nc_p_Bracket_qq(pCopy(p2),p1));
1097  }
1098  poly pL=pOne();
1099  poly m1=pOne();
1100  poly m2=pOne();
1101  pLcm(p1,p2,pL);
1102  p_Setm(pL,r);
1103#ifdef PDEBUG
1104  p_Test(pL,r);
1105#endif
1106  p_ExpVectorDiff(m1,pL,p1,r);
1107  //p_SetComp(m1,0,r);
1108  //p_Setm(m1,r);
1109#ifdef PDEBUG
1110  p_Test(m1,r);
1111#endif
1112  p_ExpVectorDiff(m2,pL,p2,r);
1113  //p_SetComp(m2,0,r);
1114  //p_Setm(m2,r);
1115#ifdef PDEBUG
1116  p_Test(m2,r);
1117#endif
1118  p_Delete(&pL,r);
1119  /* zero exponents ! */
1120  poly M1    = nc_mm_Mult_p(m1,p_Head(p1,r),r);
1121  number C1  = n_Copy(p_GetCoeff(M1,r),r);
1122  poly M2    = nc_mm_Mult_p(m2,p_Head(p2,r),r);
1123  number C2  = n_Copy(p_GetCoeff(M2,r),r);
1124  /* GCD stuff */
1125  number C = nGcd(C1,C2,r);
1126  if (!nEqual(C,n_Init(1,r)))
1127  {
1128    C1=nDiv(C1,C);
1129    C2=nDiv(C2,C);
1130  }
1131  M1=p_Mult_nn(M1,C2,r);
1132  p_SetCoeff(m1,C2,r);
1133  number MinusOne=n_Init(-1,r);
1134  if (n_Equal(C1,MinusOne,r))
1135  {
1136    M2=p_Add_q(M1,M2,r);
1137  }
1138  else
1139  {
1140    C1=n_Neg(C1,r);
1141    M2=p_Mult_nn(M2,C1,r);
1142    M2=p_Add_q(M1,M2,r);
1143    p_SetCoeff(m2,C1,r);
1144  }
1145  /* M1 is killed, M2=res = C2 M1 - C1 M2 */
1146  poly tmp=p_Copy(p1,r);
1147  tmp=p_LmDeleteAndNext(tmp,r);
1148  M1=nc_mm_Mult_p(m1,tmp,r);
1149  tmp=p_Copy(p2,r);
1150  tmp=p_LmDeleteAndNext(tmp,r);
1151  M2=p_Add_q(M2,M1,r);
1152  M1=nc_mm_Mult_p(m2,tmp,r);
1153  M2=p_Add_q(M2,M1,r);
1154  p_Delete(&m1,r);
1155  p_Delete(&m2,r);
1156  //  n_Delete(&C1,r);
1157  //  n_Delete(&C2,r);
1158  n_Delete(&MinusOne,r);
1159#ifdef PDEBUG
1160  p_Test(M2,r);
1161#endif
1162  if (M2!=NULL) pContent(M2);
1163  return(M2);
1164}
1165
1166/*5
1167* reduction of tail(q) with p1
1168* lead(p1) divides lead(pNext(q2)) and pNext(q2) is reduced
1169* do not destroy p1, but tail(q)
1170*/
1171void nc_ReduceSpolyTail(poly p1, poly q, poly q2, poly spNoether, const ring r)
1172{
1173  poly a1=p_Head(p1,r);
1174  poly Q=pNext(q2);
1175  number cQ=p_GetCoeff(Q,r);
1176  poly m=pOne();
1177  p_ExpVectorDiff(m,Q,p1,r);
1178  //  p_SetComp(m,0,r);
1179  //p_Setm(m,r);
1180#ifdef PDEBUG
1181  p_Test(m,r);
1182#endif
1183  /* pSetComp(m,r)=0? */
1184  poly M=nc_mm_Mult_p(m,p_Copy(p1,r),r);
1185  number C=p_GetCoeff(M,r);
1186  M=p_Add_q(M,nc_mm_Mult_p(m,p_LmDeleteAndNext(p_Copy(p1,r),r),r),r);
1187  q=p_Mult_nn(q,C,r);
1188  number MinusOne=n_Init(-1,r);
1189  if (!n_Equal(cQ,MinusOne,r))
1190  {
1191    cQ=nNeg(cQ);
1192    M=p_Mult_nn(M,cQ,r);
1193  }
1194  Q=p_Add_q(Q,M,r);
1195  pNext(q2)=Q;
1196
1197  p_Delete(&m,r);
1198  n_Delete(&C,r);
1199  n_Delete(&cQ,r);
1200  n_Delete(&MinusOne,r);
1201  /*  return(q); */
1202}
1203
1204/*6
1205* creates the commutative lcm(lm(p1),lm(p2))
1206* do not destroy p1 and p2
1207*/
1208poly nc_CreateShortSpoly(poly p1, poly p2, const ring r)
1209{
1210  if (p_GetComp(p1,r)!=p_GetComp(p2,r))
1211  {
1212    Print("spShort:exponent mismatch!");
1213    return(NULL);
1214  }
1215  poly m=pOne();
1216  pLcm(p1,p2,m);
1217  p_Setm(m,r);
1218#ifdef PDEBUG
1219  p_Test(m,r);
1220#endif
1221  return(m);
1222}
1223
1224void nc_kBucketPolyRed(kBucket_pt b, poly p, number *c)
1225{
1226  // b will not by multiplied by any constant in this impl.
1227  // ==> *c=1
1228  *c=nInit(1);
1229  poly m=pOne();
1230  pExpVectorDiff(m,kBucketGetLm(b),p);
1231  //pSetm(m);
1232#ifdef PDEBUG
1233  pTest(m);
1234#endif
1235  poly pp=nc_mm_Mult_p(m,pCopy(p),currRing);
1236  pDelete(&m);
1237  number n=nCopy(pGetCoeff(pp));
1238  number MinusOne=nInit(-1);
1239  number nn;
1240  if (!nEqual(n,MinusOne))
1241  {
1242    nn=nNeg(nInvers(n));
1243  }
1244  else nn=nInit(1);
1245  nDelete(&n);
1246  n=nMult(nn,pGetCoeff(kBucketGetLm(b)));
1247  nDelete(&nn);
1248  pp=p_Mult_nn(pp,n,currRing);
1249  nDelete(&n);
1250  nDelete(&MinusOne);
1251  int l=pLength(pp);
1252  kBucket_Add_q(b,pp,&l);
1253}
1254
1255void nc_PolyPolyRed(poly &b, poly p, number *c)
1256  // reduces b with p, do not delete both
1257{
1258  // b will not by multiplied by any constant in this impl.
1259  // ==> *c=1
1260  *c=nInit(1);
1261  poly m=pOne();
1262  pExpVectorDiff(m,pHead(b),p);
1263  //pSetm(m);
1264#ifdef PDEBUG
1265  pTest(m);
1266#endif
1267  poly pp=nc_mm_Mult_p(m,pCopy(p),currRing);
1268  pDelete(&m);
1269  number n=nCopy(pGetCoeff(pp));
1270  number MinusOne=nInit(-1);
1271  number nn;
1272  if (!nEqual(n,MinusOne))
1273  {
1274    nn=nNeg(nInvers(n));
1275  }
1276  else nn=nInit(1);
1277  nDelete(&n);
1278  n=nMult(nn,pGetCoeff(b));
1279  nDelete(&nn);
1280  pp=p_Mult_nn(pp,n,currRing);
1281  nDelete(&n);
1282  nDelete(&MinusOne);
1283  b=p_Add_q(b,pp,currRing);
1284}
1285
1286poly nc_p_Bracket_qq(poly p, poly q)
1287  /* returns [p,q], destroys p */
1288{
1289  if (!rIsPluralRing(currRing)) return(NULL);
1290  if (pComparePolys(p,q)) return(NULL);
1291  /* Components !? */
1292  poly Q=NULL;
1293  number coef=NULL;
1294  poly res=NULL;
1295  poly pres=NULL;
1296  int UseBuckets=1;
1297  if ((pLength(p)< MIN_LENGTH_BUCKET/2) && (pLength(q)< MIN_LENGTH_BUCKET/2) || TEST_OPT_NOT_BUCKETS) UseBuckets=0;
1298  sBucket_pt bu_out;
1299  if (UseBuckets) bu_out=sBucketCreate(currRing);
1300  while (p!=NULL)
1301  {
1302    Q=q;
1303    while(Q!=NULL)
1304    {
1305      pres=nc_mm_Bracket_nn(p,Q); /* since no coeffs are taken into account there */
1306      if (pres!=NULL)
1307      {
1308        coef = nMult(pGetCoeff(p),pGetCoeff(Q));
1309        pres = p_Mult_nn(pres,coef,currRing);
1310        if (UseBuckets) sBucket_Add_p(bu_out,pres,pLength(pres));
1311        else res=p_Add_q(res,pres,currRing);
1312        nDelete(&coef);
1313      }
1314      pIter(Q);
1315    }
1316    p=pLmDeleteAndNext(p);
1317  }
1318  if (UseBuckets)
1319  {
1320    res = NULL;
1321    int len = pLength(res);
1322    sBucketDestroyAdd(bu_out, &res, &len);
1323  }
1324  return(res);
1325}
1326
1327poly nc_mm_Bracket_nn(poly m1, poly m2)
1328  /*returns [m1,m2] for two monoms, destroys nothing */
1329  /* without coeffs */
1330{
1331  if (pLmIsConstant(m1) || pLmIsConstant(m1)) return(NULL);
1332  if (pLmCmp(m1,m2)==0) return(NULL);
1333  int rN=currRing->N;
1334  int *M1=(int *)omAlloc0((rN+1)*sizeof(int));
1335  int *M2=(int *)omAlloc0((rN+1)*sizeof(int));
1336  int *PREFIX=(int *)omAlloc0((rN+1)*sizeof(int));
1337  int *SUFFIX=(int *)omAlloc0((rN+1)*sizeof(int));
1338  pGetExpV(m1,M1);
1339  pGetExpV(m2,M2);
1340  poly res=NULL;
1341  poly ares=NULL;
1342  poly bres=NULL;
1343  poly prefix=NULL;
1344  poly suffix=NULL;
1345  int nMin,nMax;
1346  number nTmp=NULL;
1347  int i,j,k;
1348  for (i=1;i<=rN;i++)
1349  {
1350    if (M2[i]!=0)
1351    {
1352      ares=NULL;
1353      for (j=1;j<=rN;j++)
1354      {
1355        if (M1[j]!=0)
1356        {
1357          bres=NULL;
1358          /* compute [ x_j^M1[j],x_i^M2[i] ] */
1359          if (i<j) {nMax=j;  nMin=i;} else {nMax=i;  nMin=j;}
1360          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)*/
1361          { bres=NULL; }
1362          else
1363          {
1364            if (i<j) { bres=nc_uu_Mult_ww(j,M1[j],i,M2[i],currRing); }
1365            else bres=nc_uu_Mult_ww(i,M2[i],j,M1[j],currRing);
1366            if (nIsOne(pGetCoeff(bres)))
1367            {
1368              bres=pLmDeleteAndNext(bres);
1369            }
1370            else
1371            {
1372              nTmp=nSub(pGetCoeff(bres),nInit(1));
1373              pSetCoeff(bres,nTmp); /* only lc ! */
1374            }
1375#ifdef PDEBUG
1376            pTest(bres);
1377#endif
1378            if (i>j)  bres=p_Neg(bres, currRing);
1379          }
1380          if (bres!=NULL)
1381          {
1382            /* now mult (prefix, bres, suffix) */
1383            memcpy(SUFFIX, M1,(rN+1)*sizeof(int));
1384            memcpy(PREFIX, M1,(rN+1)*sizeof(int));
1385            for (k=1;k<=j;k++) SUFFIX[k]=0;
1386            for (k=j;k<=rN;k++) PREFIX[k]=0;
1387            SUFFIX[0]=0;
1388            PREFIX[0]=0;
1389            prefix=pOne();
1390            suffix=pOne();
1391            pSetExpV(prefix,PREFIX);
1392            pSetm(prefix);
1393            pSetExpV(suffix,SUFFIX);
1394            pSetm(suffix);
1395            if (!pLmIsConstant(prefix)) bres = nc_mm_Mult_p(prefix, bres,currRing);
1396            if (!pLmIsConstant(suffix)) bres = nc_p_Mult_mm(bres, suffix,currRing);
1397            ares=p_Add_q(ares, bres,currRing);
1398            /* What to give free? */
1399            /* Do we have to free PREFIX/SUFFIX? it seems so */
1400            pDelete(&prefix);
1401            pDelete(&suffix);
1402          }
1403        }
1404      }
1405      if (ares!=NULL)
1406      {
1407        /* now mult (prefix, bres, suffix) */
1408        memcpy(SUFFIX, M2,(rN+1)*sizeof(int));
1409        memcpy(PREFIX, M2,(rN+1)*sizeof(int));
1410        for (k=1;k<=i;k++) SUFFIX[k]=0;
1411        for (k=i;k<=rN;k++) PREFIX[k]=0;
1412        SUFFIX[0]=0;
1413        PREFIX[0]=0;
1414        prefix=pOne();
1415        suffix=pOne();
1416        pSetExpV(prefix,PREFIX);
1417        pSetm(prefix);
1418        pSetExpV(suffix,SUFFIX);
1419        pSetm(suffix);
1420        bres=ares;
1421        if (!pLmIsConstant(prefix)) bres = nc_mm_Mult_p(prefix, bres,currRing);
1422        if (!pLmIsConstant(suffix)) bres = nc_p_Mult_mm(bres, suffix,currRing);
1423        res=p_Add_q(res, bres,currRing);
1424        pDelete(&prefix);
1425        pDelete(&suffix);
1426      }
1427    }
1428  }
1429  freeT(M1, rN);
1430  freeT(M2, rN);
1431  freeT(PREFIX, rN);
1432  freeT(SUFFIX, rN);
1433  pTest(res);
1434  return(res);
1435}
1436
1437ideal twostd(ideal I)
1438{
1439  int i;
1440  int j;
1441  int s;
1442  int flag;
1443  poly p=NULL;
1444  poly q=NULL;
1445  ideal J=kStd(I, currQuotient,testHomog,NULL,NULL,0,0,NULL);
1446  idSkipZeroes(J);
1447  ideal K=NULL;
1448  poly varj=NULL;
1449  ideal Q=NULL;
1450  ideal id_tmp=NULL;
1451  int rN=currRing->N;
1452  int iSize=0;
1453  loop
1454  {
1455    flag=0;
1456    K=NULL;
1457    s=idElem(J);
1458    for (i=0;i<=s-1;i++)
1459    {
1460      p=J->m[i];
1461      for (j=1;j<=rN;j++)
1462      {
1463        varj = pOne();
1464        pSetExp(varj,j,1);
1465        pSetm(varj);
1466        q = nc_p_Mult_mm(pCopy(p),varj,currRing);
1467        pDelete(&varj);
1468        q = nc_ReduceSpoly(p,q,NULL,currRing);
1469        q = kNF(J,currQuotient,q,0,0);
1470        if (q!=NULL)
1471        {
1472          if (pIsConstant(q))
1473          {
1474            Q=idInit(1,1);
1475            Q->m[0]=pOne();
1476            idDelete(&J);
1477            pDelete(&q);
1478            if (K!=NULL) idDelete(&K);
1479            return(Q);
1480          }
1481          flag=1;
1482          Q=idInit(1,1);
1483          Q->m[0]=q;
1484          id_tmp=idSimpleAdd(K,Q);
1485          idDelete(&K);
1486          K=id_tmp;
1487          idDelete(&Q);
1488        }
1489      }
1490    }
1491    if (flag==0)
1492      /* i.e. all elements are two-sided */
1493    {
1494      idDelete(&K);
1495      return(J);
1496    }
1497    /* now we update GrBasis J with K */
1498    //    iSize=IDELEMS(J);
1499    iSize=idElem(J);
1500    id_tmp=idSimpleAdd(J,K);
1501    idDelete(&K);
1502    idDelete(&J); 
1503    BITSET save_test=test;
1504    test|=Sy_bit(OPT_SB_1);
1505    J=kStd(id_tmp, currQuotient, testHomog,NULL,NULL,0,iSize);
1506    test=save_test;
1507    idSkipZeroes(J);
1508  }
1509}
1510
1511matrix nc_PrintMat(int a, int b, ring r, int metric)
1512  /* returns matrix with the info on noncomm multiplication */
1513{
1514
1515  if ( (a==b) || !rIsPluralRing(r) ) return(NULL);
1516  int i;
1517  int j;
1518  if (a>b) {j=b; i=a;}
1519  else {j=a; i=b;}
1520  /* i<j */
1521  int rN=r->N;
1522  int size=r->nc->MTsize[UPMATELEM(i,j,rN)];
1523  matrix M = r->nc->MT[UPMATELEM(i,j,rN)];
1524  /*  return(M); */
1525  int sizeofres;
1526  if (metric==0)
1527  {
1528    sizeofres=sizeof(int);
1529  }
1530  if (metric==1)
1531  {
1532    sizeofres=sizeof(number);
1533  }
1534  matrix res=mpNew(size,size);
1535  int s;
1536  int t;
1537  int length;
1538  long totdeg;
1539  poly p;
1540  for(s=1;s<=size;s++)
1541  {
1542    for(t=1;t<=size;t++)
1543    {
1544      p=MATELEM(M,s,t);
1545      if (p==NULL)
1546      {
1547        MATELEM(res,s,t)=0;
1548      }
1549      else
1550      {
1551        length = pLength(p);
1552        if (metric==0) /* length */
1553        {
1554          MATELEM(res,s,t)= p_ISet(length,r);
1555        }
1556        else if (metric==1) /* sum of deg divided by the length */
1557        {
1558          totdeg=0;
1559          while (p!=NULL)
1560          {
1561            totdeg=totdeg+pDeg(p,r);
1562            pIter(p);
1563          }
1564          number ntd = nInit(totdeg);
1565          number nln = nInit(length);
1566          number nres=nDiv(ntd,nln);
1567          nDelete(&ntd);
1568          nDelete(&nln);
1569          MATELEM(res,s,t)=p_NSet(nres,r);
1570        }
1571      }
1572    }
1573  }
1574  return(res);
1575}
1576
1577void ncKill(ring r)
1578  /* kills the nc extension of ring r */
1579{
1580  int i,j;
1581  int rN=r->N;
1582  if ( rN > 1 )
1583  {
1584    for(i=1;i<rN;i++)
1585    {
1586      for(j=i+1;j<=rN;j++)
1587      {
1588        id_Delete((ideal *)&(r->nc->MT[UPMATELEM(i,j,rN)]),r->nc->basering);
1589      }
1590    }
1591    omFreeSize((ADDRESS)r->nc->MT,rN*(rN-1)/2*sizeof(matrix));
1592    omFreeSize((ADDRESS)r->nc->MTsize,rN*(rN-1)/2*sizeof(int));
1593    id_Delete((ideal *)&(r->nc->COM),r->nc->basering);
1594  }
1595  id_Delete((ideal *)&(r->nc->C),r->nc->basering);
1596  id_Delete((ideal *)&(r->nc->D),r->nc->basering);
1597  omFreeSize((ADDRESS)r->nc,sizeof(nc_struct));
1598  r->nc=NULL;
1599}
1600
1601void ncCleanUp(ring r)
1602{
1603  /* small CleanUp of r->nc */
1604  omFreeSize((ADDRESS)r->nc,sizeof(nc_struct));
1605  r->nc = NULL;
1606}
1607
1608poly nc_p_CopyGet(poly a, const ring r)
1609/* for use in getting the mult. matrix elements*/
1610{
1611  if (!rIsPluralRing(r)) return(p_Copy(a,r));
1612  if (r==r->nc->basering) return(p_Copy(a,r));
1613  else
1614  {
1615    return(prCopyR_NoSort(a,r->nc->basering,r));
1616  }
1617}
1618
1619poly nc_p_CopyPut(poly a, const ring r)
1620/* for use in defining the mult. matrix elements*/
1621{
1622  if (!rIsPluralRing(r)) return(p_Copy(a,r));
1623  if (r==r->nc->basering) return(p_Copy(a,r));
1624  else
1625  {
1626    return(prCopyR_NoSort(a,r,r->nc->basering));
1627  }
1628}
1629
1630int nc_CheckSubalgebra(poly PolyVar, ring r)
1631  /* returns TRUE if product of vars from PolyVar defines */
1632  /* an admissible subalgebra of r */
1633{
1634  int rN=r->N;
1635  int *ExpVar=(int*)omAlloc0((rN+1)*sizeof(int));
1636  int *ExpTmp=(int*)omAlloc0((rN+1)*sizeof(int));
1637  p_GetExpV(PolyVar, ExpVar, r);
1638  int i; int j; int k;
1639  poly test=NULL;
1640  int OK=1;
1641  for (i=1;i<rN;i++)
1642  {
1643    if (ExpVar[i]==0) /* i.e. not in PolyVar */
1644    { 
1645      for (j=i+1;j<=rN;j++)
1646      {
1647        if (ExpVar[j]==0)
1648        {
1649          test=nc_p_CopyGet(MATELEM(r->nc->D,i,j),r);
1650          while (test!=NULL)
1651          {
1652            p_GetExpV(test, ExpTmp, r);
1653            OK=1;
1654            for (k=1;k<=rN;k++)
1655            {
1656              if (ExpTmp[k]!=0)
1657              {
1658                if (ExpVar[k]!=0) OK=0;
1659              }
1660            }
1661            if (!OK) return(FALSE);
1662            pIter(test);
1663          }
1664        }
1665      }
1666    }
1667  }
1668  p_Delete(&test,r);
1669  freeT(ExpVar,rN);
1670  freeT(ExpTmp,rN);
1671  return(TRUE);
1672}
1673
1674// int Commutative_Context(ring r, leftv expression)
1675//   /* returns 1 if expression consists */
1676//   /*  of commutative elements */
1677// {
1678//   /* crucial: poly -> ideal, module, matrix  */
1679 
1680// }
1681
1682// int Comm_Context_Poly(ring r, poly p)
1683// {
1684//   poly COMM=r->nc->COMM;
1685//   poly pp=pOne();
1686//   memset(pp->exp,0,r->ExpL_Size*sizeof(long));
1687//   while (p!=NULL)
1688//   {
1689//     for (i=0;i<=r->ExpL_Size;i++)
1690//     {
1691//       if ((p->exp[i]) && (pp->exp[i]))  return(FALSE);
1692//       /* nonzero exponent of non-comm variable */
1693//     }
1694//     pIter(p);
1695//   }
1696//   return(TRUE);
1697// }
1698
1699BOOLEAN nc_CallPlural(matrix CCC, matrix DDD, poly CCN, poly DDN, ring r)
1700  /* returns TRUE if there were errors */
1701  /* analyze inputs, check them for consistency */
1702  /* detect nc_type, DO NOT initialize multiplication */
1703  /* check the ordering condition and evtl. NDC */
1704{
1705  matrix CC = NULL; 
1706  matrix DD = NULL;
1707  poly CN = NULL;
1708  poly DN = NULL;
1709  matrix C;
1710  matrix D;
1711  number nN,pN,qN;
1712  int tmpIsSkewConstant;
1713  int i,j;
1714  if (r->nc != NULL)
1715  {
1716    WarnS("redefining algebra structure");
1717    if (r->nc->ref>1) /* in use by somebody else */
1718    {
1719      r->nc->ref--;
1720    }
1721    else  /* kill the previous nc data */
1722    {
1723      ncKill(r); 
1724    }
1725  }
1726  r->nc = (nc_struct *)omAlloc0(sizeof(nc_struct));
1727  r->nc->ref = 1;
1728  r->nc->basering = r;
1729  r->nc->type = nc_undef;
1730
1731  /* initialition of the matrix C */
1732  /* check the correctness of arguments */
1733
1734  if ((CCC != NULL) && ( (MATCOLS(CCC)==1) || MATROWS(CCC)==1 ) )
1735  {
1736    CN = MATELEM(CCC,1,1);
1737  }
1738  else 
1739  {
1740    if ((CCC != NULL) && ( (MATCOLS(CCC)!=r->N) || (MATROWS(CCC)!=r->N) ))
1741    {
1742      Werror("Square %d x %d  matrix expected",r->N,r->N);
1743      ncCleanUp(r);
1744      return TRUE;
1745    }
1746  }
1747  if (( CCC != NULL) && (CC == NULL)) CC = mpCopy(CCC);
1748  if (( CCN != NULL) && (CN == NULL)) CN = CCN;
1749
1750  /* initialition of the matrix D */
1751  /* check the correctness of arguments */
1752
1753  if ((DDD != NULL) && ( (MATCOLS(DDD)==1) || MATROWS(DDD)==1 ) )
1754  {
1755    DN = MATELEM(DDD,1,1);
1756  }
1757  else 
1758  {
1759    if ((DDD != NULL) && ( (MATCOLS(DDD)!=r->N) || (MATROWS(DDD)!=r->N) ))
1760    {
1761      Werror("Square %d x %d  matrix expected",r->N,r->N);
1762      ncCleanUp(r);
1763      return TRUE;
1764    }
1765  }
1766  if (( DDD != NULL) && (DD == NULL)) DD = mpCopy(DDD);
1767  if (( DDN != NULL) && (DN == NULL)) DN = DDN;
1768
1769  /* further checks */
1770
1771  if (CN != NULL)       /* create matrix C = CN * Id */
1772  {
1773    nN = p_GetCoeff(CN,r);
1774    if (n_IsZero(nN,r))
1775    {
1776      Werror("Incorrect input : zero coefficients are not allowed");
1777      ncCleanUp(r);
1778      return TRUE;
1779    }
1780    if (nIsOne(nN)) 
1781    {
1782      r->nc->type = nc_lie; 
1783    }
1784    else 
1785    {
1786      r->nc->type = nc_general;
1787    }
1788    r->nc->IsSkewConstant = 1;
1789    C = mpNew(r->N,r->N);
1790    for(i=1; i<r->N; i++)
1791    {
1792      for(j=i+1; j<=r->N; j++)
1793      {
1794        MATELEM(C,i,j) = nc_p_CopyPut(CN,r);
1795      }
1796    }
1797  }
1798  if ( (CN == NULL) && (CC != NULL) ) /* copy matrix C */
1799  {
1800    C = mpCopy(CC);
1801    /* analyze C */
1802    pN = p_GetCoeff(MATELEM(C,1,2),r);
1803    tmpIsSkewConstant = 1;
1804    for(i=1; i<r->N; i++)
1805    {
1806      for(j=i+1; j<=r->N; j++)
1807      { 
1808        qN = p_GetCoeff(MATELEM(C,i,j),r);
1809        if ( qN == NULL )   /* check the consistency: Cij!=0 */
1810        {
1811          Werror("Incorrect input : matrix of coefficients contains zeros in the upper triangle");
1812          ncCleanUp(r);
1813          return TRUE;
1814        }
1815        if (!nEqual(pN,qN)) tmpIsSkewConstant = 0;
1816      }
1817    }
1818    r->nc->IsSkewConstant=tmpIsSkewConstant;
1819    if ( (tmpIsSkewConstant) && (nIsOne(pN)) ) 
1820    {
1821      r->nc->type = nc_lie;
1822    }
1823    else 
1824    {
1825      r->nc->type = nc_general;
1826    }
1827  }
1828
1829  /* initialition of the matrix D */
1830  if ( DD == NULL ) 
1831    /* we treat DN only (it could also be NULL) */
1832  {
1833    D = mpNew(r->N,r->N);
1834    if (DN  == NULL)
1835    {
1836      if ( (currRing->nc->type == nc_lie) || (currRing->nc->type == nc_undef) ) 
1837      {
1838        currRing->nc->type = nc_comm; /* it was nc_skew earlier */
1839      }
1840      else /* nc_general, nc_skew */
1841      {
1842        currRing->nc->type = nc_skew;
1843      }
1844    }
1845    else /* DN  != NULL */
1846    { 
1847      for(i=1; i<r->N; i++)
1848      {
1849        for(j=i+1; j<=r->N; j++)
1850        {
1851          MATELEM(D,i,j) = nc_p_CopyPut(DN,r);
1852        }
1853      }
1854    }
1855  }
1856  else /* DD != NULL */
1857  { 
1858    D = mpCopy(DD); 
1859  }
1860  /* analyze D */ 
1861  /* check the ordering condition for D (both matrix and poly cases) */
1862  poly p,q;
1863  int report = 1;
1864  for(i=1; i<r->N; i++)
1865  {
1866    for(j=i+1; j<=r->N; j++)
1867    { 
1868      p = MATELEM(D,i,j);
1869      if ( p != NULL)
1870      {
1871        q = pOne();
1872        p_SetExp(q,i,1,r);
1873        p_SetExp(q,j,1,r);
1874        p_Setm(q,r);
1875        if (p_LmCmp(q,p,r) != 1) /* i.e. lm(p)<=lm(q)  */
1876        {
1877          Print("Bad ordering at %d,%d",i,j);
1878          report = 0;
1879        }
1880        p_Delete(&q,r);
1881        p = NULL;
1882      }
1883    }
1884  }
1885  if (!report)
1886  {
1887    Werror("Matrix of polynomials violates the ordering condition");
1888    ncCleanUp(r);
1889    return TRUE;
1890  }
1891  r->nc->C = C;
1892  r->nc->D = D;
1893  return nc_InitMultiplication(r);
1894}
1895
1896BOOLEAN nc_InitMultiplication(ring r)
1897{
1898  /* returns TRUE if there were errors */
1899  /* initialize the multiplication: */
1900  /*  r->nc->MTsize, r->nc->MT, r->nc->COM, */
1901  /* and r->nc->IsSkewConstant for the skew case */
1902  if (rVar(r)==1)
1903  {
1904    r->nc->type=nc_comm;
1905    r->nc->IsSkewConstant=1;
1906    return FALSE;
1907  }
1908  int i,j;
1909  matrix COM;
1910  r->nc->MT = (matrix *)omAlloc0(r->N*(r->N-1)/2*sizeof(matrix));
1911  r->nc->MTsize = (int *)omAlloc0(r->N*(r->N-1)/2*sizeof(int));
1912  COM = mpCopy(r->nc->C);
1913  poly p,q;
1914  short DefMTsize=7;
1915  int IsNonComm=0;
1916  int tmpIsSkewConstant;
1917 
1918  for(i=1; i<r->N; i++)
1919  {
1920    for(j=i+1; j<=r->N; j++)
1921    {
1922      if ( MATELEM(r->nc->D,i,j) == NULL ) /* quasicommutative case */
1923      {
1924        /* 1x1 mult.matrix */
1925        r->nc->MTsize[UPMATELEM(i,j,r->N)] = 1;
1926        r->nc->MT[UPMATELEM(i,j,r->N)] = mpNew(1,1);
1927      }
1928      else /* pure noncommutative case */
1929      {
1930        /* TODO check the special multiplication properties */
1931        IsNonComm = 1;
1932        MATELEM(COM,i,j) = NULL;
1933        r->nc->MTsize[UPMATELEM(i,j,r->N)] = DefMTsize; /* default sizes */
1934        r->nc->MT[UPMATELEM(i,j,r->N)] = mpNew(DefMTsize, DefMTsize);
1935      }
1936      /* set MT[i,j,1,1] to c_i_j*x_i*x_j + D_i_j */
1937      p = pOne();
1938      p_SetCoeff(p,nCopy(pGetCoeff(MATELEM(r->nc->C,i,j))),r);
1939      p_SetExp(p,i,1,r);
1940      p_SetExp(p,j,1,r);
1941      p_Setm(p,r);
1942      q =  nc_p_CopyGet(MATELEM(r->nc->D,i,j),r);
1943      p = p_Add_q(p,q,r);
1944      MATELEM(r->nc->MT[UPMATELEM(i,j,r->N)],1,1) = nc_p_CopyPut(p,r);
1945      pDelete(&p);
1946      p = NULL;
1947    }
1948  }
1949  if (r->nc->type==nc_undef)
1950  {
1951    if (IsNonComm==1)
1952    {
1953      //      assume(pN!=NULL);
1954      //      if ((tmpIsSkewConstant==1) && (nIsOne(pGetCoeff(pN)))) r->nc->type=nc_lie;
1955      //      else r->nc->type=nc_general;
1956    }
1957    if (IsNonComm==0) 
1958    {
1959      r->nc->type=nc_skew; /* TODO: check whether it is commutative */
1960      r->nc->IsSkewConstant=tmpIsSkewConstant;
1961    }
1962  }
1963  r->nc->COM=COM;
1964  return FALSE;
1965}
1966
1967/* substitute the n-th variable by e in p
1968* destroy p
1969* e is not a constant
1970*/
1971poly nc_pSubst(poly p, int n, poly e)
1972{
1973  int rN=currRing->N;
1974  int *PRE = (int *)omAlloc0((rN+1)*sizeof(int));
1975  int *SUF = (int *)omAlloc0((rN+1)*sizeof(int));
1976  int i,j,pow;
1977  number C;
1978  poly suf,pre;
1979  poly res = NULL;
1980  poly out = NULL;
1981  while ( p!= NULL )
1982  {
1983    C =  pGetCoeff(p);
1984    pGetExpV(p, PRE); /* faster splitting? */
1985    pow = PRE[n]; PRE[n]=0;
1986    res = NULL;
1987    if (pow!=0)
1988    {
1989      for (i=n+1; i<=rN; i++)
1990      {
1991        SUF[i] = PRE[i];
1992        PRE[i] = 0;
1993      }
1994      res =  pPower(pCopy(e),pow);
1995      /* multiply with prefix */
1996      pre = pOne();
1997      pSetExpV(pre,PRE);
1998      pSetm(pre);
1999      pSetComp(pre,PRE[0]);
2000      res = nc_mm_Mult_p(pre,res,currRing);
2001      /* multiply with suffix */
2002      suf = pOne();
2003      pSetExpV(suf,SUF);
2004      pSetm(suf);
2005      pSetComp(suf,PRE[0]);
2006      res = nc_p_Mult_mm(res,suf,currRing);
2007      res = p_Mult_nn(res,C,currRing);
2008    }
2009    else /* pow==0 */
2010    {
2011      res = pHead(p);
2012    }
2013    p   = pLmDeleteAndNext(p);
2014    out = pAdd(out,res);
2015  }
2016  freeT(PRE,rN);
2017  freeT(SUF,rN);
2018  return(out);
2019}
2020
2021static ideal idPrepareStd(ideal T, ideal s,  int k)
2022{
2023  /* T is a left SB, without zeros, s is a list with zeros */
2024#ifdef PDEBUG
2025  if (IDELEMS(s)!=IDELEMS(T))
2026  {
2027    Print("ideals of diff. size!!!");
2028  }
2029#endif
2030  ideal t = idCopy(T);
2031  int j,rs=idRankFreeModule(s),rt=idRankFreeModule(t);
2032  poly p,q;
2033
2034  ideal res = idInit(2*idElem(t),1+idElem(t));
2035  if (rs == 0)
2036  {
2037    for (j=0; j<IDELEMS(t); j++)
2038    {
2039      if (s->m[j]!=NULL) pSetCompP(s->m[j],1);
2040      if (t->m[j]!=NULL) pSetCompP(t->m[j],1);
2041    }
2042    k = si_max(k,1);
2043  }
2044  for (j=0; j<IDELEMS(t); j++)
2045  {
2046    if (s->m[j]!=NULL)
2047    {
2048      p = s->m[j];
2049      q = pOne();
2050      pSetComp(q,k+1+j);
2051      pSetmComp(q);
2052#if 0     
2053      while (pNext(p)) pIter(p);
2054      pNext(p) = q;
2055#else
2056      p = pAdd(p,q);
2057      s->m[j] = p;
2058#ifdef PDEBUG
2059    pTest(p);
2060#endif
2061#endif
2062    }
2063  }
2064  res = idSimpleAdd(t,s);
2065  idDelete(&t);
2066  res->rank = 1+idElem(T);
2067  return(res);
2068}
2069
2070ideal Approx_Step(ideal L)
2071{
2072  int N=currRing->N;
2073  int i,j; // k=syzcomp
2074  int flag, flagcnt, syzcnt=0;
2075  int syzcomp = 0;
2076  int k=1; /* for ideals not modules */
2077  ideal I = kStd(L, currQuotient,testHomog,NULL,NULL,0,0,NULL);
2078  idSkipZeroes(I);
2079  ideal s_I;
2080  int idI = idElem(I);
2081  ideal trickyQuotient,s_trickyQuotient;
2082  if (currQuotient !=NULL)
2083  {
2084    trickyQuotient = idSimpleAdd(currQuotient,I);
2085  }
2086  else
2087    trickyQuotient = I;
2088  idSkipZeroes(trickyQuotient);
2089  poly *var = (poly *)omAlloc0((N+1)*sizeof(poly));
2090  //  poly *W = (poly *)omAlloc0((2*N+1)*sizeof(poly));
2091  resolvente S = (resolvente)omAlloc0((N+1)*sizeof(ideal));
2092  ideal SI, res;
2093  matrix MI;
2094  poly x=pOne();
2095  var[0]=x;
2096  ideal   h2, h3, s_h2, s_h3;
2097  poly    p,q,qq;
2098  /* init vars */
2099  for (i=1; i<=N; i++ )
2100  {
2101    x = pOne();
2102    pSetExp(x,i,1);
2103    pSetm(x);
2104    var[i]=pCopy(x);
2105  }
2106  /* init NF's */
2107  for (i=1; i<=N; i++ )
2108  {
2109    h2 = idInit(idI,1);
2110    flag = 0;
2111    for (j=0; j< idI; j++ )
2112    {
2113      q = nc_p_Mult_mm(pCopy(I->m[j]),var[i],currRing);
2114      q = kNF(I,currQuotient,q,0,0);
2115      if (q!=0)
2116      {
2117        h2->m[j]=pCopy(q);
2118        //      pShift(&(h2->m[flag]),1);
2119        flag++;
2120        pDelete(&q);
2121      }
2122      else
2123        h2->m[j]=0;
2124    }
2125    /* W[1..idElems(I)] */
2126    if (flag >0)
2127    {
2128      /* compute syzygies with values in I*/
2129      //      idSkipZeroes(h2);
2130      //      h2 = idSimpleAdd(h2,I);
2131      //      h2->rank=flag+idI+1;
2132      idTest(h2);
2133      idShow(h2);
2134      ring orig_ring=currRing;
2135      ring syz_ring=rCurrRingAssure_SyzComp();
2136      syzcomp = 1;
2137      rSetSyzComp(syzcomp);
2138      if (orig_ring != syz_ring)
2139      {
2140        s_h2=idrCopyR_NoSort(h2,orig_ring);
2141        //      s_trickyQuotient=idrCopyR_NoSort(trickyQuotient,orig_ring);
2142        //      rDebugPrint(syz_ring);
2143        s_I=idrCopyR_NoSort(I,orig_ring);
2144      }
2145      else
2146      {
2147        s_h2 = h2;
2148        s_I  = I;
2149        //      s_trickyQuotient=trickyQuotient;
2150      }
2151      idTest(s_h2);
2152      //      idTest(s_trickyQuotient);
2153      Print(".proceeding with the variable %d\n",i);
2154      s_h3 = idPrepareStd(s_I, s_h2, 1);
2155      BITSET save_test=test;
2156      test|=Sy_bit(OPT_SB_1);
2157      idTest(s_h3);
2158      idDelete(&s_h2);
2159      s_h2=idCopy(s_h3);
2160      idDelete(&s_h3);
2161      Print("...computing Syz");
2162      s_h3 = kStd(s_h2, currQuotient,(tHomog)FALSE,NULL,NULL,syzcomp,idI);
2163      test=save_test;
2164      idShow(s_h3);
2165      if (orig_ring != syz_ring)
2166      {
2167        idDelete(&s_h2);
2168        for (j=0; j<IDELEMS(s_h3); j++)
2169        {
2170          if (s_h3->m[j] != NULL)
2171          {
2172            if (p_MinComp(s_h3->m[j],syz_ring) > syzcomp) /* i.e. it is a syzygy */
2173              pShift(&s_h3->m[j], -syzcomp);
2174            else
2175              pDelete(&s_h3->m[j]);
2176          }
2177        }
2178        idSkipZeroes(s_h3);
2179        s_h3->rank -= syzcomp;
2180        rChangeCurrRing(orig_ring);
2181        //      s_h3 = idrMoveR_NoSort(s_h3, syz_ring);
2182        s_h3 = idrMoveR_NoSort(s_h3, syz_ring);
2183        rKill(syz_ring);
2184      }
2185      idTest(s_h3);
2186      S[syzcnt]=kStd(s_h3,currQuotient,(tHomog)FALSE,NULL,NULL);
2187      syzcnt++;
2188      idDelete(&s_h3);
2189    } /* end if flag >0 */
2190    else 
2191    {
2192      flagcnt++;
2193    }
2194  }
2195  if (flagcnt == N) 
2196  {
2197    Print("the input is a two--sided ideal");
2198    return(I);
2199  }
2200  if (syzcnt >0)
2201  {
2202    Print("..computing Intersect of %d modules\n",syzcnt);
2203    if (syzcnt == 1)
2204      SI = S[0];
2205    else
2206      SI = idMultSect(S, syzcnt);
2207    idShow(SI);
2208    MI = idModule2Matrix(SI);
2209    res= idInit(MATCOLS(MI),1);
2210    for (i=1; i<= MATCOLS(MI); i++)
2211    {   
2212      p = NULL;
2213      for (j=0; j< idElem(I); j++)
2214      { 
2215        q = pCopy(MATELEM(MI,j+1,i));
2216        if (q!=NULL)
2217        {
2218          q = pMult(q,pCopy(I->m[j]));
2219          p = pAdd(p,q);
2220        }
2221      }
2222      res->m[i-1]=p;
2223    }
2224    Print("final std");
2225    res = kStd(res, currQuotient,testHomog,NULL,NULL,0,0,NULL);
2226    idSkipZeroes(res);
2227    return(res);
2228  }
2229  else
2230  {
2231    Print("No syzygies");
2232    return(I);
2233  }
2234}
2235
2236
2237ring nc_rCreateNCcomm(ring r)
2238  /* creates a commutative nc extension; "converts" comm.ring to a Plural ring */
2239{
2240  if (rIsPluralRing(r)) return r;
2241  r->nc = (nc_struct *)omAlloc0(sizeof(nc_struct));
2242  r->nc->ref = 1;
2243  r->nc->basering = r;
2244  r->nc->type = nc_comm;
2245  r->nc->IsSkewConstant = 1;
2246  matrix C = mpNew(r->N,r->N);
2247  matrix D = mpNew(r->N,r->N);
2248  int i,j;
2249  for(i=1; i<r->N; i++)
2250  {
2251    for(j=i+1; j<=r->N; j++)
2252    {
2253      MATELEM(C,i,j) = pOne();
2254    }
2255  }
2256  r->nc->C = C;
2257  r->nc->D = D;
2258  if (nc_InitMultiplication(r))
2259  {
2260    WarnS("Error initializing multiplication!");
2261  }
2262  return r;
2263}
2264
2265poly p_CopyEmbed(poly p, ring srcRing, int shift, int par_shift)
2266  /* NOT USED ANYMORE: replaced by maFindPerm in ring.cc */
2267  /* for use with embeddings: currRing is a sum of smaller rings */
2268  /* and srcRing is one of such smaller rings */
2269  /* shift defines the position of a subring in srcRing */
2270  /* par_shift defines the position of a subfield in basefield of CurrRing */
2271{
2272  if (currRing == srcRing)
2273  {
2274    return(p_Copy(p,currRing));
2275  }
2276  nMapFunc nMap=nSetMap(srcRing);
2277  poly q;
2278  //  if ( nMap == nCopy)
2279  //  {
2280  //    q = prCopyR(p,srcRing);
2281  //  }
2282  //  else
2283  {
2284    int *perm = (int *)omAlloc0((srcRing->N+1)*sizeof(int));
2285    int *par_perm = (int *)omAlloc0((srcRing->P+1)*sizeof(int));
2286    //    int *par_perm = (int *)omAlloc0((srcRing->P+1)*sizeof(int));
2287    int i;
2288    //    if (srcRing->P > 0)
2289    //    {
2290    //      for (i=0; i<srcRing->P; i++)
2291    //  par_perm[i]=-i;
2292    //    }
2293    if ((shift<0) || (shift > currRing->N))
2294    {
2295      Werror("bad shifts in p_CopyEmbed");
2296      return(0);
2297    }
2298    for (i=1; i<= srcRing->N; i++)
2299    {
2300      perm[i] = shift+i;
2301    }
2302    q = pPermPoly(p,perm,srcRing,nMap,par_perm,srcRing->P);
2303  }
2304  return(q);
2305}
2306
2307poly p_Oppose(ring Rop, poly p)
2308{
2309  /* TODO check Rop == rOpposite(currRing) */
2310  /* the same basefield, same number of variables */
2311
2312  /* the simplest case:*/
2313  if (Rop==currRing) return pCopy(p); // ok
2314  nMapFunc nMap=nSetMap(Rop);
2315  int *perm=(int *)omAlloc0((Rop->N+1)*sizeof(int));
2316  /* we know perm exactly */
2317  int i;
2318  for(i=1; i<=Rop->N; i++)
2319  {
2320    perm[i] = Rop->N+1-i;
2321  }
2322  /*  int *par_perm=NULL; */
2323  poly res = pPermPoly(p, perm, Rop, nMap);
2324  omFreeSize((ADDRESS)perm,(Rop->N+1)*sizeof(int));
2325  //omFreeSize((ADDRESS)par_perm,rPar(r)*sizeof(int));
2326  return res;
2327}
2328
2329
2330#endif
Note: See TracBrowser for help on using the repository browser.