[GiNaC-devel] [PATCH 04/10] refactor gcd() a little bit (no functional changes).

Alexei Sheplyakov varg at theor.jinr.ru
Mon Aug 25 14:54:10 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 4:

refactor gcd() a little bit, so subsequent patch(es) won't be so big and ugly.
---
 ginac/normal.cpp |   41 ++++++++++++++++++++++-------------------
 1 files changed, 22 insertions(+), 19 deletions(-)

diff --git a/ginac/normal.cpp b/ginac/normal.cpp
index 0cb9100..0227f4e 100644
--- a/ginac/normal.cpp
+++ b/ginac/normal.cpp
@@ -1602,33 +1602,36 @@ 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) {
-#if STATISTICS
-		heur_gcd_failed++;
-#endif
-		g = sr_gcd(aex, bex, var);
-		if (g.is_equal(_ex1)) {
-			// Keep cofactors factored if possible
-			if (ca)
-				*ca = a;
-			if (cb)
-				*cb = b;
-		} else {
-			if (ca)
-				divide(aex, g, *ca, false);
-			if (cb)
-				divide(bex, g, *cb, false);
-		}
-	} else {
+	if (found) {
+		// heur_gcd have already computed cofactors...
 		if (g.is_equal(_ex1)) {
-			// Keep cofactors factored if possible
+			// ... but we want to keep them factored if possible.
 			if (ca)
 				*ca = a;
 			if (cb)
 				*cb = b;
 		}
+		return g;
 	}
+#if STATISTICS
+	else {
+		heur_gcd_failed++;
+	}
+#endif
 
+	g = sr_gcd(aex, bex, var);
+	if (g.is_equal(_ex1)) {
+		// Keep cofactors factored if possible
+		if (ca)
+			*ca = a;
+		if (cb)
+			*cb = b;
+	} else {
+		if (ca)
+			divide(aex, g, *ca, false);
+		if (cb)
+			divide(bex, g, *cb, false);
+	}
 	return g;
 }
 
-- 
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/4637232b/attachment.sig 


More information about the GiNaC-devel mailing list