source: git/kernel/polys.cc @ 2f2bb21

spielwiese
Last change on this file since 2f2bb21 was 107986, checked in by Hans Schönemann <hannes@…>, 16 years ago
*hannes: part stuff ,const ring git-svn-id: file:///usr/local/Singular/svn/trunk@10909 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 21.0 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: polys.cc,v 1.34 2008-07-25 14:37:55 Singular Exp $ */
5
6/*
7* ABSTRACT - all basic methods to manipulate polynomials
8*/
9
10/* includes */
11#include <stdio.h>
12#include <string.h>
13#include <ctype.h>
14#include "mod2.h"
15#include "structs.h"
16#include "omalloc.h"
17#include "febase.h"
18#include "numbers.h"
19#include "polys.h"
20#include "ring.h"
21
22#ifdef HAVE_PLURAL
23#include "gring.h"
24#include "sca.h"
25#endif
26
27/* ----------- global variables, set by pSetGlobals --------------------- */
28/* computes length and maximal degree of a POLYnomial */
29pLDegProc pLDeg;
30/* computes the degree of the initial term, used for std */
31pFDegProc pFDeg;
32/* the monomial ordering of the head monomials a and b */
33/* returns -1 if a comes before b, 0 if a=b, 1 otherwise */
34
35int pVariables;     // number of variables
36
37/* 1 for polynomial ring, -1 otherwise */
38int     pOrdSgn;
39// it is of type int, not BOOLEAN because it is also in ip
40/* TRUE if the monomial ordering is not compatible with pFDeg */
41BOOLEAN pLexOrder;
42
43/* ----------- global variables, set by procedures from hecke/kstd1 ----- */
44/* the highest monomial below pHEdge */
45poly      ppNoether = NULL;
46
47/* -------------------------------------------------------- */
48/*2
49* change all global variables to fit the description of the new ring
50*/
51
52
53void pSetGlobals(const ring r, BOOLEAN complete)
54{
55  int i;
56  if (ppNoether!=NULL) pDelete(&ppNoether);
57  pVariables = r->N;
58  pOrdSgn = r->OrdSgn;
59  pFDeg=r->pFDeg;
60  pLDeg=r->pLDeg;
61  pLexOrder=r->LexOrder;
62
63  if (complete)
64  {
65    test &= ~ TEST_RINGDEP_OPTS;
66    test |= r->options;
67  }
68}
69
70// resets the pFDeg and pLDeg: if pLDeg is not given, it is
71// set to currRing->pLDegOrig, i.e. to the respective LDegProc which
72// only uses pFDeg (and not pDeg, or pTotalDegree, etc)
73void pSetDegProcs(pFDegProc new_FDeg, pLDegProc new_lDeg)
74{
75  assume(new_FDeg != NULL);
76  pFDeg = new_FDeg;
77  currRing->pFDeg = new_FDeg;
78
79  if (new_lDeg == NULL)
80    new_lDeg = currRing->pLDegOrig;
81
82  pLDeg = new_lDeg;
83  currRing->pLDeg = new_lDeg;
84}
85
86
87// restores pFDeg and pLDeg:
88extern void pRestoreDegProcs(pFDegProc old_FDeg, pLDegProc old_lDeg)
89{
90  assume(old_FDeg != NULL && old_lDeg != NULL);
91  pFDeg = old_FDeg;
92  currRing->pFDeg = old_FDeg;
93  pLDeg = old_lDeg;
94  currRing->pLDeg = old_lDeg;
95}
96
97/*2
98* assumes that the head term of b is a multiple of the head term of a
99* and return the multiplicant *m
100*/
101poly pDivide(poly a, poly b)
102{
103  int i;
104  poly result = pInit();
105
106  for(i=(int)pVariables; i; i--)
107    pSetExp(result,i, pGetExp(a,i)- pGetExp(b,i));
108  pSetComp(result, pGetComp(a) - pGetComp(b));
109  pSetm(result);
110  return result;
111}
112
113#ifdef HAVE_RINGS   //TODO Oliver
114#define pDiv_nn(p, n)              p_Div_nn(p, n, currRing)
115
116poly p_Div_nn(poly p, const number n, const ring r)
117{
118  pAssume(!n_IsZero(n,r));
119  p_Test(p, r);
120
121  poly q = p;
122  while (p != NULL)
123  {
124    number nc = pGetCoeff(p);
125    pSetCoeff0(p, n_Div(nc, n, r));
126    n_Delete(&nc, r);
127    pIter(p);
128  }
129  p_Test(q, r);
130  return q;
131}
132#endif
133
134/*2
135* divides a by the monomial b, ignores monomials which are not divisible
136* assumes that b is not NULL
137*/
138poly pDivideM(poly a, poly b)
139{
140  if (a==NULL) return NULL;
141  poly result=a;
142  poly prev=NULL;
143  int i;
144#ifdef HAVE_RINGS
145  number inv=pGetCoeff(b);
146#else
147  number inv=nInvers(pGetCoeff(b));
148#endif
149
150  while (a!=NULL)
151  {
152    if (pDivisibleBy(b,a))
153    {
154      for(i=(int)pVariables; i; i--)
155         pSubExp(a,i, pGetExp(b,i));
156      pSubComp(a, pGetComp(b));
157      pSetm(a);
158      prev=a;
159      pIter(a);
160    }
161    else
162    {
163      if (prev==NULL)
164      {
165        pDeleteLm(&result);
166        a=result;
167      }
168      else
169      {
170        pDeleteLm(&pNext(prev));
171        a=pNext(prev);
172      }
173    }
174  }
175#ifdef HAVE_RINGS
176  if (nIsUnit(inv))
177  {
178    inv = nInvers(inv);
179    pMult_nn(result,inv);
180    nDelete(&inv);
181  }
182  else
183  {
184    pDiv_nn(result,inv);
185  }
186#else
187  pMult_nn(result,inv);
188  nDelete(&inv);
189#endif
190  pDelete(&b);
191  return result;
192}
193
194/*2
195* returns the LCM of the head terms of a and b in *m
196*/
197void pLcm(poly a, poly b, poly m)
198{
199  int i;
200  for (i=pVariables; i; i--)
201  {
202    pSetExp(m,i, si_max( pGetExp(a,i), pGetExp(b,i)));
203  }
204  pSetComp(m, si_max(pGetComp(a), pGetComp(b)));
205  /* Don't do a pSetm here, otherwise hres/lres chockes */
206}
207
208/*2
209* convert monomial given as string to poly, e.g. 1x3y5z
210*/
211const char * p_Read(const char *st, poly &rc, const ring r)
212{
213  if (r==NULL) { rc=NULL;return st;}
214  int i,j;
215  rc = p_Init(r);
216  const char *s = r->cf->nRead(st,&(rc->coef));
217  if (s==st)
218  /* i.e. it does not start with a coeff: test if it is a ringvar*/
219  {
220    j = r_IsRingVar(s,r);
221    if (j >= 0)
222    {
223      p_IncrExp(rc,1+j,r);
224      while (*s!='\0') s++;
225      goto done;
226    }
227  }
228  while (*s!='\0')
229  {
230    char ss[2];
231    ss[0] = *s++;
232    ss[1] = '\0';
233    j = r_IsRingVar(ss,r);
234    if (j >= 0)
235    {
236      const char *s_save=s;
237      s = eati(s,&i);
238      if (((unsigned long)i) >  r->bitmask)
239      {
240        // exponent to large: it is not a monomial
241        p_DeleteLm(&rc,r);
242        return s_save;
243      }
244      p_AddExp(rc,1+j, (Exponent_t)i, r);
245    }
246    else
247    {
248      // 1st char of is not a varname
249      p_DeleteLm(&rc,r);
250      s--;
251      return s;
252    }
253  }
254done:
255  if (r->cf->nIsZero(pGetCoeff(rc))) p_DeleteLm(&rc,r);
256  else
257  {
258#ifdef HAVE_PLURAL
259    // in super-commutative ring
260    // squares of anti-commutative variables are zeroes!
261    if(rIsSCA(r))
262    {
263      const unsigned int iFirstAltVar = scaFirstAltVar(r);
264      const unsigned int iLastAltVar  = scaLastAltVar(r);
265
266      assume(rc != NULL);
267
268      for(unsigned int k = iFirstAltVar; k <= iLastAltVar; k++)
269        if( p_GetExp(rc, k, r) > 1 )
270        {
271          p_DeleteLm(&rc, r);
272          goto finish;
273        }
274    }
275#endif
276   
277    p_Setm(rc,r);
278  }
279finish: 
280  return s;
281}
282
283poly pmInit(const char *st, BOOLEAN &ok)
284{
285  poly p;
286  const char *s=p_Read(st,p,currRing);
287  if (*s!='\0')
288  {
289    if ((s!=st)&&isdigit(st[0]))
290    {
291      errorreported=TRUE;
292    }
293    ok=FALSE;
294    pDelete(&p);
295    return NULL;
296  }
297  ok=!errorreported;
298  return p;
299}
300
301/*2
302*make p homogeneous by multiplying the monomials by powers of x_varnum
303*assume: deg(var(varnum))==1
304*/
305poly pHomogen (poly p, int varnum)
306{
307  poly q=NULL, qn;
308  int  o,ii;
309  sBucket_pt bp;
310
311  if (p!=NULL)
312  {
313    if ((varnum < 1) || (varnum > pVariables))
314    {
315      return NULL;
316    }
317    o=pWTotaldegree(p);
318    q=pNext(p);
319    while (q != NULL)
320    {
321      ii=pWTotaldegree(q);
322      if (ii>o) o=ii;
323      pIter(q);
324    }
325    q = pCopy(p);
326    bp = sBucketCreate(currRing);
327    while (q != NULL)
328    {
329      ii = o-pWTotaldegree(q);
330      if (ii!=0)
331      {
332        pAddExp(q,varnum, (Exponent_t)ii);
333        pSetm(q);
334      }
335      qn = pNext(q);
336      pNext(q) = NULL;
337      sBucket_Add_p(bp, q, 1);
338      q = qn;
339    }
340    sBucketDestroyAdd(bp, &q, &ii);
341  }
342  return q;
343}
344
345/*2
346*replaces the maximal powers of the leading monomial of p2 in p1 by
347*the same powers of n, utility for dehomogenization
348*/
349poly pDehomogen (poly p1,poly p2,number n)
350{
351  polyset P;
352  int     SizeOfSet=5;
353  int     i;
354  poly    p;
355  number  nn;
356
357  P = (polyset)omAlloc0(5*sizeof(poly));
358  //for (i=0; i<5; i++)
359  //{
360  //  P[i] = NULL;
361  //}
362  pCancelPolyByMonom(p1,p2,&P,&SizeOfSet);
363  p = P[0];
364  //P[0] = NULL ;// for safety, may be removed later
365  for (i=1; i<SizeOfSet; i++)
366  {
367    if (P[i] != NULL)
368    {
369      nPower(n,i,&nn);
370      pMult_nn(P[i],nn);
371      p = pAdd(p,P[i]);
372      //P[i] =NULL; // for safety, may be removed later
373      nDelete(&nn);
374    }
375  }
376  omFreeSize((ADDRESS)P,SizeOfSet*sizeof(poly));
377  return p;
378}
379
380/*4
381*Returns the exponent of the maximal power of the leading monomial of
382*p2 in that of p1
383*/
384static int pGetMaxPower (poly p1,poly p2)
385{
386  int     i,k,res = 32000; /*a very large integer*/
387
388  if (p1 == NULL) return 0;
389  for (i=1; i<=pVariables; i++)
390  {
391    if ( pGetExp(p2,i) != 0)
392    {
393      k =  pGetExp(p1,i) /  pGetExp(p2,i);
394      if (k < res) res = k;
395    }
396  }
397  return res;
398}
399
400/*2
401*Returns as i-th entry of P the coefficient of the (i-1) power of
402*the leading monomial of p2 in p1
403*/
404void pCancelPolyByMonom (poly p1,poly p2,polyset * P,int * SizeOfSet)
405{
406  int   maxPow;
407  poly  p,qp,Coeff;
408
409  if (*P == NULL)
410  {
411    *P = (polyset) omAlloc(5*sizeof(poly));
412    *SizeOfSet = 5;
413  }
414  p = pCopy(p1);
415  while (p != NULL)
416  {
417    qp = p->next;
418    p->next = NULL;
419    maxPow = pGetMaxPower(p,p2);
420    Coeff = pDivByMonom(p,p2);
421    if (maxPow > *SizeOfSet)
422    {
423      pEnlargeSet(P,*SizeOfSet,maxPow+1-*SizeOfSet);
424      *SizeOfSet = maxPow+1;
425    }
426    (*P)[maxPow] = pAdd((*P)[maxPow],Coeff);
427    pDelete(&p);
428    p = qp;
429  }
430}
431
432/*2
433*returns the leading monomial of p1 divided by the maximal power of that
434*of p2
435*/
436poly pDivByMonom (poly p1,poly p2)
437{
438  int     k, i;
439
440  if (p1 == NULL) return NULL;
441  k = pGetMaxPower(p1,p2);
442  if (k == 0)
443    return pHead(p1);
444  else
445  {
446    number n;
447    poly p = pInit();
448
449    p->next = NULL;
450    for (i=1; i<=pVariables; i++)
451    {
452       pSetExp(p,i, pGetExp(p1,i)-k* pGetExp(p2,i));
453    }
454    nPower(p2->coef,k,&n);
455    pSetCoeff0(p,nDiv(p1->coef,n));
456    nDelete(&n);
457    pSetm(p);
458    return p;
459  }
460}
461/*----------utilities for syzygies--------------*/
462poly pTakeOutComp(poly * p, int k)
463{
464  poly q = *p,qq=NULL,result = NULL;
465
466  if (q==NULL) return NULL;
467  if (pGetComp(q)==k)
468  {
469    result = q;
470    do
471    {
472      pSetComp(q,0);
473      pSetmComp(q);
474      qq = q;
475      pIter(q);
476    }
477    while ((q!=NULL) && (pGetComp(q)==k));
478    *p = q;
479    pNext(qq) = NULL;
480  }
481  if (q==NULL) return result;
482  if (pGetComp(q) > k)
483  {
484    pDecrComp(q);
485    pSetmComp(q);
486  }
487  poly pNext_q;
488  while ((pNext_q=pNext(q))!=NULL)
489  {
490    if (pGetComp(pNext_q)==k)
491    {
492      if (result==NULL)
493      {
494        result = pNext_q;
495        qq = result;
496      }
497      else
498      {
499        pNext(qq) = pNext_q;
500        pIter(qq);
501      }
502      pNext(q) = pNext(pNext_q);
503      pNext(qq) =NULL;
504      pSetComp(qq,0);
505      pSetmComp(qq);
506    }
507    else
508    {
509      /*pIter(q);*/ q=pNext_q;
510      if (pGetComp(q) > k)
511      {
512        pDecrComp(q);
513        pSetmComp(q);
514      }
515    }
516  }
517  return result;
518}
519
520// Splits *p into two polys: *q which consists of all monoms with
521// component == comp and *p of all other monoms *lq == pLength(*q)
522void pTakeOutComp(poly *r_p, Exponent_t comp, poly *r_q, int *lq)
523{
524  spolyrec pp, qq;
525  poly p, q, p_prev;
526  int l = 0;
527
528#ifdef HAVE_ASSUME
529  int lp = pLength(*r_p);
530#endif
531
532  pNext(&pp) = *r_p;
533  p = *r_p;
534  p_prev = &pp;
535  q = &qq;
536
537  while(p != NULL)
538  {
539    while (pGetComp(p) == comp)
540    {
541      pNext(q) = p;
542      pIter(q);
543      pSetComp(p, 0);
544      pSetmComp(p);
545      pIter(p);
546      l++;
547      if (p == NULL)
548      {
549        pNext(p_prev) = NULL;
550        goto Finish;
551      }
552    }
553    pNext(p_prev) = p;
554    p_prev = p;
555    pIter(p);
556  }
557
558  Finish:
559  pNext(q) = NULL;
560  *r_p = pNext(&pp);
561  *r_q = pNext(&qq);
562  *lq = l;
563#ifdef HAVE_ASSUME
564  assume(pLength(*r_p) + pLength(*r_q) == lp);
565#endif
566  pTest(*r_p);
567  pTest(*r_q);
568}
569
570void pDecrOrdTakeOutComp(poly *r_p, Exponent_t comp, Order_t order,
571                         poly *r_q, int *lq)
572{
573  spolyrec pp, qq;
574  poly p, q, p_prev;
575  int l = 0;
576
577  pNext(&pp) = *r_p;
578  p = *r_p;
579  p_prev = &pp;
580  q = &qq;
581
582#ifdef HAVE_ASSUME
583  if (p != NULL)
584  {
585    while (pNext(p) != NULL)
586    {
587      assume(pGetOrder(p) >= pGetOrder(pNext(p)));
588      pIter(p);
589    }
590  }
591  p = *r_p;
592#endif
593
594  while (p != NULL && pGetOrder(p) > order) pIter(p);
595
596  while(p != NULL && pGetOrder(p) == order)
597  {
598    while (pGetComp(p) == comp)
599    {
600      pNext(q) = p;
601      pIter(q);
602      pIter(p);
603      pSetComp(p, 0);
604      pSetmComp(p);
605      l++;
606      if (p == NULL || pGetOrder(p) != order)
607      {
608        pNext(p_prev) = p;
609        goto Finish;
610      }
611    }
612    pNext(p_prev) = p;
613    p_prev = p;
614    pIter(p);
615  }
616
617  Finish:
618  pNext(q) = NULL;
619  *r_p = pNext(&pp);
620  *r_q = pNext(&qq);
621  *lq = l;
622}
623
624#if 1
625poly pTakeOutComp1(poly * p, int k)
626{
627  poly q = *p;
628
629  if (q==NULL) return NULL;
630
631  poly qq=NULL,result = NULL;
632
633  if (pGetComp(q)==k)
634  {
635    result = q; /* *p */
636    while ((q!=NULL) && (pGetComp(q)==k))
637    {
638      pSetComp(q,0);
639      pSetmComp(q);
640      qq = q;
641      pIter(q);
642    }
643    *p = q;
644    pNext(qq) = NULL;
645  }
646  if (q==NULL) return result;
647//  if (pGetComp(q) > k) pGetComp(q)--;
648  while (pNext(q)!=NULL)
649  {
650    if (pGetComp(pNext(q))==k)
651    {
652      if (result==NULL)
653      {
654        result = pNext(q);
655        qq = result;
656      }
657      else
658      {
659        pNext(qq) = pNext(q);
660        pIter(qq);
661      }
662      pNext(q) = pNext(pNext(q));
663      pNext(qq) =NULL;
664      pSetComp(qq,0);
665      pSetmComp(qq);
666    }
667    else
668    {
669      pIter(q);
670//      if (pGetComp(q) > k) pGetComp(q)--;
671    }
672  }
673  return result;
674}
675#endif
676
677void pDeleteComp(poly * p,int k)
678{
679  poly q;
680
681  while ((*p!=NULL) && (pGetComp(*p)==k)) pDeleteLm(p);
682  if (*p==NULL) return;
683  q = *p;
684  if (pGetComp(q)>k)
685  {
686    pDecrComp(q);
687    pSetmComp(q);
688  }
689  while (pNext(q)!=NULL)
690  {
691    if (pGetComp(pNext(q))==k)
692      pDeleteLm(&(pNext(q)));
693    else
694    {
695      pIter(q);
696      if (pGetComp(q)>k)
697      {
698        pDecrComp(q);
699        pSetmComp(q);
700      }
701    }
702  }
703}
704/*----------end of utilities for syzygies--------------*/
705
706/*2
707* pair has no common factor ? or is no polynomial
708*/
709BOOLEAN pHasNotCF(poly p1, poly p2)
710{
711
712  if (!TEST_OPT_IDLIFT)
713  {
714    if (pGetComp(p1) > 0 || pGetComp(p2) > 0)
715      return FALSE;
716  }
717  int i = 1;
718  loop
719  {
720    if ((pGetExp(p1, i) > 0) && (pGetExp(p2, i) > 0))   return FALSE;
721    if (i == pVariables)                                return TRUE;
722    i++;
723  }
724}
725
726/*2
727*divides p1 by its leading coefficient
728*/
729void pNorm(poly p1)
730{
731#ifdef HAVE_RINGS
732  if (rField_is_Ring(currRing))
733  {
734    Werror("pNorm not possible in the case of coefficient rings.");
735  }
736  else
737#endif
738  if (p1!=NULL)
739  {
740    if (pNext(p1)==NULL)
741    {
742      pSetCoeff(p1,nInit(1));
743      return;
744    }
745    poly h;
746    if (!nIsOne(pGetCoeff(p1)))
747    {
748      number k, c;
749      nNormalize(pGetCoeff(p1));
750      k = pGetCoeff(p1);
751      c = nInit(1);
752      pSetCoeff0(p1,c);
753      h = pNext(p1);
754      while (h!=NULL)
755      {
756        c=nDiv(pGetCoeff(h),k);
757        // no need to normalize: Z/p, R
758        // normalize already in nDiv: Q_a, Z/p_a
759        // remains: Q
760        if (rField_is_Q() && (!nIsOne(c))) nNormalize(c);
761        pSetCoeff(h,c);
762        pIter(h);
763      }
764      nDelete(&k);
765    }
766    else
767    {
768      if (nNormalize != nDummy2)
769      {
770        h = pNext(p1);
771        while (h!=NULL)
772        {
773          nNormalize(pGetCoeff(h));
774          pIter(h);
775        }
776      }
777    }
778  }
779}
780
781/*2
782*normalize all coefficients
783*/
784void p_Normalize(poly p,const ring r)
785{
786  if (rField_has_simple_inverse(r)) return; /* Z/p, GF(p,n), R, long R/C */
787  while (p!=NULL)
788  {
789    if (currRing==r) {nTest(pGetCoeff(p));}
790    n_Normalize(pGetCoeff(p),r);
791    pIter(p);
792  }
793}
794
795// splits p into polys with Exp(n) == 0 and Exp(n) != 0
796// Poly with Exp(n) != 0 is reversed
797static void pSplitAndReversePoly(poly p, int n, poly *non_zero, poly *zero)
798{
799  if (p == NULL)
800  {
801    *non_zero = NULL;
802    *zero = NULL;
803    return;
804  }
805  spolyrec sz;
806  poly z, n_z, next;
807  z = &sz;
808  n_z = NULL;
809
810  while(p != NULL)
811  {
812    next = pNext(p);
813    if (pGetExp(p, n) == 0)
814    {
815      pNext(z) = p;
816      pIter(z);
817    }
818    else
819    {
820      pNext(p) = n_z;
821      n_z = p;
822    }
823    p = next;
824  }
825  pNext(z) = NULL;
826  *zero = pNext(&sz);
827  *non_zero = n_z;
828  return;
829}
830
831/*3
832* substitute the n-th variable by 1 in p
833* destroy p
834*/
835static poly pSubst1 (poly p,int n)
836{
837  poly qq=NULL, result = NULL;
838  poly zero=NULL, non_zero=NULL;
839
840  // reverse, so that add is likely to be linear
841  pSplitAndReversePoly(p, n, &non_zero, &zero);
842
843  while (non_zero != NULL)
844  {
845    assume(pGetExp(non_zero, n) != 0);
846    qq = non_zero;
847    pIter(non_zero);
848    qq->next = NULL;
849    pSetExp(qq,n,0);
850    pSetm(qq);
851    result = pAdd(result,qq);
852  }
853  p = pAdd(result, zero);
854  pTest(p);
855  return p;
856}
857
858/*3
859* substitute the n-th variable by number e in p
860* destroy p
861*/
862static poly pSubst2 (poly p,int n, number e)
863{
864  assume( ! nIsZero(e) );
865  poly qq,result = NULL;
866  number nn, nm;
867  poly zero, non_zero;
868
869  // reverse, so that add is likely to be linear
870  pSplitAndReversePoly(p, n, &non_zero, &zero);
871
872  while (non_zero != NULL)
873  {
874    assume(pGetExp(non_zero, n) != 0);
875    qq = non_zero;
876    pIter(non_zero);
877    qq->next = NULL;
878    nPower(e, pGetExp(qq, n), &nn);
879    nm = nMult(nn, pGetCoeff(qq));
880#ifdef HAVE_RINGS
881    if (nIsZero(nm))
882    {
883      pLmFree(&qq);
884      nDelete(&nm);
885    }
886    else
887#endif
888    {
889      pSetCoeff(qq, nm);
890      pSetExp(qq, n, 0);
891      pSetm(qq);
892      result = pAdd(result,qq);
893    }
894    nDelete(&nn);
895  }
896  p = pAdd(result, zero);
897  pTest(p);
898  return p;
899}
900
901
902/* delete monoms whose n-th exponent is different from zero */
903poly pSubst0(poly p, int n)
904{
905  spolyrec res;
906  poly h = &res;
907  pNext(h) = p;
908
909  while (pNext(h)!=NULL)
910  {
911    if (pGetExp(pNext(h),n)!=0)
912    {
913      pDeleteLm(&pNext(h));
914    }
915    else
916    {
917      pIter(h);
918    }
919  }
920  pTest(pNext(&res));
921  return pNext(&res);
922}
923
924/*2
925* substitute the n-th variable by e in p
926* destroy p
927*/
928poly pSubst(poly p, int n, poly e)
929{
930  if (e == NULL) return pSubst0(p, n);
931
932  if (pIsConstant(e))
933  {
934    if (nIsOne(pGetCoeff(e))) return pSubst1(p,n);
935    else return pSubst2(p, n, pGetCoeff(e));
936  }
937
938#ifdef HAVE_PLURAL
939  if (rIsPluralRing(currRing))
940  {
941    return nc_pSubst(p,n,e);
942  }
943#endif
944
945  int exponent,i;
946  poly h, res, m;
947  int *me,*ee;
948  number nu,nu1;
949
950  me=(int *)omAlloc((pVariables+1)*sizeof(int));
951  ee=(int *)omAlloc((pVariables+1)*sizeof(int));
952  if (e!=NULL) pGetExpV(e,ee);
953  res=NULL;
954  h=p;
955  while (h!=NULL)
956  {
957    if ((e!=NULL) || (pGetExp(h,n)==0))
958    {
959      m=pHead(h);
960      pGetExpV(m,me);
961      exponent=me[n];
962      me[n]=0;
963      for(i=pVariables;i>0;i--)
964        me[i]+=exponent*ee[i];
965      pSetExpV(m,me);
966      if (e!=NULL)
967      {
968        nPower(pGetCoeff(e),exponent,&nu);
969        nu1=nMult(pGetCoeff(m),nu);
970        nDelete(&nu);
971        pSetCoeff(m,nu1);
972      }
973      res=pAdd(res,m);
974    }
975    pDeleteLm(&h);
976  }
977  omFreeSize((ADDRESS)me,(pVariables+1)*sizeof(int));
978  omFreeSize((ADDRESS)ee,(pVariables+1)*sizeof(int));
979  return res;
980}
981
982/* Returns TRUE if
983     * LM(p) | LM(lcm)
984     * LC(p) | LC(lcm) only if ring
985     * Exists i, j:
986         * LE(p, i)  != LE(lcm, i)
987         * LE(p1, i) != LE(lcm, i)   ==> LCM(p1, p) != lcm
988         * LE(p, j)  != LE(lcm, j)
989         * LE(p2, j) != LE(lcm, j)   ==> LCM(p2, p) != lcm
990*/
991BOOLEAN pCompareChain (poly p,poly p1,poly p2,poly lcm)
992{
993  int k, j;
994
995  if (lcm==NULL) return FALSE;
996
997  for (j=pVariables; j; j--)
998    if ( pGetExp(p,j) >  pGetExp(lcm,j)) return FALSE;
999  if ( pGetComp(p) !=  pGetComp(lcm)) return FALSE;
1000  for (j=pVariables; j; j--)
1001  {
1002    if (pGetExp(p1,j)!=pGetExp(lcm,j))
1003    {
1004      if (pGetExp(p,j)!=pGetExp(lcm,j))
1005      {
1006        for (k=pVariables; k>j; k--)
1007        {
1008          if ((pGetExp(p,k)!=pGetExp(lcm,k))
1009          && (pGetExp(p2,k)!=pGetExp(lcm,k)))
1010            return TRUE;
1011        }
1012        for (k=j-1; k; k--)
1013        {
1014          if ((pGetExp(p,k)!=pGetExp(lcm,k))
1015          && (pGetExp(p2,k)!=pGetExp(lcm,k)))
1016            return TRUE;
1017        }
1018        return FALSE;
1019      }
1020    }
1021    else if (pGetExp(p2,j)!=pGetExp(lcm,j))
1022    {
1023      if (pGetExp(p,j)!=pGetExp(lcm,j))
1024      {
1025        for (k=pVariables; k>j; k--)
1026        {
1027          if ((pGetExp(p,k)!=pGetExp(lcm,k))
1028          && (pGetExp(p1,k)!=pGetExp(lcm,k)))
1029            return TRUE;
1030        }
1031        for (k=j-1; k!=0 ; k--)
1032        {
1033          if ((pGetExp(p,k)!=pGetExp(lcm,k))
1034          && (pGetExp(p1,k)!=pGetExp(lcm,k)))
1035            return TRUE;
1036        }
1037        return FALSE;
1038      }
1039    }
1040  }
1041  return FALSE;
1042}
1043BOOLEAN pCompareChainPart (poly p,poly p1,poly p2,poly lcm)
1044{
1045  int k, j;
1046
1047  if (lcm==NULL) return FALSE;
1048
1049  for (j=currRing->real_var_end; j>=currRing->real_var_start; j--)
1050    if ( pGetExp(p,j) >  pGetExp(lcm,j)) return FALSE;
1051  if ( pGetComp(p) !=  pGetComp(lcm)) return FALSE;
1052  for (j=currRing->real_var_end; j>=currRing->real_var_start; j--)
1053  {
1054    if (pGetExp(p1,j)!=pGetExp(lcm,j))
1055    {
1056      if (pGetExp(p,j)!=pGetExp(lcm,j))
1057      {
1058        for (k=pVariables; k>j; k--)
1059        for (k=currRing->real_var_end; k>j; k--)
1060        {
1061          if ((pGetExp(p,k)!=pGetExp(lcm,k))
1062          && (pGetExp(p2,k)!=pGetExp(lcm,k)))
1063            return TRUE;
1064        }
1065        for (k=j-1; k>=currRing->real_var_start; k--)
1066        {
1067          if ((pGetExp(p,k)!=pGetExp(lcm,k))
1068          && (pGetExp(p2,k)!=pGetExp(lcm,k)))
1069            return TRUE;
1070        }
1071        return FALSE;
1072      }
1073    }
1074    else if (pGetExp(p2,j)!=pGetExp(lcm,j))
1075    {
1076      if (pGetExp(p,j)!=pGetExp(lcm,j))
1077      {
1078        for (k=currRing->real_var_end; k>j; k--)
1079        {
1080          if ((pGetExp(p,k)!=pGetExp(lcm,k))
1081          && (pGetExp(p1,k)!=pGetExp(lcm,k)))
1082            return TRUE;
1083        }
1084        for (k=j-1; k>=currRing->real_var_start; k--)
1085        {
1086          if ((pGetExp(p,k)!=pGetExp(lcm,k))
1087          && (pGetExp(p1,k)!=pGetExp(lcm,k)))
1088            return TRUE;
1089        }
1090        return FALSE;
1091      }
1092    }
1093  }
1094  return FALSE;
1095}
1096
1097int pSize(poly p)
1098{
1099  int count = 0;
1100  while ( p != NULL )
1101  {
1102    count+= nSize( pGetCoeff( p ) );
1103    pIter( p );
1104  }
1105  return count;
1106}
1107
Note: See TracBrowser for help on using the repository browser.