]> www.ginac.de Git - ginac.git/blobdiff - ginac/normal.cpp
normal() uses an additional reverse lookup map
[ginac.git] / ginac / normal.cpp
index cd2ad8eca5a0b29024bc142e81c19866f031bea7..2603086a743b495b84033855ef59c822aca7af97 100644 (file)
@@ -730,12 +730,12 @@ static bool divide_in_z(const ex &a, const ex &b, ex &q, sym_desc_vec::const_ite
        numeric point = _num0;
        ex c;
        for (i=0; i<=adeg; i++) {
-               ex bs = b.subs(*x == point);
+               ex bs = b.subs(*x == point, subs_options::no_pattern);
                while (bs.is_zero()) {
                        point += _num1;
-                       bs = b.subs(*x == point);
+                       bs = b.subs(*x == point, subs_options::no_pattern);
                }
-               if (!divide_in_z(a.subs(*x == point), bs, c, var+1))
+               if (!divide_in_z(a.subs(*x == point, subs_options::no_pattern), bs, c, var+1))
                        return false;
                alpha.push_back(point);
                u.push_back(c);
@@ -1187,7 +1187,7 @@ 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), q.subs(x == xi), &cp, &cq, var+1).expand();
+               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<fail>(gamma)) {
 
                        // Reconstruct polynomial from GCD of mapped polynomials
@@ -1681,25 +1681,23 @@ ex sqrfree_parfrac(const ex & a, const symbol & x)
 
 
 /** Create a symbol for replacing the expression "e" (or return a previously
- *  assigned symbol). The symbol is appended to sym_lst and returned, the
- *  expression is appended to repl_lst.
+ *  assigned symbol). The symbol and expression are appended to repl, for
+ *  a later application of subs().
  *  @see ex::normal */
-static ex replace_with_symbol(const ex &e, lst &sym_lst, lst &repl_lst)
+static ex replace_with_symbol(const ex & e, exmap & repl, exmap & rev_lookup)
 {
-       // Expression already in repl_lst? Then return the assigned symbol
-       lst::const_iterator its, itr;
-       for (its = sym_lst.begin(), itr = repl_lst.begin(); itr != repl_lst.end(); ++its, ++itr)
-               if (itr->is_equal(e))
-                       return *its;
+       // Expression already replaced? Then return the assigned symbol
+       exmap::const_iterator it = rev_lookup.find(e);
+       if (it != rev_lookup.end())
+               return it->second;
        
        // Otherwise create new symbol and add to list, taking care that the
-       // replacement expression doesn't contain symbols from the sym_lst
+       // replacement expression doesn't itself contain symbols from repl,
        // because subs() is not recursive
-       symbol s;
-       ex es(s);
-       ex e_replaced = e.subs(sym_lst, repl_lst);
-       sym_lst.append(es);
-       repl_lst.append(e_replaced);
+       ex es = (new symbol)->setflag(status_flags::dynallocated);
+       ex e_replaced = e.subs(repl, subs_options::no_pattern);
+       repl[es] = e_replaced;
+       rev_lookup[e_replaced] = es;
        return es;
 }
 
@@ -1708,7 +1706,7 @@ static ex replace_with_symbol(const ex &e, lst &sym_lst, lst &repl_lst)
  *  to repl_lst and the symbol is returned.
  *  @see basic::to_rational
  *  @see basic::to_polynomial */
-static ex replace_with_symbol(const ex &e, lst &repl_lst)
+static ex replace_with_symbol(const ex & e, lst & repl_lst)
 {
        // Expression already in repl_lst? Then return the assigned symbol
        for (lst::const_iterator it = repl_lst.begin(); it != repl_lst.end(); ++it)
@@ -1716,11 +1714,10 @@ static ex replace_with_symbol(const ex &e, lst &repl_lst)
                        return it->op(0);
        
        // Otherwise create new symbol and add to list, taking care that the
-       // replacement expression doesn't contain symbols from the sym_lst
+       // replacement expression doesn't itself contain symbols from the repl_lst,
        // because subs() is not recursive
-       symbol s;
-       ex es(s);
-       ex e_replaced = e.subs(repl_lst);
+       ex es = (new symbol)->setflag(status_flags::dynallocated);
+       ex e_replaced = e.subs(repl_lst, subs_options::no_pattern);
        repl_lst.append(es == e_replaced);
        return es;
 }
@@ -1736,18 +1733,18 @@ struct normal_map_function : public map_function {
 /** Default implementation of ex::normal(). It normalizes the children and
  *  replaces the object with a temporary symbol.
  *  @see ex::normal */
-ex basic::normal(lst &sym_lst, lst &repl_lst, int level) const
+ex basic::normal(exmap & repl, exmap & rev_lookup, int level) const
 {
        if (nops() == 0)
-               return (new lst(replace_with_symbol(*this, sym_lst, repl_lst), _ex1))->setflag(status_flags::dynallocated);
+               return (new lst(replace_with_symbol(*this, repl, rev_lookup), _ex1))->setflag(status_flags::dynallocated);
        else {
                if (level == 1)
-                       return (new lst(replace_with_symbol(*this, sym_lst, repl_lst), _ex1))->setflag(status_flags::dynallocated);
+                       return (new lst(replace_with_symbol(*this, repl, rev_lookup), _ex1))->setflag(status_flags::dynallocated);
                else if (level == -max_recursion_level)
                        throw(std::runtime_error("max recursion level reached"));
                else {
                        normal_map_function map_normal(level - 1);
-                       return (new lst(replace_with_symbol(map(map_normal), sym_lst, repl_lst), _ex1))->setflag(status_flags::dynallocated);
+                       return (new lst(replace_with_symbol(map(map_normal), repl, rev_lookup), _ex1))->setflag(status_flags::dynallocated);
                }
        }
 }
@@ -1755,7 +1752,7 @@ ex basic::normal(lst &sym_lst, lst &repl_lst, int level) const
 
 /** Implementation of ex::normal() for symbols. This returns the unmodified symbol.
  *  @see ex::normal */
-ex symbol::normal(lst &sym_lst, lst &repl_lst, int level) const
+ex symbol::normal(exmap & repl, exmap & rev_lookup, int level) const
 {
        return (new lst(*this, _ex1))->setflag(status_flags::dynallocated);
 }
@@ -1765,19 +1762,19 @@ ex symbol::normal(lst &sym_lst, lst &repl_lst, int level) const
  *  into re+I*im and replaces I and non-rational real numbers with a temporary
  *  symbol.
  *  @see ex::normal */
-ex numeric::normal(lst &sym_lst, lst &repl_lst, int level) const
+ex numeric::normal(exmap & repl, exmap & rev_lookup, int level) const
 {
        numeric num = numer();
        ex numex = num;
 
        if (num.is_real()) {
                if (!num.is_integer())
-                       numex = replace_with_symbol(numex, sym_lst, repl_lst);
+                       numex = replace_with_symbol(numex, repl, rev_lookup);
        } else { // complex
                numeric re = num.real(), im = num.imag();
-               ex re_ex = re.is_rational() ? re : replace_with_symbol(re, sym_lst, repl_lst);
-               ex im_ex = im.is_rational() ? im : replace_with_symbol(im, sym_lst, repl_lst);
-               numex = re_ex + im_ex * replace_with_symbol(I, sym_lst, repl_lst);
+               ex re_ex = re.is_rational() ? re : replace_with_symbol(re, repl, rev_lookup);
+               ex im_ex = im.is_rational() ? im : replace_with_symbol(im, repl, rev_lookup);
+               numex = re_ex + im_ex * replace_with_symbol(I, repl, rev_lookup);
        }
 
        // Denominator is always a real integer (see numeric::denom())
@@ -1849,10 +1846,10 @@ static ex frac_cancel(const ex &n, const ex &d)
 /** Implementation of ex::normal() for a sum. It expands terms and performs
  *  fractional addition.
  *  @see ex::normal */
-ex add::normal(lst &sym_lst, lst &repl_lst, int level) const
+ex add::normal(exmap & repl, exmap & rev_lookup, int level) const
 {
        if (level == 1)
-               return (new lst(replace_with_symbol(*this, sym_lst, repl_lst), _ex1))->setflag(status_flags::dynallocated);
+               return (new lst(replace_with_symbol(*this, repl, rev_lookup), _ex1))->setflag(status_flags::dynallocated);
        else if (level == -max_recursion_level)
                throw(std::runtime_error("max recursion level reached"));
 
@@ -1862,12 +1859,12 @@ ex add::normal(lst &sym_lst, lst &repl_lst, int level) const
        dens.reserve(seq.size()+1);
        epvector::const_iterator it = seq.begin(), itend = seq.end();
        while (it != itend) {
-               ex n = ex_to<basic>(recombine_pair_to_ex(*it)).normal(sym_lst, repl_lst, level-1);
+               ex n = ex_to<basic>(recombine_pair_to_ex(*it)).normal(repl, rev_lookup, level-1);
                nums.push_back(n.op(0));
                dens.push_back(n.op(1));
                it++;
        }
-       ex n = ex_to<numeric>(overall_coeff).normal(sym_lst, repl_lst, level-1);
+       ex n = ex_to<numeric>(overall_coeff).normal(repl, rev_lookup, level-1);
        nums.push_back(n.op(0));
        dens.push_back(n.op(1));
        GINAC_ASSERT(nums.size() == dens.size());
@@ -1908,10 +1905,10 @@ ex add::normal(lst &sym_lst, lst &repl_lst, int level) const
 /** Implementation of ex::normal() for a product. It cancels common factors
  *  from fractions.
  *  @see ex::normal() */
-ex mul::normal(lst &sym_lst, lst &repl_lst, int level) const
+ex mul::normal(exmap & repl, exmap & rev_lookup, int level) const
 {
        if (level == 1)
-               return (new lst(replace_with_symbol(*this, sym_lst, repl_lst), _ex1))->setflag(status_flags::dynallocated);
+               return (new lst(replace_with_symbol(*this, repl, rev_lookup), _ex1))->setflag(status_flags::dynallocated);
        else if (level == -max_recursion_level)
                throw(std::runtime_error("max recursion level reached"));
 
@@ -1921,12 +1918,12 @@ ex mul::normal(lst &sym_lst, lst &repl_lst, int level) const
        ex n;
        epvector::const_iterator it = seq.begin(), itend = seq.end();
        while (it != itend) {
-               n = ex_to<basic>(recombine_pair_to_ex(*it)).normal(sym_lst, repl_lst, level-1);
+               n = ex_to<basic>(recombine_pair_to_ex(*it)).normal(repl, rev_lookup, level-1);
                num.push_back(n.op(0));
                den.push_back(n.op(1));
                it++;
        }
-       n = ex_to<numeric>(overall_coeff).normal(sym_lst, repl_lst, level-1);
+       n = ex_to<numeric>(overall_coeff).normal(repl, rev_lookup, level-1);
        num.push_back(n.op(0));
        den.push_back(n.op(1));
 
@@ -1936,20 +1933,20 @@ ex mul::normal(lst &sym_lst, lst &repl_lst, int level) const
 }
 
 
-/** Implementation of ex::normal() for powers. It normalizes the basis,
+/** Implementation of ex::normal([B) for powers. It normalizes the basis,
  *  distributes integer exponents to numerator and denominator, and replaces
  *  non-integer powers by temporary symbols.
  *  @see ex::normal */
-ex power::normal(lst &sym_lst, lst &repl_lst, int level) const
+ex power::normal(exmap & repl, exmap & rev_lookup, int level) const
 {
        if (level == 1)
-               return (new lst(replace_with_symbol(*this, sym_lst, repl_lst), _ex1))->setflag(status_flags::dynallocated);
+               return (new lst(replace_with_symbol(*this, repl, rev_lookup), _ex1))->setflag(status_flags::dynallocated);
        else if (level == -max_recursion_level)
                throw(std::runtime_error("max recursion level reached"));
 
        // Normalize basis and exponent (exponent gets reassembled)
-       ex n_basis = ex_to<basic>(basis).normal(sym_lst, repl_lst, level-1);
-       ex n_exponent = ex_to<basic>(exponent).normal(sym_lst, repl_lst, level-1);
+       ex n_basis = ex_to<basic>(basis).normal(repl, rev_lookup, level-1);
+       ex n_exponent = ex_to<basic>(exponent).normal(repl, rev_lookup, level-1);
        n_exponent = n_exponent.op(0) / n_exponent.op(1);
 
        if (n_exponent.info(info_flags::integer)) {
@@ -1970,32 +1967,32 @@ ex power::normal(lst &sym_lst, lst &repl_lst, int level) const
                if (n_exponent.info(info_flags::positive)) {
 
                        // (a/b)^x -> {sym((a/b)^x), 1}
-                       return (new lst(replace_with_symbol(power(n_basis.op(0) / n_basis.op(1), n_exponent), sym_lst, repl_lst), _ex1))->setflag(status_flags::dynallocated);
+                       return (new lst(replace_with_symbol(power(n_basis.op(0) / n_basis.op(1), n_exponent), repl, rev_lookup), _ex1))->setflag(status_flags::dynallocated);
 
                } else if (n_exponent.info(info_flags::negative)) {
 
                        if (n_basis.op(1).is_equal(_ex1)) {
 
                                // a^-x -> {1, sym(a^x)}
-                               return (new lst(_ex1, replace_with_symbol(power(n_basis.op(0), -n_exponent), sym_lst, repl_lst)))->setflag(status_flags::dynallocated);
+                               return (new lst(_ex1, replace_with_symbol(power(n_basis.op(0), -n_exponent), repl, rev_lookup)))->setflag(status_flags::dynallocated);
 
                        } else {
 
                                // (a/b)^-x -> {sym((b/a)^x), 1}
-                               return (new lst(replace_with_symbol(power(n_basis.op(1) / n_basis.op(0), -n_exponent), sym_lst, repl_lst), _ex1))->setflag(status_flags::dynallocated);
+                               return (new lst(replace_with_symbol(power(n_basis.op(1) / n_basis.op(0), -n_exponent), repl, rev_lookup), _ex1))->setflag(status_flags::dynallocated);
                        }
                }
        }
 
        // (a/b)^x -> {sym((a/b)^x, 1}
-       return (new lst(replace_with_symbol(power(n_basis.op(0) / n_basis.op(1), n_exponent), sym_lst, repl_lst), _ex1))->setflag(status_flags::dynallocated);
+       return (new lst(replace_with_symbol(power(n_basis.op(0) / n_basis.op(1), n_exponent), repl, rev_lookup), _ex1))->setflag(status_flags::dynallocated);
 }
 
 
 /** Implementation of ex::normal() for pseries. It normalizes each coefficient
  *  and replaces the series by a temporary symbol.
  *  @see ex::normal */
-ex pseries::normal(lst &sym_lst, lst &repl_lst, int level) const
+ex pseries::normal(exmap & repl, exmap & rev_lookup, int level) const
 {
        epvector newseq;
        epvector::const_iterator i = seq.begin(), end = seq.end();
@@ -2006,7 +2003,7 @@ ex pseries::normal(lst &sym_lst, lst &repl_lst, int level) const
                ++i;
        }
        ex n = pseries(relational(var,point), newseq);
-       return (new lst(replace_with_symbol(n, sym_lst, repl_lst), _ex1))->setflag(status_flags::dynallocated);
+       return (new lst(replace_with_symbol(n, repl, rev_lookup), _ex1))->setflag(status_flags::dynallocated);
 }
 
 
@@ -2024,14 +2021,14 @@ ex pseries::normal(lst &sym_lst, lst &repl_lst, int level) const
  *  @return normalized expression */
 ex ex::normal(int level) const
 {
-       lst sym_lst, repl_lst;
+       exmap repl, rev_lookup;
 
-       ex e = bp->normal(sym_lst, repl_lst, level);
+       ex e = bp->normal(repl, rev_lookup, level);
        GINAC_ASSERT(is_a<lst>(e));
 
        // Re-insert replaced symbols
-       if (sym_lst.nops() > 0)
-               e = e.subs(sym_lst, repl_lst);
+       if (!repl.empty())
+               e = e.subs(repl, subs_options::no_pattern);
 
        // Convert {numerator, denominator} form back to fraction
        return e.op(0) / e.op(1);
@@ -2045,16 +2042,16 @@ ex ex::normal(int level) const
  *  @return numerator */
 ex ex::numer() const
 {
-       lst sym_lst, repl_lst;
+       exmap repl, rev_lookup;
 
-       ex e = bp->normal(sym_lst, repl_lst, 0);
+       ex e = bp->normal(repl, rev_lookup, 0);
        GINAC_ASSERT(is_a<lst>(e));
 
        // Re-insert replaced symbols
-       if (sym_lst.nops() > 0)
-               return e.op(0).subs(sym_lst, repl_lst);
-       else
+       if (repl.empty())
                return e.op(0);
+       else
+               return e.op(0).subs(repl, subs_options::no_pattern);
 }
 
 /** Get denominator of an expression. If the expression is not of the normal
@@ -2065,16 +2062,16 @@ ex ex::numer() const
  *  @return denominator */
 ex ex::denom() const
 {
-       lst sym_lst, repl_lst;
+       exmap repl, rev_lookup;
 
-       ex e = bp->normal(sym_lst, repl_lst, 0);
+       ex e = bp->normal(repl, rev_lookup, 0);
        GINAC_ASSERT(is_a<lst>(e));
 
        // Re-insert replaced symbols
-       if (sym_lst.nops() > 0)
-               return e.op(1).subs(sym_lst, repl_lst);
-       else
+       if (repl.empty())
                return e.op(1);
+       else
+               return e.op(1).subs(repl, subs_options::no_pattern);
 }
 
 /** Get numerator and denominator of an expression. If the expresison is not
@@ -2085,16 +2082,16 @@ ex ex::denom() const
  *  @return a list [numerator, denominator] */
 ex ex::numer_denom() const
 {
-       lst sym_lst, repl_lst;
+       exmap repl, rev_lookup;
 
-       ex e = bp->normal(sym_lst, repl_lst, 0);
+       ex e = bp->normal(repl, rev_lookup, 0);
        GINAC_ASSERT(is_a<lst>(e));
 
        // Re-insert replaced symbols
-       if (sym_lst.nops() > 0)
-               return e.subs(sym_lst, repl_lst);
-       else
+       if (repl.empty())
                return e;
+       else
+               return e.subs(repl, subs_options::no_pattern);
 }
 
 
@@ -2337,7 +2334,7 @@ ex collect_common_factors(const ex & e)
                lst repl;
                ex factor = 1;
                ex r = find_common_factor(e, factor, repl);
-               return factor.subs(repl) * r.subs(repl);
+               return factor.subs(repl, subs_options::no_pattern) * r.subs(repl, subs_options::no_pattern);
 
        } else
                return e;