source: git/kernel/p_polys.cc @ 11ca48

spielwiese
Last change on this file since 11ca48 was 599326, checked in by Kai Krüger <krueger@…>, 14 years ago
Anne, Kai, Frank: - changes to #include "..." statements to allow cleaner build structure - affected directories: omalloc, kernel, Singular - not yet done: IntergerProgramming git-svn-id: file:///usr/local/Singular/svn/trunk@13032 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 25.2 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 <kernel/mod2.h>
14
15#ifndef NDEBUG
16# define MYTEST 0
17#else /* ifndef NDEBUG */
18# define MYTEST 0
19#endif /* ifndef NDEBUG */
20
21#include <kernel/structs.h>
22#include <kernel/structs.h>
23#include <kernel/p_polys.h>
24#include <kernel/ring.h>
25#include <kernel/ideals.h>
26#include <kernel/int64vec.h>
27#ifndef NDEBUG
28#include <kernel/febase.h>
29#endif
30
31/***************************************************************
32 *
33 * Completing what needs to be set for the monomial
34 *
35 ***************************************************************/
36// this is special for the syz stuff
37static int* _Components = NULL;
38static long* _ShiftedComponents = NULL;
39static int _ExternalComponents = 0;
40
41BOOLEAN pSetm_error=0;
42
43void p_Setm_General(poly p, const ring r)
44{
45  p_LmCheckPolyRing(p, r);
46  int pos=0;
47  if (r->typ!=NULL)
48  {
49    loop
50    {
51      long ord=0;
52      sro_ord* o=&(r->typ[pos]);
53      switch(o->ord_typ)
54      {
55        case ro_dp:
56        {
57          int a,e;
58          a=o->data.dp.start;
59          e=o->data.dp.end;
60          for(int i=a;i<=e;i++) ord+=p_GetExp(p,i,r);
61          p->exp[o->data.dp.place]=ord;
62          break;
63        }
64        case ro_wp_neg:
65          ord=POLY_NEGWEIGHT_OFFSET;
66          // no break;
67        case ro_wp:
68        {
69          int a,e;
70          a=o->data.wp.start;
71          e=o->data.wp.end;
72          int *w=o->data.wp.weights;
73#if 1
74          for(int i=a;i<=e;i++) ord+=p_GetExp(p,i,r)*w[i-a];
75#else
76          long ai;
77          int ei,wi;
78          for(int i=a;i<=e;i++)
79          {
80             ei=p_GetExp(p,i,r);
81             wi=w[i-a];
82             ai=ei*wi;
83             if (ai/ei!=wi) pSetm_error=TRUE;
84             ord+=ai;
85             if (ord<ai) pSetm_error=TRUE;
86          }
87#endif
88          p->exp[o->data.wp.place]=ord;
89          break;
90        }
91      case ro_wp64:
92        {
93          int64 ord=0;
94          int a,e;
95          a=o->data.wp64.start;
96          e=o->data.wp64.end;
97          int64 *w=o->data.wp64.weights64;
98          int64 ei,wi,ai;
99          for(int i=a;i<=e;i++)
100          {
101            //Print("exp %d w %d \n",p_GetExp(p,i,r),(int)w[i-a]);
102            //ord+=((int64)p_GetExp(p,i,r))*w[i-a];
103            ei=(int64)p_GetExp(p,i,r);
104            wi=w[i-a];
105            ai=ei*wi;
106            if(ei!=0 && ai/ei!=wi)
107            {
108              pSetm_error=TRUE;
109              #if SIZEOF_LONG == 4
110              Print("ai %lld, wi %lld\n",ai,wi);
111              #else
112              Print("ai %ld, wi %ld\n",ai,wi);
113              #endif
114            }
115            ord+=ai;
116            if (ord<ai)
117            {
118              pSetm_error=TRUE;
119              #if SIZEOF_LONG == 4
120              Print("ai %lld, ord %lld\n",ai,ord);
121              #else
122              Print("ai %ld, ord %ld\n",ai,ord);
123              #endif
124            }
125          }
126          int64 mask=(int64)0x7fffffff;
127          long a_0=(long)(ord&mask); //2^31
128          long a_1=(long)(ord >>31 ); /*(ord/(mask+1));*/
129
130          //Print("mask: %x,  ord: %d,  a_0: %d,  a_1: %d\n"
131          //,(int)mask,(int)ord,(int)a_0,(int)a_1);
132                    //Print("mask: %d",mask);
133
134          p->exp[o->data.wp64.place]=a_1;
135          p->exp[o->data.wp64.place+1]=a_0;
136//            if(p_Setm_error) Print("***************************\n
137//                                    ***************************\n
138//                                    **WARNING: overflow error**\n
139//                                    ***************************\n
140//                                    ***************************\n");
141          break;
142        }
143        case ro_cp:
144        {
145          int a,e;
146          a=o->data.cp.start;
147          e=o->data.cp.end;
148          int pl=o->data.cp.place;
149          for(int i=a;i<=e;i++) { p->exp[pl]=p_GetExp(p,i,r); pl++; }
150          break;
151        }
152        case ro_syzcomp:
153        {
154          int c=p_GetComp(p,r);
155          long sc = c;
156          int* Components = (_ExternalComponents ? _Components :
157                             o->data.syzcomp.Components);
158          long* ShiftedComponents = (_ExternalComponents ? _ShiftedComponents:
159                                     o->data.syzcomp.ShiftedComponents);
160          if (ShiftedComponents != NULL)
161          {
162            assume(Components != NULL);
163            assume(c == 0 || Components[c] != 0);
164            sc = ShiftedComponents[Components[c]];
165            assume(c == 0 || sc != 0);
166          }
167          p->exp[o->data.syzcomp.place]=sc;
168          break;
169        }
170        case ro_syz:
171        {
172          int c=p_GetComp(p, r);
173          if (c > o->data.syz.limit)
174            p->exp[o->data.syz.place] = o->data.syz.curr_index;
175          else if (c > 0)
176            p->exp[o->data.syz.place]= o->data.syz.syz_index[c];
177          else
178          {
179            assume(c == 0);
180            p->exp[o->data.syz.place]= 0;
181          }
182          break;
183        }
184        // Prefix for Induced Schreyer ordering
185        case ro_isTemp: // Do nothing?? (to be removed into suffix later on...?)
186        {
187          assume(p != NULL);
188
189#ifndef NDEBUG
190#if MYTEST
191          Print("isTemp ord in rSetm: pos: %d, p: ", pos);  p_DebugPrint(p, r, r, 0);
192#endif
193#endif
194          int c = p_GetComp(p, r);
195
196          assume( c >= 0 );
197          const int limit = o->data.is.limit;
198
199          assume( limit >= 0 );
200
201          // Let's simulate case ro_syz above....
202          // Should accumulate (by Suffix) and be a level indicator
203          const int* const pVarOffset = o->data.isTemp.pVarOffset;
204
205          assume( pVarOffset != NULL );
206
207          if( c > limit )
208            p->exp[o->data.isTemp.start] = 1;
209          else
210          {
211            p->exp[o->data.isTemp.start] = 0;
212          }
213
214          // TODO: Can this be done in the suffix???
215          for( int i = 1; i <= r->N; i++ ) // No v[0] here!!!
216          {
217            const int vo = pVarOffset[i];
218            if( vo != -1) // TODO: optimize: can be done once!
219            {
220              p_SetExp(p, p_GetExp(p, i, r), vo, r); // copy put them verbatim
221              assume( p_GetExp(p, vo, r) == p_GetExp(p, i, r) ); // copy put them verbatim
222            }
223          }
224
225       
226
227#ifndef NDEBUG
228          for( int i = 1; i <= r->N; i++ ) // No v[0] here!!!
229          {
230            const int vo = pVarOffset[i];
231            if( vo != -1) // TODO: optimize: can be done once!
232            {
233              assume( p_GetExp(p, vo, r) == p_GetExp(p, i, r) ); // copy put them verbatim
234            }
235          }
236
237#if MYTEST
238//          if( p->exp[o->data.isTemp.start] > 0 )
239//          {
240//            PrintS("Initial Value: "); p_DebugPrint(p, r, r, 1);
241//          }
242#endif
243#endif
244
245          break;
246        }
247
248        // Suffix for Induced Schreyer ordering
249        case ro_is:
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("is ord 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
276#ifndef NDEBUG
277#if MYTEST
278            Print("Respective F[c - %d: %d] pp: ", limit, c); 
279            p_DebugPrint(pp, r, r, 1);
280#endif
281#endif
282
283
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            assume(pp != NULL);
291
292            for( int i = start; i <= end; i++) // v[0] may be here...
293              p->exp[i] += pp->exp[i]; // !!!!!!!! ADD corresponding LT(F)
294
295#ifndef NDEBUG
296            const int* const pVarOffset = o->data.is.pVarOffset;
297
298            assume( pVarOffset != NULL );
299
300            for( int i = 1; i <= r->N; i++ ) // No v[0] here!!!
301            {
302              const int vo = pVarOffset[i];
303              if( vo != -1) // TODO: optimize: can be done once!
304                assume( p_GetExp(p, vo, r) == (p_GetExp(p, i, r) + p_GetExp(pp, vo, r)) );
305            }
306            // TODO: how to check this for computed values???
307#endif
308#ifndef NDEBUG
309#if MYTEST
310            PrintS("IS::Suffix::Result: "); // p_Write(p, r, r);
311            p_DebugPrint(p, r, r, 1);
312#endif
313#endif
314
315          } else
316          {
317            const int* const pVarOffset = o->data.is.pVarOffset;
318
319            // What about v[0] - component: it will be added later by
320            // suffix!!!
321            // TODO: Test it!
322            const int vo = pVarOffset[0];
323            if( vo != -1 )
324              p->exp[vo] = c; // initial component v[0]!
325          }
326
327          break;
328        }
329        default:
330          dReportError("wrong ord in rSetm:%d\n",o->ord_typ);
331          return;
332      }
333      pos++;
334      if (pos == r->OrdSize) return;
335    }
336  }
337}
338
339void p_Setm_Syz(poly p, ring r, int* Components, long* ShiftedComponents)
340{
341  _Components = Components;
342  _ShiftedComponents = ShiftedComponents;
343  _ExternalComponents = 1;
344  p_Setm_General(p, r);
345  _ExternalComponents = 0;
346}
347
348// dummy for lp, ls, etc
349void p_Setm_Dummy(poly p, const ring r)
350{
351  p_LmCheckPolyRing(p, r);
352}
353
354// for dp, Dp, ds, etc
355void p_Setm_TotalDegree(poly p, const ring r)
356{
357  p_LmCheckPolyRing(p, r);
358  p->exp[r->pOrdIndex] = p_ExpVectorQuerSum(p, r);
359}
360
361// for wp, Wp, ws, etc
362void p_Setm_WFirstTotalDegree(poly p, const ring r)
363{
364  p_LmCheckPolyRing(p, r);
365  p->exp[r->pOrdIndex] = pWFirstTotalDegree(p, r);
366}
367
368p_SetmProc p_GetSetmProc(ring r)
369{
370  // covers lp, rp, ls,
371  if (r->typ == NULL) return p_Setm_Dummy;
372
373  if (r->OrdSize == 1)
374  {
375    if (r->typ[0].ord_typ == ro_dp &&
376        r->typ[0].data.dp.start == 1 &&
377        r->typ[0].data.dp.end == r->N &&
378        r->typ[0].data.dp.place == r->pOrdIndex)
379      return p_Setm_TotalDegree;
380    if (r->typ[0].ord_typ == ro_wp &&
381        r->typ[0].data.wp.start == 1 &&
382        r->typ[0].data.wp.end == r->N &&
383        r->typ[0].data.wp.place == r->pOrdIndex &&
384        r->typ[0].data.wp.weights == r->firstwv)
385      return p_Setm_WFirstTotalDegree;
386  }
387  return p_Setm_General;
388}
389
390
391/* -------------------------------------------------------------------*/
392/* several possibilities for pFDeg: the degree of the head term       */
393
394/* comptible with ordering */
395long pDeg(poly a, const ring r)
396{
397  p_LmCheckPolyRing(a, r);
398  assume(p_GetOrder(a, r) == pWTotaldegree(a, r));
399  return p_GetOrder(a, r);
400}
401
402/*2
403* compute the degree of the leading monomial of p
404* with respect to weigths 1
405* (all are 1 so save multiplications or they are of different signs)
406* the ordering is not compatible with degree so do not use p->Order
407*/
408long pTotaldegree(poly p, const ring r)
409{
410  p_LmCheckPolyRing(p, r);
411  return (long)p_ExpVectorQuerSum(p, r);
412}
413
414// pWTotalDegree for weighted orderings
415// whose first block covers all variables
416static inline long _pWFirstTotalDegree(poly p, const ring r)
417{
418  int i;
419  long sum = 0;
420
421  for (i=1; i<= r->firstBlockEnds; i++)
422  {
423    sum += p_GetExp(p, i, r)*r->firstwv[i-1];
424  }
425  return sum;
426}
427
428long pWFirstTotalDegree(poly p, const ring r)
429{
430  return (long) _pWFirstTotalDegree(p, r);
431}
432
433/*2
434* compute the degree of the leading monomial of p
435* with respect to weigths from the ordering
436* the ordering is not compatible with degree so do not use p->Order
437*/
438long pWTotaldegree(poly p, const ring r)
439{
440  p_LmCheckPolyRing(p, r);
441  int i, k;
442  long j =0;
443
444  // iterate through each block:
445  for (i=0;r->order[i]!=0;i++)
446  {
447    int b0=r->block0[i];
448    int b1=r->block1[i];
449    switch(r->order[i])
450    {
451      case ringorder_M:
452        for (k=b0 /*r->block0[i]*/;k<=b1 /*r->block1[i]*/;k++)
453        { // in jedem block:
454          j+= p_GetExp(p,k,r)*r->wvhdl[i][k - b0 /*r->block0[i]*/]*r->OrdSgn;
455        }
456        break;
457      case ringorder_wp:
458      case ringorder_ws:
459      case ringorder_Wp:
460      case ringorder_Ws:
461        for (k=b0 /*r->block0[i]*/;k<=b1 /*r->block1[i]*/;k++)
462        { // in jedem block:
463          j+= p_GetExp(p,k,r)*r->wvhdl[i][k - b0 /*r->block0[i]*/];
464        }
465        break;
466      case ringorder_lp:
467      case ringorder_ls:
468      case ringorder_rs:
469      case ringorder_dp:
470      case ringorder_ds:
471      case ringorder_Dp:
472      case ringorder_Ds:
473      case ringorder_rp:
474        for (k=b0 /*r->block0[i]*/;k<=b1 /*r->block1[i]*/;k++)
475        {
476          j+= p_GetExp(p,k,r);
477        }
478        break;
479      case ringorder_a64:
480        {
481          int64* w=(int64*)r->wvhdl[i];
482          for (k=0;k<=(b1 /*r->block1[i]*/ - b0 /*r->block0[i]*/);k++)
483          {
484            //there should be added a line which checks if w[k]>2^31
485            j+= p_GetExp(p,k+1, r)*(long)w[k];
486          }
487          //break;
488          return j;
489        }
490      case ringorder_c:
491      case ringorder_C:
492      case ringorder_S:
493      case ringorder_s:
494      case ringorder_IS:
495      case ringorder_aa:
496        break;
497      case ringorder_a:
498        for (k=b0 /*r->block0[i]*/;k<=b1 /*r->block1[i]*/;k++)
499        { // only one line
500          j+= p_GetExp(p,k, r)*r->wvhdl[i][ k- b0 /*r->block0[i]*/];
501        }
502        //break;
503        return j;
504
505#ifndef NDEBUG
506      default:
507        Print("missing order %d in pWTotaldegree\n",r->order[i]);
508        break;
509#endif
510    }
511  }
512  return  j;
513}
514
515int pWeight(int i, const ring r)
516{
517  if ((r->firstwv==NULL) || (i>r->firstBlockEnds))
518  {
519    return 1;
520  }
521  return r->firstwv[i-1];
522}
523
524long pWDegree(poly p, const ring r)
525{
526  if (r->firstwv==NULL) return pTotaldegree(p, r);
527  p_LmCheckPolyRing(p, r);
528  int i, k;
529  long j =0;
530
531  for(i=1;i<=r->firstBlockEnds;i++)
532    j+=p_GetExp(p, i, r)*r->firstwv[i-1];
533
534  for (;i<=r->N;i++)
535    j+=p_GetExp(p,i, r)*pWeight(i, r);
536
537  return j;
538}
539
540
541/* ---------------------------------------------------------------------*/
542/* several possibilities for pLDeg: the maximal degree of a monomial in p*/
543/*  compute in l also the pLength of p                                   */
544
545/*2
546* compute the length of a polynomial (in l)
547* and the degree of the monomial with maximal degree: the last one
548*/
549long pLDeg0(poly p,int *l, const ring r)
550{
551  p_CheckPolyRing(p, r);
552  long k= p_GetComp(p, r);
553  int ll=1;
554
555  if (k > 0)
556  {
557    while ((pNext(p)!=NULL) && (p_GetComp(pNext(p), r)==k))
558    {
559      pIter(p);
560      ll++;
561    }
562  }
563  else
564  {
565     while (pNext(p)!=NULL)
566     {
567       pIter(p);
568       ll++;
569     }
570  }
571  *l=ll;
572  return r->pFDeg(p, r);
573}
574
575/*2
576* compute the length of a polynomial (in l)
577* and the degree of the monomial with maximal degree: the last one
578* but search in all components before syzcomp
579*/
580long pLDeg0c(poly p,int *l, const ring r)
581{
582  assume(p!=NULL);
583#ifdef PDEBUG
584  _p_Test(p,r,PDEBUG);
585#endif
586  p_CheckPolyRing(p, r);
587  long o;
588  int ll=1;
589
590  if (! rIsSyzIndexRing(r))
591  {
592    while (pNext(p) != NULL)
593    {
594      pIter(p);
595      ll++;
596    }
597    o = r->pFDeg(p, r);
598  }
599  else
600  {
601    int curr_limit = rGetCurrSyzLimit(r);
602    poly pp = p;
603    while ((p=pNext(p))!=NULL)
604    {
605      if (p_GetComp(p, r)<=curr_limit/*syzComp*/)
606        ll++;
607      else break;
608      pp = p;
609    }
610#ifdef PDEBUG
611    _p_Test(pp,r,PDEBUG);
612#endif
613    o = r->pFDeg(pp, r);
614  }
615  *l=ll;
616  return o;
617}
618
619/*2
620* compute the length of a polynomial (in l)
621* and the degree of the monomial with maximal degree: the first one
622* this works for the polynomial case with degree orderings
623* (both c,dp and dp,c)
624*/
625long pLDegb(poly p,int *l, const ring r)
626{
627  p_CheckPolyRing(p, r);
628  long k= p_GetComp(p, r);
629  long o = r->pFDeg(p, r);
630  int ll=1;
631
632  if (k != 0)
633  {
634    while (((p=pNext(p))!=NULL) && (p_GetComp(p, r)==k))
635    {
636      ll++;
637    }
638  }
639  else
640  {
641    while ((p=pNext(p)) !=NULL)
642    {
643      ll++;
644    }
645  }
646  *l=ll;
647  return o;
648}
649
650/*2
651* compute the length of a polynomial (in l)
652* and the degree of the monomial with maximal degree:
653* this is NOT the last one, we have to look for it
654*/
655long pLDeg1(poly p,int *l, const ring r)
656{
657  p_CheckPolyRing(p, r);
658  long k= p_GetComp(p, r);
659  int ll=1;
660  long  t,max;
661
662  max=r->pFDeg(p, r);
663  if (k > 0)
664  {
665    while (((p=pNext(p))!=NULL) && (p_GetComp(p, r)==k))
666    {
667      t=r->pFDeg(p, r);
668      if (t>max) max=t;
669      ll++;
670    }
671  }
672  else
673  {
674    while ((p=pNext(p))!=NULL)
675    {
676      t=r->pFDeg(p, r);
677      if (t>max) max=t;
678      ll++;
679    }
680  }
681  *l=ll;
682  return max;
683}
684
685/*2
686* compute the length of a polynomial (in l)
687* and the degree of the monomial with maximal degree:
688* this is NOT the last one, we have to look for it
689* in all components
690*/
691long pLDeg1c(poly p,int *l, const ring r)
692{
693  p_CheckPolyRing(p, r);
694  int ll=1;
695  long  t,max;
696
697  max=r->pFDeg(p, r);
698  if (rIsSyzIndexRing(r))
699  {
700    long limit = rGetCurrSyzLimit(r);
701    while ((p=pNext(p))!=NULL)
702    {
703      if (p_GetComp(p, r)<=limit)
704      {
705        if ((t=r->pFDeg(p, r))>max) max=t;
706        ll++;
707      }
708      else break;
709    }
710  }
711  else
712  {
713    while ((p=pNext(p))!=NULL)
714    {
715      if ((t=r->pFDeg(p, r))>max) max=t;
716      ll++;
717    }
718  }
719  *l=ll;
720  return max;
721}
722
723// like pLDeg1, only pFDeg == pDeg
724long pLDeg1_Deg(poly p,int *l, const ring r)
725{
726  assume(r->pFDeg == pDeg);
727  p_CheckPolyRing(p, r);
728  long k= p_GetComp(p, r);
729  int ll=1;
730  long  t,max;
731
732  max=p_GetOrder(p, r);
733  if (k > 0)
734  {
735    while (((p=pNext(p))!=NULL) && (p_GetComp(p, r)==k))
736    {
737      t=p_GetOrder(p, r);
738      if (t>max) max=t;
739      ll++;
740    }
741  }
742  else
743  {
744    while ((p=pNext(p))!=NULL)
745    {
746      t=p_GetOrder(p, r);
747      if (t>max) max=t;
748      ll++;
749    }
750  }
751  *l=ll;
752  return max;
753}
754
755long pLDeg1c_Deg(poly p,int *l, const ring r)
756{
757  assume(r->pFDeg == pDeg);
758  p_CheckPolyRing(p, r);
759  int ll=1;
760  long  t,max;
761
762  max=p_GetOrder(p, r);
763  if (rIsSyzIndexRing(r))
764  {
765    long limit = rGetCurrSyzLimit(r);
766    while ((p=pNext(p))!=NULL)
767    {
768      if (p_GetComp(p, r)<=limit)
769      {
770        if ((t=p_GetOrder(p, r))>max) max=t;
771        ll++;
772      }
773      else break;
774    }
775  }
776  else
777  {
778    while ((p=pNext(p))!=NULL)
779    {
780      if ((t=p_GetOrder(p, r))>max) max=t;
781      ll++;
782    }
783  }
784  *l=ll;
785  return max;
786}
787
788// like pLDeg1, only pFDeg == pTotoalDegree
789long pLDeg1_Totaldegree(poly p,int *l, const ring r)
790{
791  p_CheckPolyRing(p, r);
792  long k= p_GetComp(p, r);
793  int ll=1;
794  long  t,max;
795
796  max=p_ExpVectorQuerSum(p, r);
797  if (k > 0)
798  {
799    while (((p=pNext(p))!=NULL) && (p_GetComp(p, r)==k))
800    {
801      t=p_ExpVectorQuerSum(p, r);
802      if (t>max) max=t;
803      ll++;
804    }
805  }
806  else
807  {
808    while ((p=pNext(p))!=NULL)
809    {
810      t=p_ExpVectorQuerSum(p, r);
811      if (t>max) max=t;
812      ll++;
813    }
814  }
815  *l=ll;
816  return max;
817}
818
819long pLDeg1c_Totaldegree(poly p,int *l, const ring r)
820{
821  p_CheckPolyRing(p, r);
822  int ll=1;
823  long  t,max;
824
825  max=p_ExpVectorQuerSum(p, r);
826  if (rIsSyzIndexRing(r))
827  {
828    long limit = rGetCurrSyzLimit(r);
829    while ((p=pNext(p))!=NULL)
830    {
831      if (p_GetComp(p, r)<=limit)
832      {
833        if ((t=p_ExpVectorQuerSum(p, r))>max) max=t;
834        ll++;
835      }
836      else break;
837    }
838  }
839  else
840  {
841    while ((p=pNext(p))!=NULL)
842    {
843      if ((t=p_ExpVectorQuerSum(p, r))>max) max=t;
844      ll++;
845    }
846  }
847  *l=ll;
848  return max;
849}
850
851// like pLDeg1, only pFDeg == pWFirstTotalDegree
852long pLDeg1_WFirstTotalDegree(poly p,int *l, const ring r)
853{
854  p_CheckPolyRing(p, r);
855  long k= p_GetComp(p, r);
856  int ll=1;
857  long  t,max;
858
859  max=_pWFirstTotalDegree(p, r);
860  if (k > 0)
861  {
862    while (((p=pNext(p))!=NULL) && (p_GetComp(p, r)==k))
863    {
864      t=_pWFirstTotalDegree(p, r);
865      if (t>max) max=t;
866      ll++;
867    }
868  }
869  else
870  {
871    while ((p=pNext(p))!=NULL)
872    {
873      t=_pWFirstTotalDegree(p, r);
874      if (t>max) max=t;
875      ll++;
876    }
877  }
878  *l=ll;
879  return max;
880}
881
882long pLDeg1c_WFirstTotalDegree(poly p,int *l, const ring r)
883{
884  p_CheckPolyRing(p, r);
885  int ll=1;
886  long  t,max;
887
888  max=_pWFirstTotalDegree(p, r);
889  if (rIsSyzIndexRing(r))
890  {
891    long limit = rGetCurrSyzLimit(r);
892    while ((p=pNext(p))!=NULL)
893    {
894      if (p_GetComp(p, r)<=limit)
895      {
896        if ((t=p_ExpVectorQuerSum(p, r))>max) max=t;
897        ll++;
898      }
899      else break;
900    }
901  }
902  else
903  {
904    while ((p=pNext(p))!=NULL)
905    {
906      if ((t=p_ExpVectorQuerSum(p, r))>max) max=t;
907      ll++;
908    }
909  }
910  *l=ll;
911  return max;
912}
913
914/***************************************************************
915 *
916 * Maximal Exponent business
917 *
918 ***************************************************************/
919
920static inline unsigned long
921p_GetMaxExpL2(unsigned long l1, unsigned long l2, const ring r,
922              unsigned long number_of_exp)
923{
924  const unsigned long bitmask = r->bitmask;
925  unsigned long ml1 = l1 & bitmask;
926  unsigned long ml2 = l2 & bitmask;
927  unsigned long max = (ml1 > ml2 ? ml1 : ml2);
928  unsigned long j = number_of_exp - 1;
929
930  if (j > 0)
931  {
932    unsigned long mask = bitmask << r->BitsPerExp;
933    while (1)
934    {
935      ml1 = l1 & mask;
936      ml2 = l2 & mask;
937      max |= ((ml1 > ml2 ? ml1 : ml2) & mask);
938      j--;
939      if (j == 0) break;
940      mask = mask << r->BitsPerExp;
941    }
942  }
943  return max;
944}
945
946static inline unsigned long
947p_GetMaxExpL2(unsigned long l1, unsigned long l2, const ring r)
948{
949  return p_GetMaxExpL2(l1, l2, r, r->ExpPerLong);
950}
951
952poly p_GetMaxExpP(poly p, const ring r)
953{
954  p_CheckPolyRing(p, r);
955  if (p == NULL) return p_Init(r);
956  poly max = p_LmInit(p, r);
957  pIter(p);
958  if (p == NULL) return max;
959  int i, offset;
960  unsigned long l_p, l_max;
961  unsigned long divmask = r->divmask;
962
963  do
964  {
965    offset = r->VarL_Offset[0];
966    l_p = p->exp[offset];
967    l_max = max->exp[offset];
968    // do the divisibility trick to find out whether l has an exponent
969    if (l_p > l_max ||
970        (((l_max & divmask) ^ (l_p & divmask)) != ((l_max-l_p) & divmask)))
971      max->exp[offset] = p_GetMaxExpL2(l_max, l_p, r);
972
973    for (i=1; i<r->VarL_Size; i++)
974    {
975      offset = r->VarL_Offset[i];
976      l_p = p->exp[offset];
977      l_max = max->exp[offset];
978      // do the divisibility trick to find out whether l has an exponent
979      if (l_p > l_max ||
980          (((l_max & divmask) ^ (l_p & divmask)) != ((l_max-l_p) & divmask)))
981        max->exp[offset] = p_GetMaxExpL2(l_max, l_p, r);
982    }
983    pIter(p);
984  }
985  while (p != NULL);
986  return max;
987}
988
989unsigned long p_GetMaxExpL(poly p, const ring r, unsigned long l_max)
990{
991  unsigned long l_p, divmask = r->divmask;
992  int i;
993
994  while (p != NULL)
995  {
996    l_p = p->exp[r->VarL_Offset[0]];
997    if (l_p > l_max ||
998        (((l_max & divmask) ^ (l_p & divmask)) != ((l_max-l_p) & divmask)))
999      l_max = p_GetMaxExpL2(l_max, l_p, r);
1000    for (i=1; i<r->VarL_Size; i++)
1001    {
1002      l_p = p->exp[r->VarL_Offset[i]];
1003      // do the divisibility trick to find out whether l has an exponent
1004      if (l_p > l_max ||
1005          (((l_max & divmask) ^ (l_p & divmask)) != ((l_max-l_p) & divmask)))
1006        l_max = p_GetMaxExpL2(l_max, l_p, r);
1007    }
1008    pIter(p);
1009  }
1010  return l_max;
1011}
1012
1013
1014
1015
1016/***************************************************************
1017 *
1018 * Misc things
1019 *
1020 ***************************************************************/
1021// returns TRUE, if all monoms have the same component
1022BOOLEAN p_OneComp(poly p, const ring r)
1023{
1024  if(p!=NULL)
1025  {
1026    long i = p_GetComp(p, r);
1027    while (pNext(p)!=NULL)
1028    {
1029      pIter(p);
1030      if(i != p_GetComp(p, r)) return FALSE;
1031    }
1032  }
1033  return TRUE;
1034}
1035
1036/*2
1037*test if a monomial /head term is a pure power
1038*/
1039int p_IsPurePower(const poly p, const ring r)
1040{
1041  int i,k=0;
1042
1043  for (i=r->N;i;i--)
1044  {
1045    if (p_GetExp(p,i, r)!=0)
1046    {
1047      if(k!=0) return 0;
1048      k=i;
1049    }
1050  }
1051  return k;
1052}
1053
1054/*2
1055*test if a polynomial is univariate
1056* return -1 for constant,
1057* 0 for not univariate,s
1058* i if dep. on var(i)
1059*/
1060int p_IsUnivariate(poly p, const ring r)
1061{
1062  int i,k=-1;
1063
1064  while (p!=NULL)
1065  {
1066    for (i=r->N;i;i--)
1067    {
1068      if (p_GetExp(p,i, r)!=0)
1069      {
1070        if((k!=-1)&&(k!=i)) return 0;
1071        k=i;
1072      }
1073    }
1074    pIter(p);
1075  }
1076  return k;
1077}
1078
1079// set entry e[i] to 1 if var(i) occurs in p, ignore var(j) if e[j]>0
1080int  p_GetVariables(poly p, int * e, const ring r)
1081{
1082  int i;
1083  int n=0;
1084  while(p!=NULL)
1085  {
1086    n=0;
1087    for(i=r->N; i>0; i--)
1088    {
1089      if(e[i]==0)
1090      {
1091        if (p_GetExp(p,i,r)>0)
1092        {
1093          e[i]=1;
1094          n++;
1095        }
1096      }
1097      else
1098        n++;
1099    }
1100    if (n==r->N) break;
1101    pIter(p);
1102  }
1103  return n;
1104}
1105
1106
1107/*2
1108* returns a polynomial representing the integer i
1109*/
1110poly p_ISet(int i, const ring r)
1111{
1112  poly rc = NULL;
1113  if (i!=0)
1114  {
1115    rc = p_Init(r);
1116    pSetCoeff0(rc,n_Init(i,r));
1117    if (r->cf->nIsZero(p_GetCoeff(rc,r)))
1118      p_LmDelete(&rc,r);
1119  }
1120  return rc;
1121}
1122
1123/*2
1124* an optimized version of p_ISet for the special case 1
1125*/
1126poly p_One(const ring r)
1127{
1128  poly rc = p_Init(r);
1129  pSetCoeff0(rc,n_Init(1,r));
1130  return rc;
1131}
1132
1133/*2
1134* returns a polynomial representing the number n
1135* destroys n
1136*/
1137poly p_NSet(number n, const ring r)
1138{
1139  if (r->cf->nIsZero(n))
1140  {
1141    r->cf->cfDelete(&n, r);
1142    return NULL;
1143  }
1144  else
1145  {
1146    poly rc = p_Init(r);
1147    pSetCoeff0(rc,n);
1148    return rc;
1149  }
1150}
1151
1152/***************************************************************
1153 *
1154 * p_ShallowDelete
1155 *
1156 ***************************************************************/
1157#undef LINKAGE
1158#define LINKAGE
1159#undef p_Delete
1160#define p_Delete p_ShallowDelete
1161#undef n_Delete
1162#define n_Delete(n, r) ((void)0)
1163
1164#include <kernel/p_Delete__T.cc>
1165
Note: See TracBrowser for help on using the repository browser.