gcd(): allow user to override (some of) heuristics.
authorAlexei Sheplyakov <varg@theor.jinr.ru>
Mon, 25 Aug 2008 12:54:46 +0000 (16:54 +0400)
committerJens Vollinga <jensv@nikhef.nl>
Wed, 27 Aug 2008 14:22:59 +0000 (16:22 +0200)
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

index 0227f4e3fa0c08efd83ec491a9a3ed3189f7cf1f..5d044fc6f40f6da06a6aaff3b12c670115cd7c3a 100644 (file)
@@ -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<mul>(a) || is_exactly_a<mul>(b))
-               return gcd_pf_mul(a, b, ca, cb, check_args);
+       if (!(options & gcd_options::no_part_factored)) {
+               if (is_exactly_a<mul>(a) || is_exactly_a<mul>(b))
+                       return gcd_pf_mul(a, b, ca, cb, check_args);
 #if FAST_COMPARE
-       if (is_exactly_a<power>(a) || is_exactly_a<power>(b))
-               return gcd_pf_pow(a, b, ca, cb, check_args);
+               if (is_exactly_a<power>(a) || is_exactly_a<power>(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)) {