]> www.ginac.de Git - ginac.git/blobdiff - ginac/mul.cpp
- fixed typos
[ginac.git] / ginac / mul.cpp
index a00c9c928ed96436cc3d932a3479fc97897014ce..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"
@@ -126,99 +127,113 @@ DEFAULT_ARCHIVING(mul)
 
 // 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 (precedence() <= level) {
+                       if (is_of_type(c, print_latex))
+                               c.s << "{(";
+                       else
+                               c.s << "(";
+               }
 
-               // 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(";
+               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
-                               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);
+               // 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++;
+               }
 
-               // 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 << "/";
+               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
@@ -327,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));
@@ -390,9 +405,58 @@ ex mul::evalf(int level) const
        return mul(s,overall_coeff.evalf(level));
 }
 
+ex mul::evalm(void) const
+{
+       // 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++;
+       }
+
+       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
@@ -694,12 +758,4 @@ epvector * mul::expandchildren(unsigned options) const
        return 0; // nothing has changed
 }
 
-//////////
-// static member variables
-//////////
-
-// protected
-
-unsigned mul::precedence = 50;
-
 } // namespace GiNaC