]> www.ginac.de Git - ginac.git/blobdiff - ginac/normal.cpp
- pseries::coeff() now uses a binary instead of a linear search algorithm
[ginac.git] / ginac / normal.cpp
index ad49f9ac0cab4ca03292111d115063cbddba9748..a5ebeb8b691ee4b6d95355fff45a17d68d87a414 100644 (file)
@@ -1076,7 +1076,7 @@ static ex heur_gcd(const ex &a, const ex &b, ex *ca, ex *cb, sym_desc_vec::const
             if (divide_in_z(p, g, ca ? *ca : dummy, var) && divide_in_z(q, g, cb ? *cb : dummy, var)) {
                 g *= gc;
                 ex lc = g.lcoeff(*x);
-                if (is_ex_exactly_of_type(lc, numeric) && lc.compare(_ex0()) < 0)
+                if (is_ex_exactly_of_type(lc, numeric) && ex_to_numeric(lc).is_negative())
                     return -g;
                 else
                     return g;
@@ -1101,6 +1101,8 @@ static ex heur_gcd(const ex &a, const ex &b, ex *ca, ex *cb, sym_desc_vec::const
 
 ex gcd(const ex &a, const ex &b, ex *ca, ex *cb, bool check_args)
 {
+//clog << "gcd(" << a << "," << b << ")\n";
+
        // Partially factored cases (to avoid expanding large expressions)
        if (is_ex_exactly_of_type(a, mul)) {
                if (is_ex_exactly_of_type(b, mul) && b.nops() > a.nops())
@@ -1442,13 +1444,15 @@ static ex frac_cancel(const ex &n, const ex &d)
        // as defined by get_first_symbol() is made positive)
        const symbol *x;
        if (get_first_symbol(den, x)) {
-               if (den.unit(*x).compare(_ex0()) < 0) {
+                GINAC_ASSERT(is_ex_exactly_of_type(den.unit(*x),numeric));
+               if (ex_to_numeric(den.unit(*x)).is_negative()) {
                        num *= _ex_1();
                        den *= _ex_1();
                }
        }
 
        // Return result as list
+//clog << " returns num = " << num << ", den = " << den << ", pre_factor = " << pre_factor << endl;
     return (new lst(num * pre_factor.numer(), den * pre_factor.denom()))->setflag(status_flags::dynallocated);
 }
 
@@ -1479,7 +1483,7 @@ ex add::normal(lst &sym_lst, lst &repl_lst, int level) const
                        // split it into numerator and denominator
                        GINAC_ASSERT(ex_to_numeric(ex_to_add(n.op(0)).overall_coeff).is_rational());
                        numeric overall = ex_to_numeric(ex_to_add(n.op(0)).overall_coeff);
-            o.push_back((new lst(overall.numer(), overall.denom()))->setflag(status_flags::dynallocated));
+            o.push_back((new lst(overall.numer(), overall.denom() * n.op(1)))->setflag(status_flags::dynallocated));
         } else
             o.push_back(n);
         it++;
@@ -1491,10 +1495,13 @@ ex add::normal(lst &sym_lst, lst &repl_lst, int level) const
     // Determine common denominator
     ex den = _ex1();
     exvector::const_iterator ait = o.begin(), aitend = o.end();
+//clog << "add::normal uses the following summands:\n";
     while (ait != aitend) {
+//clog << " num = " << ait->op(0) << ", den = " << ait->op(1) << endl;
         den = lcm(ait->op(1), den, false);
         ait++;
     }
+//clog << " common denominator = " << den << endl;
 
     // Add fractions
     if (den.is_equal(_ex1())) {
@@ -1516,7 +1523,7 @@ ex add::normal(lst &sym_lst, lst &repl_lst, int level) const
                 // should not happen
                 throw(std::runtime_error("invalid expression in add::normal, division failed"));
             }
-            num_seq.push_back(ait->op(0) * q);
+            num_seq.push_back((ait->op(0) * q).expand());
         }
         ex num = (new add(num_seq))->setflag(status_flags::dynallocated);