source: git/kernel/polys1.cc @ 338842d

spielwiese
Last change on this file since 338842d was 0ffc823, checked in by Viktor Levandovskyy <levandov@…>, 15 years ago
*levandov: early unit detection in rational GB git-svn-id: file:///usr/local/Singular/svn/trunk@11495 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 35.5 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: polys1.cc,v 1.37 2009-02-27 19:30:47 levandov Exp $ */
5
6/*
7* ABSTRACT - all basic methods to manipulate polynomials:
8* independent of representation
9*/
10
11/* includes */
12#include <string.h>
13#include "mod2.h"
14#include "structs.h"
15#include "numbers.h"
16#include "ffields.h"
17#include "omalloc.h"
18#include "febase.h"
19#include "weight.h"
20#include "intvec.h"
21#include "longalg.h"
22#include "ring.h"
23#include "ideals.h"
24#include "polys.h"
25//#include "ipid.h"
26#ifdef HAVE_FACTORY
27#include "clapsing.h"
28#endif
29
30#ifdef HAVE_RATGRING
31#include "ratgring.h"
32#endif
33
34/*-------- several access procedures to monomials -------------------- */
35/*
36* the module weights for std
37*/
38static pFDegProc pOldFDeg;
39static pLDegProc pOldLDeg;
40static intvec * pModW;
41static BOOLEAN pOldLexOrder;
42
43static long pModDeg(poly p, ring r = currRing)
44{
45  long d=pOldFDeg(p, r);
46  int c=p_GetComp(p, r);
47  if ((c>0) && (pModW->range(c-1))) d+= (*pModW)[c-1];
48  return d;
49  //return pOldFDeg(p, r)+(*pModW)[p_GetComp(p, r)-1];
50}
51
52void pSetModDeg(intvec *w)
53{
54  if (w!=NULL)
55  {
56    pModW = w;
57    pOldFDeg = pFDeg;
58    pOldLDeg = pLDeg;
59    pOldLexOrder = pLexOrder;
60    pSetDegProcs(pModDeg);
61    pLexOrder = TRUE;
62  }
63  else
64  {
65    pModW = NULL;
66    pRestoreDegProcs(pOldFDeg, pOldLDeg);
67    pLexOrder = pOldLexOrder;
68  }
69}
70
71
72/*2
73* subtract p2 from p1, p1 and p2 are destroyed
74* do not put attention on speed: the procedure is only used in the interpreter
75*/
76poly pSub(poly p1, poly p2)
77{
78  return pAdd(p1, pNeg(p2));
79}
80
81/*3
82*  create binomial coef.
83*/
84static number* pnBin(int exp)
85{
86  int e, i, h;
87  number x, y, *bin=NULL;
88
89  x = nInit(exp);
90  if (nIsZero(x))
91  {
92    nDelete(&x);
93    return bin;
94  }
95  h = (exp >> 1) + 1;
96  bin = (number *)omAlloc0(h*sizeof(number));
97  bin[1] = x;
98  if (exp < 4)
99    return bin;
100  i = exp - 1;
101  for (e=2; e<h; e++)
102  {
103      x = nInit(i);
104      i--;
105      y = nMult(x,bin[e-1]);
106      nDelete(&x);
107      x = nInit(e);
108      bin[e] = nIntDiv(y,x);
109      nDelete(&x);
110      nDelete(&y);
111  }
112  return bin;
113}
114
115static void pnFreeBin(number *bin, int exp)
116{
117  int e, h = (exp >> 1) + 1;
118
119  if (bin[1] != NULL)
120  {
121    for (e=1; e<h; e++)
122      nDelete(&(bin[e]));
123  }
124  omFreeSize((ADDRESS)bin, h*sizeof(number));
125}
126
127/*3
128* compute for a monomial m
129* the power m^exp, exp > 1
130* destroys p
131*/
132static poly pMonPower(poly p, int exp)
133{
134  int i;
135
136  if(!nIsOne(pGetCoeff(p)))
137  {
138    number x, y;
139    y = pGetCoeff(p);
140    nPower(y,exp,&x);
141    nDelete(&y);
142    pSetCoeff0(p,x);
143  }
144  for (i=pVariables; i!=0; i--)
145  {
146    pMultExp(p,i, exp);
147  }
148  pSetm(p);
149  return p;
150}
151
152/*3
153* compute for monomials p*q
154* destroys p, keeps q
155*/
156static void pMonMult(poly p, poly q)
157{
158  number x, y;
159  int i;
160
161  y = pGetCoeff(p);
162  x = nMult(y,pGetCoeff(q));
163  nDelete(&y);
164  pSetCoeff0(p,x);
165  //for (i=pVariables; i!=0; i--)
166  //{
167  //  pAddExp(p,i, pGetExp(q,i));
168  //}
169  //p->Order += q->Order;
170  pExpVectorAdd(p,q);
171}
172
173/*3
174* compute for monomials p*q
175* keeps p, q
176*/
177static poly pMonMultC(poly p, poly q)
178{
179  number x;
180  int i;
181  poly r = pInit();
182
183  x = nMult(pGetCoeff(p),pGetCoeff(q));
184  pSetCoeff0(r,x);
185  pExpVectorSum(r,p, q);
186  return r;
187}
188
189/*
190*  compute for a poly p = head+tail, tail is monomial
191*          (head + tail)^exp, exp > 1
192*          with binomial coef.
193*/
194static poly pTwoMonPower(poly p, int exp)
195{
196  int eh, e;
197  long al;
198  poly *a;
199  poly tail, b, res, h;
200  number x;
201  number *bin = pnBin(exp);
202
203  tail = pNext(p);
204  if (bin == NULL)
205  {
206    pMonPower(p,exp);
207    pMonPower(tail,exp);
208#ifdef PDEBUG
209    pTest(p);
210#endif
211    return p;
212  }
213  eh = exp >> 1;
214  al = (exp + 1) * sizeof(poly);
215  a = (poly *)omAlloc(al);
216  a[1] = p;
217  for (e=1; e<exp; e++)
218  {
219    a[e+1] = pMonMultC(a[e],p);
220  }
221  res = a[exp];
222  b = pHead(tail);
223  for (e=exp-1; e>eh; e--)
224  {
225    h = a[e];
226    x = nMult(bin[exp-e],pGetCoeff(h));
227    pSetCoeff(h,x);
228    pMonMult(h,b);
229    res = pNext(res) = h;
230    pMonMult(b,tail);
231  }
232  for (e=eh; e!=0; e--)
233  {
234    h = a[e];
235    x = nMult(bin[e],pGetCoeff(h));
236    pSetCoeff(h,x);
237    pMonMult(h,b);
238    res = pNext(res) = h;
239    pMonMult(b,tail);
240  }
241  pDeleteLm(&tail);
242  pNext(res) = b;
243  pNext(b) = NULL;
244  res = a[exp];
245  omFreeSize((ADDRESS)a, al);
246  pnFreeBin(bin, exp);
247//  tail=res;
248// while((tail!=NULL)&&(pNext(tail)!=NULL))
249// {
250//   if(nIsZero(pGetCoeff(pNext(tail))))
251//   {
252//     pDeleteLm(&pNext(tail));
253//   }
254//   else
255//     pIter(tail);
256// }
257#ifdef PDEBUG
258  pTest(res);
259#endif
260  return res;
261}
262
263static poly pPow(poly p, int i)
264{
265  poly rc = pCopy(p);
266  i -= 2;
267  do
268  {
269    rc = pMult(rc,pCopy(p));
270    pNormalize(rc);
271    i--;
272  }
273  while (i != 0);
274  return pMult(rc,p);
275}
276
277/*2
278* returns the i-th power of p
279* p will be destroyed
280*/
281poly pPower(poly p, int i)
282{
283  poly rc=NULL;
284
285  if (i==0)
286  {
287    pDelete(&p);
288    return pOne();
289  }
290
291  if(p!=NULL)
292  {
293    if ( (i > 0) && ((unsigned long ) i > (currRing->bitmask)))
294    {
295      Werror("exponent %d is too large, max. is %d",i,currRing->bitmask);
296      return NULL;
297    }
298    switch (i)
299    {
300// cannot happen, see above
301//      case 0:
302//      {
303//        rc=pOne();
304//        pDelete(&p);
305//        break;
306//      }
307      case 1:
308        rc=p;
309        break;
310      case 2:
311        rc=pMult(pCopy(p),p);
312        break;
313      default:
314        if (i < 0)
315        {
316          pDelete(&p);
317          return NULL;
318        }
319        else
320        {
321#ifdef HAVE_PLURAL
322          if (rIsPluralRing(currRing)) /* in the NC case nothing helps :-( */
323          {
324            int j=i;
325            rc = pCopy(p);
326            while (j>1)
327            {
328              rc = pMult(pCopy(p),rc);
329              j--;
330            }
331            pDelete(&p);
332            return rc;
333          }
334#endif
335          rc = pNext(p);
336          if (rc == NULL)
337            return pMonPower(p,i);
338          /* else: binom ?*/
339          int char_p=rChar(currRing);
340          if ((pNext(rc) != NULL)
341#ifdef HAVE_RINGS
342             || rField_is_Ring(currRing)
343#endif
344             )
345            return pPow(p,i);
346          if ((char_p==0) || (i<=char_p))
347            return pTwoMonPower(p,i);
348          poly p_p=pTwoMonPower(pCopy(p),char_p);
349          return pMult(pPower(p,i-char_p),p_p);
350        }
351      /*end default:*/
352    }
353  }
354  return rc;
355}
356
357/*2
358* returns the partial differentiate of a by the k-th variable
359* does not destroy the input
360*/
361poly pDiff(poly a, int k)
362{
363  poly res, f, last;
364  number t;
365
366  last = res = NULL;
367  while (a!=NULL)
368  {
369    if (pGetExp(a,k)!=0)
370    {
371      f = pLmInit(a);
372      t = nInit(pGetExp(a,k));
373      pSetCoeff0(f,nMult(t,pGetCoeff(a)));
374      nDelete(&t);
375      if (nIsZero(pGetCoeff(f)))
376        pDeleteLm(&f);
377      else
378      {
379        pDecrExp(f,k);
380        pSetm(f);
381        if (res==NULL)
382        {
383          res=last=f;
384        }
385        else
386        {
387          pNext(last)=f;
388          last=f;
389        }
390      }
391    }
392    pIter(a);
393  }
394  return res;
395}
396
397static poly pDiffOpM(poly a, poly b,BOOLEAN multiply)
398{
399  int i,j,s;
400  number n,h,hh;
401  poly p=pOne();
402  n=nMult(pGetCoeff(a),pGetCoeff(b));
403  for(i=pVariables;i>0;i--)
404  {
405    s=pGetExp(b,i);
406    if (s<pGetExp(a,i))
407    {
408      nDelete(&n);
409      pDeleteLm(&p);
410      return NULL;
411    }
412    if (multiply)
413    {
414      for(j=pGetExp(a,i); j>0;j--)
415      {
416        h = nInit(s);
417        hh=nMult(n,h);
418        nDelete(&h);
419        nDelete(&n);
420        n=hh;
421        s--;
422      }
423      pSetExp(p,i,s);
424    }
425    else
426    {
427      pSetExp(p,i,s-pGetExp(a,i));
428    }
429  }
430  pSetm(p);
431  /*if (multiply)*/ pSetCoeff(p,n);
432  return p;
433}
434
435poly pDiffOp(poly a, poly b,BOOLEAN multiply)
436{
437  poly result=NULL;
438  poly h;
439  for(;a!=NULL;pIter(a))
440  {
441    for(h=b;h!=NULL;pIter(h))
442    {
443      result=pAdd(result,pDiffOpM(a,h,multiply));
444    }
445  }
446  return result;
447}
448
449
450void pSplit(poly p, poly *h)
451{
452  *h=pNext(p);
453  pNext(p)=NULL;
454}
455
456
457
458int pMaxCompProc(poly p)
459{
460  return pMaxComp(p);
461}
462
463/*2
464* handle memory request for sets of polynomials (ideals)
465* l is the length of *p, increment is the difference (may be negative)
466*/
467void pEnlargeSet(polyset *p, int l, int increment)
468{
469  int i;
470  polyset h;
471
472  h=(polyset)omReallocSize((poly*)*p,l*sizeof(poly),(l+increment)*sizeof(poly));
473  if (increment>0)
474  {
475    //for (i=l; i<l+increment; i++)
476    //  h[i]=NULL;
477    memset(&(h[l]),0,increment*sizeof(poly));
478  }
479  *p=h;
480}
481
482number pInitContent(poly ph);
483number pInitContent_a(poly ph);
484
485void pContent(poly ph)
486{
487#ifdef HAVE_RINGS
488  if (rField_is_Ring(currRing))
489  {
490    if ((ph!=NULL) && rField_has_Units(currRing))
491    {
492      number k = nGetUnit(pGetCoeff(ph));
493      if (!nIsOne(k))
494      {
495        number tmpGMP = k;
496        k = nInvers(k);
497        nDelete(&tmpGMP);
498        poly h = pNext(ph);
499        pSetCoeff(ph, nMult(pGetCoeff(ph), k));
500        while (h != NULL)
501        {
502          pSetCoeff(h, nMult(pGetCoeff(h), k));
503          pIter(h);
504        }
505      }
506      nDelete(&k);
507    }
508    return;
509  }
510#endif
511  number h,d;
512  poly p;
513
514  if(TEST_OPT_CONTENTSB) return;
515  if(pNext(ph)==NULL)
516  {
517    pSetCoeff(ph,nInit(1));
518  }
519  else
520  {
521    nNormalize(pGetCoeff(ph));
522    if(!nGreaterZero(pGetCoeff(ph))) ph = pNeg(ph);
523    if (rField_is_Q())
524    {
525      h=pInitContent(ph);
526      p=ph;
527    }
528    else if ((rField_is_Extension())
529    && ((rPar(currRing)>1)||(currRing->minpoly==NULL)))
530    {
531      h=pInitContent_a(ph);
532      p=ph;
533    }
534    else
535    {
536      h=nCopy(pGetCoeff(ph));
537      p = pNext(ph);
538    }
539    while (p!=NULL)
540    {
541      nNormalize(pGetCoeff(p));
542      d=nGcd(h,pGetCoeff(p),currRing);
543      nDelete(&h);
544      h = d;
545      if(nIsOne(h))
546      {
547        break;
548      }
549      pIter(p);
550    }
551    p = ph;
552    //number tmp;
553    if(!nIsOne(h))
554    {
555      while (p!=NULL)
556      {
557        //d = nDiv(pGetCoeff(p),h);
558        //tmp = nIntDiv(pGetCoeff(p),h);
559        //if (!nEqual(d,tmp))
560        //{
561        //  StringSetS("** div0:");nWrite(pGetCoeff(p));StringAppendS("/");
562        //  nWrite(h);StringAppendS("=");nWrite(d);StringAppendS(" int:");
563        //  nWrite(tmp);Print(StringAppendS("\n"));
564        //}
565        //nDelete(&tmp);
566        d = nIntDiv(pGetCoeff(p),h);
567        pSetCoeff(p,d);
568        pIter(p);
569      }
570    }
571    nDelete(&h);
572#ifdef HAVE_FACTORY
573    if ( (nGetChar() == 1) || (nGetChar() < 0) ) /* Q[a],Q(a),Zp[a],Z/p(a) */
574    {
575      singclap_divide_content(ph);
576      if(!nGreaterZero(pGetCoeff(ph))) ph = pNeg(ph);
577    }
578#endif
579    if (rField_is_Q_a())
580    {
581      number hzz = nlInit(1);
582      h = nlInit(1);
583      p=ph;
584      while (p!=NULL)
585      { // each monom: coeff in Q_a
586        lnumber c_n_n=(lnumber)pGetCoeff(p);
587        napoly c_n=c_n_n->z;
588        while (c_n!=NULL)
589        { // each monom: coeff in Q
590          d=nlLcm(hzz,pGetCoeff(c_n),currRing->algring);
591          n_Delete(&hzz,currRing->algring);
592          hzz=d;
593          pIter(c_n);
594        }
595        c_n=c_n_n->n;
596        while (c_n!=NULL)
597        { // each monom: coeff in Q
598          d=nlLcm(h,pGetCoeff(c_n),currRing->algring);
599          n_Delete(&h,currRing->algring);
600          h=d;
601          pIter(c_n);
602        }
603        pIter(p);
604      }
605      /* hzz contains the 1/lcm of all denominators in c_n_n->z*/
606      /* h contains the 1/lcm of all denominators in c_n_n->n*/
607      number htmp=nlInvers(h);
608      number hzztmp=nlInvers(hzz);
609      number hh=nlMult(hzz,h);
610      nlDelete(&hzz,currRing->algring);
611      nlDelete(&h,currRing->algring);
612      number hg=nlGcd(hzztmp,htmp,currRing->algring);
613      nlDelete(&hzztmp,currRing->algring);
614      nlDelete(&htmp,currRing->algring);
615      h=nlMult(hh,hg);
616      nlDelete(&hg,currRing->algring);
617      nlDelete(&hh,currRing->algring);
618      nlNormalize(h);
619      if(!nlIsOne(h))
620      {
621        p=ph;
622        while (p!=NULL)
623        { // each monom: coeff in Q_a
624          lnumber c_n_n=(lnumber)pGetCoeff(p);
625          napoly c_n=c_n_n->z;
626          while (c_n!=NULL)
627          { // each monom: coeff in Q
628            d=nlMult(h,pGetCoeff(c_n));
629            nlNormalize(d);
630            nlDelete(&pGetCoeff(c_n),currRing->algring);
631            pGetCoeff(c_n)=d;
632            pIter(c_n);
633          }
634          c_n=c_n_n->n;
635          while (c_n!=NULL)
636          { // each monom: coeff in Q
637            d=nlMult(h,pGetCoeff(c_n));
638            nlNormalize(d);
639            nlDelete(&pGetCoeff(c_n),currRing->algring);
640            pGetCoeff(c_n)=d;
641            pIter(c_n);
642          }
643          pIter(p);
644        }
645      }
646      nlDelete(&h,currRing->algring);
647    }
648  }
649}
650void pSimpleContent(poly ph,int smax)
651{
652  if(TEST_OPT_CONTENTSB) return;
653  if (ph==NULL) return;
654  if (pNext(ph)==NULL)
655  {
656    pSetCoeff(ph,nInit(1));
657    return;
658  }
659  if ((pNext(pNext(ph))==NULL)||(!rField_is_Q()))
660  {
661    return;
662  }
663  number d=pInitContent(ph);
664  if (nlSize(d)<=smax)
665  {
666    //if (TEST_OPT_PROT) PrintS("G");
667    return;
668  }
669  poly p=ph;
670  number h=d;
671  if (smax==1) smax=2;
672  while (p!=NULL)
673  {
674#if 0
675    d=nlGcd(h,pGetCoeff(p),currRing);
676    nlDelete(&h,currRing);
677    h = d;
678#else
679    nlInpGcd(h,pGetCoeff(p),currRing);
680#endif
681    if(nlSize(h)<smax)
682    {
683      //if (TEST_OPT_PROT) PrintS("g");
684      return;
685    }
686    pIter(p);
687  }
688  p = ph;
689  if (!nlGreaterZero(pGetCoeff(p))) h=nlNeg(h);
690  if(nlIsOne(h)) return;
691  //if (TEST_OPT_PROT) PrintS("c");
692  while (p!=NULL)
693  {
694#if 1
695    d = nlIntDiv(pGetCoeff(p),h);
696    pSetCoeff(p,d);
697#else
698    nlInpIntDiv(pGetCoeff(p),h,currRing);
699#endif
700    pIter(p);
701  }
702  nlDelete(&h,currRing);
703}
704
705number pInitContent(poly ph)
706// only for coefficients in Q
707#if 0
708{
709  assume(!TEST_OPT_CONTENTSB);
710  assume(ph!=NULL);
711  assume(pNext(ph)!=NULL);
712  assume(rField_is_Q());
713  if (pNext(pNext(ph))==NULL)
714  {
715    return nlGetNom(pGetCoeff(pNext(ph)),currRing);
716  }
717  poly p=ph;
718  number n1=nlGetNom(pGetCoeff(p),currRing);
719  pIter(p);
720  number n2=nlGetNom(pGetCoeff(p),currRing);
721  pIter(p);
722  number d;
723  number t;
724  loop
725  {
726    nlNormalize(pGetCoeff(p));
727    t=nlGetNom(pGetCoeff(p),currRing);
728    if (nlGreaterZero(t))
729      d=nlAdd(n1,t);
730    else
731      d=nlSub(n1,t);
732    nlDelete(&t,currRing);
733    nlDelete(&n1,currRing);
734    n1=d;
735    pIter(p);
736    if (p==NULL) break;
737    nlNormalize(pGetCoeff(p));
738    t=nlGetNom(pGetCoeff(p),currRing);
739    if (nlGreaterZero(t))
740      d=nlAdd(n2,t);
741    else
742      d=nlSub(n2,t);
743    nlDelete(&t,currRing);
744    nlDelete(&n2,currRing);
745    n2=d;
746    pIter(p);
747    if (p==NULL) break;
748  }
749  d=nlGcd(n1,n2,currRing);
750  nlDelete(&n1,currRing);
751  nlDelete(&n2,currRing);
752  return d;
753}
754#else
755{
756  number d=pGetCoeff(ph);
757  if(SR_HDL(d)&SR_INT) return d;
758  int s=mpz_size1(&d->z);
759  int s2=-1;
760  number d2;
761  loop
762  {
763    pIter(ph);
764    if(ph==NULL)
765    {
766      if (s2==-1) return nlCopy(d);
767      break;
768    }
769    if (SR_HDL(pGetCoeff(ph))&SR_INT)
770    {
771      s2=s;
772      d2=d;
773      s=0;
774      d=pGetCoeff(ph);
775      if (s2==0) break;
776    }
777    else
778    if (mpz_size1(&(pGetCoeff(ph)->z))<=s)
779    {
780      s2=s;
781      d2=d;
782      d=pGetCoeff(ph);
783      s=mpz_size1(&d->z);
784    }
785  }
786  return nlGcd(d,d2,currRing);
787}
788#endif
789
790number pInitContent_a(poly ph)
791// only for coefficients in K(a) anf K(a,...)
792{
793  number d=pGetCoeff(ph);
794  int s=naParDeg(d);
795  if (s /* naParDeg(d)*/ <=1) return naCopy(d);
796  int s2=-1;
797  number d2;
798  int ss;
799  loop
800  {
801    pIter(ph);
802    if(ph==NULL)
803    {
804      if (s2==-1) return naCopy(d);
805      break;
806    }
807    if ((ss=naParDeg(pGetCoeff(ph)))<s)
808    {
809      s2=s;
810      d2=d;
811      s=ss;
812      d=pGetCoeff(ph);
813      if (s2<=1) break;
814    }
815  }
816  return naGcd(d,d2,currRing);
817}
818
819
820//void pContent(poly ph)
821//{
822//  number h,d;
823//  poly p;
824//
825//  p = ph;
826//  if(pNext(p)==NULL)
827//  {
828//    pSetCoeff(p,nInit(1));
829//  }
830//  else
831//  {
832//#ifdef PDEBUG
833//    if (!pTest(p)) return;
834//#endif
835//    nNormalize(pGetCoeff(p));
836//    if(!nGreaterZero(pGetCoeff(ph)))
837//    {
838//      ph = pNeg(ph);
839//      nNormalize(pGetCoeff(p));
840//    }
841//    h=pGetCoeff(p);
842//    pIter(p);
843//    while (p!=NULL)
844//    {
845//      nNormalize(pGetCoeff(p));
846//      if (nGreater(h,pGetCoeff(p))) h=pGetCoeff(p);
847//      pIter(p);
848//    }
849//    h=nCopy(h);
850//    p=ph;
851//    while (p!=NULL)
852//    {
853//      d=nGcd(h,pGetCoeff(p));
854//      nDelete(&h);
855//      h = d;
856//      if(nIsOne(h))
857//      {
858//        break;
859//      }
860//      pIter(p);
861//    }
862//    p = ph;
863//    //number tmp;
864//    if(!nIsOne(h))
865//    {
866//      while (p!=NULL)
867//      {
868//        d = nIntDiv(pGetCoeff(p),h);
869//        pSetCoeff(p,d);
870//        pIter(p);
871//      }
872//    }
873//    nDelete(&h);
874//#ifdef HAVE_FACTORY
875//    if ( (nGetChar() == 1) || (nGetChar() < 0) ) /* Q[a],Q(a),Zp[a],Z/p(a) */
876//    {
877//      pTest(ph);
878//      singclap_divide_content(ph);
879//      pTest(ph);
880//    }
881//#endif
882//  }
883//}
884#if 0
885void p_Content(poly ph, ring r)
886{
887  number h,d;
888  poly p;
889
890  if(pNext(ph)==NULL)
891  {
892    pSetCoeff(ph,n_Init(1,r));
893  }
894  else
895  {
896    n_Normalize(pGetCoeff(ph),r);
897    if(!n_GreaterZero(pGetCoeff(ph),r)) ph = p_Neg(ph,r);
898    h=n_Copy(pGetCoeff(ph),r);
899    p = pNext(ph);
900    while (p!=NULL)
901    {
902      n_Normalize(pGetCoeff(p),r);
903      d=n_Gcd(h,pGetCoeff(p),r);
904      n_Delete(&h,r);
905      h = d;
906      if(n_IsOne(h,r))
907      {
908        break;
909      }
910      pIter(p);
911    }
912    p = ph;
913    //number tmp;
914    if(!n_IsOne(h,r))
915    {
916      while (p!=NULL)
917      {
918        //d = nDiv(pGetCoeff(p),h);
919        //tmp = nIntDiv(pGetCoeff(p),h);
920        //if (!nEqual(d,tmp))
921        //{
922        //  StringSetS("** div0:");nWrite(pGetCoeff(p));StringAppendS("/");
923        //  nWrite(h);StringAppendS("=");nWrite(d);StringAppendS(" int:");
924        //  nWrite(tmp);Print(StringAppendS("\n"));
925        //}
926        //nDelete(&tmp);
927        d = n_IntDiv(pGetCoeff(p),h,r);
928        p_SetCoeff(p,d,r);
929        pIter(p);
930      }
931    }
932    n_Delete(&h,r);
933#ifdef HAVE_FACTORY
934    //if ( (n_GetChar(r) == 1) || (n_GetChar(r) < 0) ) /* Q[a],Q(a),Zp[a],Z/p(a) */
935    //{
936    //  singclap_divide_content(ph);
937    //  if(!n_GreaterZero(pGetCoeff(ph),r)) ph = p_Neg(ph,r);
938    //}
939#endif
940  }
941}
942#endif
943
944poly pCleardenom(poly ph)
945{
946  poly start=ph;
947  number d, h;
948  poly p;
949
950#ifdef HAVE_RINGS
951  if (rField_is_Ring(currRing))
952  {
953    pContent(ph);
954    return start;
955  }
956#endif
957  if (rField_is_Zp() && TEST_OPT_INTSTRATEGY) return start;
958  p = ph;
959  if(pNext(p)==NULL)
960  {
961    if (TEST_OPT_CONTENTSB)
962    {
963      number n=nGetDenom(pGetCoeff(p));
964      if (!nIsOne(n))
965      {
966        number nn=nMult(pGetCoeff(p),n);
967        nNormalize(nn);
968        pSetCoeff(p,nn);
969      }
970      nDelete(&n);
971    }
972    else
973      pSetCoeff(p,nInit(1));
974  }
975  else
976  {
977    h = nInit(1);
978    while (p!=NULL)
979    {
980      nNormalize(pGetCoeff(p));
981      d=nLcm(h,pGetCoeff(p),currRing);
982      nDelete(&h);
983      h=d;
984      pIter(p);
985    }
986    /* contains the 1/lcm of all denominators */
987    if(!nIsOne(h))
988    {
989      p = ph;
990      while (p!=NULL)
991      {
992        /* should be:
993        * number hh;
994        * nGetDenom(p->coef,&hh);
995        * nMult(&h,&hh,&d);
996        * nNormalize(d);
997        * nDelete(&hh);
998        * nMult(d,p->coef,&hh);
999        * nDelete(&d);
1000        * nDelete(&(p->coef));
1001        * p->coef =hh;
1002        */
1003        d=nMult(h,pGetCoeff(p));
1004        nNormalize(d);
1005        pSetCoeff(p,d);
1006        pIter(p);
1007      }
1008      nDelete(&h);
1009      if (nGetChar()==1)
1010      {
1011        loop
1012        {
1013          h = nInit(1);
1014          p=ph;
1015          while (p!=NULL)
1016          {
1017            d=nLcm(h,pGetCoeff(p),currRing);
1018            nDelete(&h);
1019            h=d;
1020            pIter(p);
1021          }
1022          /* contains the 1/lcm of all denominators */
1023          if(!nIsOne(h))
1024          {
1025            p = ph;
1026            while (p!=NULL)
1027            {
1028              /* should be:
1029              * number hh;
1030              * nGetDenom(p->coef,&hh);
1031              * nMult(&h,&hh,&d);
1032              * nNormalize(d);
1033              * nDelete(&hh);
1034              * nMult(d,p->coef,&hh);
1035              * nDelete(&d);
1036              * nDelete(&(p->coef));
1037              * p->coef =hh;
1038              */
1039              d=nMult(h,pGetCoeff(p));
1040              nNormalize(d);
1041              pSetCoeff(p,d);
1042              pIter(p);
1043            }
1044            nDelete(&h);
1045          }
1046          else
1047          {
1048            nDelete(&h);
1049            break;
1050          }
1051        }
1052      }
1053    }
1054    if (h!=NULL) nDelete(&h);
1055 
1056    pContent(ph);
1057#ifdef HAVE_RATGRING
1058    if (rIsRatGRing(currRing))
1059    {
1060      /* quick unit detection in the rational case is done in gr_nc_bba */
1061      pContentRat(ph);
1062      start=ph;
1063    }
1064#endif
1065  }
1066  return start;
1067}
1068
1069void pCleardenom_n(poly ph,number &c)
1070{
1071  number d, h;
1072  poly p;
1073
1074  p = ph;
1075  if(pNext(p)==NULL)
1076  {
1077    c=nInvers(pGetCoeff(p));
1078    pSetCoeff(p,nInit(1));
1079  }
1080  else
1081  {
1082    h = nInit(1);
1083    while (p!=NULL)
1084    {
1085      nNormalize(pGetCoeff(p));
1086      d=nLcm(h,pGetCoeff(p),currRing);
1087      nDelete(&h);
1088      h=d;
1089      pIter(p);
1090    }
1091    c=h;
1092    /* contains the 1/lcm of all denominators */
1093    if(!nIsOne(h))
1094    {
1095      p = ph;
1096      while (p!=NULL)
1097      {
1098        /* should be:
1099        * number hh;
1100        * nGetDenom(p->coef,&hh);
1101        * nMult(&h,&hh,&d);
1102        * nNormalize(d);
1103        * nDelete(&hh);
1104        * nMult(d,p->coef,&hh);
1105        * nDelete(&d);
1106        * nDelete(&(p->coef));
1107        * p->coef =hh;
1108        */
1109        d=nMult(h,pGetCoeff(p));
1110        nNormalize(d);
1111        pSetCoeff(p,d);
1112        pIter(p);
1113      }
1114      if (nGetChar()==1)
1115      {
1116        loop
1117        {
1118          h = nInit(1);
1119          p=ph;
1120          while (p!=NULL)
1121          {
1122            d=nLcm(h,pGetCoeff(p),currRing);
1123            nDelete(&h);
1124            h=d;
1125            pIter(p);
1126          }
1127          /* contains the 1/lcm of all denominators */
1128          if(!nIsOne(h))
1129          {
1130            p = ph;
1131            while (p!=NULL)
1132            {
1133              /* should be:
1134              * number hh;
1135              * nGetDenom(p->coef,&hh);
1136              * nMult(&h,&hh,&d);
1137              * nNormalize(d);
1138              * nDelete(&hh);
1139              * nMult(d,p->coef,&hh);
1140              * nDelete(&d);
1141              * nDelete(&(p->coef));
1142              * p->coef =hh;
1143              */
1144              d=nMult(h,pGetCoeff(p));
1145              nNormalize(d);
1146              pSetCoeff(p,d);
1147              pIter(p);
1148            }
1149            number t=nMult(c,h);
1150            nDelete(&c);
1151            c=t;
1152          }
1153          else
1154          {
1155            break;
1156          }
1157          nDelete(&h);
1158        }
1159      }
1160    }
1161  }
1162}
1163
1164/*2
1165*tests if p is homogeneous with respect to the actual weigths
1166*/
1167BOOLEAN pIsHomogeneous (poly p)
1168{
1169  poly qp=p;
1170  int o;
1171
1172  if ((p == NULL) || (pNext(p) == NULL)) return TRUE;
1173  pFDegProc d=(pLexOrder ? pTotaldegree : pFDeg );
1174  o = d(p,currRing);
1175  do
1176  {
1177    if (d(qp,currRing) != o) return FALSE;
1178    pIter(qp);
1179  }
1180  while (qp != NULL);
1181  return TRUE;
1182}
1183
1184// orders monoms of poly using merge sort (ususally faster than
1185// insertion sort). ASSUMES that pSetm was performed on monoms
1186poly pOrdPolyMerge(poly p)
1187{
1188  poly qq,pp,result=NULL;
1189
1190  if (p == NULL) return NULL;
1191
1192  loop
1193  {
1194    qq = p;
1195    loop
1196    {
1197      if (pNext(p) == NULL)
1198      {
1199        result=pAdd(result, qq);
1200        pTest(result);
1201        return result;
1202      }
1203      if (pLmCmp(p,pNext(p)) != 1)
1204      {
1205        pp = p;
1206        pIter(p);
1207        pNext(pp) = NULL;
1208        result = pAdd(result, qq);
1209        break;
1210      }
1211      pIter(p);
1212    }
1213  }
1214}
1215
1216// orders monoms of poly using insertion sort, performs pSetm on each monom
1217poly pOrdPolyInsertSetm(poly p)
1218{
1219  poly qq,result = NULL;
1220
1221#if 0
1222  while (p != NULL)
1223  {
1224    qq = p;
1225    pIter(p);
1226    qq->next = NULL;
1227    pSetm(qq);
1228    result = pAdd(result,qq);
1229    pTest(result);
1230  }
1231#else
1232  while (p != NULL)
1233  {
1234    qq = p;
1235    pIter(p);
1236    qq->next = result;
1237    result = qq;
1238    pSetm(qq);
1239  }
1240  p = result;
1241  result = NULL;
1242  while (p != NULL)
1243  {
1244    qq = p;
1245    pIter(p);
1246    qq->next = NULL;
1247    result = pAdd(result, qq);
1248  }
1249  pTest(result);
1250#endif
1251  return result;
1252}
1253
1254/*2
1255*returns a re-ordered copy of a polynomial, with permutation of the variables
1256*/
1257poly pPermPoly (poly p, int * perm, const ring oldRing, nMapFunc nMap,
1258   int *par_perm, int OldPar)
1259{
1260  int OldpVariables = oldRing->N;
1261  poly result = NULL;
1262  poly result_last = NULL;
1263  poly aq=NULL; /* the map coefficient */
1264  poly qq; /* the mapped monomial */
1265
1266  while (p != NULL)
1267  {
1268    if ((OldPar==0)||(rField_is_GF(oldRing)))
1269    {
1270      qq = pInit();
1271      number n=nMap(pGetCoeff(p));
1272      if ((currRing->minpoly!=NULL)
1273      && ((rField_is_Zp_a()) || (rField_is_Q_a())))
1274      {
1275        nNormalize(n);
1276      }
1277      pGetCoeff(qq)=n;
1278    // coef may be zero:  pTest(qq);
1279    }
1280    else
1281    {
1282      qq=pOne();
1283      aq=naPermNumber(pGetCoeff(p),par_perm,OldPar, oldRing);
1284      if ((currRing->minpoly!=NULL)
1285      && ((rField_is_Zp_a()) || (rField_is_Q_a())))
1286      {
1287        poly tmp=aq;
1288        while (tmp!=NULL)
1289        {
1290          number n=pGetCoeff(tmp);
1291          nNormalize(n);
1292          pGetCoeff(tmp)=n;
1293          pIter(tmp);
1294        }
1295      }
1296      pTest(aq);
1297    }
1298    if (rRing_has_Comp(currRing)) pSetComp(qq, p_GetComp(p,oldRing));
1299    if (nIsZero(pGetCoeff(qq)))
1300    {
1301      pDeleteLm(&qq);
1302    }
1303    else
1304    {
1305      int i;
1306      int mapped_to_par=0;
1307      for(i=1; i<=OldpVariables; i++)
1308      {
1309        int e=p_GetExp(p,i,oldRing);
1310        if (e!=0)
1311        {
1312          if (perm==NULL)
1313          {
1314            pSetExp(qq,i, e);
1315          }
1316          else if (perm[i]>0)
1317            pAddExp(qq,perm[i], e/*p_GetExp( p,i,oldRing)*/);
1318          else if (perm[i]<0)
1319          {
1320            if (rField_is_GF())
1321            {
1322              number c=pGetCoeff(qq);
1323              number ee=nfPar(1);
1324              number eee;nfPower(ee,e,&eee); //nfDelete(ee,currRing);
1325              ee=nfMult(c,eee);
1326              //nfDelete(c,currRing);nfDelete(eee,currRing);
1327              pSetCoeff0(qq,ee);
1328            }
1329            else
1330            {
1331              lnumber c=(lnumber)pGetCoeff(qq);
1332              if (c->z->next==NULL)
1333                napAddExp(c->z,-perm[i],e/*p_GetExp( p,i,oldRing)*/);
1334              else /* more difficult: we have really to multiply: */
1335              {
1336                lnumber mmc=(lnumber)naInit(1);
1337                napSetExp(mmc->z,-perm[i],e/*p_GetExp( p,i,oldRing)*/);
1338                napSetm(mmc->z);
1339                pGetCoeff(qq)=naMult((number)c,(number)mmc);
1340                nDelete((number *)&c);
1341                nDelete((number *)&mmc); 
1342              }
1343              mapped_to_par=1;
1344            }
1345          }
1346          else
1347          {
1348            /* this variable maps to 0 !*/
1349            pDeleteLm(&qq);
1350            break;
1351          }
1352        }
1353      }
1354      if (mapped_to_par
1355      && (currRing->minpoly!=NULL))
1356      {
1357        number n=pGetCoeff(qq);
1358        nNormalize(n);
1359        pGetCoeff(qq)=n;
1360      }
1361    }
1362    pIter(p);
1363#if 1
1364    if (qq!=NULL)
1365    {
1366      pSetm(qq);
1367      pTest(aq);
1368      pTest(qq);
1369      if (aq!=NULL) qq=pMult(aq,qq);
1370      aq = qq;
1371      while (pNext(aq) != NULL) pIter(aq);
1372      if (result_last==NULL)
1373      {
1374        result=qq;
1375      }
1376      else
1377      {
1378        pNext(result_last)=qq;
1379      }
1380      result_last=aq;
1381      aq = NULL;
1382    }
1383    else if (aq!=NULL)
1384    {
1385      pDelete(&aq);
1386    }
1387  }
1388  result=pSortAdd(result);
1389#else
1390  //  if (qq!=NULL)
1391  //  {
1392  //    pSetm(qq);
1393  //    pTest(qq);
1394  //    pTest(aq);
1395  //    if (aq!=NULL) qq=pMult(aq,qq);
1396  //    aq = qq;
1397  //    while (pNext(aq) != NULL) pIter(aq);
1398  //    pNext(aq) = result;
1399  //    aq = NULL;
1400  //    result = qq;
1401  //  }
1402  //  else if (aq!=NULL)
1403  //  {
1404  //    pDelete(&aq);
1405  //  }
1406  //}
1407  //p = result;
1408  //result = NULL;
1409  //while (p != NULL)
1410  //{
1411  //  qq = p;
1412  //  pIter(p);
1413  //  qq->next = NULL;
1414  //  result = pAdd(result, qq);
1415  //}
1416#endif
1417  pTest(result);
1418  return result;
1419}
1420
1421#if 0
1422/*2
1423*returns a re-ordered copy of a polynomial, with permutation of the variables
1424*/
1425poly p_PermPoly (poly p, int * perm, ring oldRing,
1426   int *par_perm, int OldPar, ring newRing)
1427{
1428  int OldpVariables = oldRing->N;
1429  poly result = NULL;
1430  poly result_last = NULL;
1431  poly aq=NULL; /* the map coefficient */
1432  poly qq; /* the mapped monomial */
1433
1434  while (p != NULL)
1435  {
1436    if (OldPar==0)
1437    {
1438      qq = pInit();
1439      number n=newRing->cf->nMap(pGetCoeff(p));
1440      if ((newRing->minpoly!=NULL)
1441      && ((rField_is_Zp_a(newRing)) || (rField_is_Q_a(newRing))))
1442      {
1443        newRing->cf->nNormalize(n);
1444      }
1445      pGetCoeff(qq)=n;
1446    // coef may be zero:  pTest(qq);
1447    }
1448    else
1449    {
1450      qq=p_ISet(1, newRing);
1451      aq=naPermNumber(pGetCoeff(p),par_perm,OldPar, oldRing);
1452      if ((newRing->minpoly!=NULL)
1453      && ((rField_is_Zp_a(newRing)) || (rField_is_Q_a(newRing))))
1454      {
1455        poly tmp=aq;
1456        while (tmp!=NULL)
1457        {
1458          number n=pGetCoeff(tmp);
1459          newRing->cf->nNormalize(n);
1460          pGetCoeff(tmp)=n;
1461          pIter(tmp);
1462        }
1463      }
1464      //pTest(aq);
1465    }
1466    p_SetComp(qq, p_GetComp(p,oldRing), newRing);
1467    if (newRing->cf->nIsZero(pGetCoeff(qq)))
1468    {
1469      p_DeleteLm(&qq, newRing);
1470    }
1471    else
1472    {
1473      int i;
1474      int mapped_to_par=0;
1475      for(i=1; i<=OldpVariables; i++)
1476      {
1477        int e=p_GetExp(p,i,oldRing);
1478        if (e!=0)
1479        {
1480          if (perm==NULL)
1481          {
1482            p_SetExp(qq,i, e, newRing);
1483          }
1484          else if (perm[i]>0)
1485            p_AddExp(qq,perm[i], e/*p_GetExp( p,i,oldRing)*/, newRing);
1486          else if (perm[i]<0)
1487          {
1488            lnumber c=(lnumber)pGetCoeff(qq);
1489            napAddExp(c->z,-perm[i],e/*p_GetExp( p,i,oldRing)*/);
1490            mapped_to_par=1;
1491          }
1492          else
1493          {
1494            /* this variable maps to 0 !*/
1495            p_DeleteLm(&qq, newRing);
1496            break;
1497          }
1498        }
1499      }
1500      if (mapped_to_par
1501      && (newRing->minpoly!=NULL))
1502      {
1503        number n=pGetCoeff(qq);
1504        newRing->cf->nNormalize(n);
1505        pGetCoeff(qq)=n;
1506      }
1507    }
1508    pIter(p);
1509    if (qq!=NULL)
1510    {
1511      p_Setm(qq, newRing);
1512      //pTest(aq);
1513      //pTest(qq);
1514      if (aq!=NULL) qq=pMult(aq,qq);
1515      aq = qq;
1516      while (pNext(aq) != NULL) pIter(aq);
1517      if (result_last==NULL)
1518      {
1519        result=qq;
1520      }
1521      else
1522      {
1523        pNext(result_last)=qq;
1524      }
1525      result_last=aq;
1526      aq = NULL;
1527    }
1528    else if (aq!=NULL)
1529    {
1530      p_Delete(&aq, newRing);
1531    }
1532  }
1533  result=pOrdPolyMerge(result);
1534  //pTest(result);
1535  return result;
1536}
1537#endif
1538
1539poly ppJet(poly p, int m)
1540{
1541  poly r=NULL;
1542  poly t=NULL;
1543
1544  while (p!=NULL)
1545  {
1546    if (pTotaldegree(p)<=m)
1547    {
1548      if (r==NULL)
1549        r=pHead(p);
1550      else
1551      if (t==NULL)
1552      {
1553        pNext(r)=pHead(p);
1554        t=pNext(r);
1555      }
1556      else
1557      {
1558        pNext(t)=pHead(p);
1559        pIter(t);
1560      }
1561    }
1562    pIter(p);
1563  }
1564  return r;
1565}
1566
1567poly pJet(poly p, int m)
1568{
1569  poly t=NULL;
1570
1571  while((p!=NULL) && (pTotaldegree(p)>m)) pLmDelete(&p);
1572  if (p==NULL) return NULL;
1573  poly r=p;
1574  while (pNext(p)!=NULL)
1575  {
1576    if (pTotaldegree(pNext(p))>m)
1577    {
1578      pLmDelete(&pNext(p));
1579    }
1580    else
1581      pIter(p);
1582  }
1583  return r;
1584}
1585
1586poly ppJetW(poly p, int m, short *w)
1587{
1588  poly r=NULL;
1589  poly t=NULL;
1590  while (p!=NULL)
1591  {
1592    if (totaldegreeWecart_IV(p,currRing,w)<=m)
1593    {
1594      if (r==NULL)
1595        r=pHead(p);
1596      else
1597      if (t==NULL)
1598      {
1599        pNext(r)=pHead(p);
1600        t=pNext(r);
1601      }
1602      else
1603      {
1604        pNext(t)=pHead(p);
1605        pIter(t);
1606      }
1607    }
1608    pIter(p);
1609  }
1610  return r;
1611}
1612
1613poly pJetW(poly p, int m, short *w)
1614{
1615  poly t=NULL;
1616  while((p!=NULL) && (totaldegreeWecart_IV(p,currRing,w)>m)) pLmDelete(&p);
1617  if (p==NULL) return NULL;
1618  poly r=p;
1619  while (pNext(p)!=NULL)
1620  {
1621    if (totaldegreeWecart_IV(pNext(p),currRing,w)>m)
1622    {
1623      pLmDelete(&pNext(p));
1624    }
1625    else
1626      pIter(p);
1627  }
1628  return r;
1629}
1630
1631int pMinDeg(poly p,intvec *w)
1632{
1633  if(p==NULL)
1634    return -1;
1635  int d=-1;
1636  while(p!=NULL)
1637  {
1638    int d0=0;
1639    for(int j=0;j<pVariables;j++)
1640      if(w==NULL||j>=w->length())
1641        d0+=pGetExp(p,j+1);
1642      else
1643        d0+=(*w)[j]*pGetExp(p,j+1);
1644    if(d0<d||d==-1)
1645      d=d0;
1646    pIter(p);
1647  }
1648  return d;
1649}
1650
1651poly pSeries(int n,poly p,poly u, intvec *w)
1652{
1653  short *ww=iv2array(w);
1654  if(p!=NULL)
1655  {
1656    if(u==NULL)
1657      p=pJetW(p,n,ww);
1658    else
1659      p=pJetW(pMult(p,pInvers(n-pMinDeg(p,w),u,w)),n,ww);
1660  }
1661  omFreeSize((ADDRESS)ww,(pVariables+1)*sizeof(short));
1662  return p;
1663}
1664
1665poly pInvers(int n,poly u,intvec *w)
1666{
1667  short *ww=iv2array(w);
1668  if(n<0)
1669    return NULL;
1670  number u0=nInvers(pGetCoeff(u));
1671  poly v=pNSet(u0);
1672  if(n==0)
1673    return v;
1674  poly u1=pJetW(pSub(pOne(),pMult_nn(u,u0)),n,ww);
1675  if(u1==NULL)
1676    return v;
1677  poly v1=pMult_nn(pCopy(u1),u0);
1678  v=pAdd(v,pCopy(v1));
1679  for(int i=n/pMinDeg(u1,w);i>1;i--)
1680  {
1681    v1=pJetW(pMult(v1,pCopy(u1)),n,ww);
1682    v=pAdd(v,pCopy(v1));
1683  }
1684  pDelete(&u1);
1685  pDelete(&v1);
1686  omFreeSize((ADDRESS)ww,(pVariables+1)*sizeof(short));
1687  return v;
1688}
1689
1690long pDegW(poly p, const short *w)
1691{
1692  long r=-LONG_MAX;
1693
1694  while (p!=NULL)
1695  {
1696    long t=totaldegreeWecart_IV(p,currRing,w);
1697    if (t>r) r=t;
1698    pIter(p);
1699  }
1700  return r;
1701}
1702
1703/*-----------type conversions ----------------------------*/
1704/*2
1705* input: a set of polys (len elements: p[0]..p[len-1])
1706* output: a vector
1707* p will not be changed
1708*/
1709poly  pPolys2Vec(polyset p, int len)
1710{
1711  poly v=NULL;
1712  poly h;
1713  int i;
1714
1715  for (i=len-1; i>=0; i--)
1716  {
1717    if (p[i])
1718    {
1719      h=pCopy(p[i]);
1720      pSetCompP(h,i+1);
1721      v=pAdd(v,h);
1722    }
1723  }
1724 return v;
1725}
1726
1727/*2
1728* convert a vector to a set of polys,
1729* allocates the polyset, (entries 0..(*len)-1)
1730* the vector will not be changed
1731*/
1732void  pVec2Polys(poly v, polyset *p, int *len)
1733{
1734  poly h;
1735  int k;
1736
1737  *len=pMaxComp(v);
1738  if (*len==0) *len=1;
1739  *p=(polyset)omAlloc0((*len)*sizeof(poly));
1740  while (v!=NULL)
1741  {
1742    h=pHead(v);
1743    k=pGetComp(h);
1744    pSetComp(h,0);
1745    (*p)[k-1]=pAdd((*p)[k-1],h);
1746    pIter(v);
1747  }
1748}
1749
1750int p_Var(poly m,const ring r)
1751{
1752  if (m==NULL) return 0;
1753  if (pNext(m)!=NULL) return 0;
1754  int i,e=0;
1755  for (i=r->N; i>0; i--)
1756  {
1757    if (p_GetExp(m,i,r)==1)
1758    {
1759      if (e==0) e=i;
1760      else return 0;
1761    }
1762  }
1763  return e;
1764}
1765
1766/*----------utilities for syzygies--------------*/
1767//BOOLEAN   pVectorHasUnitM(poly p, int * k)
1768//{
1769//  while (p!=NULL)
1770//  {
1771//    if (pLmIsConstantComp(p))
1772//    {
1773//      *k = pGetComp(p);
1774//      return TRUE;
1775//    }
1776//    else pIter(p);
1777//  }
1778//  return FALSE;
1779//}
1780
1781BOOLEAN   pVectorHasUnitB(poly p, int * k)
1782{
1783  poly q=p,qq;
1784  int i;
1785
1786  while (q!=NULL)
1787  {
1788    if (pLmIsConstantComp(q))
1789    {
1790      i = pGetComp(q);
1791      qq = p;
1792      while ((qq != q) && (pGetComp(qq) != i)) pIter(qq);
1793      if (qq == q)
1794      {
1795        *k = i;
1796        return TRUE;
1797      }
1798      else
1799        pIter(q);
1800    }
1801    else pIter(q);
1802  }
1803  return FALSE;
1804}
1805
1806void   pVectorHasUnit(poly p, int * k, int * len)
1807{
1808  poly q=p,qq;
1809  int i,j=0;
1810
1811  *len = 0;
1812  while (q!=NULL)
1813  {
1814    if (pLmIsConstantComp(q))
1815    {
1816      i = pGetComp(q);
1817      qq = p;
1818      while ((qq != q) && (pGetComp(qq) != i)) pIter(qq);
1819      if (qq == q)
1820      {
1821       j = 0;
1822       while (qq!=NULL)
1823       {
1824         if (pGetComp(qq)==i) j++;
1825        pIter(qq);
1826       }
1827       if ((*len == 0) || (j<*len))
1828       {
1829         *len = j;
1830         *k = i;
1831       }
1832      }
1833    }
1834    pIter(q);
1835  }
1836}
1837
1838/*2
1839* returns TRUE if p1 = p2
1840*/
1841BOOLEAN p_EqualPolys(poly p1,poly p2, ring r)
1842{
1843  while ((p1 != NULL) && (p2 != NULL))
1844  {
1845    if (! p_LmEqual(p1, p2,r))
1846      return FALSE;
1847    if (! n_Equal(p_GetCoeff(p1,r), p_GetCoeff(p2,r),r ))
1848      return FALSE;
1849    pIter(p1);
1850    pIter(p2);
1851  }
1852  return (p1==p2);
1853}
1854
1855/*2
1856*returns TRUE if p1 is a skalar multiple of p2
1857*assume p1 != NULL and p2 != NULL
1858*/
1859BOOLEAN pComparePolys(poly p1,poly p2)
1860{
1861  number n,nn;
1862  int i;
1863  pAssume(p1 != NULL && p2 != NULL);
1864
1865  if (!pLmEqual(p1,p2)) //compare leading mons
1866      return FALSE;
1867  if ((pNext(p1)==NULL) && (pNext(p2)!=NULL))
1868     return FALSE;
1869  if ((pNext(p2)==NULL) && (pNext(p1)!=NULL))
1870     return FALSE;
1871  if (pLength(p1) != pLength(p2))
1872    return FALSE;
1873  n=nDiv(pGetCoeff(p1),pGetCoeff(p2));
1874  while ((p1 != NULL) /*&& (p2 != NULL)*/)
1875  {
1876    if ( ! pLmEqual(p1, p2))
1877    {
1878        nDelete(&n);
1879        return FALSE;
1880    }
1881    if (!nEqual(pGetCoeff(p1),nn=nMult(pGetCoeff(p2),n)))
1882    {
1883      nDelete(&n);
1884      nDelete(&nn);
1885      return FALSE;
1886    }
1887    nDelete(&nn);
1888    pIter(p1);
1889    pIter(p2);
1890  }
1891  nDelete(&n);
1892  return TRUE;
1893}
Note: See TracBrowser for help on using the repository browser.