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.17 2000-12-06 11:03:26 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 |
---|
37 | static BOOLEAN d_poly_error_reporting = FALSE; |
---|
38 | BOOLEAN 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 | ***************************************************************/ |
---|
72 | BOOLEAN 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 | |
---|
98 | BOOLEAN 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 | |
---|
108 | BOOLEAN p_CheckPolyRing(poly p, ring r) |
---|
109 | { |
---|
110 | pAssumeReturn(r != NULL && r->PolyBin != NULL); |
---|
111 | return p_CheckIsFromRing(p, r); |
---|
112 | } |
---|
113 | |
---|
114 | BOOLEAN 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 | } |
---|
120 | BOOLEAN 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 | ***************************************************************/ |
---|
131 | BOOLEAN 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 | ***************************************************************/ |
---|
151 | BOOLEAN 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 | } |
---|
161 | BOOLEAN 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 | ***************************************************************/ |
---|
179 | extern void p_Setm_General(poly p, ring r); |
---|
180 | |
---|
181 | static 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 | |
---|
198 | BOOLEAN _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 mix poly/vec representation |
---|
231 | pPolyAssumeReturnMsg(ismod == (p_GetComp(p, r) > 0), "mixed poly/vector"); |
---|
232 | |
---|
233 | // special check for ringorder_s/S |
---|
234 | if (r->order[1] == ringorder_S) |
---|
235 | { |
---|
236 | long c1, cc1, ccc1, ec1; |
---|
237 | sro_ord* o = &(r->typ[1]); |
---|
238 | |
---|
239 | c1 = p_GetComp(p, r); |
---|
240 | cc1 = o->data.syzcomp.Components[c1]; |
---|
241 | ccc1 = o->data.syzcomp.ShiftedComponents[cc1]; |
---|
242 | pPolyAssumeReturnMsg(c1 == 0 || cc1 != 0, "Component <-> TrueComponent zero mismatch"); |
---|
243 | pPolyAssumeReturnMsg(c1 == 0 || ccc1 != 0,"Component <-> ShiftedComponent zero mismatch"); |
---|
244 | ec1 = p->exp[r->typ[1].data.syzcomp.place]; |
---|
245 | pPolyAssumeReturnMsg(ec1 == ccc1, "Shifted comp out of sync. should %d, is %d"); |
---|
246 | } |
---|
247 | |
---|
248 | // check that p_Setm works ok |
---|
249 | if (level > 0) |
---|
250 | { |
---|
251 | poly p_should_equal = p_DebugInit(p, r, r); |
---|
252 | pPolyAssumeReturnMsg(p_ExpVectorEqual(p, p_should_equal, r), "p_Setm field(s) out of sync"); |
---|
253 | p_LmFree(p_should_equal, r); |
---|
254 | } |
---|
255 | |
---|
256 | // check order |
---|
257 | if (p_prev != NULL) |
---|
258 | { |
---|
259 | int cmp = p_LmCmp(p_prev, p, r); |
---|
260 | if (cmp == 0) |
---|
261 | { |
---|
262 | _pPolyAssumeReturnMsg(0, "monoms p and p->next are equal", p_prev, r); |
---|
263 | } |
---|
264 | else |
---|
265 | _pPolyAssumeReturnMsg(p_LmCmp(p_prev, p, r) == 1, "wrong order", p_prev, r); |
---|
266 | |
---|
267 | // check that compare worked sensibly |
---|
268 | if (level > 1 && p_GetComp(p_prev, r) == p_GetComp(p, r)) |
---|
269 | { |
---|
270 | int i; |
---|
271 | for (i=r->N; i>0; i--) |
---|
272 | { |
---|
273 | if (p_GetExp(p_prev, i, r) != p_GetExp(p, i, r)) break; |
---|
274 | } |
---|
275 | _pPolyAssumeReturnMsg(i > 0, "Exponents equal but compare different", p_prev, r); |
---|
276 | } |
---|
277 | } |
---|
278 | p_prev = p; |
---|
279 | pIter(p); |
---|
280 | } |
---|
281 | return TRUE; |
---|
282 | } |
---|
283 | |
---|
284 | BOOLEAN _p_LmTest(poly p, ring r, int level) |
---|
285 | { |
---|
286 | if (level < 0 || p == NULL) return TRUE; |
---|
287 | poly pnext = pNext(p); |
---|
288 | pNext(p) = NULL; |
---|
289 | BOOLEAN test_res = _p_Test(p, r, level); |
---|
290 | pNext(p) = pnext; |
---|
291 | return test_res; |
---|
292 | } |
---|
293 | |
---|
294 | BOOLEAN _pp_Test(poly p, ring lmRing, ring tailRing, int level) |
---|
295 | { |
---|
296 | if (PDEBUG > level) level = PDEBUG; |
---|
297 | if (level < 0 || p == NULL) return TRUE; |
---|
298 | if (pNext(p) == NULL || lmRing == tailRing) return _p_Test(p, lmRing, level); |
---|
299 | |
---|
300 | pFalseReturn(_p_LmTest(p, lmRing, level)); |
---|
301 | pFalseReturn(_p_Test(pNext(p), tailRing, level)); |
---|
302 | |
---|
303 | // check that lm > Lm(tail) |
---|
304 | if (level > 1) |
---|
305 | { |
---|
306 | poly lm = p; |
---|
307 | poly tail = p_DebugInit(pNext(p), tailRing, lmRing); |
---|
308 | poly pnext = pNext(lm); |
---|
309 | pNext(lm) = tail; |
---|
310 | BOOLEAN cmp = p_LmCmp(lm, tail, lmRing); |
---|
311 | if (cmp != 1) |
---|
312 | dPolyReportError(lm, lmRing, "wrong order: lm <= Lm(tail)"); |
---|
313 | p_LmFree(tail, lmRing); |
---|
314 | pNext(lm) = pnext; |
---|
315 | return (cmp == 1); |
---|
316 | } |
---|
317 | return TRUE; |
---|
318 | } |
---|
319 | |
---|
320 | #endif // PDEBUG |
---|
321 | |
---|
322 | #include "pInline1.h" |
---|
323 | |
---|
324 | #if defined(PDEBUG) || defined(PDIV_DEBUG) |
---|
325 | static unsigned long pDivisibleBy_number = 1; |
---|
326 | static unsigned long pDivisibleBy_FALSE = 1; |
---|
327 | static unsigned long pDivisibleBy_ShortFalse = 1; |
---|
328 | BOOLEAN pDebugLmShortDivisibleBy(poly p1, unsigned long sev_1, ring r_1, |
---|
329 | poly p2, unsigned long not_sev_2, ring r_2) |
---|
330 | { |
---|
331 | _pPolyAssume(p_GetShortExpVector(p1, r_1) == sev_1, p1, r_1); |
---|
332 | _pPolyAssume(p_GetShortExpVector(p2, r_2) == ~ not_sev_2, p2, r_2); |
---|
333 | |
---|
334 | pDivisibleBy_number++; |
---|
335 | BOOLEAN ret; |
---|
336 | if (r_1 == r_2) |
---|
337 | ret = p_LmDivisibleBy(p1, p2, r_1); |
---|
338 | else |
---|
339 | ret = p_LmDivisibleBy(p1, r_1, p2, r_2); |
---|
340 | |
---|
341 | if (! ret) pDivisibleBy_FALSE++; |
---|
342 | if (sev_1 & not_sev_2) |
---|
343 | { |
---|
344 | pDivisibleBy_ShortFalse++; |
---|
345 | if (ret) |
---|
346 | dReportError("p1 divides p2, but sev's are wrong"); |
---|
347 | } |
---|
348 | return ret; |
---|
349 | } |
---|
350 | |
---|
351 | void pPrintDivisbleByStat() |
---|
352 | { |
---|
353 | Print("#Tests: %d; #FALSE %d(%d); #SHORT %d(%d)\n", |
---|
354 | pDivisibleBy_number, |
---|
355 | pDivisibleBy_FALSE, (unsigned long) ((double)pDivisibleBy_FALSE*((double) 100)/(double)pDivisibleBy_number), |
---|
356 | pDivisibleBy_ShortFalse, (unsigned long) ((double)pDivisibleBy_ShortFalse*((double)100)/(double)pDivisibleBy_FALSE)); |
---|
357 | } |
---|
358 | |
---|
359 | #if (OM_TRACK > 2) && defined(OM_TRACK_CUSTOM) |
---|
360 | |
---|
361 | #include "ideals.h" |
---|
362 | #include "lists.h" |
---|
363 | #include "ipid.h" |
---|
364 | |
---|
365 | |
---|
366 | void p_SetRingOfPoly(poly p, ring r) |
---|
367 | { |
---|
368 | while (p != NULL) |
---|
369 | { |
---|
370 | p_SetRingOfLm(p, r); |
---|
371 | pIter(p); |
---|
372 | } |
---|
373 | } |
---|
374 | |
---|
375 | void p_SetRingOfIdeal(ideal id, ring r) |
---|
376 | { |
---|
377 | if (id == NULL) return; |
---|
378 | |
---|
379 | int i, n = id->ncols*id->nrows; |
---|
380 | |
---|
381 | for (i=0; i<n; i++) |
---|
382 | { |
---|
383 | p_SetRingOfPoly(id->m[i], r); |
---|
384 | } |
---|
385 | } |
---|
386 | |
---|
387 | void p_SetRingOfList(lists L, ring r) |
---|
388 | { |
---|
389 | int i; |
---|
390 | for (i=0; i<L->nr; i++) |
---|
391 | { |
---|
392 | p_SetRingOfLeftv(&(L->m[i]), r); |
---|
393 | } |
---|
394 | } |
---|
395 | |
---|
396 | void p_SetRingOfCommand(command cmd, ring r) |
---|
397 | { |
---|
398 | if (cmd->op == PROC_CMD && cmd->argc == 2) |
---|
399 | p_SetRingOfLeftv(&(cmd->arg2), r); |
---|
400 | else if (cmd->argc > 0) |
---|
401 | { |
---|
402 | p_SetRingOfLeftv(&(cmd->arg1), r); |
---|
403 | if (cmd->argc > 1) |
---|
404 | { |
---|
405 | p_SetRingOfLeftv(&(cmd->arg2), r); |
---|
406 | if (cmd->argc > 2) |
---|
407 | p_SetRingOfLeftv(&(cmd->arg3), r); |
---|
408 | } |
---|
409 | } |
---|
410 | } |
---|
411 | |
---|
412 | void p_SetRingOfLeftv(leftv l, ring r) |
---|
413 | { |
---|
414 | while (l != NULL) |
---|
415 | { |
---|
416 | switch(l->rtyp) |
---|
417 | { |
---|
418 | case POLY_CMD: |
---|
419 | case VECTOR_CMD: |
---|
420 | p_SetRingOfPoly((poly) l->data, r); |
---|
421 | break; |
---|
422 | |
---|
423 | case IDEAL_CMD: |
---|
424 | case MODUL_CMD: |
---|
425 | case MATRIX_CMD: |
---|
426 | case MAP_CMD: |
---|
427 | p_SetRingOfIdeal((ideal) l->data, r); |
---|
428 | break; |
---|
429 | |
---|
430 | case LIST_CMD: |
---|
431 | p_SetRingOfList((lists) l->data, r); |
---|
432 | break; |
---|
433 | |
---|
434 | case COMMAND: |
---|
435 | p_SetRingOfCommand((command)l->data, r); |
---|
436 | default: |
---|
437 | break; |
---|
438 | } |
---|
439 | l = l->next; |
---|
440 | } |
---|
441 | } |
---|
442 | #endif // (OM_TRACK > 2) && defined(OM_TRACK_CUSTOM) |
---|
443 | |
---|
444 | #endif // PDEBUG |
---|
445 | |
---|
446 | #endif // PDEBUG_CC |
---|