]> www.ginac.de Git - ginac.git/blobdiff - ginac/mul.cpp
added some checks for subs() behavior
[ginac.git] / ginac / mul.cpp
index d41179444dce765642e3a8e726586a0e6eb4d992..80f48571ac01fcb36be91cfa80467bc267570341 100644 (file)
@@ -3,7 +3,7 @@
  *  Implementation of GiNaC's products of expressions. */
 
 /*
- *  GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
+ *  GiNaC Copyright (C) 1999-2002 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
@@ -20,6 +20,7 @@
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
+#include <iostream>
 #include <vector>
 #include <stdexcept>
 
@@ -28,7 +29,6 @@
 #include "power.h"
 #include "matrix.h"
 #include "archive.h"
-#include "debugmsg.h"
 #include "utils.h"
 
 namespace GiNaC {
@@ -36,12 +36,11 @@ namespace GiNaC {
 GINAC_IMPLEMENT_REGISTERED_CLASS(mul, expairseq)
 
 //////////
-// default ctor, dctor, copy ctor assignment operator and helpers
+// default ctor, dtor, copy ctor, assignment operator and helpers
 //////////
 
 mul::mul()
 {
-       debugmsg("mul default ctor",LOGLEVEL_CONSTRUCT);
        tinfo_key = TINFO_mul;
 }
 
@@ -56,7 +55,6 @@ DEFAULT_DESTROY(mul)
 
 mul::mul(const ex & lh, const ex & rh)
 {
-       debugmsg("mul ctor from ex,ex",LOGLEVEL_CONSTRUCT);
        tinfo_key = TINFO_mul;
        overall_coeff = _ex1;
        construct_from_2_ex(lh,rh);
@@ -65,7 +63,6 @@ mul::mul(const ex & lh, const ex & rh)
 
 mul::mul(const exvector & v)
 {
-       debugmsg("mul ctor from exvector",LOGLEVEL_CONSTRUCT);
        tinfo_key = TINFO_mul;
        overall_coeff = _ex1;
        construct_from_exvector(v);
@@ -74,7 +71,6 @@ mul::mul(const exvector & v)
 
 mul::mul(const epvector & v)
 {
-       debugmsg("mul ctor from epvector",LOGLEVEL_CONSTRUCT);
        tinfo_key = TINFO_mul;
        overall_coeff = _ex1;
        construct_from_epvector(v);
@@ -83,7 +79,6 @@ mul::mul(const epvector & v)
 
 mul::mul(const epvector & v, const ex & oc)
 {
-       debugmsg("mul ctor from epvector,ex",LOGLEVEL_CONSTRUCT);
        tinfo_key = TINFO_mul;
        overall_coeff = oc;
        construct_from_epvector(v);
@@ -92,7 +87,6 @@ mul::mul(const epvector & v, const ex & oc)
 
 mul::mul(epvector * vp, const ex & oc)
 {
-       debugmsg("mul ctor from epvector *,ex",LOGLEVEL_CONSTRUCT);
        tinfo_key = TINFO_mul;
        GINAC_ASSERT(vp!=0);
        overall_coeff = oc;
@@ -103,7 +97,6 @@ mul::mul(epvector * vp, const ex & oc)
 
 mul::mul(const ex & lh, const ex & mh, const ex & rh)
 {
-       debugmsg("mul ctor from ex,ex,ex",LOGLEVEL_CONSTRUCT);
        tinfo_key = TINFO_mul;
        exvector factors;
        factors.reserve(3);
@@ -129,8 +122,6 @@ DEFAULT_ARCHIVING(mul)
 
 void mul::print(const print_context & c, unsigned level) const
 {
-       debugmsg("mul print", LOGLEVEL_PRINT);
-
        if (is_a<print_tree>(c)) {
 
                inherited::print(c, level);
@@ -150,7 +141,7 @@ void mul::print(const print_context & c, unsigned level) const
                while (it != itend) {
 
                        // If the first argument is a negative integer power, it gets printed as "1.0/<expr>"
-                       if (it == seq.begin() && ex_to<numeric>(it->coeff).is_integer() && it->coeff.compare(_num0) < 0) {
+                       if (it == seq.begin() && ex_to<numeric>(it->coeff).is_integer() && it->coeff.info(info_flags::negative)) {
                                if (is_a<print_csrc_cl_N>(c))
                                        c.s << "recip(";
                                else
@@ -158,7 +149,7 @@ void mul::print(const print_context & c, unsigned level) const
                        }
 
                        // If the exponent is 1 or -1, it is left out
-                       if (it->coeff.compare(_ex1) == 0 || it->coeff.compare(_num_1) == 0)
+                       if (it->coeff.is_equal(_ex1) || it->coeff.is_equal(_ex_1))
                                it->rest.print(c, precedence());
                        else {
                                // Outer parens around ex needed for broken gcc-2.95 parser:
@@ -168,7 +159,7 @@ void mul::print(const print_context & c, unsigned level) const
                        // Separator is "/" for negative integer powers, "*" otherwise
                        ++it;
                        if (it != itend) {
-                               if (ex_to<numeric>(it->coeff).is_integer() && it->coeff.compare(_num0) < 0)
+                               if (ex_to<numeric>(it->coeff).is_integer() && it->coeff.info(info_flags::negative))
                                        c.s << "/";
                                else
                                        c.s << "*";
@@ -178,6 +169,14 @@ void mul::print(const print_context & c, unsigned level) const
                if (precedence() <= level)
                        c.s << ")";
 
+       } else if (is_a<print_python_repr>(c)) {
+               c.s << class_name() << '(';
+               op(0).print(c);
+               for (unsigned i=1; i<nops(); ++i) {
+                       c.s << ',';
+                       op(i).print(c);
+               }
+               c.s << ')';
        } else {
 
                if (precedence() <= level) {
@@ -341,8 +340,6 @@ ex mul::coeff(const ex & s, int n) const
  *  @param level cut-off in recursive evaluation */
 ex mul::eval(int level) const
 {
-       debugmsg("mul eval",LOGLEVEL_MEMBER_FUNCTION);
-       
        epvector *evaled_seqp = evalchildren(level);
        if (evaled_seqp) {
                // do more evaluation later
@@ -696,9 +693,10 @@ ex mul::expand(unsigned options) const
                                exvector distrseq;
                                distrseq.reserve(n1*n2);
                                for (int i1=0; i1<n1; ++i1) {
-                                       for (int i2=0; i2<n2; ++i2) {
-                                               distrseq.push_back(add1.op(i1) * add2.op(i2));
-                                       }
+                                       // cache the first operand (for efficiency):
+                                       const ex op1 = add1.op(i1);
+                                       for (int i2=0; i2<n2; ++i2)
+                                               distrseq.push_back(op1 * add2.op(i2));
                                }
                                last_expanded = (new add(distrseq))->
                                                 setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0));
@@ -756,7 +754,7 @@ ex mul::expand(unsigned options) const
  *  pointer, if sequence is unchanged. */
 epvector * mul::expandchildren(unsigned options) const
 {
-       epvector::const_iterator last = seq.end();
+       const epvector::const_iterator last = seq.end();
        epvector::const_iterator cit = seq.begin();
        while (cit!=last) {
                const ex & factor = recombine_pair_to_ex(*cit);