From c77689e7ac8d8f4dbca0f337b6e9acf2419010ff Mon Sep 17 00:00:00 2001 From: Alexei Sheplyakov Date: Mon, 25 Aug 2008 16:54:46 +0400 Subject: [PATCH] gcd(): allow user to override (some of) heuristics. GiNaC tries to avoid expanding expressions while computing GCDs and applies a number of heuristics. Usually this improves performance, but in some cases it doesn't. Allow user to switch off heuristics. Part 5: * gcd(): don't use heuristic GCD algorithm if gcd_options::no_heur_gcd flag is set. * gcd(): don't handle partially factored expressions in a special way if gcd_options::no_part_factored flag is set. --- ginac/normal.cpp | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/ginac/normal.cpp b/ginac/normal.cpp index 0227f4e3..5d044fc6 100644 --- a/ginac/normal.cpp +++ b/ginac/normal.cpp @@ -1465,12 +1465,14 @@ ex gcd(const ex &a, const ex &b, ex *ca, ex *cb, bool check_args, unsigned optio } // Partially factored cases (to avoid expanding large expressions) - if (is_exactly_a(a) || is_exactly_a(b)) - return gcd_pf_mul(a, b, ca, cb, check_args); + if (!(options & gcd_options::no_part_factored)) { + if (is_exactly_a(a) || is_exactly_a(b)) + return gcd_pf_mul(a, b, ca, cb, check_args); #if FAST_COMPARE - if (is_exactly_a(a) || is_exactly_a(b)) - return gcd_pf_pow(a, b, ca, cb, check_args); + if (is_exactly_a(a) || is_exactly_a(b)) + return gcd_pf_pow(a, b, ca, cb, check_args); #endif + } // Some trivial cases ex aex = a.expand(), bex = b.expand(); @@ -1601,23 +1603,25 @@ ex gcd(const ex &a, const ex &b, ex *ca, ex *cb, bool check_args, unsigned optio // Try heuristic algorithm first, fall back to PRS if that failed ex g; - bool found = heur_gcd(g, aex, bex, ca, cb, var); - if (found) { - // heur_gcd have already computed cofactors... - if (g.is_equal(_ex1)) { - // ... but we want to keep them factored if possible. - if (ca) - *ca = a; - if (cb) - *cb = b; + if (!(options & gcd_options::no_heur_gcd)) { + bool found = heur_gcd(g, aex, bex, ca, cb, var); + if (found) { + // heur_gcd have already computed cofactors... + if (g.is_equal(_ex1)) { + // ... but we want to keep them factored if possible. + if (ca) + *ca = a; + if (cb) + *cb = b; + } + return g; } - return g; - } #if STATISTICS - else { - heur_gcd_failed++; - } + else { + heur_gcd_failed++; + } #endif + } g = sr_gcd(aex, bex, var); if (g.is_equal(_ex1)) { -- 2.44.0