X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=blobdiff_plain;f=ginac%2Fnormal.cpp;h=09773d37489693a4854964f129002abb186b2c5b;hp=18d606b1c9f77f571a1206dddc7de7876d3cfd0c;hb=c7d9b624cb4bd5658a36fbcac2f73dbdb99edd5f;hpb=7d870583a6bf21a2ffb7b6f051b702064623892e diff --git a/ginac/normal.cpp b/ginac/normal.cpp index 18d606b1..09773d37 100644 --- a/ginac/normal.cpp +++ b/ginac/normal.cpp @@ -1268,17 +1268,19 @@ class gcdheu_failed {}; * polynomials and an iterator to the first element of the sym_desc vector * passed in. This function is used internally by gcd(). * - * @param a first multivariate polynomial (expanded) - * @param b second multivariate polynomial (expanded) + * @param a first integer multivariate polynomial (expanded) + * @param b second integer multivariate polynomial (expanded) * @param ca cofactor of polynomial a (returned), NULL to suppress * calculation of cofactor * @param cb cofactor of polynomial b (returned), NULL to suppress * calculation of cofactor * @param var iterator to first element of vector of sym_desc structs - * @return the GCD as a new expression + * @param res the GCD (returned) + * @return true if GCD was computed, false otherwise. * @see gcd * @exception gcdheu_failed() */ -static ex heur_gcd(const ex &a, const ex &b, ex *ca, ex *cb, sym_desc_vec::const_iterator var) +static bool heur_gcd_z(ex& res, const ex &a, const ex &b, ex *ca, ex *cb, + sym_desc_vec::const_iterator var) { #if STATISTICS heur_gcd_called++; @@ -1286,7 +1288,7 @@ static ex heur_gcd(const ex &a, const ex &b, ex *ca, ex *cb, sym_desc_vec::const // Algorithm only works for non-vanishing input polynomials if (a.is_zero() || b.is_zero()) - return (new fail())->setflag(status_flags::dynallocated); + return false; // GCD of two numeric values -> CLN if (is_exactly_a(a) && is_exactly_a(b)) { @@ -1295,7 +1297,8 @@ static ex heur_gcd(const ex &a, const ex &b, ex *ca, ex *cb, sym_desc_vec::const *ca = ex_to(a) / g; if (cb) *cb = ex_to(b) / g; - return g; + res = g; + return true; } // The first symbol is our main variable @@ -1325,9 +1328,13 @@ static ex heur_gcd(const ex &a, const ex &b, ex *ca, ex *cb, sym_desc_vec::const // Apply evaluation homomorphism and calculate GCD ex cp, cq; - ex gamma = heur_gcd(p.subs(x == xi, subs_options::no_pattern), q.subs(x == xi, subs_options::no_pattern), &cp, &cq, var+1).expand(); - if (!is_exactly_a(gamma)) { - + ex gamma; + bool found = heur_gcd_z(gamma, + p.subs(x == xi, subs_options::no_pattern), + q.subs(x == xi, subs_options::no_pattern), + &cp, &cq, var+1); + if (found) { + gamma = gamma.expand(); // Reconstruct polynomial from GCD of mapped polynomials ex g = interpolate(gamma, xi, x, maxdeg); @@ -1338,16 +1345,83 @@ static ex heur_gcd(const ex &a, const ex &b, ex *ca, ex *cb, sym_desc_vec::const ex dummy; if (divide_in_z(p, g, ca ? *ca : dummy, var) && divide_in_z(q, g, cb ? *cb : dummy, var)) { g *= gc; - return g; + res = g; + return true; } } // Next evaluation point xi = iquo(xi * isqrt(isqrt(xi)) * numeric(73794), numeric(27011)); } - return (new fail())->setflag(status_flags::dynallocated); + return false; } +/** Compute GCD of multivariate polynomials using the heuristic GCD algorithm. + * get_symbol_stats() must have been called previously with the input + * polynomials and an iterator to the first element of the sym_desc vector + * passed in. This function is used internally by gcd(). + * + * @param a first rational multivariate polynomial (expanded) + * @param b second rational multivariate polynomial (expanded) + * @param ca cofactor of polynomial a (returned), NULL to suppress + * calculation of cofactor + * @param cb cofactor of polynomial b (returned), NULL to suppress + * calculation of cofactor + * @param var iterator to first element of vector of sym_desc structs + * @param res the GCD (returned) + * @return true if GCD was computed, false otherwise. + * @see heur_gcd_z + * @see gcd + */ +static bool heur_gcd(ex& res, const ex& a, const ex& b, ex *ca, ex *cb, + sym_desc_vec::const_iterator var) +{ + if (a.info(info_flags::integer_polynomial) && + b.info(info_flags::integer_polynomial)) { + try { + return heur_gcd_z(res, a, b, ca, cb, var); + } catch (gcdheu_failed) { + return false; + } + } + + // convert polynomials to Z[X] + const numeric a_lcm = lcm_of_coefficients_denominators(a); + const numeric ab_lcm = lcmcoeff(b, a_lcm); + + const ex ai = a*ab_lcm; + const ex bi = b*ab_lcm; + if (!ai.info(info_flags::integer_polynomial)) + throw std::logic_error("heur_gcd: not an integer polynomial [1]"); + + if (!bi.info(info_flags::integer_polynomial)) + throw std::logic_error("heur_gcd: not an integer polynomial [2]"); + + bool found = false; + try { + found = heur_gcd_z(res, ai, bi, ca, cb, var); + } catch (gcdheu_failed) { + return false; + } + + // GCD is not unique, it's defined up to a unit (i.e. invertible + // element). If the coefficient ring is a field, every its element is + // invertible, so one can multiply the polynomial GCD with any element + // of the coefficient field. We use this ambiguity to make cofactors + // integer polynomials. + if (found) + res /= ab_lcm; + return found; +} + + +// 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); + +// 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); /** 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, @@ -1360,7 +1434,7 @@ static ex heur_gcd(const ex &a, const ex &b, ex *ca, ex *cb, sym_desc_vec::const * @param check_args check whether a and b are polynomials with rational * coefficients (defaults to "true") * @return the GCD as a new expression */ -ex gcd(const ex &a, const ex &b, ex *ca, ex *cb, bool check_args) +ex gcd(const ex &a, const ex &b, ex *ca, ex *cb, bool check_args, unsigned options) { #if STATISTICS gcd_called++; @@ -1391,150 +1465,14 @@ ex gcd(const ex &a, const ex &b, ex *ca, ex *cb, bool check_args) } // Partially factored cases (to avoid expanding large expressions) - if (is_exactly_a(a)) { - if (is_exactly_a(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; isetflag(status_flags::dynallocated); - if (cb) - *cb = part_b; - return (new mul(g))->setflag(status_flags::dynallocated); - } else if (is_exactly_a(b)) { - if (is_exactly_a(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; isetflag(status_flags::dynallocated); - return (new mul(g))->setflag(status_flags::dynallocated); - } - + if (!(options & gcd_options::no_part_factored)) { + if (is_exactly_a(a) || is_exactly_a(b)) + return gcd_pf_mul(a, b, ca, cb); #if FAST_COMPARE - // Input polynomials of the form poly^n are sometimes also trivial - if (is_exactly_a(a)) { - ex p = a.op(0); - const ex& exp_a = a.op(1); - if (is_exactly_a(b)) { - ex pb = b.op(0); - const ex& exp_b = b.op(1); - if (p.is_equal(pb)) { - // a = p^n, b = p^m, gcd = p^min(n, m) - if (exp_a < exp_b) { - if (ca) - *ca = _ex1; - if (cb) - *cb = power(p, exp_b - exp_a); - return power(p, exp_a); - } else { - if (ca) - *ca = power(p, exp_a - exp_b); - if (cb) - *cb = _ex1; - return power(p, exp_b); - } - } else { - ex p_co, pb_co; - ex p_gcd = gcd(p, pb, &p_co, &pb_co, check_args); - 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 - if (ca) - *ca = a; - if (cb) - *cb = b; - return _ex1; - // XXX: do I need to check for p_gcd = -1? - } else { - // there are common factors: - // a(x) = g(x)^n A(x)^n, b(x) = g(x)^m B(x)^m ==> - // gcd(a, b) = g(x)^n gcd(A(x)^n, g(x)^(n-m) B(x)^m - if (exp_a < exp_b) { - return power(p_gcd, exp_a)* - gcd(power(p_co, exp_a), power(p_gcd, exp_b-exp_a)*power(pb_co, exp_b), ca, cb, false); - } else { - return power(p_gcd, exp_b)* - gcd(power(p_gcd, exp_a - exp_b)*power(p_co, exp_a), power(pb_co, exp_b), ca, cb, false); - } - } // p_gcd.is_equal(_ex1) - } // p.is_equal(pb) - - } else { - if (p.is_equal(b)) { - // a = p^n, b = p, gcd = p - if (ca) - *ca = power(p, a.op(1) - 1); - if (cb) - *cb = _ex1; - return p; - } - - ex p_co, bpart_co; - ex p_gcd = gcd(p, b, &p_co, &bpart_co, false); - - if (p_gcd.is_equal(_ex1)) { - // a(x) = p(x)^n, gcd(p, b) = 1 ==> gcd(a, b) = 1 - if (ca) - *ca = a; - if (cb) - *cb = b; - return _ex1; - } else { - // a(x) = g(x)^n A(x)^n, b(x) = g(x) B(x) ==> gcd(a, b) = g(x) gcd(g(x)^(n-1) A(x)^n, B(x)) - return p_gcd*gcd(power(p_gcd, exp_a-1)*power(p_co, exp_a), bpart_co, ca, cb, false); - } - } // is_exactly_a(b) - - } else if (is_exactly_a(b)) { - ex p = b.op(0); - if (p.is_equal(a)) { - // a = p, b = p^n, gcd = p - if (ca) - *ca = _ex1; - if (cb) - *cb = power(p, b.op(1) - 1); - return p; - } - - ex p_co, apart_co; - const ex& exp_b(b.op(1)); - ex p_gcd = gcd(a, p, &apart_co, &p_co, false); - if (p_gcd.is_equal(_ex1)) { - // b=p(x)^n, gcd(a, p) = 1 ==> gcd(a, b) == 1 - if (ca) - *ca = a; - if (cb) - *cb = b; - return _ex1; - } else { - // there are common factors: - // a(x) = g(x) A(x), b(x) = g(x)^n B(x)^n ==> gcd = g(x) gcd(g(x)^(n-1) A(x)^n, B(x)) - - return p_gcd*gcd(apart_co, power(p_gcd, exp_b-1)*power(p_co, exp_b), ca, cb, false); - } // p_gcd.is_equal(_ex1) - } + if (is_exactly_a(a) || is_exactly_a(b)) + return gcd_pf_pow(a, b, ca, cb); #endif + } // Some trivial cases ex aex = a.expand(), bex = b.expand(); @@ -1665,41 +1603,155 @@ factored_b: // Try heuristic algorithm first, fall back to PRS if that failed ex g; - try { - g = heur_gcd(aex, bex, ca, cb, var); - } catch (gcdheu_failed) { - g = fail(); - } - if (is_exactly_a(g)) { + 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; + } #if STATISTICS - heur_gcd_failed++; + else { + heur_gcd_failed++; + } #endif - g = sr_gcd(aex, bex, var); - if (g.is_equal(_ex1)) { - // Keep cofactors factored if possible + } + + 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; +} + +// gcd helper to handle partially factored polynomials (to avoid expanding +// large expressions). Both arguments should be powers. +static ex gcd_pf_pow_pow(const ex& a, const ex& b, ex* ca, ex* cb) +{ + ex p = a.op(0); + const ex& exp_a = a.op(1); + ex pb = b.op(0); + const ex& exp_b = b.op(1); + + // a = p^n, b = p^m, gcd = p^min(n, m) + if (p.is_equal(pb)) { + if (exp_a < exp_b) { if (ca) - *ca = a; + *ca = _ex1; if (cb) - *cb = b; + *cb = power(p, exp_b - exp_a); + return power(p, exp_a); } else { if (ca) - divide(aex, g, *ca, false); + *ca = power(p, exp_a - exp_b); if (cb) - divide(bex, g, *cb, false); + *cb = _ex1; + return power(p, exp_b); } - } else { - if (g.is_equal(_ex1)) { - // Keep cofactors factored if possible + } + + ex p_co, pb_co; + ex p_gcd = gcd(p, pb, &p_co, &pb_co, false); + // a(x) = p(x)^n, b(x) = p_b(x)^m, gcd (p, p_b) = 1 ==> gcd(a,b) = 1 + if (p_gcd.is_equal(_ex1)) { if (ca) *ca = a; if (cb) *cb = b; - } + return _ex1; + // XXX: do I need to check for p_gcd = -1? } - return g; + // there are common factors: + // a(x) = g(x)^n A(x)^n, b(x) = g(x)^m B(x)^m ==> + // gcd(a, b) = g(x)^n gcd(A(x)^n, g(x)^(n-m) B(x)^m + if (exp_a < exp_b) { + ex pg = gcd(power(p_co, exp_a), power(p_gcd, exp_b-exp_a)*power(pb_co, exp_b), ca, cb, false); + return power(p_gcd, exp_a)*pg; + } else { + ex pg = gcd(power(p_gcd, exp_a - exp_b)*power(p_co, exp_a), power(pb_co, exp_b), ca, cb, false); + return power(p_gcd, exp_b)*pg; + } +} + +static ex gcd_pf_pow(const ex& a, const ex& b, ex* ca, ex* cb) +{ + if (is_exactly_a(a) && is_exactly_a(b)) + return gcd_pf_pow_pow(a, b, ca, cb); + + if (is_exactly_a(b) && (! is_exactly_a(a))) + return gcd_pf_pow(b, a, cb, ca); + + GINAC_ASSERT(is_exactly_a(a)); + + ex p = a.op(0); + const ex& exp_a = a.op(1); + if (p.is_equal(b)) { + // a = p^n, b = p, gcd = p + if (ca) + *ca = power(p, a.op(1) - 1); + if (cb) + *cb = _ex1; + return p; + } + + ex p_co, bpart_co; + ex p_gcd = gcd(p, b, &p_co, &bpart_co, false); + + // a(x) = p(x)^n, gcd(p, b) = 1 ==> gcd(a, b) = 1 + if (p_gcd.is_equal(_ex1)) { + if (ca) + *ca = a; + if (cb) + *cb = b; + return _ex1; + } + // a(x) = g(x)^n A(x)^n, b(x) = g(x) B(x) ==> gcd(a, b) = g(x) gcd(g(x)^(n-1) A(x)^n, B(x)) + ex rg = gcd(power(p_gcd, exp_a-1)*power(p_co, exp_a), bpart_co, ca, cb, false); + return p_gcd*rg; } +static ex gcd_pf_mul(const ex& a, const ex& b, ex* ca, ex* cb) +{ + if (is_exactly_a(a) && is_exactly_a(b) + && (b.nops() > a.nops())) + return gcd_pf_mul(b, a, cb, ca); + + if (is_exactly_a(b) && (!is_exactly_a(a))) + return gcd_pf_mul(b, a, cb, ca); + + GINAC_ASSERT(is_exactly_a(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; isetflag(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]. *