]> www.ginac.de Git - ginac.git/blobdiff - ginac/normal.cpp
- fixed bug in normal(): normal(x^a) became (x^(-a))^(-1)
[ginac.git] / ginac / normal.cpp
index ad49f9ac0cab4ca03292111d115063cbddba9748..d23b2289d520aa535bd0dd6fb43b6e43080536a7 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);
 
@@ -1567,19 +1574,20 @@ ex power::normal(lst &sym_lst, lst &repl_lst, int level) const
                        // (a/b)^n -> {a^n, b^n}
                        return (new lst(power(n.op(0), exponent), power(n.op(1), exponent)))->setflag(status_flags::dynallocated);
 
-               } else if (exponent.info(info_flags::negint)) {
+               } else if (exponent.info(info_flags::negative)) {
 
                        // (a/b)^-n -> {b^n, a^n}
                        return (new lst(power(n.op(1), -exponent), power(n.op(0), -exponent)))->setflag(status_flags::dynallocated);
                }
 
        } else {
+
                if (exponent.info(info_flags::positive)) {
 
-                       // (a/b)^z -> {sym((a/b)^z), 1}
+                       // (a/b)^x -> {sym((a/b)^x), 1}
                        return (new lst(replace_with_symbol(power(n.op(0) / n.op(1), exponent), sym_lst, repl_lst), _ex1()))->setflag(status_flags::dynallocated);
 
-               } else {
+               } else if (exponent.info(info_flags::negative)) {
 
                        if (n.op(1).is_equal(_ex1())) {
 
@@ -1588,9 +1596,14 @@ ex power::normal(lst &sym_lst, lst &repl_lst, int level) const
 
                        } else {
 
-                               // (a/b)^-x -> {(b/a)^x, 1}
+                               // (a/b)^-x -> {sym((b/a)^x), 1}
                                return (new lst(replace_with_symbol(power(n.op(1) / n.op(0), -exponent), sym_lst, repl_lst), _ex1()))->setflag(status_flags::dynallocated);
                        }
+
+               } else {        // exponent not numeric
+
+                       // (a/b)^x -> {sym((a/b)^x, 1}
+                       return (new lst(replace_with_symbol(power(n.op(0) / n.op(1), exponent), sym_lst, repl_lst), _ex1()))->setflag(status_flags::dynallocated);
                }
     }
 }