source: git/polys/monomials/p_polys.cc @ a7ee69

spielwiese
Last change on this file since a7ee69 was a7ee69, checked in by Hans Schoenemann <hannes@…>, 13 years ago
moved pLcm to p_poly: p_Lcm
  • Property mode set to 100644
File size: 29.7 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/***************************************************************
5 *  File:    p_polys.cc
6 *  Purpose: implementation of currRing independent poly procedures
7 *  Author:  obachman (Olaf Bachmann)
8 *  Created: 8/00
9 *  Version: $Id$
10 *******************************************************************/
11
12
13#include <auxialiary.h>
14
15#include "ring.h"
16#include "p_polys.h"
17#include "ring.h"
18#include "ideals.h"
19#include "int64vec.h"
20#ifndef NDEBUG
21#include <kernel/febase.h>
22#endif
23
24/***************************************************************
25 *
26 * Completing what needs to be set for the monomial
27 *
28 ***************************************************************/
29// this is special for the syz stuff
30static int* _components = NULL;
31static long* _componentsShifted = NULL;
32static int _componentsExternal = 0;
33
34BOOLEAN pSetm_error=0;
35
36#ifndef NDEBUG
37# define MYTEST 0
38#else /* ifndef NDEBUG */
39# define MYTEST 0
40#endif /* ifndef NDEBUG */
41
42void p_Setm_General(poly p, const ring r)
43{
44  p_LmCheckPolyRing(p, r);
45  int pos=0;
46  if (r->typ!=NULL)
47  {
48    loop
49    {
50      long ord=0;
51      sro_ord* o=&(r->typ[pos]);
52      switch(o->ord_typ)
53      {
54        case ro_dp:
55        {
56          int a,e;
57          a=o->data.dp.start;
58          e=o->data.dp.end;
59          for(int i=a;i<=e;i++) ord+=p_GetExp(p,i,r);
60          p->exp[o->data.dp.place]=ord;
61          break;
62        }
63        case ro_wp_neg:
64          ord=POLY_NEGWEIGHT_OFFSET;
65          // no break;
66        case ro_wp:
67        {
68          int a,e;
69          a=o->data.wp.start;
70          e=o->data.wp.end;
71          int *w=o->data.wp.weights;
72#if 1
73          for(int i=a;i<=e;i++) ord+=p_GetExp(p,i,r)*w[i-a];
74#else
75          long ai;
76          int ei,wi;
77          for(int i=a;i<=e;i++)
78          {
79             ei=p_GetExp(p,i,r);
80             wi=w[i-a];
81             ai=ei*wi;
82             if (ai/ei!=wi) pSetm_error=TRUE;
83             ord+=ai;
84             if (ord<ai) pSetm_error=TRUE;
85          }
86#endif
87          p->exp[o->data.wp.place]=ord;
88          break;
89        }
90      case ro_wp64:
91        {
92          int64 ord=0;
93          int a,e;
94          a=o->data.wp64.start;
95          e=o->data.wp64.end;
96          int64 *w=o->data.wp64.weights64;
97          int64 ei,wi,ai;
98          for(int i=a;i<=e;i++)
99          {
100            //Print("exp %d w %d \n",p_GetExp(p,i,r),(int)w[i-a]);
101            //ord+=((int64)p_GetExp(p,i,r))*w[i-a];
102            ei=(int64)p_GetExp(p,i,r);
103            wi=w[i-a];
104            ai=ei*wi;
105            if(ei!=0 && ai/ei!=wi)
106            {
107              pSetm_error=TRUE;
108              #if SIZEOF_LONG == 4
109              Print("ai %lld, wi %lld\n",ai,wi);
110              #else
111              Print("ai %ld, wi %ld\n",ai,wi);
112              #endif
113            }
114            ord+=ai;
115            if (ord<ai)
116            {
117              pSetm_error=TRUE;
118              #if SIZEOF_LONG == 4
119              Print("ai %lld, ord %lld\n",ai,ord);
120              #else
121              Print("ai %ld, ord %ld\n",ai,ord);
122              #endif
123            }
124          }
125          int64 mask=(int64)0x7fffffff;
126          long a_0=(long)(ord&mask); //2^31
127          long a_1=(long)(ord >>31 ); /*(ord/(mask+1));*/
128
129          //Print("mask: %x,  ord: %d,  a_0: %d,  a_1: %d\n"
130          //,(int)mask,(int)ord,(int)a_0,(int)a_1);
131                    //Print("mask: %d",mask);
132
133          p->exp[o->data.wp64.place]=a_1;
134          p->exp[o->data.wp64.place+1]=a_0;
135//            if(p_Setm_error) Print("***************************\n
136//                                    ***************************\n
137//                                    **WARNING: overflow error**\n
138//                                    ***************************\n
139//                                    ***************************\n");
140          break;
141        }
142        case ro_cp:
143        {
144          int a,e;
145          a=o->data.cp.start;
146          e=o->data.cp.end;
147          int pl=o->data.cp.place;
148          for(int i=a;i<=e;i++) { p->exp[pl]=p_GetExp(p,i,r); pl++; }
149          break;
150        }
151        case ro_syzcomp:
152        {
153          int c=p_GetComp(p,r);
154          long sc = c;
155          int* Components = (_componentsExternal ? _components :
156                             o->data.syzcomp.Components);
157          long* ShiftedComponents = (_componentsExternal ? _componentsShifted:
158                                     o->data.syzcomp.ShiftedComponents);
159          if (ShiftedComponents != NULL)
160          {
161            assume(Components != NULL);
162            assume(c == 0 || Components[c] != 0);
163            sc = ShiftedComponents[Components[c]];
164            assume(c == 0 || sc != 0);
165          }
166          p->exp[o->data.syzcomp.place]=sc;
167          break;
168        }
169        case ro_syz:
170        {
171          const unsigned long c = p_GetComp(p, r);
172          const short place = o->data.syz.place;
173          const int limit = o->data.syz.limit;
174         
175          if (c > limit)
176            p->exp[place] = o->data.syz.curr_index;
177          else if (c > 0)
178          {
179            assume( (1 <= c) && (c <= limit) );
180            p->exp[place]= o->data.syz.syz_index[c];
181          }
182          else
183          {
184            assume(c == 0);
185            p->exp[place]= 0;
186          }
187          break;
188        }
189        // Prefix for Induced Schreyer ordering
190        case ro_isTemp: // Do nothing?? (to be removed into suffix later on...?)
191        {
192          assume(p != NULL);
193
194#ifndef NDEBUG
195#if MYTEST
196          Print("p_Setm_General: isTemp ord: pos: %d, p: ", pos);  p_DebugPrint(p, r, r, 1);
197#endif
198#endif
199          int c = p_GetComp(p, r);
200
201          assume( c >= 0 );
202
203          // Let's simulate case ro_syz above....
204          // Should accumulate (by Suffix) and be a level indicator
205          const int* const pVarOffset = o->data.isTemp.pVarOffset;
206
207          assume( pVarOffset != NULL );
208
209          // TODO: Can this be done in the suffix???
210          for( int i = 1; i <= r->N; i++ ) // No v[0] here!!!
211          {
212            const int vo = pVarOffset[i];
213            if( vo != -1) // TODO: optimize: can be done once!
214            {
215              // Hans! Please don't break it again! p_SetExp(p, ..., r, vo) is correct:
216              p_SetExp(p, p_GetExp(p, i, r), r, vo); // copy put them verbatim
217              // Hans! Please don't break it again! p_GetExp(p, r, vo) is correct:
218              assume( p_GetExp(p, r, vo) == p_GetExp(p, i, r) ); // copy put them verbatim
219            }
220          }
221           
222#ifndef NDEBUG
223          for( int i = 1; i <= r->N; i++ ) // No v[0] here!!!
224          {
225            const int vo = pVarOffset[i];
226            if( vo != -1) // TODO: optimize: can be done once!
227            {
228              // Hans! Please don't break it again! p_GetExp(p, r, vo) is correct:
229              assume( p_GetExp(p, r, vo) == p_GetExp(p, i, r) ); // copy put them verbatim
230            }
231          }
232#if MYTEST
233//          if( p->exp[o->data.isTemp.start] > 0 )
234//          {
235//            PrintS("Initial Value: "); p_DebugPrint(p, r, r, 1);
236//          }
237#endif
238#endif
239          break;
240        }
241
242        // Suffix for Induced Schreyer ordering
243        case ro_is:
244        {
245#ifndef NDEBUG
246#if MYTEST
247          Print("p_Setm_General: ro_is ord: pos: %d, p: ", pos);  p_DebugPrint(p, r, r, 1);
248#endif
249#endif
250
251          assume(p != NULL);
252
253          int c = p_GetComp(p, r);
254
255          assume( c >= 0 );
256          const ideal F = o->data.is.F;
257          const int limit = o->data.is.limit;
258
259          if( F != NULL && c > limit )
260          {
261#ifndef NDEBUG
262#if MYTEST
263            Print("p_Setm_General: ro_is : in rSetm: pos: %d, c: %d >  limit: %d\n", c, pos, limit); // p_DebugPrint(p, r, r, 1);
264#endif
265#endif
266
267            c -= limit;
268            assume( c > 0 );
269            c--;
270
271            assume( c < IDELEMS(F) ); // What about others???
272
273            const poly pp = F->m[c]; // get reference monomial!!!
274
275#ifndef NDEBUG
276#if MYTEST
277            Print("Respective F[c - %d: %d] pp: ", limit, c); 
278            p_DebugPrint(pp, r, r, 1);
279#endif
280#endif
281
282
283            assume(pp != NULL);
284            if(pp == NULL) break;
285
286            const int start = o->data.is.start;
287            const int end = o->data.is.end;
288
289            assume(start <= end);
290             
291//          const int limit = o->data.is.limit;
292          assume( limit >= 0 );
293
294//        const int st = o->data.isTemp.start;       
295
296          if( c > limit )
297            p->exp[start] = 1;
298//          else
299//            p->exp[start] = 0;
300
301             
302#ifndef NDEBUG
303            Print("p_Setm_General: is(-Temp-) :: c: %d, limit: %d, [st:%d] ===>>> %ld\n", c, limit, start, p->exp[start]);
304#endif       
305   
306
307            for( int i = start; i <= end; i++) // v[0] may be here...
308              p->exp[i] += pp->exp[i]; // !!!!!!!! ADD corresponding LT(F)
309
310       
311
312             
313#ifndef NDEBUG
314            const int* const pVarOffset = o->data.is.pVarOffset;
315
316            assume( pVarOffset != NULL );
317
318            for( int i = 1; i <= r->N; i++ ) // No v[0] here!!!
319            {
320              const int vo = pVarOffset[i];
321              if( vo != -1) // TODO: optimize: can be done once!
322                // Hans! Please don't break it again! p_GetExp(p/pp, r, vo) is correct:
323                assume( p_GetExp(p, r, vo) == (p_GetExp(p, i, r) + p_GetExp(pp, r, vo)) );
324            }
325            // TODO: how to check this for computed values???
326#endif
327          } else
328          {
329            const int* const pVarOffset = o->data.is.pVarOffset;
330
331            // What about v[0] - component: it will be added later by
332            // suffix!!!
333            // TODO: Test it!
334            const int vo = pVarOffset[0];
335            if( vo != -1 )
336              p->exp[vo] = c; // initial component v[0]!
337
338#ifndef NDEBUG
339#if MYTEST
340            Print("p_Setm_General: ro_is :: c: %d <= limit: %d, vo: %d, exp: %d\n", c, limit, vo, p->exp[vo]);
341            p_DebugPrint(p, r, r, 1);
342#endif       
343#endif       
344          }
345           
346
347          break;
348        }
349        default:
350          dReportError("wrong ord in rSetm:%d\n",o->ord_typ);
351          return;
352      }
353      pos++;
354      if (pos == r->OrdSize) return;
355    }
356  }
357}
358
359void p_Setm_Syz(poly p, ring r, int* Components, long* ShiftedComponents)
360{
361  _components = Components;
362  _componentsShifted = ShiftedComponents;
363  _componentsExternal = 1;
364  p_Setm_General(p, r);
365  _componentsExternal = 0;
366}
367
368// dummy for lp, ls, etc
369void p_Setm_Dummy(poly p, const ring r)
370{
371  p_LmCheckPolyRing(p, r);
372}
373
374// for dp, Dp, ds, etc
375void p_Setm_TotalDegree(poly p, const ring r)
376{
377  p_LmCheckPolyRing(p, r);
378  p->exp[r->pOrdIndex] = p_Totaldegree(p, r);
379}
380
381// for wp, Wp, ws, etc
382void p_Setm_WFirstTotalDegree(poly p, const ring r)
383{
384  p_LmCheckPolyRing(p, r);
385  p->exp[r->pOrdIndex] = p_WFirstTotalDegree(p, r);
386}
387
388p_SetmProc p_GetSetmProc(ring r)
389{
390  // covers lp, rp, ls,
391  if (r->typ == NULL) return p_Setm_Dummy;
392
393  if (r->OrdSize == 1)
394  {
395    if (r->typ[0].ord_typ == ro_dp &&
396        r->typ[0].data.dp.start == 1 &&
397        r->typ[0].data.dp.end == r->N &&
398        r->typ[0].data.dp.place == r->pOrdIndex)
399      return p_Setm_TotalDegree;
400    if (r->typ[0].ord_typ == ro_wp &&
401        r->typ[0].data.wp.start == 1 &&
402        r->typ[0].data.wp.end == r->N &&
403        r->typ[0].data.wp.place == r->pOrdIndex &&
404        r->typ[0].data.wp.weights == r->firstwv)
405      return p_Setm_WFirstTotalDegree;
406  }
407  return p_Setm_General;
408}
409
410
411/* -------------------------------------------------------------------*/
412/* several possibilities for pFDeg: the degree of the head term       */
413
414/* comptible with ordering */
415long pDeg(poly a, const ring r)
416{
417  p_LmCheckPolyRing(a, r);
418  assume(p_GetOrder(a, r) == p_WTotaldegree(a, r));
419  return p_GetOrder(a, r);
420}
421
422// p_WTotalDegree for weighted orderings
423// whose first block covers all variables
424long p_WFirstTotalDegree(poly p, const ring r)
425{
426  int i;
427  long sum = 0;
428
429  for (i=1; i<= r->firstBlockEnds; i++)
430  {
431    sum += p_GetExp(p, i, r)*r->firstwv[i-1];
432  }
433  return sum;
434}
435
436/*2
437* compute the degree of the leading monomial of p
438* with respect to weigths from the ordering
439* the ordering is not compatible with degree so do not use p->Order
440*/
441long p_WTotaldegree(poly p, const ring r)
442{
443  p_LmCheckPolyRing(p, r);
444  int i, k;
445  long j =0;
446
447  // iterate through each block:
448  for (i=0;r->order[i]!=0;i++)
449  {
450    int b0=r->block0[i];
451    int b1=r->block1[i];
452    switch(r->order[i])
453    {
454      case ringorder_M:
455        for (k=b0 /*r->block0[i]*/;k<=b1 /*r->block1[i]*/;k++)
456        { // in jedem block:
457          j+= p_GetExp(p,k,r)*r->wvhdl[i][k - b0 /*r->block0[i]*/]*r->OrdSgn;
458        }
459        break;
460      case ringorder_wp:
461      case ringorder_ws:
462      case ringorder_Wp:
463      case ringorder_Ws:
464        for (k=b0 /*r->block0[i]*/;k<=b1 /*r->block1[i]*/;k++)
465        { // in jedem block:
466          j+= p_GetExp(p,k,r)*r->wvhdl[i][k - b0 /*r->block0[i]*/];
467        }
468        break;
469      case ringorder_lp:
470      case ringorder_ls:
471      case ringorder_rs:
472      case ringorder_dp:
473      case ringorder_ds:
474      case ringorder_Dp:
475      case ringorder_Ds:
476      case ringorder_rp:
477        for (k=b0 /*r->block0[i]*/;k<=b1 /*r->block1[i]*/;k++)
478        {
479          j+= p_GetExp(p,k,r);
480        }
481        break;
482      case ringorder_a64:
483        {
484          int64* w=(int64*)r->wvhdl[i];
485          for (k=0;k<=(b1 /*r->block1[i]*/ - b0 /*r->block0[i]*/);k++)
486          {
487            //there should be added a line which checks if w[k]>2^31
488            j+= p_GetExp(p,k+1, r)*(long)w[k];
489          }
490          //break;
491          return j;
492        }
493      case ringorder_c:
494      case ringorder_C:
495      case ringorder_S:
496      case ringorder_s:
497      case ringorder_IS:
498      case ringorder_aa:
499        break;
500      case ringorder_a:
501        for (k=b0 /*r->block0[i]*/;k<=b1 /*r->block1[i]*/;k++)
502        { // only one line
503          j+= p_GetExp(p,k, r)*r->wvhdl[i][ k- b0 /*r->block0[i]*/];
504        }
505        //break;
506        return j;
507
508#ifndef NDEBUG
509      default:
510        Print("missing order %d in p_WTotaldegree\n",r->order[i]);
511        break;
512#endif
513    }
514  }
515  return  j;
516}
517
518int pWeight(int i, const ring r)
519{
520  if ((r->firstwv==NULL) || (i>r->firstBlockEnds))
521  {
522    return 1;
523  }
524  return r->firstwv[i-1];
525}
526
527long pWDegree(poly p, const ring r)
528{
529  if (r->firstwv==NULL) return p_Totaldegree(p, r);
530  p_LmCheckPolyRing(p, r);
531  int i;
532  long j =0;
533
534  for(i=1;i<=r->firstBlockEnds;i++)
535    j+=p_GetExp(p, i, r)*r->firstwv[i-1];
536
537  for (;i<=r->N;i++)
538    j+=p_GetExp(p,i, r)*pWeight(i, r);
539
540  return j;
541}
542
543
544/* ---------------------------------------------------------------------*/
545/* several possibilities for pLDeg: the maximal degree of a monomial in p*/
546/*  compute in l also the pLength of p                                   */
547
548/*2
549* compute the length of a polynomial (in l)
550* and the degree of the monomial with maximal degree: the last one
551*/
552long pLDeg0(poly p,int *l, const ring r)
553{
554  p_CheckPolyRing(p, r);
555  long k= p_GetComp(p, r);
556  int ll=1;
557
558  if (k > 0)
559  {
560    while ((pNext(p)!=NULL) && (p_GetComp(pNext(p), r)==k))
561    {
562      pIter(p);
563      ll++;
564    }
565  }
566  else
567  {
568     while (pNext(p)!=NULL)
569     {
570       pIter(p);
571       ll++;
572     }
573  }
574  *l=ll;
575  return r->pFDeg(p, r);
576}
577
578/*2
579* compute the length of a polynomial (in l)
580* and the degree of the monomial with maximal degree: the last one
581* but search in all components before syzcomp
582*/
583long pLDeg0c(poly p,int *l, const ring r)
584{
585  assume(p!=NULL);
586#ifdef PDEBUG
587  _p_Test(p,r,PDEBUG);
588#endif
589  p_CheckPolyRing(p, r);
590  long o;
591  int ll=1;
592
593  if (! rIsSyzIndexRing(r))
594  {
595    while (pNext(p) != NULL)
596    {
597      pIter(p);
598      ll++;
599    }
600    o = r->pFDeg(p, r);
601  }
602  else
603  {
604    int curr_limit = rGetCurrSyzLimit(r);
605    poly pp = p;
606    while ((p=pNext(p))!=NULL)
607    {
608      if (p_GetComp(p, r)<=curr_limit/*syzComp*/)
609        ll++;
610      else break;
611      pp = p;
612    }
613#ifdef PDEBUG
614    _p_Test(pp,r,PDEBUG);
615#endif
616    o = r->pFDeg(pp, r);
617  }
618  *l=ll;
619  return o;
620}
621
622/*2
623* compute the length of a polynomial (in l)
624* and the degree of the monomial with maximal degree: the first one
625* this works for the polynomial case with degree orderings
626* (both c,dp and dp,c)
627*/
628long pLDegb(poly p,int *l, const ring r)
629{
630  p_CheckPolyRing(p, r);
631  long k= p_GetComp(p, r);
632  long o = r->pFDeg(p, r);
633  int ll=1;
634
635  if (k != 0)
636  {
637    while (((p=pNext(p))!=NULL) && (p_GetComp(p, r)==k))
638    {
639      ll++;
640    }
641  }
642  else
643  {
644    while ((p=pNext(p)) !=NULL)
645    {
646      ll++;
647    }
648  }
649  *l=ll;
650  return o;
651}
652
653/*2
654* compute the length of a polynomial (in l)
655* and the degree of the monomial with maximal degree:
656* this is NOT the last one, we have to look for it
657*/
658long pLDeg1(poly p,int *l, const ring r)
659{
660  p_CheckPolyRing(p, r);
661  long k= p_GetComp(p, r);
662  int ll=1;
663  long  t,max;
664
665  max=r->pFDeg(p, r);
666  if (k > 0)
667  {
668    while (((p=pNext(p))!=NULL) && (p_GetComp(p, r)==k))
669    {
670      t=r->pFDeg(p, r);
671      if (t>max) max=t;
672      ll++;
673    }
674  }
675  else
676  {
677    while ((p=pNext(p))!=NULL)
678    {
679      t=r->pFDeg(p, r);
680      if (t>max) max=t;
681      ll++;
682    }
683  }
684  *l=ll;
685  return max;
686}
687
688/*2
689* compute the length of a polynomial (in l)
690* and the degree of the monomial with maximal degree:
691* this is NOT the last one, we have to look for it
692* in all components
693*/
694long pLDeg1c(poly p,int *l, const ring r)
695{
696  p_CheckPolyRing(p, r);
697  int ll=1;
698  long  t,max;
699
700  max=r->pFDeg(p, r);
701  if (rIsSyzIndexRing(r))
702  {
703    long limit = rGetCurrSyzLimit(r);
704    while ((p=pNext(p))!=NULL)
705    {
706      if (p_GetComp(p, r)<=limit)
707      {
708        if ((t=r->pFDeg(p, r))>max) max=t;
709        ll++;
710      }
711      else break;
712    }
713  }
714  else
715  {
716    while ((p=pNext(p))!=NULL)
717    {
718      if ((t=r->pFDeg(p, r))>max) max=t;
719      ll++;
720    }
721  }
722  *l=ll;
723  return max;
724}
725
726// like pLDeg1, only pFDeg == pDeg
727long pLDeg1_Deg(poly p,int *l, const ring r)
728{
729  assume(r->pFDeg == pDeg);
730  p_CheckPolyRing(p, r);
731  long k= p_GetComp(p, r);
732  int ll=1;
733  long  t,max;
734
735  max=p_GetOrder(p, r);
736  if (k > 0)
737  {
738    while (((p=pNext(p))!=NULL) && (p_GetComp(p, r)==k))
739    {
740      t=p_GetOrder(p, r);
741      if (t>max) max=t;
742      ll++;
743    }
744  }
745  else
746  {
747    while ((p=pNext(p))!=NULL)
748    {
749      t=p_GetOrder(p, r);
750      if (t>max) max=t;
751      ll++;
752    }
753  }
754  *l=ll;
755  return max;
756}
757
758long pLDeg1c_Deg(poly p,int *l, const ring r)
759{
760  assume(r->pFDeg == pDeg);
761  p_CheckPolyRing(p, r);
762  int ll=1;
763  long  t,max;
764
765  max=p_GetOrder(p, r);
766  if (rIsSyzIndexRing(r))
767  {
768    long limit = rGetCurrSyzLimit(r);
769    while ((p=pNext(p))!=NULL)
770    {
771      if (p_GetComp(p, r)<=limit)
772      {
773        if ((t=p_GetOrder(p, r))>max) max=t;
774        ll++;
775      }
776      else break;
777    }
778  }
779  else
780  {
781    while ((p=pNext(p))!=NULL)
782    {
783      if ((t=p_GetOrder(p, r))>max) max=t;
784      ll++;
785    }
786  }
787  *l=ll;
788  return max;
789}
790
791// like pLDeg1, only pFDeg == pTotoalDegree
792long pLDeg1_Totaldegree(poly p,int *l, const ring r)
793{
794  p_CheckPolyRing(p, r);
795  long k= p_GetComp(p, r);
796  int ll=1;
797  long  t,max;
798
799  max=p_Totaldegree(p, r);
800  if (k > 0)
801  {
802    while (((p=pNext(p))!=NULL) && (p_GetComp(p, r)==k))
803    {
804      t=p_Totaldegree(p, r);
805      if (t>max) max=t;
806      ll++;
807    }
808  }
809  else
810  {
811    while ((p=pNext(p))!=NULL)
812    {
813      t=p_Totaldegree(p, r);
814      if (t>max) max=t;
815      ll++;
816    }
817  }
818  *l=ll;
819  return max;
820}
821
822long pLDeg1c_Totaldegree(poly p,int *l, const ring r)
823{
824  p_CheckPolyRing(p, r);
825  int ll=1;
826  long  t,max;
827
828  max=p_Totaldegree(p, r);
829  if (rIsSyzIndexRing(r))
830  {
831    long limit = rGetCurrSyzLimit(r);
832    while ((p=pNext(p))!=NULL)
833    {
834      if (p_GetComp(p, r)<=limit)
835      {
836        if ((t=p_Totaldegree(p, r))>max) max=t;
837        ll++;
838      }
839      else break;
840    }
841  }
842  else
843  {
844    while ((p=pNext(p))!=NULL)
845    {
846      if ((t=p_Totaldegree(p, r))>max) max=t;
847      ll++;
848    }
849  }
850  *l=ll;
851  return max;
852}
853
854// like pLDeg1, only pFDeg == p_WFirstTotalDegree
855long pLDeg1_WFirstTotalDegree(poly p,int *l, const ring r)
856{
857  p_CheckPolyRing(p, r);
858  long k= p_GetComp(p, r);
859  int ll=1;
860  long  t,max;
861
862  max=p_WFirstTotalDegree(p, r);
863  if (k > 0)
864  {
865    while (((p=pNext(p))!=NULL) && (p_GetComp(p, r)==k))
866    {
867      t=p_WFirstTotalDegree(p, r);
868      if (t>max) max=t;
869      ll++;
870    }
871  }
872  else
873  {
874    while ((p=pNext(p))!=NULL)
875    {
876      t=p_WFirstTotalDegree(p, r);
877      if (t>max) max=t;
878      ll++;
879    }
880  }
881  *l=ll;
882  return max;
883}
884
885long pLDeg1c_WFirstTotalDegree(poly p,int *l, const ring r)
886{
887  p_CheckPolyRing(p, r);
888  int ll=1;
889  long  t,max;
890
891  max=p_WFirstTotalDegree(p, r);
892  if (rIsSyzIndexRing(r))
893  {
894    long limit = rGetCurrSyzLimit(r);
895    while ((p=pNext(p))!=NULL)
896    {
897      if (p_GetComp(p, r)<=limit)
898      {
899        if ((t=p_Totaldegree(p, r))>max) max=t;
900        ll++;
901      }
902      else break;
903    }
904  }
905  else
906  {
907    while ((p=pNext(p))!=NULL)
908    {
909      if ((t=p_Totaldegree(p, r))>max) max=t;
910      ll++;
911    }
912  }
913  *l=ll;
914  return max;
915}
916
917/***************************************************************
918 *
919 * Maximal Exponent business
920 *
921 ***************************************************************/
922
923static inline unsigned long
924p_GetMaxExpL2(unsigned long l1, unsigned long l2, const ring r,
925              unsigned long number_of_exp)
926{
927  const unsigned long bitmask = r->bitmask;
928  unsigned long ml1 = l1 & bitmask;
929  unsigned long ml2 = l2 & bitmask;
930  unsigned long max = (ml1 > ml2 ? ml1 : ml2);
931  unsigned long j = number_of_exp - 1;
932
933  if (j > 0)
934  {
935    unsigned long mask = bitmask << r->BitsPerExp;
936    while (1)
937    {
938      ml1 = l1 & mask;
939      ml2 = l2 & mask;
940      max |= ((ml1 > ml2 ? ml1 : ml2) & mask);
941      j--;
942      if (j == 0) break;
943      mask = mask << r->BitsPerExp;
944    }
945  }
946  return max;
947}
948
949static inline unsigned long
950p_GetMaxExpL2(unsigned long l1, unsigned long l2, const ring r)
951{
952  return p_GetMaxExpL2(l1, l2, r, r->ExpPerLong);
953}
954
955poly p_GetMaxExpP(poly p, const ring r)
956{
957  p_CheckPolyRing(p, r);
958  if (p == NULL) return p_Init(r);
959  poly max = p_LmInit(p, r);
960  pIter(p);
961  if (p == NULL) return max;
962  int i, offset;
963  unsigned long l_p, l_max;
964  unsigned long divmask = r->divmask;
965
966  do
967  {
968    offset = r->VarL_Offset[0];
969    l_p = p->exp[offset];
970    l_max = max->exp[offset];
971    // do the divisibility trick to find out whether l has an exponent
972    if (l_p > l_max ||
973        (((l_max & divmask) ^ (l_p & divmask)) != ((l_max-l_p) & divmask)))
974      max->exp[offset] = p_GetMaxExpL2(l_max, l_p, r);
975
976    for (i=1; i<r->VarL_Size; i++)
977    {
978      offset = r->VarL_Offset[i];
979      l_p = p->exp[offset];
980      l_max = max->exp[offset];
981      // do the divisibility trick to find out whether l has an exponent
982      if (l_p > l_max ||
983          (((l_max & divmask) ^ (l_p & divmask)) != ((l_max-l_p) & divmask)))
984        max->exp[offset] = p_GetMaxExpL2(l_max, l_p, r);
985    }
986    pIter(p);
987  }
988  while (p != NULL);
989  return max;
990}
991
992unsigned long p_GetMaxExpL(poly p, const ring r, unsigned long l_max)
993{
994  unsigned long l_p, divmask = r->divmask;
995  int i;
996
997  while (p != NULL)
998  {
999    l_p = p->exp[r->VarL_Offset[0]];
1000    if (l_p > l_max ||
1001        (((l_max & divmask) ^ (l_p & divmask)) != ((l_max-l_p) & divmask)))
1002      l_max = p_GetMaxExpL2(l_max, l_p, r);
1003    for (i=1; i<r->VarL_Size; i++)
1004    {
1005      l_p = p->exp[r->VarL_Offset[i]];
1006      // do the divisibility trick to find out whether l has an exponent
1007      if (l_p > l_max ||
1008          (((l_max & divmask) ^ (l_p & divmask)) != ((l_max-l_p) & divmask)))
1009        l_max = p_GetMaxExpL2(l_max, l_p, r);
1010    }
1011    pIter(p);
1012  }
1013  return l_max;
1014}
1015
1016
1017
1018
1019/***************************************************************
1020 *
1021 * Misc things
1022 *
1023 ***************************************************************/
1024// returns TRUE, if all monoms have the same component
1025BOOLEAN p_OneComp(poly p, const ring r)
1026{
1027  if(p!=NULL)
1028  {
1029    long i = p_GetComp(p, r);
1030    while (pNext(p)!=NULL)
1031    {
1032      pIter(p);
1033      if(i != p_GetComp(p, r)) return FALSE;
1034    }
1035  }
1036  return TRUE;
1037}
1038
1039/*2
1040*test if a monomial /head term is a pure power
1041*/
1042int p_IsPurePower(const poly p, const ring r)
1043{
1044  int i,k=0;
1045
1046  for (i=r->N;i;i--)
1047  {
1048    if (p_GetExp(p,i, r)!=0)
1049    {
1050      if(k!=0) return 0;
1051      k=i;
1052    }
1053  }
1054  return k;
1055}
1056
1057/*2
1058*test if a polynomial is univariate
1059* return -1 for constant,
1060* 0 for not univariate,s
1061* i if dep. on var(i)
1062*/
1063int p_IsUnivariate(poly p, const ring r)
1064{
1065  int i,k=-1;
1066
1067  while (p!=NULL)
1068  {
1069    for (i=r->N;i;i--)
1070    {
1071      if (p_GetExp(p,i, r)!=0)
1072      {
1073        if((k!=-1)&&(k!=i)) return 0;
1074        k=i;
1075      }
1076    }
1077    pIter(p);
1078  }
1079  return k;
1080}
1081
1082// set entry e[i] to 1 if var(i) occurs in p, ignore var(j) if e[j]>0
1083int  p_GetVariables(poly p, int * e, const ring r)
1084{
1085  int i;
1086  int n=0;
1087  while(p!=NULL)
1088  {
1089    n=0;
1090    for(i=r->N; i>0; i--)
1091    {
1092      if(e[i]==0)
1093      {
1094        if (p_GetExp(p,i,r)>0)
1095        {
1096          e[i]=1;
1097          n++;
1098        }
1099      }
1100      else
1101        n++;
1102    }
1103    if (n==r->N) break;
1104    pIter(p);
1105  }
1106  return n;
1107}
1108
1109
1110/*2
1111* returns a polynomial representing the integer i
1112*/
1113poly p_ISet(int i, const ring r)
1114{
1115  poly rc = NULL;
1116  if (i!=0)
1117  {
1118    rc = p_Init(r);
1119    pSetCoeff0(rc,n_Init(i,r));
1120    if (r->cf->nIsZero(p_GetCoeff(rc,r)))
1121      p_LmDelete(&rc,r);
1122  }
1123  return rc;
1124}
1125
1126/*2
1127* an optimized version of p_ISet for the special case 1
1128*/
1129poly p_One(const ring r)
1130{
1131  poly rc = p_Init(r);
1132  pSetCoeff0(rc,n_Init(1,r));
1133  return rc;
1134}
1135
1136void p_Split(poly p, poly *h)
1137{
1138  *h=pNext(p);
1139  pNext(p)=NULL;
1140}
1141
1142/*2
1143* pair has no common factor ? or is no polynomial
1144*/
1145BOOLEAN p_HasNotCF(poly p1, poly p2, const ring r)
1146{
1147
1148  if (p_GetComp(p1,r) > 0 || p_GetComp(p2,r) > 0)
1149    return FALSE;
1150  int i = rVar(r);
1151  loop
1152  {
1153    if ((p_GetExp(p1, i, r) > 0) && (p_GetExp(p2, i, r) > 0))
1154      return FALSE;
1155    i--;
1156    if (i == 0)
1157      return TRUE;
1158  }
1159}
1160
1161/*2
1162* convert monomial given as string to poly, e.g. 1x3y5z
1163*/
1164const char * p_Read(const char *st, poly &rc, const ring r)
1165{
1166  if (r==NULL) { rc=NULL;return st;}
1167  int i,j;
1168  rc = p_Init(r);
1169  const char *s = r->cf->nRead(st,&(rc->coef));
1170  if (s==st)
1171  /* i.e. it does not start with a coeff: test if it is a ringvar*/
1172  {
1173    j = r_IsRingVar(s,r);
1174    if (j >= 0)
1175    {
1176      p_IncrExp(rc,1+j,r);
1177      while (*s!='\0') s++;
1178      goto done;
1179    }
1180  }
1181  while (*s!='\0')
1182  {
1183    char ss[2];
1184    ss[0] = *s++;
1185    ss[1] = '\0';
1186    j = r_IsRingVar(ss,r);
1187    if (j >= 0)
1188    {
1189      const char *s_save=s;
1190      s = eati(s,&i);
1191      if (((unsigned long)i) >  r->bitmask)
1192      {
1193        // exponent to large: it is not a monomial
1194        p_LmDelete(&rc,r);
1195        return s_save;
1196      }
1197      p_AddExp(rc,1+j, (long)i, r);
1198    }
1199    else
1200    {
1201      // 1st char of is not a varname
1202      p_LmDelete(&rc,r);
1203      s--;
1204      return s;
1205    }
1206  }
1207done:
1208  if (r->cf->nIsZero(pGetCoeff(rc))) p_LmDelete(&rc,r);
1209  else
1210  {
1211#ifdef HAVE_PLURAL
1212    // in super-commutative ring
1213    // squares of anti-commutative variables are zeroes!
1214    if(rIsSCA(r))
1215    {
1216      const unsigned int iFirstAltVar = scaFirstAltVar(r);
1217      const unsigned int iLastAltVar  = scaLastAltVar(r);
1218
1219      assume(rc != NULL);
1220
1221      for(unsigned int k = iFirstAltVar; k <= iLastAltVar; k++)
1222        if( p_GetExp(rc, k, r) > 1 )
1223        {
1224          p_LmDelete(&rc, r);
1225          goto finish;
1226        }
1227    }
1228#endif
1229   
1230    p_Setm(rc,r);
1231  }
1232finish: 
1233  return s;
1234}
1235poly p_mInit(const char *st, BOOLEAN &ok, const ring r)
1236{
1237  poly p;
1238  const char *s=p_Read(st,p,r);
1239  if (*s!='\0')
1240  {
1241    if ((s!=st)&&isdigit(st[0]))
1242    {
1243      errorreported=TRUE;
1244    }
1245    ok=FALSE;
1246    p_Delete(&p,r);
1247    return NULL;
1248  }
1249  #ifdef PDEBUG
1250  _p_Test(p,r,PDEBUG);
1251  #endif
1252  ok=!errorreported;
1253  return p;
1254}
1255
1256/*2
1257* returns a polynomial representing the number n
1258* destroys n
1259*/
1260poly p_NSet(number n, const ring r)
1261{
1262  if (r->cf->nIsZero(n))
1263  {
1264    r->cf->cfDelete(&n, r);
1265    return NULL;
1266  }
1267  else
1268  {
1269    poly rc = p_Init(r);
1270    pSetCoeff0(rc,n);
1271    return rc;
1272  }
1273}
1274/*2
1275* assumes that the head term of b is a multiple of the head term of a
1276* and return the multiplicant *m
1277* Frank's observation: If LM(b) = LM(a)*m, then we may actually set
1278* negative(!) exponents in the below loop. I suspect that the correct
1279* comment should be "assumes that LM(a) = LM(b)*m, for some monomial m..."
1280*/
1281poly p_Divide(poly a, poly b, const ring r)
1282{
1283  assume((p_GetComp(a,r)==p_GetComp(b,r)) || (p_GetComp(b,r)==0));
1284  int i;
1285  poly result = pInit();
1286
1287  for(i=(int)r->N; i; i--)
1288    p_SetExp(result,i, p_GetExp(a,i,r)- p_GetExp(b,i,r),r);
1289  p_SetComp(result, p_GetComp(a,r) - p_GetComp(b,r),r);
1290  p_Setm(result,r);
1291  return result;
1292}
1293
1294/*2
1295* divides a by the monomial b, ignores monomials which are not divisible
1296* assumes that b is not NULL
1297*/
1298poly p_DivideM(poly a, poly b, const ring r)
1299{
1300  if (a==NULL) return NULL;
1301  poly result=a;
1302  poly prev=NULL;
1303  int i;
1304#ifdef HAVE_RINGS
1305  number inv=pGetCoeff(b);
1306#else
1307  number inv=n_Invers(pGetCoeff(b),r->cf);
1308#endif
1309
1310  while (a!=NULL)
1311  {
1312    if (p_DivisibleBy(b,a,r))
1313    {
1314      for(i=(int)r->N; i; i--)
1315         p_SubExp(a,i, p_GetExp(b,i,r),r);
1316      p_SubComp(a, p_GetComp(b,r),r);
1317      p_Setm(a,r);
1318      prev=a;
1319      pIter(a);
1320    }
1321    else
1322    {
1323      if (prev==NULL)
1324      {
1325        p_DeleteLm(&result,r);
1326        a=result;
1327      }
1328      else
1329      {
1330        p_DeleteLm(&pNext(prev),r);
1331        a=pNext(prev);
1332      }
1333    }
1334  }
1335#ifdef HAVE_RINGS
1336  if (n_IsUnit(inv,r->cf))
1337  {
1338    inv = n_Invers(inv,r->cf);
1339    p_Mult_nn(result,inv,r);
1340    n_Delete(&inv, r->cf);
1341  }
1342  else
1343  {
1344    p_Div_nn(result,inv,r);
1345  }
1346#else
1347  p_Mult_nn(result,inv,r);
1348  n_Delete(&inv, r->cf);
1349#endif
1350  p_Delete(&b, r);
1351  return result;
1352}
1353
1354/*2
1355* returns the LCM of the head terms of a and b in *m
1356*/
1357void p_Lcm(poly a, poly b, poly m, const ring r)
1358{
1359  int i;
1360  for (i=rVar(r); i; i--)
1361  {
1362    p_SetExp(m,i, si_max( p_GetExp(a,i,r), p_GetExp(b,i,r)),r);
1363  }
1364  p_SetComp(m, si_max(p_GetComp(a,r), p_GetComp(b,r)),r);
1365  /* Don't do a pSetm here, otherwise hres/lres chockes */
1366}
1367
1368/***************************************************************
1369 *
1370 * p_ShallowDelete
1371 *
1372 ***************************************************************/
1373#undef LINKAGE
1374#define LINKAGE
1375#undef p_Delete
1376#define p_Delete p_ShallowDelete
1377#undef n_Delete
1378#define n_Delete(n, r) ((void)0)
1379
1380#include <kernel/p_Delete__T.cc>
1381
Note: See TracBrowser for help on using the repository browser.