source: git/kernel/p_polys.cc @ 24cece

spielwiese
Last change on this file since 24cece was 24cece, checked in by Hans Schönemann <hannes@…>, 14 years ago
static inline git-svn-id: file:///usr/local/Singular/svn/trunk@12724 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 25.1 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 "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 "structs.h"
22#include "structs.h"
23#include "p_polys.h"
24#include "ring.h"
25#include "ideals.h"
26#include "int64vec.h"
27#ifndef NDEBUG
28#include "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), r, vo); // copy put them verbatim
221              assume( p_GetExp(p, r, vo) == 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, r, vo) == 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, r, vo) == (p_GetExp(p, i, r) + p_GetExp(pp, r, vo)) );
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_dp:
469      case ringorder_ds:
470      case ringorder_Dp:
471      case ringorder_Ds:
472      case ringorder_rp:
473        for (k=b0 /*r->block0[i]*/;k<=b1 /*r->block1[i]*/;k++)
474        {
475          j+= p_GetExp(p,k,r);
476        }
477        break;
478      case ringorder_a64:
479        {
480          int64* w=(int64*)r->wvhdl[i];
481          for (k=0;k<=(b1 /*r->block1[i]*/ - b0 /*r->block0[i]*/);k++)
482          {
483            //there should be added a line which checks if w[k]>2^31
484            j+= p_GetExp(p,k+1, r)*(long)w[k];
485          }
486          //break;
487          return j;
488        }
489      case ringorder_c:
490      case ringorder_C:
491      case ringorder_S:
492      case ringorder_s:
493      case ringorder_IS:
494      case ringorder_aa:
495        break;
496      case ringorder_a:
497        for (k=b0 /*r->block0[i]*/;k<=b1 /*r->block1[i]*/;k++)
498        { // only one line
499          j+= p_GetExp(p,k, r)*r->wvhdl[i][ k- b0 /*r->block0[i]*/];
500        }
501        //break;
502        return j;
503
504#ifndef NDEBUG
505      default:
506        Print("missing order %d in pWTotaldegree\n",r->order[i]);
507        break;
508#endif
509    }
510  }
511  return  j;
512}
513
514int pWeight(int i, const ring r)
515{
516  if ((r->firstwv==NULL) || (i>r->firstBlockEnds))
517  {
518    return 1;
519  }
520  return r->firstwv[i-1];
521}
522
523long pWDegree(poly p, const ring r)
524{
525  if (r->firstwv==NULL) return pTotaldegree(p, r);
526  p_LmCheckPolyRing(p, r);
527  int i, k;
528  long j =0;
529
530  for(i=1;i<=r->firstBlockEnds;i++)
531    j+=p_GetExp(p, i, r)*r->firstwv[i-1];
532
533  for (;i<=r->N;i++)
534    j+=p_GetExp(p,i, r)*pWeight(i, r);
535
536  return j;
537}
538
539
540/* ---------------------------------------------------------------------*/
541/* several possibilities for pLDeg: the maximal degree of a monomial in p*/
542/*  compute in l also the pLength of p                                   */
543
544/*2
545* compute the length of a polynomial (in l)
546* and the degree of the monomial with maximal degree: the last one
547*/
548long pLDeg0(poly p,int *l, const ring r)
549{
550  p_CheckPolyRing(p, r);
551  long k= p_GetComp(p, r);
552  int ll=1;
553
554  if (k > 0)
555  {
556    while ((pNext(p)!=NULL) && (p_GetComp(pNext(p), r)==k))
557    {
558      pIter(p);
559      ll++;
560    }
561  }
562  else
563  {
564     while (pNext(p)!=NULL)
565     {
566       pIter(p);
567       ll++;
568     }
569  }
570  *l=ll;
571  return r->pFDeg(p, r);
572}
573
574/*2
575* compute the length of a polynomial (in l)
576* and the degree of the monomial with maximal degree: the last one
577* but search in all components before syzcomp
578*/
579long pLDeg0c(poly p,int *l, const ring r)
580{
581  assume(p!=NULL);
582#ifdef PDEBUG
583  _p_Test(p,r,PDEBUG);
584#endif
585  p_CheckPolyRing(p, r);
586  long o;
587  int ll=1;
588
589  if (! rIsSyzIndexRing(r))
590  {
591    while (pNext(p) != NULL)
592    {
593      pIter(p);
594      ll++;
595    }
596    o = r->pFDeg(p, r);
597  }
598  else
599  {
600    int curr_limit = rGetCurrSyzLimit(r);
601    poly pp = p;
602    while ((p=pNext(p))!=NULL)
603    {
604      if (p_GetComp(p, r)<=curr_limit/*syzComp*/)
605        ll++;
606      else break;
607      pp = p;
608    }
609#ifdef PDEBUG
610    _p_Test(pp,r,PDEBUG);
611#endif
612    o = r->pFDeg(pp, r);
613  }
614  *l=ll;
615  return o;
616}
617
618/*2
619* compute the length of a polynomial (in l)
620* and the degree of the monomial with maximal degree: the first one
621* this works for the polynomial case with degree orderings
622* (both c,dp and dp,c)
623*/
624long pLDegb(poly p,int *l, const ring r)
625{
626  p_CheckPolyRing(p, r);
627  long k= p_GetComp(p, r);
628  long o = r->pFDeg(p, r);
629  int ll=1;
630
631  if (k != 0)
632  {
633    while (((p=pNext(p))!=NULL) && (p_GetComp(p, r)==k))
634    {
635      ll++;
636    }
637  }
638  else
639  {
640    while ((p=pNext(p)) !=NULL)
641    {
642      ll++;
643    }
644  }
645  *l=ll;
646  return o;
647}
648
649/*2
650* compute the length of a polynomial (in l)
651* and the degree of the monomial with maximal degree:
652* this is NOT the last one, we have to look for it
653*/
654long pLDeg1(poly p,int *l, const ring r)
655{
656  p_CheckPolyRing(p, r);
657  long k= p_GetComp(p, r);
658  int ll=1;
659  long  t,max;
660
661  max=r->pFDeg(p, r);
662  if (k > 0)
663  {
664    while (((p=pNext(p))!=NULL) && (p_GetComp(p, r)==k))
665    {
666      t=r->pFDeg(p, r);
667      if (t>max) max=t;
668      ll++;
669    }
670  }
671  else
672  {
673    while ((p=pNext(p))!=NULL)
674    {
675      t=r->pFDeg(p, r);
676      if (t>max) max=t;
677      ll++;
678    }
679  }
680  *l=ll;
681  return max;
682}
683
684/*2
685* compute the length of a polynomial (in l)
686* and the degree of the monomial with maximal degree:
687* this is NOT the last one, we have to look for it
688* in all components
689*/
690long pLDeg1c(poly p,int *l, const ring r)
691{
692  p_CheckPolyRing(p, r);
693  int ll=1;
694  long  t,max;
695
696  max=r->pFDeg(p, r);
697  if (rIsSyzIndexRing(r))
698  {
699    long limit = rGetCurrSyzLimit(r);
700    while ((p=pNext(p))!=NULL)
701    {
702      if (p_GetComp(p, r)<=limit)
703      {
704        if ((t=r->pFDeg(p, r))>max) max=t;
705        ll++;
706      }
707      else break;
708    }
709  }
710  else
711  {
712    while ((p=pNext(p))!=NULL)
713    {
714      if ((t=r->pFDeg(p, r))>max) max=t;
715      ll++;
716    }
717  }
718  *l=ll;
719  return max;
720}
721
722// like pLDeg1, only pFDeg == pDeg
723long pLDeg1_Deg(poly p,int *l, const ring r)
724{
725  assume(r->pFDeg == pDeg);
726  p_CheckPolyRing(p, r);
727  long k= p_GetComp(p, r);
728  int ll=1;
729  long  t,max;
730
731  max=p_GetOrder(p, r);
732  if (k > 0)
733  {
734    while (((p=pNext(p))!=NULL) && (p_GetComp(p, r)==k))
735    {
736      t=p_GetOrder(p, r);
737      if (t>max) max=t;
738      ll++;
739    }
740  }
741  else
742  {
743    while ((p=pNext(p))!=NULL)
744    {
745      t=p_GetOrder(p, r);
746      if (t>max) max=t;
747      ll++;
748    }
749  }
750  *l=ll;
751  return max;
752}
753
754long pLDeg1c_Deg(poly p,int *l, const ring r)
755{
756  assume(r->pFDeg == pDeg);
757  p_CheckPolyRing(p, r);
758  int ll=1;
759  long  t,max;
760
761  max=p_GetOrder(p, r);
762  if (rIsSyzIndexRing(r))
763  {
764    long limit = rGetCurrSyzLimit(r);
765    while ((p=pNext(p))!=NULL)
766    {
767      if (p_GetComp(p, r)<=limit)
768      {
769        if ((t=p_GetOrder(p, r))>max) max=t;
770        ll++;
771      }
772      else break;
773    }
774  }
775  else
776  {
777    while ((p=pNext(p))!=NULL)
778    {
779      if ((t=p_GetOrder(p, r))>max) max=t;
780      ll++;
781    }
782  }
783  *l=ll;
784  return max;
785}
786
787// like pLDeg1, only pFDeg == pTotoalDegree
788long pLDeg1_Totaldegree(poly p,int *l, const ring r)
789{
790  p_CheckPolyRing(p, r);
791  long k= p_GetComp(p, r);
792  int ll=1;
793  long  t,max;
794
795  max=p_ExpVectorQuerSum(p, r);
796  if (k > 0)
797  {
798    while (((p=pNext(p))!=NULL) && (p_GetComp(p, r)==k))
799    {
800      t=p_ExpVectorQuerSum(p, r);
801      if (t>max) max=t;
802      ll++;
803    }
804  }
805  else
806  {
807    while ((p=pNext(p))!=NULL)
808    {
809      t=p_ExpVectorQuerSum(p, r);
810      if (t>max) max=t;
811      ll++;
812    }
813  }
814  *l=ll;
815  return max;
816}
817
818long pLDeg1c_Totaldegree(poly p,int *l, const ring r)
819{
820  p_CheckPolyRing(p, r);
821  int ll=1;
822  long  t,max;
823
824  max=p_ExpVectorQuerSum(p, r);
825  if (rIsSyzIndexRing(r))
826  {
827    long limit = rGetCurrSyzLimit(r);
828    while ((p=pNext(p))!=NULL)
829    {
830      if (p_GetComp(p, r)<=limit)
831      {
832        if ((t=p_ExpVectorQuerSum(p, r))>max) max=t;
833        ll++;
834      }
835      else break;
836    }
837  }
838  else
839  {
840    while ((p=pNext(p))!=NULL)
841    {
842      if ((t=p_ExpVectorQuerSum(p, r))>max) max=t;
843      ll++;
844    }
845  }
846  *l=ll;
847  return max;
848}
849
850// like pLDeg1, only pFDeg == pWFirstTotalDegree
851long pLDeg1_WFirstTotalDegree(poly p,int *l, const ring r)
852{
853  p_CheckPolyRing(p, r);
854  long k= p_GetComp(p, r);
855  int ll=1;
856  long  t,max;
857
858  max=_pWFirstTotalDegree(p, r);
859  if (k > 0)
860  {
861    while (((p=pNext(p))!=NULL) && (p_GetComp(p, r)==k))
862    {
863      t=_pWFirstTotalDegree(p, r);
864      if (t>max) max=t;
865      ll++;
866    }
867  }
868  else
869  {
870    while ((p=pNext(p))!=NULL)
871    {
872      t=_pWFirstTotalDegree(p, r);
873      if (t>max) max=t;
874      ll++;
875    }
876  }
877  *l=ll;
878  return max;
879}
880
881long pLDeg1c_WFirstTotalDegree(poly p,int *l, const ring r)
882{
883  p_CheckPolyRing(p, r);
884  int ll=1;
885  long  t,max;
886
887  max=_pWFirstTotalDegree(p, r);
888  if (rIsSyzIndexRing(r))
889  {
890    long limit = rGetCurrSyzLimit(r);
891    while ((p=pNext(p))!=NULL)
892    {
893      if (p_GetComp(p, r)<=limit)
894      {
895        if ((t=p_ExpVectorQuerSum(p, r))>max) max=t;
896        ll++;
897      }
898      else break;
899    }
900  }
901  else
902  {
903    while ((p=pNext(p))!=NULL)
904    {
905      if ((t=p_ExpVectorQuerSum(p, r))>max) max=t;
906      ll++;
907    }
908  }
909  *l=ll;
910  return max;
911}
912
913/***************************************************************
914 *
915 * Maximal Exponent business
916 *
917 ***************************************************************/
918
919static inline unsigned long
920p_GetMaxExpL2(unsigned long l1, unsigned long l2, const ring r,
921              unsigned long number_of_exp)
922{
923  const unsigned long bitmask = r->bitmask;
924  unsigned long ml1 = l1 & bitmask;
925  unsigned long ml2 = l2 & bitmask;
926  unsigned long max = (ml1 > ml2 ? ml1 : ml2);
927  unsigned long j = number_of_exp - 1;
928
929  if (j > 0)
930  {
931    unsigned long mask = bitmask << r->BitsPerExp;
932    while (1)
933    {
934      ml1 = l1 & mask;
935      ml2 = l2 & mask;
936      max |= ((ml1 > ml2 ? ml1 : ml2) & mask);
937      j--;
938      if (j == 0) break;
939      mask = mask << r->BitsPerExp;
940    }
941  }
942  return max;
943}
944
945static inline unsigned long
946p_GetMaxExpL2(unsigned long l1, unsigned long l2, const ring r)
947{
948  return p_GetMaxExpL2(l1, l2, r, r->ExpPerLong);
949}
950
951poly p_GetMaxExpP(poly p, const ring r)
952{
953  p_CheckPolyRing(p, r);
954  if (p == NULL) return p_Init(r);
955  poly max = p_LmInit(p, r);
956  pIter(p);
957  if (p == NULL) return max;
958  int i, offset;
959  unsigned long l_p, l_max;
960  unsigned long divmask = r->divmask;
961
962  do
963  {
964    offset = r->VarL_Offset[0];
965    l_p = p->exp[offset];
966    l_max = max->exp[offset];
967    // do the divisibility trick to find out whether l has an exponent
968    if (l_p > l_max ||
969        (((l_max & divmask) ^ (l_p & divmask)) != ((l_max-l_p) & divmask)))
970      max->exp[offset] = p_GetMaxExpL2(l_max, l_p, r);
971
972    for (i=1; i<r->VarL_Size; i++)
973    {
974      offset = r->VarL_Offset[i];
975      l_p = p->exp[offset];
976      l_max = max->exp[offset];
977      // do the divisibility trick to find out whether l has an exponent
978      if (l_p > l_max ||
979          (((l_max & divmask) ^ (l_p & divmask)) != ((l_max-l_p) & divmask)))
980        max->exp[offset] = p_GetMaxExpL2(l_max, l_p, r);
981    }
982    pIter(p);
983  }
984  while (p != NULL);
985  return max;
986}
987
988unsigned long p_GetMaxExpL(poly p, const ring r, unsigned long l_max)
989{
990  unsigned long l_p, divmask = r->divmask;
991  int i;
992
993  while (p != NULL)
994  {
995    l_p = p->exp[r->VarL_Offset[0]];
996    if (l_p > l_max ||
997        (((l_max & divmask) ^ (l_p & divmask)) != ((l_max-l_p) & divmask)))
998      l_max = p_GetMaxExpL2(l_max, l_p, r);
999    for (i=1; i<r->VarL_Size; i++)
1000    {
1001      l_p = p->exp[r->VarL_Offset[i]];
1002      // do the divisibility trick to find out whether l has an exponent
1003      if (l_p > l_max ||
1004          (((l_max & divmask) ^ (l_p & divmask)) != ((l_max-l_p) & divmask)))
1005        l_max = p_GetMaxExpL2(l_max, l_p, r);
1006    }
1007    pIter(p);
1008  }
1009  return l_max;
1010}
1011
1012
1013
1014
1015/***************************************************************
1016 *
1017 * Misc things
1018 *
1019 ***************************************************************/
1020// returns TRUE, if all monoms have the same component
1021BOOLEAN p_OneComp(poly p, const ring r)
1022{
1023  if(p!=NULL)
1024  {
1025    long i = p_GetComp(p, r);
1026    while (pNext(p)!=NULL)
1027    {
1028      pIter(p);
1029      if(i != p_GetComp(p, r)) return FALSE;
1030    }
1031  }
1032  return TRUE;
1033}
1034
1035/*2
1036*test if a monomial /head term is a pure power
1037*/
1038int p_IsPurePower(const poly p, const ring r)
1039{
1040  int i,k=0;
1041
1042  for (i=r->N;i;i--)
1043  {
1044    if (p_GetExp(p,i, r)!=0)
1045    {
1046      if(k!=0) return 0;
1047      k=i;
1048    }
1049  }
1050  return k;
1051}
1052
1053/*2
1054*test if a polynomial is univariate
1055* return -1 for constant,
1056* 0 for not univariate,s
1057* i if dep. on var(i)
1058*/
1059int p_IsUnivariate(poly p, const ring r)
1060{
1061  int i,k=-1;
1062
1063  while (p!=NULL)
1064  {
1065    for (i=r->N;i;i--)
1066    {
1067      if (p_GetExp(p,i, r)!=0)
1068      {
1069        if((k!=-1)&&(k!=i)) return 0;
1070        k=i;
1071      }
1072    }
1073    pIter(p);
1074  }
1075  return k;
1076}
1077
1078// set entry e[i] to 1 if var(i) occurs in p, ignore var(j) if e[j]>0
1079int  p_GetVariables(poly p, int * e, const ring r)
1080{
1081  int i;
1082  int n=0;
1083  while(p!=NULL)
1084  {
1085    n=0;
1086    for(i=r->N; i>0; i--)
1087    {
1088      if(e[i]==0)
1089      {
1090        if (p_GetExp(p,i,r)>0)
1091        {
1092          e[i]=1;
1093          n++;
1094        }
1095      }
1096      else
1097        n++;
1098    }
1099    if (n==r->N) break;
1100    pIter(p);
1101  }
1102  return n;
1103}
1104
1105
1106/*2
1107* returns a polynomial representing the integer i
1108*/
1109poly p_ISet(int i, const ring r)
1110{
1111  poly rc = NULL;
1112  if (i!=0)
1113  {
1114    rc = p_Init(r);
1115    pSetCoeff0(rc,n_Init(i,r));
1116    if (r->cf->nIsZero(p_GetCoeff(rc,r)))
1117      p_DeleteLm(&rc,r);
1118  }
1119  return rc;
1120}
1121
1122/*2
1123* an optimized version of p_ISet for the special case 1
1124*/
1125poly p_One(const ring r)
1126{
1127  poly rc = p_Init(r);
1128  pSetCoeff0(rc,n_Init(1,r));
1129  return rc;
1130}
1131
1132/*2
1133* returns a polynomial representing the number n
1134* destroys n
1135*/
1136poly p_NSet(number n, const ring r)
1137{
1138  if (r->cf->nIsZero(n))
1139  {
1140    r->cf->cfDelete(&n, r);
1141    return NULL;
1142  }
1143  else
1144  {
1145    poly rc = p_Init(r);
1146    pSetCoeff0(rc,n);
1147    return rc;
1148  }
1149}
1150
1151/***************************************************************
1152 *
1153 * p_ShallowDelete
1154 *
1155 ***************************************************************/
1156#undef LINKAGE
1157#define LINKAGE
1158#undef p_Delete
1159#define p_Delete p_ShallowDelete
1160#undef n_Delete
1161#define n_Delete(n, r) ((void)0)
1162
1163#include "p_Delete__T.cc"
1164
Note: See TracBrowser for help on using the repository browser.