[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 |
---|
[d47d1f9] | 9 | * Version: $Id: pDebug.cc,v 1.12 2009-07-02 15:15:38 ederc Exp $ |
---|
[35aab3] | 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 | { |
---|
[d47d1f9] | 76 | #if (OM_TRACK > 0) && defined(OM_TRACK_CUSTOM) |
---|
[35aab3] | 77 | void* custom = omGetCustomOfAddr(p); |
---|
| 78 | if (custom != NULL) |
---|
| 79 | { |
---|
| 80 | pPolyAssumeReturnMsg(custom == r || |
---|
| 81 | // be more sloppy for qrings |
---|
| 82 | (r->qideal != NULL && |
---|
| 83 | omIsBinPageAddr(p) && |
---|
| 84 | omSizeWOfAddr(p)==r->PolyBin->sizeW) || |
---|
[f62996] | 85 | rSamePolyRep((ring) custom, r), |
---|
[35aab3] | 86 | "monomial not from specified ring"); |
---|
| 87 | return TRUE; |
---|
| 88 | } |
---|
| 89 | else |
---|
[609e27b] | 90 | #endif |
---|
[35aab3] | 91 | { |
---|
| 92 | pPolyAssumeReturn(omIsBinPageAddr(p) && omSizeWOfAddr(p)==r->PolyBin->sizeW); |
---|
| 93 | return TRUE; |
---|
| 94 | } |
---|
| 95 | return FALSE; |
---|
| 96 | } |
---|
| 97 | return TRUE; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | BOOLEAN p_CheckIsFromRing(poly p, ring r) |
---|
| 101 | { |
---|
| 102 | while (p!=NULL) |
---|
| 103 | { |
---|
| 104 | pFalseReturn(p_LmCheckIsFromRing(p, r)); |
---|
| 105 | pIter(p); |
---|
| 106 | } |
---|
| 107 | return TRUE; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | BOOLEAN p_CheckPolyRing(poly p, ring r) |
---|
| 111 | { |
---|
| 112 | pAssumeReturn(r != NULL && r->PolyBin != NULL); |
---|
| 113 | return p_CheckIsFromRing(p, r); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | BOOLEAN p_LmCheckPolyRing(poly p, ring r) |
---|
| 117 | { |
---|
| 118 | pAssumeReturn(r != NULL && r->PolyBin != NULL); |
---|
| 119 | pAssumeReturn(p != NULL); |
---|
| 120 | return p_LmCheckIsFromRing(p, r); |
---|
| 121 | } |
---|
| 122 | BOOLEAN p_CheckRing(ring r) |
---|
| 123 | { |
---|
| 124 | pAssumeReturn(r != NULL && r->PolyBin != NULL); |
---|
| 125 | return TRUE; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | /*************************************************************** |
---|
| 129 | * |
---|
| 130 | * Debugging/statistics of pDivisibleBy |
---|
| 131 | * |
---|
| 132 | ***************************************************************/ |
---|
| 133 | BOOLEAN p_DebugLmDivisibleByNoComp(poly a, poly b, ring r) |
---|
| 134 | { |
---|
| 135 | int i=r->N; |
---|
| 136 | |
---|
| 137 | do |
---|
| 138 | { |
---|
| 139 | if (p_GetExp(a,i,r) > p_GetExp(b,i,r)) |
---|
| 140 | return FALSE; |
---|
| 141 | i--; |
---|
| 142 | } |
---|
| 143 | while (i); |
---|
[009d80] | 144 | #ifdef HAVE_RINGS |
---|
| 145 | return nDivBy(pGetCoeff(b), pGetCoeff(a)); |
---|
| 146 | #else |
---|
| 147 | return TRUE; |
---|
[994445] | 148 | #endif |
---|
[585bbcb] | 149 | } |
---|
[35aab3] | 150 | |
---|
| 151 | |
---|
| 152 | /*************************************************************** |
---|
| 153 | * |
---|
| 154 | * Misc things helpful for debugging |
---|
| 155 | * |
---|
| 156 | ***************************************************************/ |
---|
| 157 | BOOLEAN pIsMonomOf(poly p, poly m) |
---|
| 158 | { |
---|
| 159 | if (m == NULL) return TRUE; |
---|
| 160 | while (p != NULL) |
---|
| 161 | { |
---|
| 162 | if (p == m) return TRUE; |
---|
| 163 | pIter(p); |
---|
| 164 | } |
---|
| 165 | return FALSE; |
---|
| 166 | } |
---|
| 167 | BOOLEAN pHaveCommonMonoms(poly p, poly q) |
---|
| 168 | { |
---|
| 169 | while (p != NULL) |
---|
| 170 | { |
---|
| 171 | if (pIsMonomOf(q, p)) |
---|
| 172 | { |
---|
| 173 | return TRUE; |
---|
| 174 | } |
---|
| 175 | pIter(p); |
---|
| 176 | } |
---|
| 177 | return FALSE; |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | /*************************************************************** |
---|
| 181 | * |
---|
| 182 | * Testing of polys |
---|
| 183 | * |
---|
| 184 | ***************************************************************/ |
---|
| 185 | extern void p_Setm_General(poly p, ring r); |
---|
| 186 | |
---|
| 187 | static poly p_DebugInit(poly p, ring src_ring, ring dest_ring) |
---|
| 188 | { |
---|
| 189 | poly d_p = p_Init(dest_ring); |
---|
| 190 | int i; |
---|
| 191 | assume(dest_ring->N == src_ring->N); |
---|
| 192 | |
---|
| 193 | for (i=1; i<= src_ring->N; i++) |
---|
| 194 | { |
---|
| 195 | p_SetExp(d_p, i, p_GetExp(p, i, src_ring), dest_ring); |
---|
| 196 | } |
---|
| 197 | if (rRing_has_Comp(dest_ring)) |
---|
| 198 | p_SetComp(d_p, p_GetComp(p, src_ring), dest_ring); |
---|
| 199 | |
---|
| 200 | p_Setm_General(d_p, dest_ring); |
---|
| 201 | return d_p; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | BOOLEAN _p_Test(poly p, ring r, int level) |
---|
| 205 | { |
---|
| 206 | assume(r->cf !=NULL); |
---|
| 207 | |
---|
| 208 | if (PDEBUG > level) level = PDEBUG; |
---|
| 209 | if (level < 0 || p == NULL) return TRUE; |
---|
| 210 | |
---|
| 211 | poly p_prev = NULL; |
---|
| 212 | |
---|
[130ecb] | 213 | #ifndef OM_NDEBUG |
---|
[35aab3] | 214 | // check addr with level+1 so as to check bin/page of addr |
---|
| 215 | pPolyAssumeReturnMsg(omTestBinAddrSize(p, (r->PolyBin->sizeW)*SIZEOF_LONG, level+1) |
---|
| 216 | == omError_NoError, "memory error"); |
---|
[130ecb] | 217 | #endif |
---|
[35aab3] | 218 | |
---|
| 219 | pFalseReturn(p_CheckRing(r)); |
---|
| 220 | |
---|
| 221 | // this checks that p does not contain a loop: rather expensive O(length^2) |
---|
[130ecb] | 222 | #ifndef OM_NDEBUG |
---|
[35aab3] | 223 | if (level > 1) |
---|
| 224 | pFalseReturn(omTestList(p, level) == omError_NoError); |
---|
[130ecb] | 225 | #endif |
---|
[35aab3] | 226 | |
---|
| 227 | int ismod = p_GetComp(p, r) > 0; |
---|
| 228 | |
---|
| 229 | while (p != NULL) |
---|
| 230 | { |
---|
| 231 | // ring check |
---|
| 232 | pFalseReturn(p_LmCheckIsFromRing(p, r)); |
---|
[130ecb] | 233 | #ifndef OM_NDEBUG |
---|
[35aab3] | 234 | // omAddr check |
---|
| 235 | pPolyAssumeReturnMsg(omTestBinAddrSize(p, (r->PolyBin->sizeW)*SIZEOF_LONG, 1) |
---|
| 236 | == omError_NoError, "memory error"); |
---|
[130ecb] | 237 | #endif |
---|
[35aab3] | 238 | // number/coef check |
---|
| 239 | pPolyAssumeReturnMsg(p->coef != NULL || (n_GetChar(r) >= 2), "NULL coef"); |
---|
[bccc3f] | 240 | #ifdef LDEBUG |
---|
[3332cc6] | 241 | r->cf->nDBTest(p->coef,__FILE__,__LINE__); |
---|
[bccc3f] | 242 | #endif |
---|
[35aab3] | 243 | pPolyAssumeReturnMsg(!n_IsZero(p->coef, r), "Zero coef"); |
---|
| 244 | |
---|
| 245 | // check for valid comp |
---|
| 246 | pPolyAssumeReturnMsg(p_GetComp(p, r) >= 0 && (p_GetComp(p, r)<65000), "component out of range ?"); |
---|
| 247 | // check for mix poly/vec representation |
---|
| 248 | pPolyAssumeReturnMsg(ismod == (p_GetComp(p, r) > 0), "mixed poly/vector"); |
---|
| 249 | |
---|
| 250 | // special check for ringorder_s/S |
---|
[b30ac0] | 251 | if ((r->order!=NULL) &&(r->order[1] == ringorder_S)) |
---|
[35aab3] | 252 | { |
---|
| 253 | long c1, cc1, ccc1, ec1; |
---|
| 254 | sro_ord* o = &(r->typ[1]); |
---|
| 255 | |
---|
| 256 | c1 = p_GetComp(p, r); |
---|
| 257 | cc1 = o->data.syzcomp.Components[c1]; |
---|
| 258 | ccc1 = o->data.syzcomp.ShiftedComponents[cc1]; |
---|
| 259 | pPolyAssumeReturnMsg(c1 == 0 || cc1 != 0, "Component <-> TrueComponent zero mismatch"); |
---|
| 260 | pPolyAssumeReturnMsg(c1 == 0 || ccc1 != 0,"Component <-> ShiftedComponent zero mismatch"); |
---|
| 261 | ec1 = p->exp[r->typ[1].data.syzcomp.place]; |
---|
| 262 | pPolyAssumeReturnMsg(ec1 == ccc1, "Shifted comp out of sync. should %d, is %d"); |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | // check that p_Setm works ok |
---|
| 266 | if (level > 0) |
---|
| 267 | { |
---|
| 268 | poly p_should_equal = p_DebugInit(p, r, r); |
---|
| 269 | pPolyAssumeReturnMsg(p_ExpVectorEqual(p, p_should_equal, r), "p_Setm field(s) out of sync"); |
---|
| 270 | p_LmFree(p_should_equal, r); |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | // check order |
---|
| 274 | if (p_prev != NULL) |
---|
| 275 | { |
---|
| 276 | int cmp = p_LmCmp(p_prev, p, r); |
---|
| 277 | if (cmp == 0) |
---|
| 278 | { |
---|
| 279 | _pPolyAssumeReturnMsg(0, "monoms p and p->next are equal", p_prev, r); |
---|
| 280 | } |
---|
| 281 | else |
---|
| 282 | _pPolyAssumeReturnMsg(p_LmCmp(p_prev, p, r) == 1, "wrong order", p_prev, r); |
---|
| 283 | |
---|
| 284 | // check that compare worked sensibly |
---|
| 285 | if (level > 1 && p_GetComp(p_prev, r) == p_GetComp(p, r)) |
---|
| 286 | { |
---|
| 287 | int i; |
---|
| 288 | for (i=r->N; i>0; i--) |
---|
| 289 | { |
---|
| 290 | if (p_GetExp(p_prev, i, r) != p_GetExp(p, i, r)) break; |
---|
| 291 | } |
---|
| 292 | _pPolyAssumeReturnMsg(i > 0, "Exponents equal but compare different", p_prev, r); |
---|
| 293 | } |
---|
| 294 | } |
---|
| 295 | p_prev = p; |
---|
| 296 | pIter(p); |
---|
| 297 | } |
---|
| 298 | return TRUE; |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | BOOLEAN _p_LmTest(poly p, ring r, int level) |
---|
| 302 | { |
---|
| 303 | if (level < 0 || p == NULL) return TRUE; |
---|
| 304 | poly pnext = pNext(p); |
---|
| 305 | pNext(p) = NULL; |
---|
| 306 | BOOLEAN test_res = _p_Test(p, r, level); |
---|
| 307 | pNext(p) = pnext; |
---|
| 308 | return test_res; |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | BOOLEAN _pp_Test(poly p, ring lmRing, ring tailRing, int level) |
---|
| 312 | { |
---|
| 313 | if (PDEBUG > level) level = PDEBUG; |
---|
| 314 | if (level < 0 || p == NULL) return TRUE; |
---|
| 315 | if (pNext(p) == NULL || lmRing == tailRing) return _p_Test(p, lmRing, level); |
---|
| 316 | |
---|
| 317 | pFalseReturn(_p_LmTest(p, lmRing, level)); |
---|
| 318 | pFalseReturn(_p_Test(pNext(p), tailRing, level)); |
---|
| 319 | |
---|
| 320 | // check that lm > Lm(tail) |
---|
| 321 | if (level > 1) |
---|
| 322 | { |
---|
| 323 | poly lm = p; |
---|
| 324 | poly tail = p_DebugInit(pNext(p), tailRing, lmRing); |
---|
| 325 | poly pnext = pNext(lm); |
---|
| 326 | pNext(lm) = tail; |
---|
| 327 | BOOLEAN cmp = p_LmCmp(lm, tail, lmRing); |
---|
| 328 | if (cmp != 1) |
---|
| 329 | dPolyReportError(lm, lmRing, "wrong order: lm <= Lm(tail)"); |
---|
| 330 | p_LmFree(tail, lmRing); |
---|
| 331 | pNext(lm) = pnext; |
---|
| 332 | return (cmp == 1); |
---|
| 333 | } |
---|
| 334 | return TRUE; |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | #endif // PDEBUG |
---|
| 338 | |
---|
| 339 | #include "pInline1.h" |
---|
| 340 | |
---|
| 341 | #if defined(PDEBUG) || defined(PDIV_DEBUG) |
---|
| 342 | static unsigned long pDivisibleBy_number = 1; |
---|
| 343 | static unsigned long pDivisibleBy_FALSE = 1; |
---|
| 344 | static unsigned long pDivisibleBy_ShortFalse = 1; |
---|
| 345 | BOOLEAN pDebugLmShortDivisibleBy(poly p1, unsigned long sev_1, ring r_1, |
---|
| 346 | poly p2, unsigned long not_sev_2, ring r_2) |
---|
| 347 | { |
---|
| 348 | _pPolyAssume(p_GetShortExpVector(p1, r_1) == sev_1, p1, r_1); |
---|
| 349 | _pPolyAssume(p_GetShortExpVector(p2, r_2) == ~ not_sev_2, p2, r_2); |
---|
| 350 | |
---|
| 351 | pDivisibleBy_number++; |
---|
| 352 | BOOLEAN ret; |
---|
| 353 | if (r_1 == r_2) |
---|
| 354 | ret = p_LmDivisibleBy(p1, p2, r_1); |
---|
| 355 | else |
---|
| 356 | ret = p_LmDivisibleBy(p1, r_1, p2, r_2); |
---|
| 357 | |
---|
| 358 | if (! ret) pDivisibleBy_FALSE++; |
---|
| 359 | if (sev_1 & not_sev_2) |
---|
| 360 | { |
---|
| 361 | pDivisibleBy_ShortFalse++; |
---|
| 362 | if (ret) |
---|
| 363 | dReportError("p1 divides p2, but sev's are wrong"); |
---|
| 364 | } |
---|
| 365 | return ret; |
---|
| 366 | } |
---|
| 367 | |
---|
| 368 | void pPrintDivisbleByStat() |
---|
| 369 | { |
---|
[6a2e9c] | 370 | Print("#Tests: %ld; #FALSE %ld(%ld); #SHORT %ld(%ld)\n", |
---|
[35aab3] | 371 | pDivisibleBy_number, |
---|
| 372 | pDivisibleBy_FALSE, (unsigned long) ((double)pDivisibleBy_FALSE*((double) 100)/(double)pDivisibleBy_number), |
---|
| 373 | pDivisibleBy_ShortFalse, (unsigned long) ((double)pDivisibleBy_ShortFalse*((double)100)/(double)pDivisibleBy_FALSE)); |
---|
| 374 | } |
---|
| 375 | |
---|
| 376 | #endif // PDEBUG |
---|
| 377 | |
---|
| 378 | #endif // PDEBUG_CC |
---|