source: git/Singular/polys-impl.cc @ c7cdc1

spielwiese
Last change on this file since c7cdc1 was c7cdc1, checked in by Hans Schönemann <hannes@…>, 24 years ago
*hannes: removed rComp-DEBUG git-svn-id: file:///usr/local/Singular/svn/trunk@4249 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 21.9 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: polys-impl.cc,v 1.41 2000-03-31 13:21:25 Singular Exp $ */
5
6/***************************************************************
7 *
8 * File:       polys-impl.cc
9 * Purpose:    low-level procuders for polys.
10 *
11 * If you touch anything here, you better know what you are doing.
12 * What is here should not be used directly from other routines -- the
13 * encapsulations in polys.[h,cc] should be used, instead.
14 *
15 ***************************************************************/
16#ifndef POLYS_IMPL_CC
17#define POLYS_IMPL_CC
18
19#include <stdio.h>
20#include <string.h>
21#include "mod2.h"
22#include "tok.h"
23#include "structs.h"
24#include "mmprivate.h"
25#include "mmemory.h"
26#include "febase.h"
27#include "numbers.h"
28#include "polys.h"
29#include "ring.h"
30#include "polys-impl.h"
31
32#ifdef HAVE_SHIFTED_EXPONENTS
33#ifdef PDEBUG
34int pDBsyzComp=0;
35#endif
36#endif
37
38/***************************************************************
39 *
40 * Storage Managament Routines
41 *
42 ***************************************************************/
43
44
45/*2
46* delete a poly, resets pointer
47* put the monomials in the freelist
48*/
49#ifdef MDEBUG
50void pDBDelete(poly * p, memHeap heap, char * f, int l)
51{
52  poly h = *p;
53
54  while (h!=NULL)
55  {
56#ifdef LDEBUG
57    nDBDelete(&(h->coef),f,l);
58#else
59    nDelete(&(h->coef));
60#endif
61    pIter(h);
62    pDBFree1((ADDRESS)*p, heap, f,l);
63    *p=h;
64    if (l>0) l= -l;
65  }
66  *p = NULL;
67}
68#else
69void _pDelete(poly* p, memHeap heap)
70{
71  poly h = *p;
72  poly pp;
73
74  while (h!=NULL)
75  {
76    nDelete(&(h->coef));
77    pp=h;
78    pIter(h);
79    _pFree1((ADDRESS)pp, heap);
80  }
81  *p = NULL;
82}
83#endif
84
85/*2
86* remove first monom
87*/
88#ifdef MDEBUG
89void pDBDelete1(poly * p, memHeap heap, char * f, int l)
90{
91  poly h = *p;
92
93  if (h==NULL) return;
94  nDelete(&(h->coef));
95  *p = pNext(h);
96  pDBFree1((ADDRESS)h, heap, f,l);
97}
98#else
99void _pDelete1(poly* p, memHeap heap)
100{
101  poly h = *p;
102
103  if (h==NULL) return;
104  nDelete(&(h->coef));
105  *p = pNext(h);
106  _pFree1((ADDRESS)h, heap);
107}
108#endif
109
110
111void ppDelete(poly* p, ring rg)
112{
113  ring origRing = currRing;
114  rChangeCurrRing(rg, FALSE);
115  pDelete(p);
116  rChangeCurrRing(origRing, FALSE);
117}
118
119/*2
120* creates a copy of p
121*/
122#ifdef MDEBUG
123poly pDBCopy(memHeap d_h, poly s_p, char *f,int l)
124#else
125poly _pCopy(memHeap d_h, poly s_p)
126#endif
127{
128  spolyrec dp;
129  poly d_p = &dp;
130
131  assume(d_h != NULL && (d_h == mm_specHeap) ||
132         d_h->size == mm_specHeap->size);
133  pTest(s_p);
134  while (s_p != NULL)
135  {
136#ifdef MDEBUG
137    d_p->next = (poly) mmDBAllocHeap(d_h, f, l);
138#else
139    mmAllocHeap(d_p->next, d_h);
140#endif
141    d_p = d_p->next;
142    pSetCoeff0(d_p, nCopy(pGetCoeff(s_p)));
143    memcpyW(&(d_p->exp.l[0]), &(s_p->exp.l[0]), currRing->ExpLSize);
144    pIter(s_p);
145  }
146  pNext(d_p) = NULL;
147#if defined(MDEBUG) && defined(PDEBUG)
148  pDBTest(dp.next, d_h, f,l);
149#else
150  pHeapTest(dp.next, d_h);
151#endif
152  return dp.next;
153}
154
155#ifdef MDEBUG
156poly pDBCopy(poly s_p, char *f,int l)
157{
158  return pDBCopy(mm_specHeap, s_p, f, l);
159}
160#else
161poly _pCopy(poly s_p)
162{
163  return _pCopy(mm_specHeap, s_p);
164}
165#endif
166
167
168#ifdef MDEBUG
169poly pDBShallowCopyDelete(memHeap d_h,poly *p,memHeap s_h, char *f,int l)
170#else
171poly _pShallowCopyDelete(memHeap d_h, poly *p, memHeap s_h)
172#endif
173{
174  spolyrec dp;
175  poly d_p = &dp, tmp;
176  poly s_p = *p;
177
178  assume(d_h != NULL && s_h != NULL &&
179         d_h->size == s_h->size);
180
181  if (currRing->ExpLSize <= 2)
182  {
183    if (currRing->ExpLSize == 1)
184    {
185      while (s_p != NULL)
186      {
187#ifdef MDEBUG
188        d_p->next = (poly) mmDBAllocHeap(d_h, f, l);
189#else
190        mmAllocHeap(d_p->next, d_h);
191#endif
192        d_p = d_p->next;
193
194        d_p->coef = s_p->coef;
195        d_p->exp.l[0] = s_p->exp.l[0];
196
197        tmp = pNext(s_p);
198#ifdef MDEBUG
199        mmDBFreeHeap(s_p, s_h, f, l);
200#else
201        mmFreeHeap(s_p, s_h);
202#endif
203        s_p = tmp;
204      }
205    }
206    else
207    {
208      while (s_p != NULL)
209      {
210#ifdef MDEBUG
211        d_p->next = (poly) mmDBAllocHeap(d_h, f, l);
212#else
213        mmAllocHeap(d_p->next, d_h);
214#endif
215        d_p = d_p->next;
216
217        d_p->coef = s_p->coef;
218        d_p->exp.l[0] = s_p->exp.l[0];
219        d_p->exp.l[1] = s_p->exp.l[1];
220
221        tmp = pNext(s_p);
222#ifdef MDEBUG
223        mmDBFreeHeap(s_p, s_h, f, l);
224#else
225        mmFreeHeap(s_p, s_h);
226#endif
227        s_p = tmp;
228      }
229    }
230  }
231  else
232  {
233    if (currRing->ExpLSize & 1)
234    {
235      while (s_p != NULL)
236      {
237
238#ifdef MDEBUG
239        d_p->next = (poly) mmDBAllocHeap(d_h, f, l);
240#else
241        mmAllocHeap(d_p->next, d_h);
242#endif
243        d_p = d_p->next;
244
245        d_p->coef = s_p->coef;
246        memcpy_nwODD(&(d_p->exp.l[0]), &(s_p->exp.l[1]), currRing->ExpLSize);
247
248        tmp = pNext(s_p);
249#ifdef MDEBUG
250        mmDBFreeHeap(s_p, s_h, f, l);
251#else
252        mmFreeHeap(s_p, s_h);
253#endif
254        s_p = tmp;
255      }
256    }
257    else
258    {
259      while (s_p != NULL)
260      {
261
262#ifdef MDEBUG
263        d_p->next = (poly) mmDBAllocHeap(d_h, f, l);
264#else
265        mmAllocHeap(d_p->next, d_h);
266#endif
267        d_p = d_p->next;
268
269        d_p->coef = s_p->coef;
270        memcpy_nwEVEN(&(d_p->exp.l[0]), &(s_p->exp.l[1]), currRing->ExpLSize);
271
272        tmp = pNext(s_p);
273#ifdef MDEBUG
274        mmDBFreeHeap(s_p, s_h, f, l);
275#else
276        mmFreeHeap(s_p, s_h);
277#endif
278        s_p = tmp;
279      }
280    }
281  }
282  pNext(d_p) = NULL;
283  pHeapTest(dp.next, d_h);
284  *p = NULL;
285  return pNext(dp.next);
286}
287
288
289/*2
290* creates a copy of the initial monomial of p
291* sets the coeff of the copy to a defined value
292*/
293#ifdef MDEBUG
294poly pDBCopy1(poly p,char *f,int l)
295#else
296poly _pCopy1(poly p)
297#endif
298{
299  poly w;
300#ifdef MDEBUG
301  w = pDBNew(mm_specHeap, f,l);
302#else
303  w = pNew();
304#endif
305  pCopy2(w,p);
306  nNew(&(w->coef));
307  pNext(w) = NULL;
308  return w;
309}
310
311/*2
312* returns (a copy of) the head term of a
313*/
314#ifdef MDEBUG
315poly pDBHead(memHeap heap, poly p,char *f, int l)
316#else
317poly _pHead(memHeap heap, poly p)
318#endif
319{
320  poly w=NULL;
321
322  if (p!=NULL)
323  {
324    assume(heap != NULL && (heap == mm_specHeap) ||
325           heap->size == mm_specHeap->size);
326
327#ifdef MDEBUG
328    w = (poly) mmDBAllocHeap(heap, f, l);
329#else
330    mmAllocHeap(w, heap);
331#endif
332    memcpyW(&(w->exp.l[0]), &(p->exp.l[0]), currRing->ExpLSize);
333    pSetCoeff0(w,nCopy(pGetCoeff(p)));
334    pNext(w) = NULL;
335  }
336  return w;
337}
338
339#ifdef MDEBUG
340poly pDBShallowCopyDeleteHead(memHeap d_h,poly *s_p,memHeap s_h, char *f,int l)
341#else
342poly _pShallowCopyDeleteHead(memHeap d_h, poly *s_p, memHeap s_h)
343#endif
344{
345  poly w = NULL;
346  poly p = *s_p;
347
348  if (p!=NULL)
349  {
350    assume(d_h != NULL && s_h != NULL &&
351           d_h->size == s_h->size);
352
353#ifdef MDEBUG
354    w = (poly) mmDBAllocHeap(d_h, f, l);
355#else
356    mmAllocHeap(w, d_h);
357#endif
358    memcpyW(&(w->exp.l[0]), &(p->exp.l[0]), currRing->ExpLSize);
359    pSetCoeff0(w,pGetCoeff(p));
360    pNext(w) = NULL;
361
362    *s_p = pNext(p);
363#ifdef MDEBUG
364    mmDBFreeHeap(p, s_h, f, l);
365#else
366    mmFreeHeap(p, s_h);
367#endif
368  }
369  return w;
370}
371
372
373
374poly pHeadProc(poly p)
375{
376  return pHead(p);
377}
378
379/*2
380* returns (a copy of) the head term of a without the coef
381*/
382#ifdef MDEBUG
383poly pDBHead0(poly p,char *f, int l)
384#else
385poly _pHead0(poly p)
386#endif
387{
388  poly w=NULL;
389
390  if (p!=NULL)
391  {
392#if defined(PDEBUG) && defined(MDEBUG)
393    w = pDBNew(mm_specHeap, f,l);
394#else
395    w = pNew();
396#endif
397    pCopy2(w,p);
398    pSetCoeff0(w,NULL);
399    pNext(w) = NULL;
400  }
401  return w;
402}
403
404
405/***************************************************************
406 *
407 * Routines for turned on debugging
408 *
409 ***************************************************************/
410
411#if defined(PDEBUG) && PDEBUG > 1
412Exponent_t pPDSetExp(poly p, int v, Exponent_t e, char* f, int l)
413{
414  if (v == 0)
415  {
416    Warn("zero index to exponent in %s:%d\n", f, l);
417  }
418  if (v > pVariables)
419  {
420    Warn("index %d to exponent too large in %s:%d\n", v, f, l);
421  }
422  return (p)->exp.e[_pExpIndex(v)]=(e);
423}
424
425Exponent_t pPDGetExp(poly p, int v, char* f, int l)
426{
427  if (v == 0)
428  {
429    Warn("zero index to exponent in %s:%d\n", f, l);
430  }
431  if (v > pVariables)
432  {
433    Warn("index %d to exponent too large in %s:%d\n", v, f, l);
434  }
435  return (p)->exp.e[_pExpIndex(v)];
436}
437
438Exponent_t pPDRingSetExp(ring r, poly p, int v, Exponent_t e, char* f, int l)
439{
440  if (v == 0)
441  {
442    Warn("zero index to exponent in %s:%d\n", f, l);
443  }
444  if (v > r->N)
445  {
446    Warn("index %d to exponent too large in %s:%d\n", v, f, l);
447  }
448  return (p)->exp.e[_pRingExpIndex(r, v)]=(e);
449}
450
451Exponent_t pPDRingGetExp(ring r, poly p, int v, char* f, int l)
452{
453  if (v == 0)
454  {
455    Warn("zero index to exponent in %s:%d\n", f, l);
456  }
457  if (v > r->N)
458  {
459    Warn("index %d to exponent too large in %s:%d\n", v, f, l);
460  }
461  return (p)->exp.e[_pRingExpIndex(r,v)];
462}
463
464Exponent_t pPDIncrExp(poly p, int v, char* f, int l)
465{
466  if (v == 0)
467  {
468    Warn("zero index to exponent in %s:%d\n", f, l);
469  }
470  if (v > pVariables)
471  {
472    Warn("index %d to exponent too large in %s:%d\n", v, f, l);
473  }
474  return ((p)->exp.e[_pExpIndex(v)])++;
475}
476
477Exponent_t pPDDecrExp(poly p, int v, char* f, int l)
478{
479  if (v == 0)
480  {
481    Warn("zero index to exponent in %s:%d\n", f, l);
482  }
483  if (v > pVariables)
484  {
485    Warn("index %d to exponent too large in %s:%d\n", v, f, l);
486  }
487  return ((p)->exp.e[_pExpIndex(v)])--;
488}
489
490Exponent_t pPDAddExp(poly p, int v, Exponent_t e, char* f, int l)
491{
492  if (v == 0)
493  {
494    Warn("zero index to exponent in %s:%d\n", f, l);
495  }
496  if (v > pVariables)
497  {
498    Warn("index %d to exponent too large in %s:%d\n", v, f, l);
499  }
500  return ((p)->exp.e[_pExpIndex(v)]) += (e);
501}
502
503Exponent_t pPDSubExp(poly p, int v, Exponent_t e, char* f, int l)
504{
505  if (v == 0)
506  {
507    Warn("zero index to exponent in %s:%d\n", f, l);
508  }
509  if (v > pVariables)
510  {
511    Warn("index %d to exponent too large in %s:%d\n", v, f, l);
512  }
513  return ((p)->exp.e[_pExpIndex(v)]) -= (e);
514}
515
516Exponent_t pPDMultExp(poly p, int v, Exponent_t e, char* f, int l)
517{
518  if (v == 0)
519  {
520    Warn("zero index to exponent in %s:%d\n", f, l);
521  }
522  if (v > pVariables)
523  {
524    Warn("index %d to exponent too large in %s:%d\n", v, f, l);
525  }
526  return ((p)->exp.e[_pExpIndex(v)]) *= (e);
527}
528
529// routines on components
530Exponent_t pDBSetComp(poly p, Exponent_t k, int le, char* f, int l)
531{
532  if (k < 0)
533  {
534    Warn("set negative component %d in %s:%d", k, f, l);
535  }
536  if (currRing->order[1] == ringorder_S)
537  {
538    if (le <= 0) le = currRing->typ[1].data.syzcomp.length;
539    if (k > l)
540    {
541      Warn("component %d larger then max %d in %s:%d",
542            k, le, f, l);
543    }
544  }
545  return _pGetComp(p) = (k);
546}
547
548Exponent_t pDBDecrComp(poly p, char* f, int l)
549{
550  if (_pGetComp(p) < 1)
551  {
552    Warn("decrement to negative component %d in %s:%d\n", _pGetComp(p), f, l);
553  }
554  return _pGetComp(p)--;
555}
556
557Exponent_t pDBAddComp(poly p, Exponent_t k, int le, char* f, int l)
558{
559  if (_pGetComp(p) + k < 0)
560  {
561    Warn("add to negative component %d + %d = %d in %s:%d\n", _pGetComp(p),
562          k, _pGetComp(p) + k, f, l);
563  }
564  _pGetComp(p) += (k);
565
566  if (currRing->order[1] == ringorder_S)
567  {
568    if (le <= 0) le = currRing->typ[1].data.syzcomp.length;
569    if (_pGetComp(p) > le)
570    {
571      Warn("sum of components %d larger then max %d in %s:%d\n",
572            _pGetComp(p), le, f, l);
573      assume(0);
574    }
575  }
576  return _pGetComp(p);
577}
578
579Exponent_t pDBSubComp(poly p, Exponent_t k, char* f, int l)
580{
581  if (_pGetComp(p) - k < 0)
582  {
583    Warn("sub to negative component %d - %d = %d in %s:%d\n", _pGetComp(p),
584          k, _pGetComp(p) - k, f, l);
585  }
586  return _pGetComp(p) -= (k);
587}
588
589Exponent_t pDBRingSetComp(ring r, poly p, Exponent_t k, char* f, int l)
590{
591  if (k < 0)
592  {
593    Warn("set negative component %d in %s:%d\n", k, f, l);
594  }
595  return _pRingGetComp(r, p) = (k);
596}
597
598// checks whether fast monom add did not overflow
599void pDBMonAddOn(poly p1, poly p2, char* f, int l)
600{
601  poly ptemp = pHead0(p1);
602
603  if (pGetComp(p1) != 0 && pGetComp(p2) != 0)
604  {
605    Warn("Error in pMonAddOn: both components %d:%d !=0 in %s:%d",
606         pGetComp(p1), pGetComp(p2), f, l);
607  }
608
609  __pMonAddOn(p1, p2);
610
611  for (int i=1; i<=pVariables; i++)
612  {
613    pAddExp(ptemp, i, pGetExp(p2, i));
614  }
615  pAddComp(ptemp, pGetComp(p2));
616  pSetm(ptemp);
617
618  if (! pEqual(ptemp, p1))
619  {
620    Warn("Error in pMonAddOn in %s:%d\n", f, l);
621  }
622
623  pFree1(ptemp);
624}
625
626void pDBMonSubFrom(poly p1, poly p2, char* f, int l)
627{
628  poly ptemp = pNew();
629  pCopy2(ptemp, p1);
630
631  if ((pGetComp(p1) != pGetComp(p2)) && (pGetComp(p2)!=0))
632  {
633    Warn("Error in pMonSubFrom: components are different %d:%d in %s:%d",
634         pGetComp(p1), pGetComp(p2), f, l);
635  }
636
637  __pMonSubFrom(p1, p2);
638
639  for (int i=1; i<=pVariables; i++)
640  {
641    if (pGetExp(ptemp, i) < pGetExp(p2, i))
642    {
643      Warn("Error in pMonSubFrom: %dth exponent %d of p1 smaller than %d of p2", i, pGetExp(ptemp, i), pGetExp(p2, i));
644    }
645    pSubExp(ptemp, i, pGetExp(p2, i));
646  }
647  pSetComp(ptemp, pGetComp(ptemp)-pGetComp(p2));
648  pSetm(ptemp);
649
650  if (! pEqual(ptemp, p1))
651  {
652    Warn("Error in pMonSubFrom in %s:%d", f, l);
653  }
654
655  pFree1(ptemp);
656}
657
658void pDBMonAdd(poly p1, poly p2, poly p3, ring r, char* f, int l)
659{
660  if (r == currRing)
661  {
662    if (pGetComp(p3) != 0 && pGetComp(p2) != 0)
663    {
664      Warn("Error in pMonAdd: both components %d:%d !=0 in %s:%d",
665           pGetComp(p3), pGetComp(p2), f, l);
666    }
667    if (p2 == p1 || p3 == p1)
668    {
669      Warn("Error in pMonAdd: Destination equals source in %s:%d", f, l);
670    }
671  }
672  __pMonAdd(p1, p2, p3, r);
673
674  if (r == currRing)
675  {
676    poly ptemp = pInit();
677    for (int i=1; i<=pVariables; i++)
678    {
679      pSetExp(ptemp, i, pGetExp(p2, i) + pGetExp(p3, i));
680      if (pGetExp(ptemp, i) != pGetExp(p1, i))
681      {
682        Warn("Error in pMonAdd: %th exponent: %d != (%d == %d + %d)",
683             i, pGetExp(p1, i), pGetExp(ptemp, i), pGetExp(p2, i),
684             pGetExp(p3, i));
685      }
686    }
687    pSetComp(ptemp, pGetComp(p2) + pGetComp(p3));
688    pSetm(ptemp);
689
690    if (! pEqual(ptemp, p1))
691      Warn("Error in pMonAdd in %s:%d", f, l);
692    pFree1(ptemp);
693  }
694}
695
696static BOOLEAN OldpDivisibleBy(poly a, poly b)
697{
698  if ((a!=NULL)&&((pGetComp(a)==0) || (pGetComp(a) == pGetComp(b))))
699  {
700    for (int i = 1; i<=pVariables; i++)
701      if (pGetExp(a, i) > pGetExp(b,i)) return FALSE;
702    return TRUE;
703  }
704  return FALSE;
705}
706
707
708BOOLEAN pDBDivisibleBy(poly a, poly b, char* f, int l)
709{
710  BOOLEAN istrue = OldpDivisibleBy(a,b);
711  BOOLEAN f_istrue = _pDivisibleBy_orig(a, b);
712
713  if (istrue != f_istrue)
714  {
715    Warn("Error in pDivisibleBy in %s:%d\n", f, l);
716    _pDivisibleBy_orig(a, b);
717  }
718  return istrue;
719}
720
721BOOLEAN pDBDivisibleBy1(poly a, poly b, char* f, int l)
722{
723  BOOLEAN istrue = OldpDivisibleBy(a,b);
724  BOOLEAN f_istrue = _pDivisibleBy1_orig(a, b);
725
726  if (istrue != f_istrue)
727  {
728    Warn("Error in pDivisibleBy1 in %s:%d\n", f, l);
729    _pDivisibleBy1_orig(a, b);
730  }
731  return istrue;
732}
733
734BOOLEAN pDBDivisibleBy2(poly a, poly b, char* f, int l)
735{
736  BOOLEAN istrue = OldpDivisibleBy(a,b);
737  BOOLEAN f_istrue = __pDivisibleBy(a, b);
738
739  if (istrue != f_istrue)
740  {
741    Warn("Error in pDivisibleBy2 in %s:%d\n", f, l);
742    __pDivisibleBy(a, b);
743  }
744  return f_istrue;
745}
746
747#endif //  defined(PDEBUG) && PDEBUG > 1
748
749#ifdef PDEBUG
750BOOLEAN mmDBEqual(poly p, poly q, char *f, int l)
751{
752  int i;
753
754  for (i = 1; i<=pVariables; i++)
755  {
756    if (pGetExp(p,i) != pGetExp(q, i)) return FALSE;
757  }
758  if (pGetComp(p) != pGetComp(q)) return FALSE;
759  if (__pEqual(p, q) != TRUE)
760  {
761    Warn("Error in pEqual: exp/comp same, bug monoms different in %s:%d",
762         f, l);
763  }
764  return TRUE;
765}
766
767BOOLEAN prDBTest(poly p, ring r, char* f, int l)
768{
769  ring old_ring = NULL;
770  BOOLEAN res;
771
772  if (r != currRing)
773  {
774    old_ring = currRing;
775    rChangeCurrRing(r);
776  }
777  res = pDBTest(p, currRing->mm_specHeap, f, l);
778  if (old_ring != NULL)
779  {
780    rChangeCurrRing(old_ring);
781  }
782  return res;
783}
784
785
786BOOLEAN pDBTest(poly p, char *f, int l)
787{
788  return pDBTest(p, mm_specHeap, f,l);
789}
790
791BOOLEAN pDBTest(poly p, memHeap heap, char *f, int l)
792{
793  poly old=NULL;
794  BOOLEAN ismod=FALSE;
795  if (heap == NULL) heap = mm_specHeap;
796
797  while (p!=NULL)
798  {
799    if (heap != MM_UNKNOWN_HEAP)
800    {
801#ifdef MDEBUG
802    if (!mmDBTestHeapBlock(p, heap, f,l))
803      return FALSE;
804#elif defined(HEAP_DEBUG)
805      if (! mmDebugCheckHeapAddr(p, heap, MM_HEAP_ADDR_USED_FLAG, f, l))
806        return FALSE;
807#endif
808    }
809
810#ifdef LDEBUG
811    if (!nDBTest(p->coef,f,l))
812      return FALSE;
813#endif
814    if ((p->coef==NULL)&&(nGetChar()<2))
815    {
816      Warn("NULL coef in poly in %s:%d\n",f,l);
817      return FALSE;
818    }
819    if (nIsZero(p->coef))
820    {
821      Warn("zero coef in poly in %s:%d\n",f,l);
822      return FALSE;
823    }
824    int i=pVariables;
825#ifndef HAVE_SHIFTED_EXPONENTS
826    // can not hapen for SHIFTED_EXPONENTS
827    for(;i;i--)
828    {
829      if (pGetExp(p,i)<0)
830      {
831        Warn("neg. Exponent %d of x(%d) in %s:%d\n",pGetExp(p,i),i,f,l);
832        return FALSE;
833      }
834    }
835#endif
836    if (pGetComp(p)<0)
837    {
838      Warn("neg Component in %s:%d\n",f,l);
839      return FALSE;
840    }
841    if (ismod==0)
842    {
843      if (pGetComp(p)>0) ismod=1;
844      else               ismod=2;
845    }
846    else if (ismod==1)
847    {
848      if (pGetComp(p)==0)
849      {
850        Warn("mix vec./poly in %s:%d\n",f,l);
851        return FALSE;
852      }
853    }
854    else if (ismod==2)
855    {
856      if (pGetComp(p)!=0)
857      {
858        Warn("mix poly/vec. in %s:%d\n",f,l);
859        return FALSE;
860      }
861    }
862    if (currRing->order[1] == ringorder_S)
863    {
864      long c1, cc1, ccc1, ec1;
865      sro_ord* o = &(currRing->typ[1]);
866
867      c1 = pGetComp(p);
868      cc1 = o->data.syzcomp.Components[c1];
869      ccc1 = o->data.syzcomp.ShiftedComponents[cc1];
870      if (! (c1 == 0 || cc1 != 0))
871      {
872        Warn("Component <-> TrueComponent zero mismatch\n", f, l);
873        return FALSE;
874      }
875      if (! (c1 == 0 || ccc1 != 0))
876      {
877        Warn("Component <-> ShiftedComponent zero mismatch\n", f, l);
878        return FALSE;
879      }
880      ec1 = p->exp.l[currRing->typ[1].data.syzcomp.place];
881      if (ec1 != ccc1)
882      {
883        Warn("Shifted comp out of sync. should %d, is %d", ccc1, ec1);
884        return FALSE;
885      }
886    }
887    if (currRing->order[0] == ringorder_s)
888    {
889      unsigned long syzindex = p->exp.l[currRing->typ[0].data.syz.place];
890      pSetm(p);
891      if (p->exp.l[currRing->typ[0].data.syz.place] != syzindex)
892      {
893        Warn("Syzindex wrong: Was %dl but should be %d in %s:%d\n",
894             syzindex, p->exp.l[currRing->typ[0].data.syz.place], f, l);
895      }
896    }
897    old=p;
898    pIter(p);
899    if (pComp(old,p)!=1)
900    {
901      Warn("wrong order (");
902      wrp(old);
903      Print(") in %s:%d (pComp=%d)\n",f,l,pComp(old,p));
904      return FALSE;
905    }
906    if (p != NULL)
907    {
908      if (pGetComp(old) == pGetComp(p))
909      {
910        i = pVariables;
911        for (i=pVariables;i; i--)
912        {
913          if (pGetExp(old, i) != pGetExp(p, i)) break;
914        }
915        if (i == 0)
916        {
917          Warn("different Compare, but same exponent vector for");
918          wrp(old);
919          Print(" in %s%d\n", f, l);
920          return FALSE;
921        }
922      }
923    }
924  }
925  return TRUE;
926}
927
928#endif // PDEBUG
929
930static unsigned long GetBitFields(Exponent_t e,
931                                  unsigned int s, unsigned int n)
932{
933  unsigned int i = 0, ev = 0;
934  assume(n > 0 && s < BIT_SIZEOF_LONG);
935  do
936  {
937    assume(s+i < BIT_SIZEOF_LONG);
938    if (e > (Exponent_t) i) ev |= Sy_bit(s+i);
939    else break;
940    i++;
941  }
942  while (i < n);
943  return ev;
944}
945
946// Short Exponent Vectors are used for fast divisibility tests
947// ShortExpVectors "squeeze" an exponent vector into one word as follows:
948// Let n = BIT_SIZEOF_LONG / pVariables.
949// If n == 0 (i.e. pVariables > BIT_SIZE_OF_LONG), let m == the number
950// of non-zero exponents. If (m>BIT_SIZEOF_LONG), then sev = ~0, else
951// first m bits of sev are set to 1.
952// Otherwise (i.e. pVariables <= BIT_SIZE_OF_LONG)
953// represented by a bit-field of length n (resp. n+1 for some
954// exponents). If the value of an exponent is greater or equal to n, then
955// all of its respective n bits are set to 1. If the value of an exponent
956// is smaller than n, say m, then only the first m bits of the respective
957// n bits are set to 1, the others are set to 0.
958// This way, we have:
959// exp1 / exp2 ==> (ev1 & ~ev2) == 0, i.e.,
960// if (ev1 & ~ev2) then exp1 does not divide exp2
961unsigned long pGetShortExpVector(poly p)
962{
963  assume(p != NULL);
964  if (p == NULL) return 0;
965  unsigned long ev = 0; // short exponent vector
966  unsigned int n = BIT_SIZEOF_LONG / pVariables; // number of bits per exp
967  unsigned int m1; // highest bit which is filled with (n+1)
968  unsigned int i = 0, j=1;
969
970  if (n == 0)
971  {
972    for (; j<=(unsigned long) pVariables; j++)
973    {
974      if (pGetExp(p,j) > 0) i++;
975      if (i == BIT_SIZEOF_LONG) break;
976    }
977    ev = (unsigned long) ~0 >> ((unsigned long) (BIT_SIZEOF_LONG - i));
978    return ev;
979  }
980  else
981  {
982    m1 = (n+1)*(BIT_SIZEOF_LONG - n*pVariables);
983  }
984
985  n++;
986  while (i<m1)
987  {
988    ev |= GetBitFields(pGetExp(p, j), i, n);
989    i += n;
990    j++;
991  }
992
993  n--;
994  while (i<BIT_SIZEOF_LONG)
995  {
996    ev |= GetBitFields(pGetExp(p, j), i, n);
997    i += n;
998    j++;
999  }
1000  return ev;
1001}
1002
1003#ifdef PDIV_DEBUG
1004static int pDivisibleBy_number = 1;
1005static int pDivisibleBy_FALSE = 1;
1006static int pDivisibleBy_ShortFalse = 1;
1007static int pDivisibleBy_Null = 1;
1008BOOLEAN pDBShortDivisibleBy(poly p1, unsigned long sev_1,
1009                            poly p2, unsigned long not_sev_2,
1010                            char* f, int l)
1011{
1012  if (sev_1 != 0 && pGetShortExpVector(p1) != sev_1)
1013  {
1014    Warn("sev1 is %o but should be %o in %s:%d\n", sev_1,
1015          pGetShortExpVector(p1), f, l);
1016    assume(0);
1017  }
1018  if (~ pGetShortExpVector(p2) != not_sev_2)
1019  {
1020    Warn("not_sev2 is %o but should be %o in %s:%d\n", not_sev_2,
1021          ~ pGetShortExpVector(p2), f, l);
1022    assume(0);
1023  }
1024  if (sev_1 == 0) pDivisibleBy_Null++;
1025  pDivisibleBy_number++;
1026  BOOLEAN ret = pDivisibleBy(p1, p2);
1027  if (! ret) pDivisibleBy_FALSE++;
1028  if (sev_1 & not_sev_2)
1029  {
1030    pDivisibleBy_ShortFalse++;
1031    if (ret)
1032    {
1033      Warn("p1 divides p2, but sev's are wrong in %s:%d\n", f, l);
1034      assume(0);
1035    }
1036  }
1037  return ret;
1038}
1039
1040void pPrintDivisbleByStat()
1041{
1042  Print("#Tests: %d; #FALSE %d(%d); #SHORT %d(%d) #NULL:%d(%d)\n",
1043        pDivisibleBy_number,
1044        pDivisibleBy_FALSE, pDivisibleBy_FALSE*100/pDivisibleBy_number,
1045        pDivisibleBy_ShortFalse, pDivisibleBy_ShortFalse*100/pDivisibleBy_FALSE,
1046        pDivisibleBy_Null, pDivisibleBy_Null*100/pDivisibleBy_number);
1047
1048}
1049#endif
1050
1051#endif // POLYS_IMPL_CC
Note: See TracBrowser for help on using the repository browser.