My Project  debian-1:4.1.1-p2+ds-4build3
pDebug.cc
Go to the documentation of this file.
1 /****************************************
2 * Computer Algebra System SINGULAR *
3 ****************************************/
4 /***************************************************************
5  * File: pDebug.h
6  * Purpose: implementation of debug related poly routines
7  * Author: obachman (Olaf Bachmann)
8  * Created: 8/00
9  *******************************************************************/
10 
11 #ifndef PDEBUG_CC
12 #define PDEBUG_CC
13 
14 #include <stdarg.h>
15 #include <stdio.h>
16 
17 
18 
19 
20 
21 #include "misc/auxiliary.h"
22 
23 
24 #ifdef PDEBUG
25 
26 // do the following to always enforce checking of pSetm
27 // #undef PDEBUG
28 // #define PDEBUG 2
29 
30 #include "omalloc/omalloc.h"
31 
32 #include "polys/monomials/ring.h"
34 
35 #include "coeffs/coeffs.h"
36 
37 /***************************************************************
38  *
39  * Error reporting
40  *
41  ***************************************************************/
42 // avoid recursive calls
44 BOOLEAN dPolyReportError(poly p, ring r, const char* fmt, ...)
45 {
46  if (d_poly_error_reporting) return FALSE;
48  va_list ap;
49  va_start(ap, fmt);
50 
51  fprintf(stderr, "\n// ***dPolyReportError: ");
52  vfprintf(stderr, fmt, ap);
53  fprintf(stderr, "\n occurred at\n");
54  omPrintCurrentBackTraceMax(stderr, 8);
55  if (p != NULL)
56  {
57  fprintf(stderr, " occurred for poly: ");
58  p_wrp(p, r);
59  omPrintAddrInfo(stderr, p, " ");
60  }
61  dErrorBreak();
63  return FALSE;
64 }
65 
66 /***************************************************************
67  *
68  * checking for ring stuff
69  *
70  ***************************************************************/
72 {
73  if (p != NULL)
74  {
75  #if (OM_TRACK > 0) && defined(OM_TRACK_CUSTOM)
76  void* custom = omGetCustomOfAddr(p);
77  if (custom != NULL)
78  {
79  pPolyAssumeReturnMsg(custom == r ||
80  // be more sloppy for qrings
81  (r->qideal != NULL &&
82  omIsBinPageAddr(p) &&
83  omSizeWOfAddr(p)==omSizeWOfBin(r->PolyBin)) ||
84  rSamePolyRep((ring) custom, r),
85  "monomial not from specified ring",p,r);
86  return TRUE;
87  }
88  else
89  #endif
90  #ifndef X_OMALLOC
91  {
94  return TRUE;
95  }
96  return FALSE;
97  #endif
98  }
99  return TRUE;
100 }
101 
103 {
104  while (p!=NULL)
105  {
107  pIter(p);
108  }
109  return TRUE;
110 }
111 
112 BOOLEAN p_CheckPolyRing(poly p, ring r)
113 {
114  #ifndef X_OMALLOC
115  pAssumeReturn(r != NULL && r->PolyBin != NULL);
116  #endif
117  return p_CheckIsFromRing(p, r);
118 }
119 
121 {
122  #ifndef X_OMALLOC
123  pAssumeReturn(r != NULL && r->PolyBin != NULL);
124  #endif
125  pAssumeReturn(p != NULL);
126  return p_LmCheckIsFromRing(p, r);
127 }
129 {
130  #ifndef X_OMALLOC
131  pAssumeReturn(r != NULL && r->PolyBin != NULL);
132  #endif
133  return TRUE;
134 }
135 
136 /***************************************************************
137  *
138  * Debugging/statistics of pDivisibleBy
139  *
140  ***************************************************************/
141 BOOLEAN p_DebugLmDivisibleByNoComp(poly a, poly b, ring r)
142 {
143  int i=r->N;
144 
145  do
146  {
147  if (p_GetExp(a,i,r) > p_GetExp(b,i,r))
148  return FALSE;
149  i--;
150  }
151  while (i);
152 #ifdef HAVE_RINGS
153  return n_DivBy(pGetCoeff(b), pGetCoeff(a), r->cf);
154 #else
155  return TRUE;
156 #endif
157  }
158 
159 
160 /***************************************************************
161  *
162  * Misc things helpful for debugging
163  *
164  ***************************************************************/
165 BOOLEAN pIsMonomOf(poly p, poly m)
166 {
167  if (m == NULL) return TRUE;
168  while (p != NULL)
169  {
170  if (p == m) return TRUE;
171  pIter(p);
172  }
173  return FALSE;
174 }
176 {
177  while (p != NULL)
178  {
179  if (pIsMonomOf(q, p))
180  {
181  return TRUE;
182  }
183  pIter(p);
184  }
185  return FALSE;
186 }
187 
188 /***************************************************************
189  *
190  * Testing of polys
191  *
192  ***************************************************************/
193 extern void p_Setm_General(poly p, ring r);
194 
195 static poly p_DebugInit(poly p, ring src_ring, ring dest_ring)
196 {
197  poly d_p = p_Init(dest_ring);
198  int i;
199  assume(dest_ring->N == src_ring->N);
200 
201  for (i=1; i<= src_ring->N; i++)
202  {
203  p_SetExp(d_p, i, p_GetExp(p, i, src_ring), dest_ring);
204  }
205  if (rRing_has_Comp(dest_ring))
206  p_SetComp(d_p, p_GetComp(p, src_ring), dest_ring);
207 
208  p_Setm_General(d_p, dest_ring);
209  return d_p;
210 }
211 
212 BOOLEAN _p_Test(poly p, ring r, int level)
213 {
214  assume(r->cf !=NULL);
215 
216  if (PDEBUG > level) level = PDEBUG;
217  if (level < 0 || p == NULL) return TRUE;
218 
219  poly p_prev = NULL;
220 
221  #ifndef OM_NDEBUG
222  #ifndef X_OMALLOC
223  // check addr with level+1 so as to check bin/page of addr
224  _pPolyAssumeReturnMsg(omTestBinAddrSize(p, (omSizeWOfBin(r->PolyBin))*SIZEOF_LONG, level+1)
225  == omError_NoError, "memory error",p,r);
226  #endif
227  #endif
228 
230 
231  // this checks that p does not contain a loop: rather expensive O(length^2)
232  #ifndef OM_NDEBUG
233  if (level > 1)
235  #endif
236 
237  int ismod = p_GetComp(p, r) != 0;
238 
239  while (p != NULL)
240  {
241  // ring check
243  #ifndef OM_NDEBUG
244  #ifndef X_OMALLOC
245  // omAddr check
246  _pPolyAssumeReturnMsg(omTestBinAddrSize(p, (omSizeWOfBin(r->PolyBin))*SIZEOF_LONG, 1)
247  == omError_NoError, "memory error",p,r);
248  #endif
249  #endif
250  // number/coef check
251  _pPolyAssumeReturnMsg(p->coef != NULL || (n_GetChar(r->cf) >= 2), "NULL coef",p,r);
252 
253  #ifdef LDEBUG
254  _pPolyAssumeReturnMsg(n_Test(p->coef,r->cf),"coeff err",p,r);
255  #endif
256  _pPolyAssumeReturnMsg(!n_IsZero(p->coef, r->cf), "Zero coef",p,r);
257 
258  // check for valid comp
259  _pPolyAssumeReturnMsg(p_GetComp(p, r) >= 0 && (p_GetComp(p, r)<65000), "component out of range ?",p,r);
260  // check for mix poly/vec representation
261  _pPolyAssumeReturnMsg(ismod == (p_GetComp(p, r) != 0), "mixed poly/vector",p,r);
262 
263  // special check for ringorder_s/S
264  if ((r->typ!=NULL) && (r->typ[0].ord_typ == ro_syzcomp))
265  {
266  long c1, cc1, ccc1, ec1;
267  sro_ord* o = &(r->typ[0]);
268 
269  c1 = p_GetComp(p, r);
270  if (o->data.syzcomp.Components!=NULL)
271  {
272  cc1 = o->data.syzcomp.Components[c1];
273  ccc1 = o->data.syzcomp.ShiftedComponents[cc1];
274  }
275  else { cc1=0; ccc1=0; }
276  _pPolyAssumeReturnMsg(c1 == 0 || cc1 != 0, "Component <-> TrueComponent zero mismatch",p,r);
277  _pPolyAssumeReturnMsg(c1 == 0 || ccc1 != 0,"Component <-> ShiftedComponent zero mismatch",p,r);
278  ec1 = p->exp[o->data.syzcomp.place];
279  //pPolyAssumeReturnMsg(ec1 == ccc1, "Shifted comp out of sync. should %d, is %d");
280  if (ec1 != ccc1)
281  {
282  dPolyReportError(p,r,"Shifted comp out of sync. should %d, is %d",ccc1,ec1);
283  return FALSE;
284  }
285  }
286 
287  // check that p_Setm works ok
288  if (level > 0)
289  {
290  poly p_should_equal = p_DebugInit(p, r, r);
291  _pPolyAssumeReturnMsg(p_ExpVectorEqual(p, p_should_equal, r), "p_Setm field(s) out of sync",p,r);
292  p_LmFree(p_should_equal, r);
293  }
294 
295  // check order
296  if (p_prev != NULL)
297  {
298  int cmp = p_LmCmp(p_prev, p, r);
299  if (cmp == 0)
300  {
301  _pPolyAssumeReturnMsg(0, "monoms p and p->next are equal", p_prev, r);
302  }
303  else
304  _pPolyAssumeReturnMsg(p_LmCmp(p_prev, p, r) == 1, "wrong order", p_prev, r);
305 
306  // check that compare worked sensibly
307  if (level > 1 && p_GetComp(p_prev, r) == p_GetComp(p, r))
308  {
309  int i;
310  for (i=r->N; i>0; i--)
311  {
312  if (p_GetExp(p_prev, i, r) != p_GetExp(p, i, r)) break;
313  }
314  _pPolyAssumeReturnMsg(i > 0, "Exponents equal but compare different", p_prev, r);
315  }
316  }
317  p_prev = p;
318  pIter(p);
319  }
320  return TRUE;
321 }
322 
323 BOOLEAN _p_LmTest(poly p, ring r, int level)
324 {
325  if (level < 0 || p == NULL) return TRUE;
326  poly pnext = pNext(p);
327  pNext(p) = NULL;
328  BOOLEAN test_res = _p_Test(p, r, level);
329  pNext(p) = pnext;
330  return test_res;
331 }
332 
333 BOOLEAN _pp_Test(poly p, ring lmRing, ring tailRing, int level)
334 {
335  if (PDEBUG > level) level = PDEBUG;
336  if (level < 0 || p == NULL) return TRUE;
337  if (pNext(p) == NULL || lmRing == tailRing) return _p_Test(p, lmRing, level);
338 
339  pFalseReturn(_p_LmTest(p, lmRing, level));
340  pFalseReturn(_p_Test(pNext(p), tailRing, level));
341 
342  // check that lm > Lm(tail)
343  if (level > 1)
344  {
345  poly lm = p;
346  poly tail = p_DebugInit(pNext(p), tailRing, lmRing);
347  poly pnext = pNext(lm);
348  pNext(lm) = tail;
349  BOOLEAN cmp = p_LmCmp(lm, tail, lmRing);
350  if (cmp != 1)
351  dPolyReportError(lm, lmRing, "wrong order: lm <= Lm(tail)");
352  p_LmFree(tail, lmRing);
353  pNext(lm) = pnext;
354  return (cmp == 1);
355  }
356  return TRUE;
357 }
358 
359 #endif // PDEBUG
360 
361 #if defined(PDEBUG) || defined(PDIV_DEBUG)
362 static unsigned long pDivisibleBy_number = 1;
363 static unsigned long pDivisibleBy_FALSE = 1;
364 static unsigned long pDivisibleBy_ShortFalse = 1;
365 
366 BOOLEAN pDebugLmShortDivisibleBy(poly p1, unsigned long sev_1, ring r_1,
367  poly p2, unsigned long not_sev_2, ring r_2)
368 {
369  _pPolyAssume(p_GetShortExpVector(p1, r_1) == sev_1, p1, r_1);
370  _pPolyAssume(p_GetShortExpVector(p2, r_2) == ~ not_sev_2, p2, r_2);
371 
373  BOOLEAN ret;
374  if (r_1 == r_2)
375  ret = p_LmDivisibleBy(p1, p2, r_1);
376  else
377  ret = p_LmDivisibleBy(p1, r_1, p2, r_2);
378 
379  if (! ret) pDivisibleBy_FALSE++;
380  if (sev_1 & not_sev_2)
381  {
383  if (ret)
384  dReportError("p1 divides p2, but sev's are wrong");
385  }
386  return ret;
387 }
388 
389 BOOLEAN pDebugLmShortDivisibleByNoComp(poly p1, unsigned long sev_1, ring r_1,
390  poly p2, unsigned long not_sev_2, ring r_2)
391 {
392 // _pPolyAssume((p_GetComp(p1, r_1) == p_GetComp(p2, r_2)) || (p_GetComp(p1, r_1) == 0));
393  _pPolyAssume(p_GetShortExpVector(p1, r_1) == sev_1, p1, r_1);
394  _pPolyAssume(p_GetShortExpVector(p2, r_2) == ~ not_sev_2, p2, r_2);
395 
397  BOOLEAN ret;
398  if (r_1 == r_2)
399  ret = p_LmDivisibleByNoComp(p1, p2, r_1);
400  else
401  ret = p_LmDivisibleByNoComp(p1, r_1, p2, r_2);
402 
403  if (! ret) pDivisibleBy_FALSE++;
404  if (sev_1 & not_sev_2)
405  {
407  if (ret)
408  dReportError("p1 divides p2, but sev's are wrong");
409  }
410  return ret;
411 }
412 
414 {
415  Print("#Tests: %ld; #FALSE %ld(%ld); #SHORT %ld(%ld)\n",
417  pDivisibleBy_FALSE, (unsigned long) ((double)pDivisibleBy_FALSE*((double) 100)/(double)pDivisibleBy_number),
418  pDivisibleBy_ShortFalse, (unsigned long) ((double)pDivisibleBy_ShortFalse*((double)100)/(double)pDivisibleBy_FALSE));
419 }
420 
421 #endif // PDEBUG
422 
423 #endif // PDEBUG_CC
FALSE
#define FALSE
Definition: auxiliary.h:94
dReportError
int dReportError(const char *fmt,...)
Definition: dError.cc:45
omalloc.h
p_GetComp
#define p_GetComp(p, r)
Definition: monomials.h:71
p_GetExp
static long p_GetExp(const poly p, const unsigned long iBitmask, const int VarOffset)
get a single variable exponent @Note: the integer VarOffset encodes:
Definition: p_polys.h:469
p_DebugInit
static poly p_DebugInit(poly p, ring src_ring, ring dest_ring)
Definition: pDebug.cc:195
pDebugLmShortDivisibleByNoComp
BOOLEAN pDebugLmShortDivisibleByNoComp(poly p1, unsigned long sev_1, ring r_1, poly p2, unsigned long not_sev_2, ring r_2)
Definition: pDebug.cc:389
pHaveCommonMonoms
BOOLEAN pHaveCommonMonoms(poly p, poly q)
Definition: pDebug.cc:175
dErrorBreak
void dErrorBreak()
Definition: dError.cc:141
p_CheckIsFromRing
BOOLEAN p_CheckIsFromRing(poly p, ring r)
Definition: pDebug.cc:102
n_GetChar
static FORCE_INLINE int n_GetChar(const coeffs r)
Return the characteristic of the coeff. domain.
Definition: coeffs.h:445
pDivisibleBy_FALSE
static unsigned long pDivisibleBy_FALSE
Definition: pDebug.cc:363
p_Setm_General
void p_Setm_General(poly p, ring r)
Definition: p_polys.cc:154
p_DebugLmDivisibleByNoComp
BOOLEAN p_DebugLmDivisibleByNoComp(poly a, poly b, ring r)
Definition: pDebug.cc:141
pDivisibleBy_number
static unsigned long pDivisibleBy_number
Definition: pDebug.cc:362
p_LmCheckPolyRing
BOOLEAN p_LmCheckPolyRing(poly p, ring r)
Definition: pDebug.cc:120
level
int level(const CanonicalForm &f)
Definition: canonicalform.h:324
p_wrp
void p_wrp(poly p, ring lmRing, ring tailRing)
Definition: polys0.cc:235
auxiliary.h
n_IsZero
static FORCE_INLINE BOOLEAN n_IsZero(number n, const coeffs r)
TRUE iff 'n' represents the zero element.
Definition: coeffs.h:465
b
CanonicalForm b
Definition: cfModGcd.cc:4044
_pPolyAssume
#define _pPolyAssume(cond, p, r)
Definition: monomials.h:120
ap
Definition: ap.h:40
p_LmDivisibleBy
static BOOLEAN p_LmDivisibleBy(poly a, poly b, const ring r)
Definition: p_polys.h:1815
p_SetExp
static unsigned long p_SetExp(poly p, const unsigned long e, const unsigned long iBitmask, const int VarOffset)
set a single variable exponent @Note: VarOffset encodes the position in p->exp
Definition: p_polys.h:488
_p_LmTest
BOOLEAN _p_LmTest(poly p, ring r, int level)
Definition: pDebug.cc:323
p_LmCheckIsFromRing
BOOLEAN p_LmCheckIsFromRing(poly p, ring r)
Definition: pDebug.cc:71
TRUE
#define TRUE
Definition: auxiliary.h:98
i
int i
Definition: cfEzgcd.cc:125
omIsBinPageAddr
#define omIsBinPageAddr(addr)
Definition: omBinPage.h:68
sro_ord::data
union sro_ord::@0 data
pPolyAssumeReturnMsg
#define pPolyAssumeReturnMsg(cond, msg)
Definition: monomials.h:144
_pPolyAssumeReturnMsg
#define _pPolyAssumeReturnMsg(cond, msg, p, r)
Definition: monomials.h:131
omSizeWOfAddr
size_t omSizeWOfAddr(void *addr)
Definition: omAllocSystem.c:113
BOOLEAN
int BOOLEAN
Definition: auxiliary.h:85
omTestBinAddrSize
omError_t omTestBinAddrSize(void *addr, size_t size, int check_level)
Definition: omDebug.c:44
dPolyReportError
BOOLEAN dPolyReportError(poly p, ring r, const char *fmt,...)
Definition: pDebug.cc:44
d_poly_error_reporting
static BOOLEAN d_poly_error_reporting
Definition: pDebug.cc:43
pFalseReturn
#define pFalseReturn(cond)
Definition: monomials.h:146
pAssumeReturn
#define pAssumeReturn(cond)
Definition: monomials.h:85
pIter
#define pIter(p)
Definition: monomials.h:44
omError_NoError
@ omError_NoError
Definition: omError.h:27
p_polys.h
p_Init
static poly p_Init(const ring r, omBin bin)
Definition: p_polys.h:1266
p_LmCmp
static int p_LmCmp(poly p, poly q, const ring r)
Definition: p_polys.h:1507
pIsMonomOf
BOOLEAN pIsMonomOf(poly p, poly m)
Definition: pDebug.cc:165
p_CheckRing
BOOLEAN p_CheckRing(ring r)
Definition: pDebug.cc:128
p_LmFree
static void p_LmFree(poly p, ring)
Definition: p_polys.h:683
ring.h
sro_ord
Definition: ring.h:226
p_CheckPolyRing
BOOLEAN p_CheckPolyRing(poly p, ring r)
Definition: pDebug.cc:112
omTestList
#define omTestList(ptr, level)
Definition: omList.h:81
pPrintDivisbleByStat
void pPrintDivisbleByStat()
Definition: pDebug.cc:413
rRing_has_Comp
#define rRing_has_Comp(r)
Definition: monomials.h:273
Print
#define Print
Definition: emacs.cc:80
_pPolyAssumeReturn
#define _pPolyAssumeReturn(cond, p, r)
Definition: monomials.h:108
pDivisibleBy_ShortFalse
static unsigned long pDivisibleBy_ShortFalse
Definition: pDebug.cc:364
omSizeWOfBin
#define omSizeWOfBin(bin_ptr)
Definition: omAllocPrivate.h:100
ro_syzcomp
@ ro_syzcomp
Definition: ring.h:66
m
int m
Definition: cfEzgcd.cc:121
n_DivBy
static FORCE_INLINE BOOLEAN n_DivBy(number a, number b, const coeffs r)
test whether 'a' is divisible 'b'; for r encoding a field: TRUE iff 'b' does not represent zero in Z:...
Definition: coeffs.h:784
assume
#define assume(x)
Definition: mod2.h:390
NULL
#define NULL
Definition: omList.c:10
p_SetComp
static unsigned long p_SetComp(poly p, unsigned long c, ring r)
Definition: p_polys.h:247
PDEBUG
#define PDEBUG
Definition: auxiliary.h:184
rSamePolyRep
BOOLEAN rSamePolyRep(ring r1, ring r2)
returns TRUE, if r1 and r2 represents the monomials in the same way FALSE, otherwise this is an analo...
Definition: ring.cc:1668
p
int p
Definition: cfModGcd.cc:4019
p_ExpVectorEqual
static BOOLEAN p_ExpVectorEqual(poly p1, poly p2, const ring r1, const ring r2)
Definition: p_polys.cc:4410
omPrintCurrentBackTraceMax
int omPrintCurrentBackTraceMax(FILE *fd, int max)
Definition: omRet2Info.c:165
pGetCoeff
static number & pGetCoeff(poly p)
return an alias to the leading coefficient of p assumes that p != NULL NOTE: not copy
Definition: monomials.h:51
n_Test
#define n_Test(a, r)
BOOLEAN n_Test(number a, const coeffs r)
Definition: coeffs.h:739
p_GetShortExpVector
unsigned long p_GetShortExpVector(const poly p, const ring r)
Definition: p_polys.cc:4665
p_LmDivisibleByNoComp
static BOOLEAN p_LmDivisibleByNoComp(poly a, poly b, const ring r)
Definition: p_polys.h:1801
_p_Test
BOOLEAN _p_Test(poly p, ring r, int level)
Definition: pDebug.cc:212
pNext
#define pNext(p)
Definition: monomials.h:43
_pp_Test
BOOLEAN _pp_Test(poly p, ring lmRing, ring tailRing, int level)
Definition: pDebug.cc:333
omPrintAddrInfo
void omPrintAddrInfo(FILE *fd, void *addr, const char *s)
Definition: omDebugCheck.c:453
pDebugLmShortDivisibleBy
BOOLEAN pDebugLmShortDivisibleBy(poly p1, unsigned long sev_1, ring r_1, poly p2, unsigned long not_sev_2, ring r_2)
Definition: pDebug.cc:366
coeffs.h