X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=blobdiff_plain;f=ginac%2Fncmul.cpp;h=eeadf4e7feb4fa6acf7b48db84062b049e502526;hp=e3a6b552f7e6d0294022fb69603083325ec40b4f;hb=79ad926d8a937c44278b8466966c9a2e9f2ee2f0;hpb=6d7bf9ee5a7ce05cb3a23dae664e781d7325d7b8 diff --git a/ginac/ncmul.cpp b/ginac/ncmul.cpp index e3a6b552..eeadf4e7 100644 --- a/ginac/ncmul.cpp +++ b/ginac/ncmul.cpp @@ -3,7 +3,7 @@ * Implementation of GiNaC's non-commutative products of expressions. */ /* - * GiNaC Copyright (C) 1999-2003 Johannes Gutenberg University Mainz, Germany + * GiNaC Copyright (C) 1999-2005 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 @@ -17,7 +17,7 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include @@ -30,13 +30,14 @@ #include "mul.h" #include "matrix.h" #include "archive.h" +#include "indexed.h" #include "utils.h" namespace GiNaC { GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(ncmul, exprseq, print_func(&ncmul::do_print). - print_func(&basic::do_print_tree). + print_func(&ncmul::do_print_tree). print_func(&ncmul::do_print_csrc). print_func(&ncmul::do_print_csrc)) @@ -127,7 +128,8 @@ typedef std::vector intvector; ex ncmul::expand(unsigned options) const { // First, expand the children - exvector expanded_seq = expandchildren(options); + std::auto_ptr vp = expandchildren(options); + const exvector &expanded_seq = vp.get() ? *vp : this->seq; // Now, look for all the factors that are sums and remember their // position and number of terms. @@ -151,9 +153,13 @@ ex ncmul::expand(unsigned options) const } // If there are no sums, we are done - if (number_of_adds == 0) - return (new ncmul(expanded_seq, true))-> - setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0)); + if (number_of_adds == 0) { + if (vp.get()) + return (new ncmul(vp))-> + setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0)); + else + return *this; + } // Now, form all possible products of the terms of the sums with the // remaining factors, and add them together @@ -162,10 +168,25 @@ ex ncmul::expand(unsigned options) const intvector k(number_of_adds); + /* Rename indices in the static members of the product */ + exvector expanded_seq_mod; + size_t j = 0; + size_t i; + for (i=0; i setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0))); @@ -185,6 +206,9 @@ ex ncmul::expand(unsigned options) const int ncmul::degree(const ex & s) const { + if (is_equal(ex_to(s))) + return 1; + // Sum up degrees of factors int deg_sum = 0; exvector::const_iterator i = seq.begin(), end = seq.end(); @@ -197,6 +221,9 @@ int ncmul::degree(const ex & s) const int ncmul::ldegree(const ex & s) const { + if (is_equal(ex_to(s))) + return 1; + // Sum up degrees of factors int deg_sum = 0; exvector::const_iterator i = seq.begin(), end = seq.end(); @@ -209,6 +236,9 @@ int ncmul::ldegree(const ex & s) const ex ncmul::coeff(const ex & s, int n) const { + if (is_equal(ex_to(s))) + return n==1 ? _ex1 : _ex0; + exvector coeffseq; coeffseq.reserve(seq.size()); @@ -456,6 +486,25 @@ ex ncmul::thiscontainer(std::auto_ptr vp) const return (new ncmul(vp))->setflag(status_flags::dynallocated); } +ex ncmul::conjugate() const +{ + if (return_type() != return_types::noncommutative) { + return exprseq::conjugate(); + } + + if ((return_type_tinfo() & 0xffffff00U) != TINFO_clifford) { + return exprseq::conjugate(); + } + + exvector ev; + ev.reserve(nops()); + for (const_iterator i=end(); i!=begin();) { + --i; + ev.push_back(i->conjugate()); + } + return (new ncmul(ev, true))->setflag(status_flags::dynallocated).eval(); +} + // protected /** Implementation of ex::diff() for a non-commutative product. It applies @@ -511,7 +560,7 @@ unsigned ncmul::return_type() const ++i; } // all factors checked - GINAC_ASSERT(!all_commutative); // not all factors should commute, because this is a ncmul(); + GINAC_ASSERT(!all_commutative); // not all factors should commutate, because this is a ncmul(); return all_commutative ? return_types::commutative : return_types::noncommutative; } @@ -542,16 +591,34 @@ unsigned ncmul::return_type_tinfo() const // non-virtual functions in this class ////////// -exvector ncmul::expandchildren(unsigned options) const +std::auto_ptr ncmul::expandchildren(unsigned options) const { - exvector s; - s.reserve(seq.size()); - exvector::const_iterator it = seq.begin(), itend = seq.end(); - while (it != itend) { - s.push_back(it->expand(options)); - it++; + const_iterator cit = this->seq.begin(), end = this->seq.end(); + while (cit != end) { + const ex & expanded_ex = cit->expand(options); + if (!are_ex_trivially_equal(*cit, expanded_ex)) { + + // copy first part of seq which hasn't changed + std::auto_ptr s(new exvector(this->seq.begin(), cit)); + reserve(*s, this->seq.size()); + + // insert changed element + s->push_back(expanded_ex); + ++cit; + + // copy rest + while (cit != end) { + s->push_back(cit->expand(options)); + ++cit; + } + + return s; + } + + ++cit; } - return s; + + return std::auto_ptr(0); // nothing has changed } const exvector & ncmul::get_factors() const