]> www.ginac.de Git - ginac.git/blobdiff - ginac/normal.cpp
gcd_pf_{pow, mul}: don't check if the arguments are polynomials.
[ginac.git] / ginac / normal.cpp
index 0227f4e3fa0c08efd83ec491a9a3ed3189f7cf1f..e97a7ce4a2c19c7f530881e5c1257e7562f20604 100644 (file)
@@ -1417,11 +1417,11 @@ static bool heur_gcd(ex& res, const ex& a, const ex& b, ex *ca, ex *cb,
 
 // gcd helper to handle partially factored polynomials (to avoid expanding
 // large expressions). At least one of the arguments should be a power.
-static ex gcd_pf_pow(const ex& a, const ex& b, ex* ca, ex* cb, bool check_args);
+static ex gcd_pf_pow(const ex& a, const ex& b, ex* ca, ex* cb);
 
 // gcd helper to handle partially factored polynomials (to avoid expanding
 // large expressions). At least one of the arguments should be a product.
-static ex gcd_pf_mul(const ex& a, const ex& b, ex* ca, ex* cb, bool check_args);
+static ex gcd_pf_mul(const ex& a, const ex& b, ex* ca, ex* cb);
 
 /** Compute GCD (Greatest Common Divisor) of multivariate polynomials a(X)
  *  and b(X) in Z[X]. Optionally also compute the cofactors of a and b,
@@ -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);
 #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);
 #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)) {
@@ -1635,7 +1639,7 @@ ex gcd(const ex &a, const ex &b, ex *ca, ex *cb, bool check_args, unsigned optio
        return g;
 }
 
-static ex gcd_pf_pow(const ex& a, const ex& b, ex* ca, ex* cb, bool check_args)
+static ex gcd_pf_pow(const ex& a, const ex& b, ex* ca, ex* cb)
 {
        if (is_exactly_a<power>(a)) {
                ex p = a.op(0);
@@ -1660,7 +1664,7 @@ static ex gcd_pf_pow(const ex& a, const ex& b, ex* ca, ex* cb, bool check_args)
                                }
                        } else {
                                ex p_co, pb_co;
-                               ex p_gcd = gcd(p, pb, &p_co, &pb_co, check_args);
+                               ex p_gcd = gcd(p, pb, &p_co, &pb_co, false);
                                if (p_gcd.is_equal(_ex1)) {
                                        // a(x) = p(x)^n, b(x) = p_b(x)^m, gcd (p, p_b) = 1 ==>
                                        // gcd(a,b) = 1
@@ -1740,47 +1744,31 @@ static ex gcd_pf_pow(const ex& a, const ex& b, ex* ca, ex* cb, bool check_args)
        }
 }
 
-static ex gcd_pf_mul(const ex& a, const ex& b, ex* ca, ex* cb, bool check_args)
+static ex gcd_pf_mul(const ex& a, const ex& b, ex* ca, ex* cb)
 {
-       if (is_exactly_a<mul>(a)) {
-               if (is_exactly_a<mul>(b) && b.nops() > a.nops())
-                       goto factored_b;
-factored_a:
-               size_t num = a.nops();
-               exvector g; g.reserve(num);
-               exvector acc_ca; acc_ca.reserve(num);
-               ex part_b = b;
-               for (size_t i=0; i<num; i++) {
-                       ex part_ca, part_cb;
-                       g.push_back(gcd(a.op(i), part_b, &part_ca, &part_cb, check_args));
-                       acc_ca.push_back(part_ca);
-                       part_b = part_cb;
-               }
-               if (ca)
-                       *ca = (new mul(acc_ca))->setflag(status_flags::dynallocated);
-               if (cb)
-                       *cb = part_b;
-               return (new mul(g))->setflag(status_flags::dynallocated);
-       } else if (is_exactly_a<mul>(b)) {
-               if (is_exactly_a<mul>(a) && a.nops() > b.nops())
-                       goto factored_a;
-factored_b:
-               size_t num = b.nops();
-               exvector g; g.reserve(num);
-               exvector acc_cb; acc_cb.reserve(num);
-               ex part_a = a;
-               for (size_t i=0; i<num; i++) {
-                       ex part_ca, part_cb;
-                       g.push_back(gcd(part_a, b.op(i), &part_ca, &part_cb, check_args));
-                       acc_cb.push_back(part_cb);
-                       part_a = part_ca;
-               }
-               if (ca)
-                       *ca = part_a;
-               if (cb)
-                       *cb = (new mul(acc_cb))->setflag(status_flags::dynallocated);
-               return (new mul(g))->setflag(status_flags::dynallocated);
+       if (is_exactly_a<mul>(a) && is_exactly_a<mul>(b)
+                                && (b.nops() >  a.nops()))
+               return gcd_pf_mul(b, a, cb, ca);
+
+       if (is_exactly_a<mul>(b) && (!is_exactly_a<mul>(a)))
+               return gcd_pf_mul(b, a, cb, ca);
+
+       GINAC_ASSERT(is_exactly_a<mul>(a));
+       size_t num = a.nops();
+       exvector g; g.reserve(num);
+       exvector acc_ca; acc_ca.reserve(num);
+       ex part_b = b;
+       for (size_t i=0; i<num; i++) {
+               ex part_ca, part_cb;
+               g.push_back(gcd(a.op(i), part_b, &part_ca, &part_cb, false));
+               acc_ca.push_back(part_ca);
+               part_b = part_cb;
        }
+       if (ca)
+               *ca = (new mul(acc_ca))->setflag(status_flags::dynallocated);
+       if (cb)
+               *cb = part_b;
+       return (new mul(g))->setflag(status_flags::dynallocated);
 }
 
 /** Compute LCM (Least Common Multiple) of multivariate polynomials in Z[X].