]> www.ginac.de Git - ginac.git/blobdiff - ginac/mul.cpp
- fixed typos
[ginac.git] / ginac / mul.cpp
index 75f74b4ba2cb7d06fc1ce969f4072ba7e4494618..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"
@@ -136,13 +137,14 @@ void mul::print(const print_context & c, unsigned level) const
 
        } else if (is_of_type(c, print_csrc)) {
 
-               c.s << "(";
+               if (precedence() <= level)
+                       c.s << "(";
 
                if (!overall_coeff.is_equal(_ex1())) {
-                       overall_coeff.bp->print(c, precedence);
+                       overall_coeff.bp->print(c, precedence());
                        c.s << "*";
                }
-       
+
                // Print arguments, separated by "*" or "/"
                epvector::const_iterator it = seq.begin(), itend = seq.end();
                while (it != itend) {
@@ -157,7 +159,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)
-                               it->rest.print(c, precedence);
+                               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);
@@ -173,12 +175,12 @@ void mul::print(const print_context & c, unsigned level) const
                        }
                }
 
-               if (precedence <= level)
+               if (precedence() <= level)
                        c.s << ")";
 
        } else {
 
-               if (precedence <= level) {
+               if (precedence() <= level) {
                        if (is_of_type(c, print_latex))
                                c.s << "{(";
                        else
@@ -200,9 +202,9 @@ void mul::print(const print_context & c, unsigned level) const
                                        coeff.print(c);
                        } else {
                                if (coeff.csgn() == -1)
-                                       (-coeff).print(c, precedence);
+                                       (-coeff).print(c, precedence());
                                else
-                                       coeff.print(c, precedence);
+                                       coeff.print(c, precedence());
                        }
                        if (is_of_type(c, print_latex))
                                c.s << ' ';
@@ -221,11 +223,11 @@ void mul::print(const print_context & c, unsigned level) const
                        } else {
                                first = false;
                        }
-                       recombine_pair_to_ex(*it).print(c, precedence);
+                       recombine_pair_to_ex(*it).print(c, precedence());
                        it++;
                }
 
-               if (precedence <= level) {
+               if (precedence() <= level) {
                        if (is_of_type(c, print_latex))
                                c.s << ")}";
                        else
@@ -403,6 +405,46 @@ 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
 {
        if (seq.size()==0) {
@@ -716,12 +758,4 @@ epvector * mul::expandchildren(unsigned options) const
        return 0; // nothing has changed
 }
 
-//////////
-// static member variables
-//////////
-
-// protected
-
-unsigned mul::precedence = 50;
-
 } // namespace GiNaC