source: git/libpolys/polys/nc/old.gring.cc @ 95eb6d

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