source: git/libpolys/polys/pDebug.cc @ 9a4aed

spielwiese
Last change on this file since 9a4aed was 9a4aed, checked in by Hans Schoenemann <hannes@…>, 18 months ago
fix: debug stuff p_LmDivisble...
  • Property mode set to 100644
File size: 8.9 KB
Line 
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 "polys/monomials/ring.h"
31#include "polys/monomials/p_polys.h"
32
33#include "coeffs/coeffs.h"
34
35/***************************************************************
36 *
37 * Error reporting
38 *
39 ***************************************************************/
40// avoid recursive calls
41STATIC_VAR BOOLEAN d_poly_error_reporting = FALSE;
42BOOLEAN dPolyReportError(poly p, ring r, const char* fmt, ...)
43{
44  if (d_poly_error_reporting) return FALSE;
45  d_poly_error_reporting = TRUE;
46  va_list ap;
47  va_start(ap, fmt);
48
49  fprintf(stderr, "\n// ***dPolyReportError: ");
50  vfprintf(stderr, fmt, ap);
51  fprintf(stderr, "\n occurred at\n");
52  #ifdef HAVE_OMALLOC
53  omPrintCurrentBackTraceMax(stderr, 8);
54  #endif
55  if (p != NULL)
56  {
57    fprintf(stderr, " occurred for poly: ");
58    p_wrp(p, r);
59    omPrintAddrInfo(stderr, p, " ");
60  }
61  dErrorBreak();
62  d_poly_error_reporting = FALSE;
63  return FALSE;
64}
65
66/***************************************************************
67 *
68 * checking for ring stuff
69 *
70 ***************************************************************/
71BOOLEAN p_LmCheckIsFromRing(poly p, ring r)
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    {
92      _pPolyAssumeReturn(omIsBinPageAddr(p),p,r);
93      _pPolyAssumeReturn(omSizeWOfAddr(p)==omSizeWOfBin(r->PolyBin),p,r);
94      return TRUE;
95    }
96    return FALSE;
97    #endif
98  }
99  return TRUE;
100}
101
102BOOLEAN p_CheckIsFromRing(poly p, ring r)
103{
104  while (p!=NULL)
105  {
106    pFalseReturn(p_LmCheckIsFromRing(p, r));
107    pIter(p);
108  }
109  return TRUE;
110}
111
112BOOLEAN 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
120BOOLEAN p_LmCheckPolyRing(poly p, ring r)
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}
128BOOLEAN p_CheckRing(ring r)
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 ***************************************************************/
141BOOLEAN 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 ***************************************************************/
165BOOLEAN 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}
175BOOLEAN pHaveCommonMonoms(poly p, poly q)
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 ***************************************************************/
193extern void p_Setm_General(poly p, ring r);
194
195static 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
212BOOLEAN _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
229  pFalseReturn(p_CheckRing(r));
230
231  // this checks that p does not contain a loop: rather expensive O(length^2)
232  #ifndef OM_NDEBUG
233  if (level > 1)
234    pFalseReturn(omTestList(p, level) == omError_NoError);
235  #endif
236
237  int ismod = p_GetComp(p, r) != 0;
238
239  while (p != NULL)
240  {
241    // ring check
242    pFalseReturn(p_LmCheckIsFromRing(p, r));
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
323BOOLEAN _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
333BOOLEAN _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#endif // PDEBUG_CC
Note: See TracBrowser for help on using the repository browser.