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

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