source: git/kernel/polys1.cc @ 3a8520

spielwiese
Last change on this file since 3a8520 was 3a8520, checked in by Hans Schönemann <hannes@…>, 15 years ago
*hannes: pContentRat git-svn-id: file:///usr/local/Singular/svn/trunk@11475 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 35.3 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: polys1.cc,v 1.34 2009-02-26 11:24:15 Singular 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
944void pCleardenom(poly ph)
945{
946  number d, h;
947  poly p;
948
949#ifdef HAVE_RINGS
950  if (rField_is_Ring(currRing))
951  {
952    pContent(ph);
953    return;
954  }
955#endif
956  if (rField_is_Zp() && TEST_OPT_INTSTRATEGY) return;
957  p = ph;
958  if(pNext(p)==NULL)
959  {
960    if (TEST_OPT_CONTENTSB)
961    {
962      number n=nGetDenom(pGetCoeff(p));
963      if (!nIsOne(n))
964      {
965        number nn=nMult(pGetCoeff(p),n);
966        nNormalize(nn);
967        pSetCoeff(p,nn);
968      }
969      nDelete(&n);
970    }
971    else
972      pSetCoeff(p,nInit(1));
973  }
974  else
975  {
976    h = nInit(1);
977    while (p!=NULL)
978    {
979      nNormalize(pGetCoeff(p));
980      d=nLcm(h,pGetCoeff(p),currRing);
981      nDelete(&h);
982      h=d;
983      pIter(p);
984    }
985    /* contains the 1/lcm of all denominators */
986    if(!nIsOne(h))
987    {
988      p = ph;
989      while (p!=NULL)
990      {
991        /* should be:
992        * number hh;
993        * nGetDenom(p->coef,&hh);
994        * nMult(&h,&hh,&d);
995        * nNormalize(d);
996        * nDelete(&hh);
997        * nMult(d,p->coef,&hh);
998        * nDelete(&d);
999        * nDelete(&(p->coef));
1000        * p->coef =hh;
1001        */
1002        d=nMult(h,pGetCoeff(p));
1003        nNormalize(d);
1004        pSetCoeff(p,d);
1005        pIter(p);
1006      }
1007      nDelete(&h);
1008      if (nGetChar()==1)
1009      {
1010        loop
1011        {
1012          h = nInit(1);
1013          p=ph;
1014          while (p!=NULL)
1015          {
1016            d=nLcm(h,pGetCoeff(p),currRing);
1017            nDelete(&h);
1018            h=d;
1019            pIter(p);
1020          }
1021          /* contains the 1/lcm of all denominators */
1022          if(!nIsOne(h))
1023          {
1024            p = ph;
1025            while (p!=NULL)
1026            {
1027              /* should be:
1028              * number hh;
1029              * nGetDenom(p->coef,&hh);
1030              * nMult(&h,&hh,&d);
1031              * nNormalize(d);
1032              * nDelete(&hh);
1033              * nMult(d,p->coef,&hh);
1034              * nDelete(&d);
1035              * nDelete(&(p->coef));
1036              * p->coef =hh;
1037              */
1038              d=nMult(h,pGetCoeff(p));
1039              nNormalize(d);
1040              pSetCoeff(p,d);
1041              pIter(p);
1042            }
1043            nDelete(&h);
1044          }
1045          else
1046          {
1047            nDelete(&h);
1048            break;
1049          }
1050        }
1051      }
1052    }
1053    if (h!=NULL) nDelete(&h);
1054 
1055    pContent(ph);
1056#ifdef HAVE_RATGRING
1057    if (rIsRatGRing(currRing))
1058    {
1059      pContentRat(ph);
1060    }
1061#endif
1062  }
1063}
1064
1065void pCleardenom_n(poly ph,number &c)
1066{
1067  number d, h;
1068  poly p;
1069
1070  p = ph;
1071  if(pNext(p)==NULL)
1072  {
1073    c=nInvers(pGetCoeff(p));
1074    pSetCoeff(p,nInit(1));
1075  }
1076  else
1077  {
1078    h = nInit(1);
1079    while (p!=NULL)
1080    {
1081      nNormalize(pGetCoeff(p));
1082      d=nLcm(h,pGetCoeff(p),currRing);
1083      nDelete(&h);
1084      h=d;
1085      pIter(p);
1086    }
1087    c=h;
1088    /* contains the 1/lcm of all denominators */
1089    if(!nIsOne(h))
1090    {
1091      p = ph;
1092      while (p!=NULL)
1093      {
1094        /* should be:
1095        * number hh;
1096        * nGetDenom(p->coef,&hh);
1097        * nMult(&h,&hh,&d);
1098        * nNormalize(d);
1099        * nDelete(&hh);
1100        * nMult(d,p->coef,&hh);
1101        * nDelete(&d);
1102        * nDelete(&(p->coef));
1103        * p->coef =hh;
1104        */
1105        d=nMult(h,pGetCoeff(p));
1106        nNormalize(d);
1107        pSetCoeff(p,d);
1108        pIter(p);
1109      }
1110      if (nGetChar()==1)
1111      {
1112        loop
1113        {
1114          h = nInit(1);
1115          p=ph;
1116          while (p!=NULL)
1117          {
1118            d=nLcm(h,pGetCoeff(p),currRing);
1119            nDelete(&h);
1120            h=d;
1121            pIter(p);
1122          }
1123          /* contains the 1/lcm of all denominators */
1124          if(!nIsOne(h))
1125          {
1126            p = ph;
1127            while (p!=NULL)
1128            {
1129              /* should be:
1130              * number hh;
1131              * nGetDenom(p->coef,&hh);
1132              * nMult(&h,&hh,&d);
1133              * nNormalize(d);
1134              * nDelete(&hh);
1135              * nMult(d,p->coef,&hh);
1136              * nDelete(&d);
1137              * nDelete(&(p->coef));
1138              * p->coef =hh;
1139              */
1140              d=nMult(h,pGetCoeff(p));
1141              nNormalize(d);
1142              pSetCoeff(p,d);
1143              pIter(p);
1144            }
1145            number t=nMult(c,h);
1146            nDelete(&c);
1147            c=t;
1148          }
1149          else
1150          {
1151            break;
1152          }
1153          nDelete(&h);
1154        }
1155      }
1156    }
1157  }
1158}
1159
1160/*2
1161*tests if p is homogeneous with respect to the actual weigths
1162*/
1163BOOLEAN pIsHomogeneous (poly p)
1164{
1165  poly qp=p;
1166  int o;
1167
1168  if ((p == NULL) || (pNext(p) == NULL)) return TRUE;
1169  pFDegProc d=(pLexOrder ? pTotaldegree : pFDeg );
1170  o = d(p,currRing);
1171  do
1172  {
1173    if (d(qp,currRing) != o) return FALSE;
1174    pIter(qp);
1175  }
1176  while (qp != NULL);
1177  return TRUE;
1178}
1179
1180// orders monoms of poly using merge sort (ususally faster than
1181// insertion sort). ASSUMES that pSetm was performed on monoms
1182poly pOrdPolyMerge(poly p)
1183{
1184  poly qq,pp,result=NULL;
1185
1186  if (p == NULL) return NULL;
1187
1188  loop
1189  {
1190    qq = p;
1191    loop
1192    {
1193      if (pNext(p) == NULL)
1194      {
1195        result=pAdd(result, qq);
1196        pTest(result);
1197        return result;
1198      }
1199      if (pLmCmp(p,pNext(p)) != 1)
1200      {
1201        pp = p;
1202        pIter(p);
1203        pNext(pp) = NULL;
1204        result = pAdd(result, qq);
1205        break;
1206      }
1207      pIter(p);
1208    }
1209  }
1210}
1211
1212// orders monoms of poly using insertion sort, performs pSetm on each monom
1213poly pOrdPolyInsertSetm(poly p)
1214{
1215  poly qq,result = NULL;
1216
1217#if 0
1218  while (p != NULL)
1219  {
1220    qq = p;
1221    pIter(p);
1222    qq->next = NULL;
1223    pSetm(qq);
1224    result = pAdd(result,qq);
1225    pTest(result);
1226  }
1227#else
1228  while (p != NULL)
1229  {
1230    qq = p;
1231    pIter(p);
1232    qq->next = result;
1233    result = qq;
1234    pSetm(qq);
1235  }
1236  p = result;
1237  result = NULL;
1238  while (p != NULL)
1239  {
1240    qq = p;
1241    pIter(p);
1242    qq->next = NULL;
1243    result = pAdd(result, qq);
1244  }
1245  pTest(result);
1246#endif
1247  return result;
1248}
1249
1250/*2
1251*returns a re-ordered copy of a polynomial, with permutation of the variables
1252*/
1253poly pPermPoly (poly p, int * perm, const ring oldRing, nMapFunc nMap,
1254   int *par_perm, int OldPar)
1255{
1256  int OldpVariables = oldRing->N;
1257  poly result = NULL;
1258  poly result_last = NULL;
1259  poly aq=NULL; /* the map coefficient */
1260  poly qq; /* the mapped monomial */
1261
1262  while (p != NULL)
1263  {
1264    if ((OldPar==0)||(rField_is_GF(oldRing)))
1265    {
1266      qq = pInit();
1267      number n=nMap(pGetCoeff(p));
1268      if ((currRing->minpoly!=NULL)
1269      && ((rField_is_Zp_a()) || (rField_is_Q_a())))
1270      {
1271        nNormalize(n);
1272      }
1273      pGetCoeff(qq)=n;
1274    // coef may be zero:  pTest(qq);
1275    }
1276    else
1277    {
1278      qq=pOne();
1279      aq=naPermNumber(pGetCoeff(p),par_perm,OldPar, oldRing);
1280      if ((currRing->minpoly!=NULL)
1281      && ((rField_is_Zp_a()) || (rField_is_Q_a())))
1282      {
1283        poly tmp=aq;
1284        while (tmp!=NULL)
1285        {
1286          number n=pGetCoeff(tmp);
1287          nNormalize(n);
1288          pGetCoeff(tmp)=n;
1289          pIter(tmp);
1290        }
1291      }
1292      pTest(aq);
1293    }
1294    if (rRing_has_Comp(currRing)) pSetComp(qq, p_GetComp(p,oldRing));
1295    if (nIsZero(pGetCoeff(qq)))
1296    {
1297      pDeleteLm(&qq);
1298    }
1299    else
1300    {
1301      int i;
1302      int mapped_to_par=0;
1303      for(i=1; i<=OldpVariables; i++)
1304      {
1305        int e=p_GetExp(p,i,oldRing);
1306        if (e!=0)
1307        {
1308          if (perm==NULL)
1309          {
1310            pSetExp(qq,i, e);
1311          }
1312          else if (perm[i]>0)
1313            pAddExp(qq,perm[i], e/*p_GetExp( p,i,oldRing)*/);
1314          else if (perm[i]<0)
1315          {
1316            if (rField_is_GF())
1317            {
1318              number c=pGetCoeff(qq);
1319              number ee=nfPar(1);
1320              number eee;nfPower(ee,e,&eee); //nfDelete(ee,currRing);
1321              ee=nfMult(c,eee);
1322              //nfDelete(c,currRing);nfDelete(eee,currRing);
1323              pSetCoeff0(qq,ee);
1324            }
1325            else
1326            {
1327              lnumber c=(lnumber)pGetCoeff(qq);
1328              if (c->z->next==NULL)
1329                napAddExp(c->z,-perm[i],e/*p_GetExp( p,i,oldRing)*/);
1330              else /* more difficult: we have really to multiply: */
1331              {
1332                lnumber mmc=(lnumber)naInit(1);
1333                napSetExp(mmc->z,-perm[i],e/*p_GetExp( p,i,oldRing)*/);
1334                napSetm(mmc->z);
1335                pGetCoeff(qq)=naMult((number)c,(number)mmc);
1336                nDelete((number *)&c);
1337                nDelete((number *)&mmc); 
1338              }
1339              mapped_to_par=1;
1340            }
1341          }
1342          else
1343          {
1344            /* this variable maps to 0 !*/
1345            pDeleteLm(&qq);
1346            break;
1347          }
1348        }
1349      }
1350      if (mapped_to_par
1351      && (currRing->minpoly!=NULL))
1352      {
1353        number n=pGetCoeff(qq);
1354        nNormalize(n);
1355        pGetCoeff(qq)=n;
1356      }
1357    }
1358    pIter(p);
1359#if 1
1360    if (qq!=NULL)
1361    {
1362      pSetm(qq);
1363      pTest(aq);
1364      pTest(qq);
1365      if (aq!=NULL) qq=pMult(aq,qq);
1366      aq = qq;
1367      while (pNext(aq) != NULL) pIter(aq);
1368      if (result_last==NULL)
1369      {
1370        result=qq;
1371      }
1372      else
1373      {
1374        pNext(result_last)=qq;
1375      }
1376      result_last=aq;
1377      aq = NULL;
1378    }
1379    else if (aq!=NULL)
1380    {
1381      pDelete(&aq);
1382    }
1383  }
1384  result=pSortAdd(result);
1385#else
1386  //  if (qq!=NULL)
1387  //  {
1388  //    pSetm(qq);
1389  //    pTest(qq);
1390  //    pTest(aq);
1391  //    if (aq!=NULL) qq=pMult(aq,qq);
1392  //    aq = qq;
1393  //    while (pNext(aq) != NULL) pIter(aq);
1394  //    pNext(aq) = result;
1395  //    aq = NULL;
1396  //    result = qq;
1397  //  }
1398  //  else if (aq!=NULL)
1399  //  {
1400  //    pDelete(&aq);
1401  //  }
1402  //}
1403  //p = result;
1404  //result = NULL;
1405  //while (p != NULL)
1406  //{
1407  //  qq = p;
1408  //  pIter(p);
1409  //  qq->next = NULL;
1410  //  result = pAdd(result, qq);
1411  //}
1412#endif
1413  pTest(result);
1414  return result;
1415}
1416
1417#if 0
1418/*2
1419*returns a re-ordered copy of a polynomial, with permutation of the variables
1420*/
1421poly p_PermPoly (poly p, int * perm, ring oldRing,
1422   int *par_perm, int OldPar, ring newRing)
1423{
1424  int OldpVariables = oldRing->N;
1425  poly result = NULL;
1426  poly result_last = NULL;
1427  poly aq=NULL; /* the map coefficient */
1428  poly qq; /* the mapped monomial */
1429
1430  while (p != NULL)
1431  {
1432    if (OldPar==0)
1433    {
1434      qq = pInit();
1435      number n=newRing->cf->nMap(pGetCoeff(p));
1436      if ((newRing->minpoly!=NULL)
1437      && ((rField_is_Zp_a(newRing)) || (rField_is_Q_a(newRing))))
1438      {
1439        newRing->cf->nNormalize(n);
1440      }
1441      pGetCoeff(qq)=n;
1442    // coef may be zero:  pTest(qq);
1443    }
1444    else
1445    {
1446      qq=p_ISet(1, newRing);
1447      aq=naPermNumber(pGetCoeff(p),par_perm,OldPar, oldRing);
1448      if ((newRing->minpoly!=NULL)
1449      && ((rField_is_Zp_a(newRing)) || (rField_is_Q_a(newRing))))
1450      {
1451        poly tmp=aq;
1452        while (tmp!=NULL)
1453        {
1454          number n=pGetCoeff(tmp);
1455          newRing->cf->nNormalize(n);
1456          pGetCoeff(tmp)=n;
1457          pIter(tmp);
1458        }
1459      }
1460      //pTest(aq);
1461    }
1462    p_SetComp(qq, p_GetComp(p,oldRing), newRing);
1463    if (newRing->cf->nIsZero(pGetCoeff(qq)))
1464    {
1465      p_DeleteLm(&qq, newRing);
1466    }
1467    else
1468    {
1469      int i;
1470      int mapped_to_par=0;
1471      for(i=1; i<=OldpVariables; i++)
1472      {
1473        int e=p_GetExp(p,i,oldRing);
1474        if (e!=0)
1475        {
1476          if (perm==NULL)
1477          {
1478            p_SetExp(qq,i, e, newRing);
1479          }
1480          else if (perm[i]>0)
1481            p_AddExp(qq,perm[i], e/*p_GetExp( p,i,oldRing)*/, newRing);
1482          else if (perm[i]<0)
1483          {
1484            lnumber c=(lnumber)pGetCoeff(qq);
1485            napAddExp(c->z,-perm[i],e/*p_GetExp( p,i,oldRing)*/);
1486            mapped_to_par=1;
1487          }
1488          else
1489          {
1490            /* this variable maps to 0 !*/
1491            p_DeleteLm(&qq, newRing);
1492            break;
1493          }
1494        }
1495      }
1496      if (mapped_to_par
1497      && (newRing->minpoly!=NULL))
1498      {
1499        number n=pGetCoeff(qq);
1500        newRing->cf->nNormalize(n);
1501        pGetCoeff(qq)=n;
1502      }
1503    }
1504    pIter(p);
1505    if (qq!=NULL)
1506    {
1507      p_Setm(qq, newRing);
1508      //pTest(aq);
1509      //pTest(qq);
1510      if (aq!=NULL) qq=pMult(aq,qq);
1511      aq = qq;
1512      while (pNext(aq) != NULL) pIter(aq);
1513      if (result_last==NULL)
1514      {
1515        result=qq;
1516      }
1517      else
1518      {
1519        pNext(result_last)=qq;
1520      }
1521      result_last=aq;
1522      aq = NULL;
1523    }
1524    else if (aq!=NULL)
1525    {
1526      p_Delete(&aq, newRing);
1527    }
1528  }
1529  result=pOrdPolyMerge(result);
1530  //pTest(result);
1531  return result;
1532}
1533#endif
1534
1535poly ppJet(poly p, int m)
1536{
1537  poly r=NULL;
1538  poly t=NULL;
1539
1540  while (p!=NULL)
1541  {
1542    if (pTotaldegree(p)<=m)
1543    {
1544      if (r==NULL)
1545        r=pHead(p);
1546      else
1547      if (t==NULL)
1548      {
1549        pNext(r)=pHead(p);
1550        t=pNext(r);
1551      }
1552      else
1553      {
1554        pNext(t)=pHead(p);
1555        pIter(t);
1556      }
1557    }
1558    pIter(p);
1559  }
1560  return r;
1561}
1562
1563poly pJet(poly p, int m)
1564{
1565  poly t=NULL;
1566
1567  while((p!=NULL) && (pTotaldegree(p)>m)) pLmDelete(&p);
1568  if (p==NULL) return NULL;
1569  poly r=p;
1570  while (pNext(p)!=NULL)
1571  {
1572    if (pTotaldegree(pNext(p))>m)
1573    {
1574      pLmDelete(&pNext(p));
1575    }
1576    else
1577      pIter(p);
1578  }
1579  return r;
1580}
1581
1582poly ppJetW(poly p, int m, short *w)
1583{
1584  poly r=NULL;
1585  poly t=NULL;
1586  while (p!=NULL)
1587  {
1588    if (totaldegreeWecart_IV(p,currRing,w)<=m)
1589    {
1590      if (r==NULL)
1591        r=pHead(p);
1592      else
1593      if (t==NULL)
1594      {
1595        pNext(r)=pHead(p);
1596        t=pNext(r);
1597      }
1598      else
1599      {
1600        pNext(t)=pHead(p);
1601        pIter(t);
1602      }
1603    }
1604    pIter(p);
1605  }
1606  return r;
1607}
1608
1609poly pJetW(poly p, int m, short *w)
1610{
1611  poly t=NULL;
1612  while((p!=NULL) && (totaldegreeWecart_IV(p,currRing,w)>m)) pLmDelete(&p);
1613  if (p==NULL) return NULL;
1614  poly r=p;
1615  while (pNext(p)!=NULL)
1616  {
1617    if (totaldegreeWecart_IV(pNext(p),currRing,w)>m)
1618    {
1619      pLmDelete(&pNext(p));
1620    }
1621    else
1622      pIter(p);
1623  }
1624  return r;
1625}
1626
1627int pMinDeg(poly p,intvec *w)
1628{
1629  if(p==NULL)
1630    return -1;
1631  int d=-1;
1632  while(p!=NULL)
1633  {
1634    int d0=0;
1635    for(int j=0;j<pVariables;j++)
1636      if(w==NULL||j>=w->length())
1637        d0+=pGetExp(p,j+1);
1638      else
1639        d0+=(*w)[j]*pGetExp(p,j+1);
1640    if(d0<d||d==-1)
1641      d=d0;
1642    pIter(p);
1643  }
1644  return d;
1645}
1646
1647poly pSeries(int n,poly p,poly u, intvec *w)
1648{
1649  short *ww=iv2array(w);
1650  if(p!=NULL)
1651  {
1652    if(u==NULL)
1653      p=pJetW(p,n,ww);
1654    else
1655      p=pJetW(pMult(p,pInvers(n-pMinDeg(p,w),u,w)),n,ww);
1656  }
1657  omFreeSize((ADDRESS)ww,(pVariables+1)*sizeof(short));
1658  return p;
1659}
1660
1661poly pInvers(int n,poly u,intvec *w)
1662{
1663  short *ww=iv2array(w);
1664  if(n<0)
1665    return NULL;
1666  number u0=nInvers(pGetCoeff(u));
1667  poly v=pNSet(u0);
1668  if(n==0)
1669    return v;
1670  poly u1=pJetW(pSub(pOne(),pMult_nn(u,u0)),n,ww);
1671  if(u1==NULL)
1672    return v;
1673  poly v1=pMult_nn(pCopy(u1),u0);
1674  v=pAdd(v,pCopy(v1));
1675  for(int i=n/pMinDeg(u1,w);i>1;i--)
1676  {
1677    v1=pJetW(pMult(v1,pCopy(u1)),n,ww);
1678    v=pAdd(v,pCopy(v1));
1679  }
1680  pDelete(&u1);
1681  pDelete(&v1);
1682  omFreeSize((ADDRESS)ww,(pVariables+1)*sizeof(short));
1683  return v;
1684}
1685
1686long pDegW(poly p, const short *w)
1687{
1688  long r=-LONG_MAX;
1689
1690  while (p!=NULL)
1691  {
1692    long t=totaldegreeWecart_IV(p,currRing,w);
1693    if (t>r) r=t;
1694    pIter(p);
1695  }
1696  return r;
1697}
1698
1699/*-----------type conversions ----------------------------*/
1700/*2
1701* input: a set of polys (len elements: p[0]..p[len-1])
1702* output: a vector
1703* p will not be changed
1704*/
1705poly  pPolys2Vec(polyset p, int len)
1706{
1707  poly v=NULL;
1708  poly h;
1709  int i;
1710
1711  for (i=len-1; i>=0; i--)
1712  {
1713    if (p[i])
1714    {
1715      h=pCopy(p[i]);
1716      pSetCompP(h,i+1);
1717      v=pAdd(v,h);
1718    }
1719  }
1720 return v;
1721}
1722
1723/*2
1724* convert a vector to a set of polys,
1725* allocates the polyset, (entries 0..(*len)-1)
1726* the vector will not be changed
1727*/
1728void  pVec2Polys(poly v, polyset *p, int *len)
1729{
1730  poly h;
1731  int k;
1732
1733  *len=pMaxComp(v);
1734  if (*len==0) *len=1;
1735  *p=(polyset)omAlloc0((*len)*sizeof(poly));
1736  while (v!=NULL)
1737  {
1738    h=pHead(v);
1739    k=pGetComp(h);
1740    pSetComp(h,0);
1741    (*p)[k-1]=pAdd((*p)[k-1],h);
1742    pIter(v);
1743  }
1744}
1745
1746int p_Var(poly m,const ring r)
1747{
1748  if (m==NULL) return 0;
1749  if (pNext(m)!=NULL) return 0;
1750  int i,e=0;
1751  for (i=r->N; i>0; i--)
1752  {
1753    if (p_GetExp(m,i,r)==1)
1754    {
1755      if (e==0) e=i;
1756      else return 0;
1757    }
1758  }
1759  return e;
1760}
1761
1762/*----------utilities for syzygies--------------*/
1763//BOOLEAN   pVectorHasUnitM(poly p, int * k)
1764//{
1765//  while (p!=NULL)
1766//  {
1767//    if (pLmIsConstantComp(p))
1768//    {
1769//      *k = pGetComp(p);
1770//      return TRUE;
1771//    }
1772//    else pIter(p);
1773//  }
1774//  return FALSE;
1775//}
1776
1777BOOLEAN   pVectorHasUnitB(poly p, int * k)
1778{
1779  poly q=p,qq;
1780  int i;
1781
1782  while (q!=NULL)
1783  {
1784    if (pLmIsConstantComp(q))
1785    {
1786      i = pGetComp(q);
1787      qq = p;
1788      while ((qq != q) && (pGetComp(qq) != i)) pIter(qq);
1789      if (qq == q)
1790      {
1791        *k = i;
1792        return TRUE;
1793      }
1794      else
1795        pIter(q);
1796    }
1797    else pIter(q);
1798  }
1799  return FALSE;
1800}
1801
1802void   pVectorHasUnit(poly p, int * k, int * len)
1803{
1804  poly q=p,qq;
1805  int i,j=0;
1806
1807  *len = 0;
1808  while (q!=NULL)
1809  {
1810    if (pLmIsConstantComp(q))
1811    {
1812      i = pGetComp(q);
1813      qq = p;
1814      while ((qq != q) && (pGetComp(qq) != i)) pIter(qq);
1815      if (qq == q)
1816      {
1817       j = 0;
1818       while (qq!=NULL)
1819       {
1820         if (pGetComp(qq)==i) j++;
1821        pIter(qq);
1822       }
1823       if ((*len == 0) || (j<*len))
1824       {
1825         *len = j;
1826         *k = i;
1827       }
1828      }
1829    }
1830    pIter(q);
1831  }
1832}
1833
1834/*2
1835* returns TRUE if p1 = p2
1836*/
1837BOOLEAN p_EqualPolys(poly p1,poly p2, ring r)
1838{
1839  while ((p1 != NULL) && (p2 != NULL))
1840  {
1841    if (! p_LmEqual(p1, p2,r))
1842      return FALSE;
1843    if (! n_Equal(p_GetCoeff(p1,r), p_GetCoeff(p2,r),r ))
1844      return FALSE;
1845    pIter(p1);
1846    pIter(p2);
1847  }
1848  return (p1==p2);
1849}
1850
1851/*2
1852*returns TRUE if p1 is a skalar multiple of p2
1853*assume p1 != NULL and p2 != NULL
1854*/
1855BOOLEAN pComparePolys(poly p1,poly p2)
1856{
1857  number n,nn;
1858  int i;
1859  pAssume(p1 != NULL && p2 != NULL);
1860
1861  if (!pLmEqual(p1,p2)) //compare leading mons
1862      return FALSE;
1863  if ((pNext(p1)==NULL) && (pNext(p2)!=NULL))
1864     return FALSE;
1865  if ((pNext(p2)==NULL) && (pNext(p1)!=NULL))
1866     return FALSE;
1867  if (pLength(p1) != pLength(p2))
1868    return FALSE;
1869  n=nDiv(pGetCoeff(p1),pGetCoeff(p2));
1870  while ((p1 != NULL) /*&& (p2 != NULL)*/)
1871  {
1872    if ( ! pLmEqual(p1, p2))
1873    {
1874        nDelete(&n);
1875        return FALSE;
1876    }
1877    if (!nEqual(pGetCoeff(p1),nn=nMult(pGetCoeff(p2),n)))
1878    {
1879      nDelete(&n);
1880      nDelete(&nn);
1881      return FALSE;
1882    }
1883    nDelete(&nn);
1884    pIter(p1);
1885    pIter(p2);
1886  }
1887  nDelete(&n);
1888  return TRUE;
1889}
Note: See TracBrowser for help on using the repository browser.