source: git/libpolys/polys/pDebug.cc

spielwiese
Last change on this file was 738395, checked in by Hans Schoenemann <hannes@…>, 9 days ago
HAVE_RINGS is default (p2)
  • Property mode set to 100644
File size: 8.9 KB
RevLine 
[35aab3]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
[a40080]13
[35aab3]14#include <stdarg.h>
15#include <stdio.h>
16
17
[9f7665]18
19
20
[aadd638]21#include "misc/auxiliary.h"
[f0cdd1c]22#include "misc/distrib.h"
[a40080]23
24
[35aab3]25#ifdef PDEBUG
26
27// do the following to always enforce checking of pSetm
28// #undef PDEBUG
29// #define PDEBUG 2
30
[aadd638]31#include "polys/monomials/ring.h"
32#include "polys/monomials/p_polys.h"
[a40080]33
[aadd638]34#include "coeffs/coeffs.h"
[35aab3]35
36/***************************************************************
37 *
38 * Error reporting
39 *
40 ***************************************************************/
41// avoid recursive calls
[a3f0fea]42STATIC_VAR BOOLEAN d_poly_error_reporting = FALSE;
[35aab3]43BOOLEAN dPolyReportError(poly p, ring r, const char* fmt, ...)
44{
45  if (d_poly_error_reporting) return FALSE;
46  d_poly_error_reporting = TRUE;
47  va_list ap;
48  va_start(ap, fmt);
49
[49d2b5]50  fprintf(stderr, "\n// ***dPolyReportError: ");
[35aab3]51  vfprintf(stderr, fmt, ap);
[80f8f6c]52  fprintf(stderr, "\n occurred at\n");
[9cc63e]53  #ifdef HAVE_OMALLOC
[35aab3]54  omPrintCurrentBackTraceMax(stderr, 8);
[9cc63e]55  #endif
[35aab3]56  if (p != NULL)
57  {
[80f8f6c]58    fprintf(stderr, " occurred for poly: ");
[0ffdec]59    p_wrp(p, r);
60    omPrintAddrInfo(stderr, p, " ");
[35aab3]61  }
[f0cdd1c]62  #ifndef MAKE_DISTRIBUTION
[35aab3]63  dErrorBreak();
[f0cdd1c]64  #endif
[35aab3]65  d_poly_error_reporting = FALSE;
66  return FALSE;
67}
68
69/***************************************************************
70 *
71 * checking for ring stuff
72 *
73 ***************************************************************/
74BOOLEAN p_LmCheckIsFromRing(poly p, ring r)
75{
76  if (p != NULL)
77  {
[d47d1f9]78    #if (OM_TRACK > 0) && defined(OM_TRACK_CUSTOM)
[35aab3]79    void* custom = omGetCustomOfAddr(p);
80    if (custom != NULL)
81    {
82      pPolyAssumeReturnMsg(custom == r ||
83                           // be more sloppy for qrings
84                           (r->qideal != NULL &&
85                            omIsBinPageAddr(p) &&
[aa2b525]86                            omSizeWOfAddr(p)==omSizeWOfBin(r->PolyBin)) ||
[f62996]87                           rSamePolyRep((ring) custom, r),
[367c32]88                           "monomial not from specified ring",p,r);
[35aab3]89      return TRUE;
90    }
91    else
[609e27b]92    #endif
[2e06def]93    #ifndef X_OMALLOC
[35aab3]94    {
[718ba68]95      _pPolyAssumeReturn(omIsBinPageAddr(p),p,r);
96      _pPolyAssumeReturn(omSizeWOfAddr(p)==omSizeWOfBin(r->PolyBin),p,r);
[35aab3]97      return TRUE;
98    }
99    return FALSE;
[2e06def]100    #endif
[35aab3]101  }
102  return TRUE;
103}
104
105BOOLEAN p_CheckIsFromRing(poly p, ring r)
106{
107  while (p!=NULL)
108  {
109    pFalseReturn(p_LmCheckIsFromRing(p, r));
110    pIter(p);
111  }
112  return TRUE;
113}
114
115BOOLEAN p_CheckPolyRing(poly p, ring r)
116{
[2e06def]117  #ifndef X_OMALLOC
[35aab3]118  pAssumeReturn(r != NULL && r->PolyBin != NULL);
[2e06def]119  #endif
[35aab3]120  return p_CheckIsFromRing(p, r);
121}
122
123BOOLEAN p_LmCheckPolyRing(poly p, ring r)
124{
[2e06def]125  #ifndef X_OMALLOC
[35aab3]126  pAssumeReturn(r != NULL && r->PolyBin != NULL);
[2e06def]127  #endif
[35aab3]128  pAssumeReturn(p != NULL);
129  return p_LmCheckIsFromRing(p, r);
130}
131BOOLEAN p_CheckRing(ring r)
132{
[2e06def]133  #ifndef X_OMALLOC
[35aab3]134  pAssumeReturn(r != NULL && r->PolyBin != NULL);
[2e06def]135  #endif
[35aab3]136  return TRUE;
137}
138
139/***************************************************************
140 *
141 * Debugging/statistics of pDivisibleBy
142 *
143 ***************************************************************/
144BOOLEAN p_DebugLmDivisibleByNoComp(poly a, poly b, ring r)
145{
146  int i=r->N;
147
148  do
149  {
150    if (p_GetExp(a,i,r) > p_GetExp(b,i,r))
151      return FALSE;
152    i--;
153  }
154  while (i);
[0ffdec]155  return n_DivBy(pGetCoeff(b), pGetCoeff(a), r->cf);
[738395]156}
[35aab3]157
158
159/***************************************************************
160 *
161 * Misc things helpful for debugging
162 *
163 ***************************************************************/
164BOOLEAN pIsMonomOf(poly p, poly m)
165{
166  if (m == NULL) return TRUE;
167  while (p != NULL)
168  {
169    if (p == m) return TRUE;
170    pIter(p);
171  }
172  return FALSE;
173}
174BOOLEAN pHaveCommonMonoms(poly p, poly q)
175{
176  while (p != NULL)
177  {
178    if (pIsMonomOf(q, p))
179    {
180      return TRUE;
181    }
182    pIter(p);
183  }
184  return FALSE;
185}
186
187/***************************************************************
188 *
189 * Testing of polys
190 *
191 ***************************************************************/
192extern void p_Setm_General(poly p, ring r);
193
194static poly p_DebugInit(poly p, ring src_ring, ring dest_ring)
195{
196  poly d_p = p_Init(dest_ring);
197  int i;
198  assume(dest_ring->N == src_ring->N);
199
200  for (i=1; i<= src_ring->N; i++)
201  {
202    p_SetExp(d_p, i, p_GetExp(p, i, src_ring), dest_ring);
203  }
204  if (rRing_has_Comp(dest_ring))
205    p_SetComp(d_p, p_GetComp(p, src_ring), dest_ring);
206
207  p_Setm_General(d_p, dest_ring);
208  return d_p;
209}
210
211BOOLEAN _p_Test(poly p, ring r, int level)
212{
213  assume(r->cf !=NULL);
214
215  if (PDEBUG > level) level = PDEBUG;
216  if (level < 0 || p == NULL) return TRUE;
217
218  poly p_prev = NULL;
219
[130ecb]220  #ifndef OM_NDEBUG
[2e06def]221  #ifndef X_OMALLOC
[35aab3]222  // check addr with level+1 so as to check bin/page of addr
[aa2b525]223  _pPolyAssumeReturnMsg(omTestBinAddrSize(p, (omSizeWOfBin(r->PolyBin))*SIZEOF_LONG, level+1)
[367c32]224                        == omError_NoError, "memory error",p,r);
[130ecb]225  #endif
[2e06def]226  #endif
[35aab3]227
228  pFalseReturn(p_CheckRing(r));
229
230  // this checks that p does not contain a loop: rather expensive O(length^2)
[130ecb]231  #ifndef OM_NDEBUG
[35aab3]232  if (level > 1)
233    pFalseReturn(omTestList(p, level) == omError_NoError);
[130ecb]234  #endif
[35aab3]235
[9d5199]236  int ismod = p_GetComp(p, r) != 0;
[35aab3]237
238  while (p != NULL)
239  {
240    // ring check
241    pFalseReturn(p_LmCheckIsFromRing(p, r));
[130ecb]242    #ifndef OM_NDEBUG
[2e06def]243    #ifndef X_OMALLOC
[35aab3]244    // omAddr check
[aa2b525]245    _pPolyAssumeReturnMsg(omTestBinAddrSize(p, (omSizeWOfBin(r->PolyBin))*SIZEOF_LONG, 1)
[367c32]246                     == omError_NoError, "memory error",p,r);
[130ecb]247    #endif
[2e06def]248    #endif
[35aab3]249    // number/coef check
[0ffdec]250    _pPolyAssumeReturnMsg(p->coef != NULL || (n_GetChar(r->cf) >= 2), "NULL coef",p,r);
[13298a]251
[bccc3f]252    #ifdef LDEBUG
[13298a]253    _pPolyAssumeReturnMsg(n_Test(p->coef,r->cf),"coeff err",p,r);
[bccc3f]254    #endif
[0ffdec]255    _pPolyAssumeReturnMsg(!n_IsZero(p->coef, r->cf), "Zero coef",p,r);
[35aab3]256
257    // check for valid comp
[0ffdec]258    _pPolyAssumeReturnMsg(p_GetComp(p, r) >= 0 && (p_GetComp(p, r)<65000), "component out of range ?",p,r);
[35aab3]259    // check for mix poly/vec representation
[0ffdec]260    _pPolyAssumeReturnMsg(ismod == (p_GetComp(p, r) != 0), "mixed poly/vector",p,r);
[35aab3]261
262    // special check for ringorder_s/S
[31c447]263    if ((r->typ!=NULL) && (r->typ[0].ord_typ == ro_syzcomp))
[35aab3]264    {
265      long c1, cc1, ccc1, ec1;
[31c447]266      sro_ord* o = &(r->typ[0]);
[35aab3]267
268      c1 = p_GetComp(p, r);
[31c447]269      if (o->data.syzcomp.Components!=NULL)
270      {
271        cc1 = o->data.syzcomp.Components[c1];
272        ccc1 = o->data.syzcomp.ShiftedComponents[cc1];
273      }
274      else { cc1=0; ccc1=0; }
[0ffdec]275      _pPolyAssumeReturnMsg(c1 == 0 || cc1 != 0, "Component <-> TrueComponent zero mismatch",p,r);
276      _pPolyAssumeReturnMsg(c1 == 0 || ccc1 != 0,"Component <-> ShiftedComponent zero mismatch",p,r);
[31c447]277      ec1 = p->exp[o->data.syzcomp.place];
278      //pPolyAssumeReturnMsg(ec1 == ccc1, "Shifted comp out of sync. should %d, is %d");
279      if (ec1 != ccc1)
280      {
281        dPolyReportError(p,r,"Shifted comp out of sync. should %d, is %d",ccc1,ec1);
282        return FALSE;
283      }
[35aab3]284    }
285
286    // check that p_Setm works ok
287    if (level > 0)
288    {
289      poly p_should_equal = p_DebugInit(p, r, r);
[0ffdec]290      _pPolyAssumeReturnMsg(p_ExpVectorEqual(p, p_should_equal, r), "p_Setm field(s) out of sync",p,r);
[35aab3]291      p_LmFree(p_should_equal, r);
292    }
293
294    // check order
295    if (p_prev != NULL)
296    {
297      int cmp = p_LmCmp(p_prev, p, r);
298      if (cmp == 0)
299      {
[0ffdec]300        _pPolyAssumeReturnMsg(0, "monoms p and p->next are equal", p_prev, r);
[35aab3]301      }
302      else
[0ffdec]303        _pPolyAssumeReturnMsg(p_LmCmp(p_prev, p, r) == 1, "wrong order", p_prev, r);
[35aab3]304
305      // check that compare worked sensibly
306      if (level > 1 && p_GetComp(p_prev, r) == p_GetComp(p, r))
307      {
308        int i;
309        for (i=r->N; i>0; i--)
310        {
311          if (p_GetExp(p_prev, i, r) != p_GetExp(p, i, r)) break;
312        }
[0ffdec]313        _pPolyAssumeReturnMsg(i > 0, "Exponents equal but compare different", p_prev, r);
[35aab3]314      }
315    }
316    p_prev = p;
317    pIter(p);
318  }
319  return TRUE;
320}
321
322BOOLEAN _p_LmTest(poly p, ring r, int level)
323{
324  if (level < 0 || p == NULL) return TRUE;
325  poly pnext = pNext(p);
326  pNext(p) = NULL;
327  BOOLEAN test_res = _p_Test(p, r, level);
328  pNext(p) = pnext;
329  return test_res;
330}
331
332BOOLEAN _pp_Test(poly p, ring lmRing, ring tailRing, int level)
333{
334  if (PDEBUG > level) level = PDEBUG;
335  if (level < 0 || p == NULL) return TRUE;
336  if (pNext(p) == NULL || lmRing == tailRing) return _p_Test(p, lmRing, level);
337
338  pFalseReturn(_p_LmTest(p, lmRing, level));
339  pFalseReturn(_p_Test(pNext(p), tailRing, level));
340
341  // check that lm > Lm(tail)
342  if (level > 1)
343  {
344    poly lm = p;
345    poly tail = p_DebugInit(pNext(p), tailRing, lmRing);
346    poly pnext = pNext(lm);
347    pNext(lm) = tail;
348    BOOLEAN cmp = p_LmCmp(lm, tail, lmRing);
349    if (cmp != 1)
350      dPolyReportError(lm, lmRing, "wrong order: lm <= Lm(tail)");
351    p_LmFree(tail, lmRing);
352    pNext(lm) = pnext;
353    return (cmp == 1);
354  }
355  return TRUE;
356}
357
358#endif // PDEBUG
359
360#endif // PDEBUG_CC
Note: See TracBrowser for help on using the repository browser.