[GiNaC-devel] [PATCH 05/10] gcd(): allow user to override (some of) heuristics.

Alexei Sheplyakov varg at theor.jinr.ru
Mon Aug 25 14:54:46 CEST 2008


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 files changed, 22 insertions(+), 18 deletions(-)

diff --git a/ginac/normal.cpp b/ginac/normal.cpp
index 0227f4e..5d044fc 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<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)) {
-- 
1.5.6

Best regards,
	Alexei

-- 
All science is either physics or stamp collecting.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 827 bytes
Desc: Digital signature
Url : http://www.cebix.net/pipermail/ginac-devel/attachments/20080825/eaba0577/attachment-0001.sig 


More information about the GiNaC-devel mailing list