source: git/kernel/pDebug.cc @ 68349d

spielwiese
Last change on this file since 68349d was 35aab3, checked in by Hans Schönemann <hannes@…>, 21 years ago
This commit was generated by cvs2svn to compensate for changes in r6879, which included commits to RCS files with non-trunk default branches. git-svn-id: file:///usr/local/Singular/svn/trunk@6880 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 9.3 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 *  Version: $Id: pDebug.cc,v 1.1.1.1 2003-10-06 12:15:57 Singular Exp $
10 *******************************************************************/
11
12#ifndef PDEBUG_CC
13#define PDEBUG_CC
14#include <stdarg.h>
15#include <stdio.h>
16
17
18#include "mod2.h"
19#ifdef PDEBUG
20
21// do the following to always enforce checking of pSetm
22// #undef PDEBUG
23// #define PDEBUG 2
24
25#include "p_polys.h"
26#include "febase.h"
27#include "omalloc.h"
28#include "ring.h"
29#include "numbers.h"
30
31/***************************************************************
32 *
33 * Error reporting
34 *
35 ***************************************************************/
36// avoid recursive calls
37static BOOLEAN d_poly_error_reporting = FALSE;
38BOOLEAN dPolyReportError(poly p, ring r, const char* fmt, ...)
39{
40  if (d_poly_error_reporting) return FALSE;
41  d_poly_error_reporting = TRUE;
42  va_list ap;
43  va_start(ap, fmt);
44
45  fprintf(stderr, "\n// ***dPolyError: ");
46  vfprintf(stderr, fmt, ap);
47  fprintf(stderr, "\n occured at\n");
48  omPrintCurrentBackTraceMax(stderr, 8);
49  if (p != NULL)
50  {
51    if (r == currRing)
52    {
53      fprintf(stderr, " occured for poly: ");
54      p_wrp(p, r);
55      omPrintAddrInfo(stderr, p, " ");
56    }
57    else
58    {
59      omPrintAddrInfo(stderr, p, " occured for poly: ");
60    }
61  }
62  dErrorBreak();
63  d_poly_error_reporting = FALSE;
64  return FALSE;
65}
66
67/***************************************************************
68 *
69 * checking for ring stuff
70 *
71 ***************************************************************/
72BOOLEAN p_LmCheckIsFromRing(poly p, ring r)
73{
74  if (p != NULL)
75  {
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)==r->PolyBin->sizeW) ||
84                           rEqual((ring) custom, r, FALSE),
85                           "monomial not from specified ring");
86      return TRUE;
87    }
88    else
89    {
90      pPolyAssumeReturn(omIsBinPageAddr(p) && omSizeWOfAddr(p)==r->PolyBin->sizeW);
91      return TRUE;
92    }
93    return FALSE;
94  }
95  return TRUE;
96}
97
98BOOLEAN p_CheckIsFromRing(poly p, ring r)
99{
100  while (p!=NULL)
101  {
102    pFalseReturn(p_LmCheckIsFromRing(p, r));
103    pIter(p);
104  }
105  return TRUE;
106}
107
108BOOLEAN p_CheckPolyRing(poly p, ring r)
109{
110  pAssumeReturn(r != NULL && r->PolyBin != NULL);
111  return p_CheckIsFromRing(p, r);
112}
113
114BOOLEAN p_LmCheckPolyRing(poly p, ring r)
115{
116  pAssumeReturn(r != NULL && r->PolyBin != NULL);
117  pAssumeReturn(p != NULL);
118  return p_LmCheckIsFromRing(p, r);
119}
120BOOLEAN p_CheckRing(ring r)
121{
122  pAssumeReturn(r != NULL && r->PolyBin != NULL);
123  return TRUE;
124}
125
126/***************************************************************
127 *
128 * Debugging/statistics of pDivisibleBy
129 *
130 ***************************************************************/
131BOOLEAN p_DebugLmDivisibleByNoComp(poly a, poly b, ring r)
132{
133  int i=r->N;
134
135  do
136  {
137    if (p_GetExp(a,i,r) > p_GetExp(b,i,r))
138      return FALSE;
139    i--;
140  }
141  while (i);
142  return TRUE;
143}
144
145
146/***************************************************************
147 *
148 * Misc things helpful for debugging
149 *
150 ***************************************************************/
151BOOLEAN pIsMonomOf(poly p, poly m)
152{
153  if (m == NULL) return TRUE;
154  while (p != NULL)
155  {
156    if (p == m) return TRUE;
157    pIter(p);
158  }
159  return FALSE;
160}
161BOOLEAN pHaveCommonMonoms(poly p, poly q)
162{
163  while (p != NULL)
164  {
165    if (pIsMonomOf(q, p))
166    {
167      return TRUE;
168    }
169    pIter(p);
170  }
171  return FALSE;
172}
173
174/***************************************************************
175 *
176 * Testing of polys
177 *
178 ***************************************************************/
179extern void p_Setm_General(poly p, ring r);
180
181static poly p_DebugInit(poly p, ring src_ring, ring dest_ring)
182{
183  poly d_p = p_Init(dest_ring);
184  int i;
185  assume(dest_ring->N == src_ring->N);
186
187  for (i=1; i<= src_ring->N; i++)
188  {
189    p_SetExp(d_p, i, p_GetExp(p, i, src_ring), dest_ring);
190  }
191  if (rRing_has_Comp(dest_ring))
192    p_SetComp(d_p, p_GetComp(p, src_ring), dest_ring);
193
194  p_Setm_General(d_p, dest_ring);
195  return d_p;
196}
197
198BOOLEAN _p_Test(poly p, ring r, int level)
199{
200  assume(r->cf !=NULL);
201
202  if (PDEBUG > level) level = PDEBUG;
203  if (level < 0 || p == NULL) return TRUE;
204
205  poly p_prev = NULL;
206
207  // check addr with level+1 so as to check bin/page of addr
208  pPolyAssumeReturnMsg(omTestBinAddrSize(p, (r->PolyBin->sizeW)*SIZEOF_LONG, level+1)
209                        == omError_NoError, "memory error");
210
211  pFalseReturn(p_CheckRing(r));
212
213  // this checks that p does not contain a loop: rather expensive O(length^2)
214  if (level > 1)
215    pFalseReturn(omTestList(p, level) == omError_NoError);
216
217  int ismod = p_GetComp(p, r) > 0;
218
219  while (p != NULL)
220  {
221    // ring check
222    pFalseReturn(p_LmCheckIsFromRing(p, r));
223    // omAddr check
224    pPolyAssumeReturnMsg(omTestBinAddrSize(p, (r->PolyBin->sizeW)*SIZEOF_LONG, 1)
225                     == omError_NoError, "memory error");
226    // number/coef check
227    pPolyAssumeReturnMsg(p->coef != NULL || (n_GetChar(r) >= 2), "NULL coef");
228    pPolyAssumeReturnMsg(!n_IsZero(p->coef, r), "Zero coef");
229
230    // check for valid comp
231    pPolyAssumeReturnMsg(p_GetComp(p, r) >= 0 && (p_GetComp(p, r)<65000), "component out of range ?");
232    // check for mix poly/vec representation
233    pPolyAssumeReturnMsg(ismod == (p_GetComp(p, r) > 0), "mixed poly/vector");
234
235    // special check for ringorder_s/S
236    if (r->order[1] == ringorder_S)
237    {
238      long c1, cc1, ccc1, ec1;
239      sro_ord* o = &(r->typ[1]);
240
241      c1 = p_GetComp(p, r);
242      cc1 = o->data.syzcomp.Components[c1];
243      ccc1 = o->data.syzcomp.ShiftedComponents[cc1];
244      pPolyAssumeReturnMsg(c1 == 0 || cc1 != 0, "Component <-> TrueComponent zero mismatch");
245      pPolyAssumeReturnMsg(c1 == 0 || ccc1 != 0,"Component <-> ShiftedComponent zero mismatch");
246      ec1 = p->exp[r->typ[1].data.syzcomp.place];
247      pPolyAssumeReturnMsg(ec1 == ccc1, "Shifted comp out of sync. should %d, is %d");
248    }
249
250    // check that p_Setm works ok
251    if (level > 0)
252    {
253      poly p_should_equal = p_DebugInit(p, r, r);
254      pPolyAssumeReturnMsg(p_ExpVectorEqual(p, p_should_equal, r), "p_Setm field(s) out of sync");
255      p_LmFree(p_should_equal, r);
256    }
257
258    // check order
259    if (p_prev != NULL)
260    {
261      int cmp = p_LmCmp(p_prev, p, r);
262      if (cmp == 0)
263      {
264        _pPolyAssumeReturnMsg(0, "monoms p and p->next are equal", p_prev, r);
265      }
266      else
267        _pPolyAssumeReturnMsg(p_LmCmp(p_prev, p, r) == 1, "wrong order", p_prev, r);
268
269      // check that compare worked sensibly
270      if (level > 1 && p_GetComp(p_prev, r) == p_GetComp(p, r))
271      {
272        int i;
273        for (i=r->N; i>0; i--)
274        {
275          if (p_GetExp(p_prev, i, r) != p_GetExp(p, i, r)) break;
276        }
277        _pPolyAssumeReturnMsg(i > 0, "Exponents equal but compare different", p_prev, r);
278      }
279    }
280    p_prev = p;
281    pIter(p);
282  }
283  return TRUE;
284}
285
286BOOLEAN _p_LmTest(poly p, ring r, int level)
287{
288  if (level < 0 || p == NULL) return TRUE;
289  poly pnext = pNext(p);
290  pNext(p) = NULL;
291  BOOLEAN test_res = _p_Test(p, r, level);
292  pNext(p) = pnext;
293  return test_res;
294}
295
296BOOLEAN _pp_Test(poly p, ring lmRing, ring tailRing, int level)
297{
298  if (PDEBUG > level) level = PDEBUG;
299  if (level < 0 || p == NULL) return TRUE;
300  if (pNext(p) == NULL || lmRing == tailRing) return _p_Test(p, lmRing, level);
301
302  pFalseReturn(_p_LmTest(p, lmRing, level));
303  pFalseReturn(_p_Test(pNext(p), tailRing, level));
304
305  // check that lm > Lm(tail)
306  if (level > 1)
307  {
308    poly lm = p;
309    poly tail = p_DebugInit(pNext(p), tailRing, lmRing);
310    poly pnext = pNext(lm);
311    pNext(lm) = tail;
312    BOOLEAN cmp = p_LmCmp(lm, tail, lmRing);
313    if (cmp != 1)
314      dPolyReportError(lm, lmRing, "wrong order: lm <= Lm(tail)");
315    p_LmFree(tail, lmRing);
316    pNext(lm) = pnext;
317    return (cmp == 1);
318  }
319  return TRUE;
320}
321
322#endif // PDEBUG
323
324#include "pInline1.h"
325
326#if defined(PDEBUG) || defined(PDIV_DEBUG)
327static unsigned long pDivisibleBy_number = 1;
328static unsigned long pDivisibleBy_FALSE = 1;
329static unsigned long pDivisibleBy_ShortFalse = 1;
330BOOLEAN pDebugLmShortDivisibleBy(poly p1, unsigned long sev_1, ring r_1,
331                               poly p2, unsigned long not_sev_2, ring r_2)
332{
333  _pPolyAssume(p_GetShortExpVector(p1, r_1) == sev_1, p1, r_1);
334  _pPolyAssume(p_GetShortExpVector(p2, r_2) == ~ not_sev_2, p2, r_2);
335
336  pDivisibleBy_number++;
337  BOOLEAN ret;
338  if (r_1 == r_2)
339    ret = p_LmDivisibleBy(p1, p2, r_1);
340  else
341    ret = p_LmDivisibleBy(p1, r_1, p2, r_2);
342
343  if (! ret) pDivisibleBy_FALSE++;
344  if (sev_1 & not_sev_2)
345  {
346    pDivisibleBy_ShortFalse++;
347    if (ret)
348      dReportError("p1 divides p2, but sev's are wrong");
349  }
350  return ret;
351}
352
353void pPrintDivisbleByStat()
354{
355  Print("#Tests: %d; #FALSE %d(%d); #SHORT %d(%d)\n",
356        pDivisibleBy_number,
357        pDivisibleBy_FALSE, (unsigned long) ((double)pDivisibleBy_FALSE*((double) 100)/(double)pDivisibleBy_number),
358        pDivisibleBy_ShortFalse, (unsigned long) ((double)pDivisibleBy_ShortFalse*((double)100)/(double)pDivisibleBy_FALSE));
359}
360
361#endif // PDEBUG
362
363#endif // PDEBUG_CC
Note: See TracBrowser for help on using the repository browser.