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.3 2000-09-14 14:20:44 obachman Exp $ |
---|
10 | *******************************************************************/ |
---|
11 | |
---|
12 | #ifndef PDEBUG_CC |
---|
13 | #define PDEBUG_CC |
---|
14 | #include <stdarg.h> |
---|
15 | #include <stdio.h> |
---|
16 | |
---|
17 | #include "mod2.h" |
---|
18 | #ifdef PDEBUG |
---|
19 | |
---|
20 | #include "polys.h" |
---|
21 | #include "febase.h" |
---|
22 | #include "omalloc.h" |
---|
23 | #include "ring.h" |
---|
24 | |
---|
25 | |
---|
26 | /*************************************************************** |
---|
27 | * |
---|
28 | * Error reporting |
---|
29 | * |
---|
30 | ***************************************************************/ |
---|
31 | extern void wrp(poly p); |
---|
32 | BOOLEAN dPolyReportError(poly p, ring r, const char* fmt, ...) |
---|
33 | { |
---|
34 | va_list ap; |
---|
35 | va_start(ap, fmt); |
---|
36 | |
---|
37 | fprintf(stderr, "// ***dPolyError: "); |
---|
38 | vfprintf(stderr, fmt, ap); |
---|
39 | fprintf(stderr, "\n occured at\n"); |
---|
40 | omPrintCurrentBackTraceMax(stderr, 8); |
---|
41 | if (p != NULL) |
---|
42 | { |
---|
43 | if (r == currRing) |
---|
44 | { |
---|
45 | fprintf(stderr, " occured for poly: "); |
---|
46 | wrp(p); |
---|
47 | omPrintAddrInfo(stderr, p, " "); |
---|
48 | } |
---|
49 | else |
---|
50 | { |
---|
51 | omPrintAddrInfo(stderr, p, " occured for poly: "); |
---|
52 | } |
---|
53 | } |
---|
54 | dErrorBreak(); |
---|
55 | return FALSE; |
---|
56 | } |
---|
57 | |
---|
58 | /*************************************************************** |
---|
59 | * |
---|
60 | * checking for ring stuff |
---|
61 | * |
---|
62 | ***************************************************************/ |
---|
63 | BOOLEAN p_CheckIsFromRing(poly p, ring r) |
---|
64 | { |
---|
65 | if (p != NULL) |
---|
66 | { |
---|
67 | void* custom = omGetCustomOfAddr(p); |
---|
68 | if (custom != NULL) |
---|
69 | { |
---|
70 | pPolyAssumeReturn(custom == r); |
---|
71 | return TRUE; |
---|
72 | } |
---|
73 | else |
---|
74 | { |
---|
75 | pPolyAssumeReturn(omIsBinPageAddr(p) && omSizeWOfAddr(p)==r->PolyBin->sizeW); |
---|
76 | return TRUE; |
---|
77 | } |
---|
78 | return FALSE; |
---|
79 | } |
---|
80 | return TRUE; |
---|
81 | } |
---|
82 | |
---|
83 | BOOLEAN p_CheckPolyRing(poly p, ring r) |
---|
84 | { |
---|
85 | pAssumeReturn(r != NULL && r->PolyBin != NULL); |
---|
86 | pAssumeReturn(p != NULL); |
---|
87 | return p_CheckIsFromRing(p, r); |
---|
88 | } |
---|
89 | BOOLEAN p_CheckRing(ring r) |
---|
90 | { |
---|
91 | pAssumeReturn(r != NULL && r->PolyBin != NULL); |
---|
92 | return TRUE; |
---|
93 | } |
---|
94 | |
---|
95 | /*************************************************************** |
---|
96 | * |
---|
97 | * Debugging/statistics of pDivisibleBy |
---|
98 | * |
---|
99 | ***************************************************************/ |
---|
100 | static int pDivisibleBy_number = 1; |
---|
101 | static int pDivisibleBy_FALSE = 1; |
---|
102 | static int pDivisibleBy_ShortFalse = 1; |
---|
103 | BOOLEAN pDebugShortDivisibleBy(poly p1, unsigned long sev_1, ring r_1, |
---|
104 | poly p2, unsigned long not_sev_2, ring r_2) |
---|
105 | { |
---|
106 | _pPolyAssume(p_GetShortExpVector(p1, r_1) == sev_1, p1, r_1); |
---|
107 | _pPolyAssume(p_GetShortExpVector(p2, r_2) == ~ not_sev_2, p2, r_2); |
---|
108 | |
---|
109 | pDivisibleBy_number++; |
---|
110 | BOOLEAN ret = _p_DivisibleBy1(p1, r_1, p2, r_2); |
---|
111 | if (! ret) pDivisibleBy_FALSE++; |
---|
112 | if (sev_1 & not_sev_2) |
---|
113 | { |
---|
114 | pDivisibleBy_ShortFalse++; |
---|
115 | if (ret) |
---|
116 | dReportError("p1 divides p2, but sev's are wrong"); |
---|
117 | } |
---|
118 | return ret; |
---|
119 | } |
---|
120 | |
---|
121 | void pPrintDivisbleByStat() |
---|
122 | { |
---|
123 | Print("#Tests: %d; #FALSE %d(%d); #SHORT %d(%d)\n", |
---|
124 | pDivisibleBy_number, |
---|
125 | pDivisibleBy_FALSE, pDivisibleBy_FALSE*100/pDivisibleBy_number, |
---|
126 | pDivisibleBy_ShortFalse, pDivisibleBy_ShortFalse*100/pDivisibleBy_FALSE); |
---|
127 | } |
---|
128 | |
---|
129 | /*************************************************************** |
---|
130 | * |
---|
131 | * Misc things helpful for debugging |
---|
132 | * |
---|
133 | ***************************************************************/ |
---|
134 | BOOLEAN pIsMonomOf(poly p, poly m) |
---|
135 | { |
---|
136 | if (m == NULL) return TRUE; |
---|
137 | while (p != NULL) |
---|
138 | { |
---|
139 | if (p == m) return TRUE; |
---|
140 | pIter(p); |
---|
141 | } |
---|
142 | return FALSE; |
---|
143 | } |
---|
144 | BOOLEAN pHaveCommonMonoms(poly p, poly q) |
---|
145 | { |
---|
146 | while (p != NULL) |
---|
147 | { |
---|
148 | if (pIsMonomOf(q, p)) |
---|
149 | { |
---|
150 | return TRUE; |
---|
151 | } |
---|
152 | pIter(p); |
---|
153 | } |
---|
154 | return FALSE; |
---|
155 | } |
---|
156 | |
---|
157 | /*************************************************************** |
---|
158 | * |
---|
159 | * Testing of polys |
---|
160 | * |
---|
161 | ***************************************************************/ |
---|
162 | static poly p_DebugInit(poly p, ring dest_ring, ring src_ring) |
---|
163 | { |
---|
164 | poly d_p = p_Init(dest_ring); |
---|
165 | int i; |
---|
166 | assume(dest_ring->N == src_ring->N); |
---|
167 | |
---|
168 | for (i=1; i<= src_ring->N; i++) |
---|
169 | { |
---|
170 | p_SetExp(d_p, i, p_GetExp(p, i, src_ring), dest_ring); |
---|
171 | } |
---|
172 | if (rRing_has_Comp(dest_ring)) |
---|
173 | p_SetComp(d_p, p_GetComp(p, src_ring), dest_ring); |
---|
174 | p_Setm(d_p, dest_ring); |
---|
175 | return d_p; |
---|
176 | } |
---|
177 | |
---|
178 | BOOLEAN _p_Test(poly p, ring r, int level) |
---|
179 | { |
---|
180 | if (level < 0 || p == NULL) return TRUE; |
---|
181 | |
---|
182 | poly p_prev = NULL; |
---|
183 | |
---|
184 | // check addr with level+1 so as to check bin/page of addr |
---|
185 | if (level > 0) |
---|
186 | pPolyAssumeReturnMsg(omTestBinAddrSize(p, (r->PolyBin->sizeW)*SIZEOF_LONG, level+1) |
---|
187 | == omError_NoError, "memory error"); |
---|
188 | |
---|
189 | pFalseReturn(p_CheckRing(r)); |
---|
190 | int ismod = p_GetComp(p, r) > 0; |
---|
191 | |
---|
192 | while (p != NULL) |
---|
193 | { |
---|
194 | // ring check |
---|
195 | pFalseReturn(p_CheckPolyRing(p, r)); |
---|
196 | // omAddr check |
---|
197 | pPolyAssumeReturnMsg(omTestBinAddrSize(p, (r->PolyBin->sizeW)*SIZEOF_LONG, 1) |
---|
198 | == omError_NoError, "memory error"); |
---|
199 | // number/coef check |
---|
200 | pPolyAssumeReturnMsg(p->coef != NULL || (n_GetChar(r) >= 2), "NULL coef"); |
---|
201 | pPolyAssumeReturnMsg(!n_IsZero(p->coef, r), "Zero coef"); |
---|
202 | |
---|
203 | // check for mix poly/vec representation |
---|
204 | pPolyAssumeReturnMsg(ismod == (p_GetComp(p, r) > 0), "mixed poly/vector"); |
---|
205 | |
---|
206 | // special check for ringorder_s/S |
---|
207 | if (currRing->order[1] == ringorder_S) |
---|
208 | { |
---|
209 | long c1, cc1, ccc1, ec1; |
---|
210 | sro_ord* o = &(currRing->typ[1]); |
---|
211 | |
---|
212 | c1 = pGetComp(p); |
---|
213 | cc1 = o->data.syzcomp.Components[c1]; |
---|
214 | ccc1 = o->data.syzcomp.ShiftedComponents[cc1]; |
---|
215 | pPolyAssumeReturnMsg(c1 == 0 || cc1 != 0, "Component <-> TrueComponent zero mismatch"); |
---|
216 | pPolyAssumeReturnMsg(c1 == 0 || ccc1 != 0,"Component <-> ShiftedComponent zero mismatch"); |
---|
217 | ec1 = p->exp[currRing->typ[1].data.syzcomp.place]; |
---|
218 | pPolyAssumeReturnMsg(ec1 == ccc1, "Shifted comp out of sync. should %d, is %d"); |
---|
219 | } |
---|
220 | |
---|
221 | // check that p_Setm works ok |
---|
222 | if (level > 0) |
---|
223 | { |
---|
224 | poly p_should_equal = p_DebugInit(p, r, r); |
---|
225 | pPolyAssumeReturnMsg(p_ExpVectorEqual(p, p_should_equal, r), "p_Setm field(s) out of sync"); |
---|
226 | p_Free(p_should_equal, r); |
---|
227 | } |
---|
228 | |
---|
229 | // check order |
---|
230 | if (p_prev != NULL) |
---|
231 | { |
---|
232 | int cmp = p_LmCmp(p_prev, p, r); |
---|
233 | if (cmp == 0) |
---|
234 | { |
---|
235 | _pPolyAssumeReturnMsg(0, "monoms p and p->next are equal", p_prev, r); |
---|
236 | } |
---|
237 | else |
---|
238 | _pPolyAssumeReturnMsg(p_LmCmp(p_prev, p, r) == 1, "wrong order", p_prev, r); |
---|
239 | |
---|
240 | // check that compare worked sensibly |
---|
241 | if (level > 1 && p_GetComp(p_prev, r) == p_GetComp(p, r)) |
---|
242 | { |
---|
243 | int i; |
---|
244 | for (i=r->N; i>0; i--) |
---|
245 | { |
---|
246 | if (p_GetExp(p_prev, i, r) != p_GetExp(p, i, r)) break; |
---|
247 | } |
---|
248 | _pPolyAssumeReturnMsg(i > 0, "Exponents equal but compare different", p_prev, r); |
---|
249 | } |
---|
250 | } |
---|
251 | p_prev = p; |
---|
252 | pIter(p); |
---|
253 | } |
---|
254 | return TRUE; |
---|
255 | } |
---|
256 | |
---|
257 | BOOLEAN _p_LmTest(poly p, ring r, int level) |
---|
258 | { |
---|
259 | if (level < 0 || p == NULL) return TRUE; |
---|
260 | poly pnext = pNext(p); |
---|
261 | pNext(p) = NULL; |
---|
262 | BOOLEAN test_res = _p_Test(p, r, level); |
---|
263 | pNext(p) = pnext; |
---|
264 | return test_res; |
---|
265 | } |
---|
266 | |
---|
267 | BOOLEAN _pp_Test(poly p, ring lmRing, ring tailRing, int level) |
---|
268 | { |
---|
269 | if (level < 0 || p == NULL) return TRUE; |
---|
270 | if (pNext(p) == NULL || lmRing == tailRing) return _p_Test(p, lmRing, level); |
---|
271 | |
---|
272 | pFalseReturn(_p_LmTest(p, lmRing, level)); |
---|
273 | pFalseReturn(_p_Test(pNext(p), tailRing, level)); |
---|
274 | |
---|
275 | // check that lm > Lm(tail) |
---|
276 | if (level > 1) |
---|
277 | { |
---|
278 | poly lm = p; |
---|
279 | poly tail = p_DebugInit(pNext(p), lmRing, tailRing); |
---|
280 | poly pnext = pNext(lm); |
---|
281 | pNext(lm) = tail; |
---|
282 | BOOLEAN cmp = p_LmCmp(lm, tail); |
---|
283 | if (cmp != 1) |
---|
284 | dPolyReportError(lm, lmRing, "wrong order: lm <= Lm(tail)"); |
---|
285 | pNext(lm) = pnext; |
---|
286 | return (cmp == 1); |
---|
287 | } |
---|
288 | return TRUE; |
---|
289 | } |
---|
290 | |
---|
291 | #endif // PDEBUG |
---|
292 | #endif // PDEBUG_CC |
---|
293 | |
---|