]> www.ginac.de Git - ginac.git/blobdiff - ginac/mul.cpp
- fixed typos
[ginac.git] / ginac / mul.cpp
index 70c161c241bb94bfa4d65fb6a64d5d6fa59afaea..3dfe48ebd387bcf8a62671a34a82b1d63c4da310 100644 (file)
@@ -26,6 +26,7 @@
 #include "mul.h"
 #include "add.h"
 #include "power.h"
+#include "matrix.h"
 #include "archive.h"
 #include "debugmsg.h"
 #include "utils.h"
@@ -38,26 +39,14 @@ GINAC_IMPLEMENT_REGISTERED_CLASS(mul, expairseq)
 // default ctor, dctor, copy ctor assignment operator and helpers
 //////////
 
-// public
-
 mul::mul()
 {
        debugmsg("mul default ctor",LOGLEVEL_CONSTRUCT);
        tinfo_key = TINFO_mul;
 }
 
-// protected
-
-/** For use by copy ctor and assignment operator. */
-void mul::copy(const mul & other)
-{
-       inherited::copy(other);
-}
-
-void mul::destroy(bool call_parent)
-{
-       if (call_parent) inherited::destroy(call_parent);
-}
+DEFAULT_COPY(mul)
+DEFAULT_DESTROY(mul)
 
 //////////
 // other ctors
@@ -130,23 +119,7 @@ mul::mul(const ex & lh, const ex & mh, const ex & rh)
 // archiving
 //////////
 
-/** Construct object from archive_node. */
-mul::mul(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
-{
-       debugmsg("mul ctor from archive_node", LOGLEVEL_CONSTRUCT);
-}
-
-/** Unarchive the object. */
-ex mul::unarchive(const archive_node &n, const lst &sym_lst)
-{
-       return (new mul(n, sym_lst))->setflag(status_flags::dynallocated);
-}
-
-/** Archive the object. */
-void mul::archive(archive_node &n) const
-{
-       inherited::archive(n);
-}
+DEFAULT_ARCHIVING(mul)
 
 //////////
 // functions overriding virtual functions from bases classes
@@ -154,99 +127,113 @@ void mul::archive(archive_node &n) const
 
 // public
 
-void mul::print(std::ostream & os, unsigned upper_precedence) const
-{
-       debugmsg("mul print",LOGLEVEL_PRINT);
-       if (precedence<=upper_precedence) os << "(";
-       bool first = true;
-       // first print the overall numeric coefficient:
-       numeric coeff = ex_to_numeric(overall_coeff);
-       if (coeff.csgn()==-1) os << '-';
-       if (!coeff.is_equal(_num1()) &&
-               !coeff.is_equal(_num_1())) {
-               if (coeff.is_rational()) {
-                       if (coeff.is_negative())
-                               os << -coeff;
-                       else
-                               os << coeff;
-               } else {
-                       if (coeff.csgn()==-1)
-                               (-coeff).print(os, precedence);
-                       else
-                               coeff.print(os, precedence);
-               }
-               os << '*';
-       }
-       // then proceed with the remaining factors:
-       for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
-               if (!first) {
-                       os << '*';
-               } else {
-                       first=false;
+void mul::print(const print_context & c, unsigned level) const
+{
+       debugmsg("mul print", LOGLEVEL_PRINT);
+
+       if (is_of_type(c, print_tree)) {
+
+               inherited::print(c, level);
+
+       } else if (is_of_type(c, print_csrc)) {
+
+               if (precedence() <= level)
+                       c.s << "(";
+
+               if (!overall_coeff.is_equal(_ex1())) {
+                       overall_coeff.bp->print(c, precedence());
+                       c.s << "*";
                }
-               recombine_pair_to_ex(*cit).print(os,precedence);
-       }
-       if (precedence<=upper_precedence) os << ")";
-}
 
-void mul::printraw(std::ostream & os) const
-{
-       debugmsg("mul printraw",LOGLEVEL_PRINT);
+               // Print arguments, separated by "*" or "/"
+               epvector::const_iterator it = seq.begin(), itend = seq.end();
+               while (it != itend) {
 
-       os << "*(";
-       for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
-               os << "(";
-               (*it).rest.bp->printraw(os);
-               os << ",";
-               (*it).coeff.bp->printraw(os);
-               os << "),";
-       }
-       os << ",hash=" << hashvalue << ",flags=" << flags;
-       os << ")";
-}
+                       // 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 (is_of_type(c, print_csrc_cl_N))
+                                       c.s << "recip(";
+                               else
+                                       c.s << "1.0/";
+                       }
 
-void mul::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
-{
-       debugmsg("mul print csrc", LOGLEVEL_PRINT);
-       if (precedence <= upper_precedence)
-               os << "(";
+                       // If the exponent is 1 or -1, it is left out
+                       if (it->coeff.compare(_ex1()) == 0 || it->coeff.compare(_num_1()) == 0)
+                               it->rest.print(c, precedence());
+                       else {
+                               // Outer parens around ex needed for broken gcc-2.95 parser:
+                               (ex(power(it->rest, abs(ex_to_numeric(it->coeff))))).print(c, level);
+                       }
 
-       if (!overall_coeff.is_equal(_ex1())) {
-               overall_coeff.bp->printcsrc(os,type,precedence);
-               os << "*";
-       }
-       
-       // Print arguments, separated by "*" or "/"
-       epvector::const_iterator it = seq.begin();
-       epvector::const_iterator itend = seq.end();
-       while (it != itend) {
+                       // Separator is "/" for negative integer powers, "*" otherwise
+                       ++it;
+                       if (it != itend) {
+                               if (ex_to_numeric(it->coeff).is_integer() && it->coeff.compare(_num0()) < 0)
+                                       c.s << "/";
+                               else
+                                       c.s << "*";
+                       }
+               }
+
+               if (precedence() <= level)
+                       c.s << ")";
+
+       } else {
 
-               // 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 (type == csrc_types::ctype_cl_N)
-                               os << "recip(";
+               if (precedence() <= level) {
+                       if (is_of_type(c, print_latex))
+                               c.s << "{(";
                        else
-                               os << "1.0/";
+                               c.s << "(";
                }
 
-               // If the exponent is 1 or -1, it is left out
-               if (it->coeff.compare(_ex1()) == 0 || it->coeff.compare(_num_1()) == 0)
-                       it->rest.bp->printcsrc(os, type, precedence);
-               else
-                       // outer parens around ex needed for broken gcc-2.95 parser:
-                       (ex(power(it->rest, abs(ex_to_numeric(it->coeff))))).bp->printcsrc(os, type, upper_precedence);
+               bool first = true;
+
+               // First print the overall numeric coefficient
+               numeric coeff = ex_to_numeric(overall_coeff);
+               if (coeff.csgn() == -1)
+                       c.s << '-';
+               if (!coeff.is_equal(_num1()) &&
+                       !coeff.is_equal(_num_1())) {
+                       if (coeff.is_rational()) {
+                               if (coeff.is_negative())
+                                       (-coeff).print(c);
+                               else
+                                       coeff.print(c);
+                       } else {
+                               if (coeff.csgn() == -1)
+                                       (-coeff).print(c, precedence());
+                               else
+                                       coeff.print(c, precedence());
+                       }
+                       if (is_of_type(c, print_latex))
+                               c.s << ' ';
+                       else
+                               c.s << '*';
+               }
 
-               // Separator is "/" for negative integer powers, "*" otherwise
-               ++it;
-               if (it != itend) {
-                       if (ex_to_numeric(it->coeff).is_integer() && it->coeff.compare(_num0()) < 0)
-                               os << "/";
+               // Then proceed with the remaining factors
+               epvector::const_iterator it = seq.begin(), itend = seq.end();
+               while (it != itend) {
+                       if (!first) {
+                               if (is_of_type(c, print_latex))
+                                       c.s << ' ';
+                               else
+                                       c.s << '*';
+                       } else {
+                               first = false;
+                       }
+                       recombine_pair_to_ex(*it).print(c, precedence());
+                       it++;
+               }
+
+               if (precedence() <= level) {
+                       if (is_of_type(c, print_latex))
+                               c.s << ")}";
                        else
-                               os << "*";
+                               c.s << ")";
                }
        }
-       if (precedence <= upper_precedence)
-               os << ")";
 }
 
 bool mul::info(unsigned inf) const
@@ -275,7 +262,7 @@ bool mul::info(unsigned inf) const
        return inherited::info(inf);
 }
 
-int mul::degree(const symbol & s) const
+int mul::degree(const ex & s) const
 {
        int deg_sum = 0;
        for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
@@ -285,7 +272,7 @@ int mul::degree(const symbol & s) const
        return deg_sum;
 }
 
-int mul::ldegree(const symbol & s) const
+int mul::ldegree(const ex & s) const
 {
        int deg_sum = 0;
        for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
@@ -295,7 +282,7 @@ int mul::ldegree(const symbol & s) const
        return deg_sum;
 }
 
-ex mul::coeff(const symbol & s, int n) const
+ex mul::coeff(const ex & s, int n) const
 {
        exvector coeffseq;
        coeffseq.reserve(seq.size()+1);
@@ -303,7 +290,7 @@ ex mul::coeff(const symbol & s, int n) const
        if (n==0) {
                // product of individual coeffs
                // if a non-zero power of s is found, the resulting product will be 0
-               epvector::const_iterator it=seq.begin();
+               epvector::const_iterator it = seq.begin();
                while (it!=seq.end()) {
                        coeffseq.push_back(recombine_pair_to_ex(*it).coeff(s,n));
                        ++it;
@@ -311,15 +298,15 @@ ex mul::coeff(const symbol & s, int n) const
                coeffseq.push_back(overall_coeff);
                return (new mul(coeffseq))->setflag(status_flags::dynallocated);
        }
-                
+       
        epvector::const_iterator it=seq.begin();
-       bool coeff_found=0;
+       bool coeff_found = 0;
        while (it!=seq.end()) {
-               ex t=recombine_pair_to_ex(*it);
-               ex c=t.coeff(s,n);
+               ex t = recombine_pair_to_ex(*it);
+               ex c = t.coeff(s,n);
                if (!c.is_zero()) {
                        coeffseq.push_back(c);
-                       coeff_found=1;
+                       coeff_found = 1;
                } else {
                        coeffseq.push_back(t);
                }
@@ -355,7 +342,7 @@ ex mul::eval(int level) const
                             (!(ex_to_numeric((*cit).coeff).is_integer())));
                GINAC_ASSERT(!(cit->is_canonical_numeric()));
                if (is_ex_exactly_of_type(recombine_pair_to_ex(*cit),numeric))
-                   printtree(std::cerr,0);
+                   print(print_tree(std::cerr));
                GINAC_ASSERT(!is_ex_exactly_of_type(recombine_pair_to_ex(*cit),numeric));
                /* for paranoia */
                expair p = split_ex_to_pair(recombine_pair_to_ex(*cit));
@@ -418,22 +405,58 @@ ex mul::evalf(int level) const
        return mul(s,overall_coeff.evalf(level));
 }
 
-exvector mul::get_indices(void) const
+ex mul::evalm(void) const
 {
-       // return union of indices of factors
-       exvector iv;
-       for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
-               exvector subiv=(*cit).rest.get_indices();
-               iv.reserve(iv.size()+subiv.size());
-               for (exvector::const_iterator cit2=subiv.begin(); cit2!=subiv.end(); ++cit2)
-                       iv.push_back(*cit2);
+       // numeric*matrix
+       if (seq.size() == 1 && seq[0].coeff.is_equal(_ex1())
+        && is_ex_of_type(seq[0].rest, matrix))
+               return ex_to_matrix(seq[0].rest).mul(ex_to_numeric(overall_coeff));
+
+       // Evaluate children first, look whether there are any matrices at all
+       // (there can be either no matrices or one matrix; if there were more
+       // than one matrix, it would be a non-commutative product)
+       epvector *s = new epvector;
+       s->reserve(seq.size());
+
+       bool have_matrix = false;
+       epvector::iterator the_matrix;
+
+       epvector::const_iterator it = seq.begin(), itend = seq.end();
+       while (it != itend) {
+               const ex &m = recombine_pair_to_ex(*it).evalm();
+               s->push_back(split_ex_to_pair(m));
+               if (is_ex_of_type(m, matrix)) {
+                       have_matrix = true;
+                       the_matrix = s->end() - 1;
+               }
+               it++;
        }
-       return iv;
+
+       if (have_matrix) {
+
+               // The product contained a matrix. We will multiply all other factors
+               // into that matrix.
+               matrix m = ex_to_matrix(the_matrix->rest);
+               s->erase(the_matrix);
+               ex scalar = (new mul(s, overall_coeff))->setflag(status_flags::dynallocated);
+               return m.mul_scalar(scalar);
+
+       } else
+               return (new mul(s, overall_coeff))->setflag(status_flags::dynallocated);
 }
 
 ex mul::simplify_ncmul(const exvector & v) const
 {
-       throw(std::logic_error("mul::simplify_ncmul() should never have been called!"));
+       if (seq.size()==0) {
+               return inherited::simplify_ncmul(v);
+       }
+
+       // Find first noncommutative element and call its simplify_ncmul()
+       for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
+               if (cit->rest.return_type() == return_types::noncommutative)
+                       return cit->rest.simplify_ncmul(v);
+       }
+       return inherited::simplify_ncmul(v);
 }
 
 // protected
@@ -660,6 +683,8 @@ ex mul::expand(unsigned options) const
                }
                ++cit;
        }
+       if (expanded_seqp)
+               delete expanded_seqp;
 
        if (is_ex_exactly_of_type(last_expanded,add)) {
                add const & finaladd = ex_to_add(last_expanded);
@@ -733,12 +758,4 @@ epvector * mul::expandchildren(unsigned options) const
        return 0; // nothing has changed
 }
 
-//////////
-// static member variables
-//////////
-
-// protected
-
-unsigned mul::precedence = 50;
-
 } // namespace GiNaC