X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=blobdiff_plain;f=ginac%2Fex.cpp;h=28307ad73113b0c86f38cd2939a3f03503d7791b;hp=e53cbd332958194f9cda094e716db7788e02f5f8;hb=4a6052cb1463b48f2a3db01031f28a1530bc32c3;hpb=5ef801553eb39aed7bd2df9dd1aff9d752c3ea9d diff --git a/ginac/ex.cpp b/ginac/ex.cpp index e53cbd33..28307ad7 100644 --- a/ginac/ex.cpp +++ b/ginac/ex.cpp @@ -3,7 +3,7 @@ * Implementation of GiNaC's light-weight expression handles. */ /* - * GiNaC Copyright (C) 1999-2003 Johannes Gutenberg University Mainz, Germany + * GiNaC Copyright (C) 1999-2004 Johannes Gutenberg University Mainz, Germany * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -73,7 +73,7 @@ void ex::dbgprinttree() const ex ex::expand(unsigned options) const { if (options == 0 && (bp->flags & status_flags::expanded)) // The "expanded" flag only covers the standard options; someone might want to re-expand with different options - return *bp; + return *this; else return bp->expand(options); } @@ -126,7 +126,7 @@ ex ex::subs(const lst & ls, const lst & lr, unsigned options) const // Convert the lists to a map exmap m; for (lst::const_iterator its = ls.begin(), itr = lr.begin(); its != ls.end(); ++its, ++itr) { - m[*its] = *itr; + m.insert(std::make_pair(*its, *itr)); // Search for products and powers in the expressions to be substituted // (for an optimization in expairseq::subs()) @@ -146,34 +146,43 @@ ex ex::subs(const lst & ls, const lst & lr, unsigned options) const ex ex::subs(const ex & e, unsigned options) const { if (e.info(info_flags::relation_equal)) { + + // Argument is a relation: convert it to a map exmap m; - const ex & s = e.lhs(); - m[s] = e.rhs(); + const ex & s = e.op(0); + m.insert(std::make_pair(s, e.op(1))); + if (is_exactly_a(s) || is_exactly_a(s)) options |= subs_options::pattern_is_product; + else + options |= subs_options::pattern_is_not_product; + return bp->subs(m, options); - } else if (!e.info(info_flags::list)) - throw(std::invalid_argument("basic::subs(ex): argument must be a list")); - // Convert the list to a map - exmap m; - GINAC_ASSERT(is_a(e)); - for (lst::const_iterator it = ex_to(e).begin(); it != ex_to(e).end(); ++it) { - ex r = *it; - if (!r.info(info_flags::relation_equal)) - throw(std::invalid_argument("basic::subs(ex): argument must be a list of equations")); - const ex & s = r.lhs(); - m[s] = r.rhs(); + } else if (e.info(info_flags::list)) { - // Search for products and powers in the expressions to be substituted - // (for an optimization in expairseq::subs()) - if (is_exactly_a(s) || is_exactly_a(s)) - options |= subs_options::pattern_is_product; - } - if (!(options & subs_options::pattern_is_product)) - options |= subs_options::pattern_is_not_product; + // Argument is a list: convert it to a map + exmap m; + GINAC_ASSERT(is_a(e)); + for (lst::const_iterator it = ex_to(e).begin(); it != ex_to(e).end(); ++it) { + ex r = *it; + if (!r.info(info_flags::relation_equal)) + throw(std::invalid_argument("basic::subs(ex): argument must be a list of equations")); + const ex & s = r.op(0); + m.insert(std::make_pair(s, r.op(1))); + + // Search for products and powers in the expressions to be substituted + // (for an optimization in expairseq::subs()) + if (is_exactly_a(s) || is_exactly_a(s)) + options |= subs_options::pattern_is_product; + } + if (!(options & subs_options::pattern_is_product)) + options |= subs_options::pattern_is_not_product; - return bp->subs(m, options); + return bp->subs(m, options); + + } else + throw(std::invalid_argument("ex::subs(ex): argument must be a relation_equal or a list")); } /** Traverse expression tree with given visitor, preorder traversal. */ @@ -239,7 +248,20 @@ void ex::makewriteable() { GINAC_ASSERT(bp->flags & status_flags::dynallocated); bp.makewritable(); - GINAC_ASSERT(bp->refcount == 1); + GINAC_ASSERT(bp->get_refcount() == 1); +} + +/** Share equal objects between expressions. + * @see ex::compare(const ex &) */ +void ex::share(const ex & other) const +{ + if ((bp->flags | other.bp->flags) & status_flags::not_shareable) + return; + + if (bp->get_refcount() <= other.bp->get_refcount()) + bp = other.bp; + else + other.bp = bp; } /** Helper function for the ex-from-basic constructor. This is where GiNaC's @@ -274,7 +296,7 @@ ptr ex::construct_from_basic(const basic & other) // it means that eval() hit case b) above. The original object is // no longer needed (it evaluated into something different), so we // delete it (because nobody else will). - if ((other.refcount==0) && (other.flags & status_flags::dynallocated)) + if ((other.get_refcount() == 0) && (other.flags & status_flags::dynallocated)) delete &other; // yes, you can apply delete to a const pointer // We can't return a basic& here because the tmpex is destroyed as @@ -297,7 +319,7 @@ ptr ex::construct_from_basic(const basic & other) // on the heap. basic *bp = other.duplicate(); bp->setflag(status_flags::dynallocated); - GINAC_ASSERT(bp->refcount == 0); + GINAC_ASSERT(bp->get_refcount() == 0); return bp; } } @@ -359,7 +381,7 @@ basic & ex::construct_from_int(int i) default: basic *bp = new numeric(i); bp->setflag(status_flags::dynallocated); - GINAC_ASSERT(bp->refcount == 0); + GINAC_ASSERT(bp->get_refcount() == 0); return *bp; } } @@ -396,7 +418,7 @@ basic & ex::construct_from_uint(unsigned int i) default: basic *bp = new numeric(i); bp->setflag(status_flags::dynallocated); - GINAC_ASSERT(bp->refcount == 0); + GINAC_ASSERT(bp->get_refcount() == 0); return *bp; } } @@ -457,7 +479,7 @@ basic & ex::construct_from_long(long i) default: basic *bp = new numeric(i); bp->setflag(status_flags::dynallocated); - GINAC_ASSERT(bp->refcount == 0); + GINAC_ASSERT(bp->get_refcount() == 0); return *bp; } } @@ -494,7 +516,7 @@ basic & ex::construct_from_ulong(unsigned long i) default: basic *bp = new numeric(i); bp->setflag(status_flags::dynallocated); - GINAC_ASSERT(bp->refcount == 0); + GINAC_ASSERT(bp->get_refcount() == 0); return *bp; } } @@ -503,7 +525,7 @@ basic & ex::construct_from_double(double d) { basic *bp = new numeric(d); bp->setflag(status_flags::dynallocated); - GINAC_ASSERT(bp->refcount == 0); + GINAC_ASSERT(bp->get_refcount() == 0); return *bp; }