c = d;
cdeg = ddeg;
//std::clog << " dividing...\n";
- if (!divide_in_z(r, ri * pow(psi, delta), d, var+1))
+ if (!divide_in_z(r, ri * pow(psi, delta), d, var))
throw(std::runtime_error("invalid expression in sr_gcd(), division failed"));
ddeg = d.degree(x);
if (ddeg == 0) {
heur_gcd_called++;
#endif
+ // Algorithms only works for non-vanishing input polynomials
+ if (a.is_zero() || b.is_zero())
+ return *new ex(fail());
+
// GCD of two numeric values -> CLN
if (is_ex_exactly_of_type(a, numeric) && is_ex_exactly_of_type(b, numeric)) {
numeric g = gcd(ex_to_numeric(a), ex_to_numeric(b));
- numeric rg;
- if (ca || cb)
- rg = g.inverse();
if (ca)
- *ca = ex_to_numeric(a).mul(rg);
+ *ca = ex_to_numeric(a) / g;
if (cb)
- *cb = ex_to_numeric(b).mul(rg);
+ *cb = ex_to_numeric(b) / g;
return g;
}
// GCD of numerics -> CLN
if (is_ex_exactly_of_type(a, numeric) && is_ex_exactly_of_type(b, numeric)) {
numeric g = gcd(ex_to_numeric(a), ex_to_numeric(b));
- if (ca)
- *ca = ex_to_numeric(a) / g;
- if (cb)
- *cb = ex_to_numeric(b) / g;
+ if (ca || cb) {
+ if (g.is_zero()) {
+ if (ca)
+ *ca = _ex0();
+ if (cb)
+ *cb = _ex0();
+ } else {
+ if (ca)
+ *ca = ex_to_numeric(a) / g;
+ if (cb)
+ *cb = ex_to_numeric(b) / g;
+ }
+ }
return g;
}
// Add fractions
if (den.is_equal(_ex1())) {
- // Common denominator is 1, simply add all numerators
+ // Common denominator is 1, simply add all fractions
exvector num_seq;
for (ait=o.begin(); ait!=aitend; ait++) {
- num_seq.push_back(ait->op(0));
+ num_seq.push_back(ait->op(0) / ait->op(1));
}
return (new lst((new add(num_seq))->setflag(status_flags::dynallocated), den))->setflag(status_flags::dynallocated);