+ if (level==1)
+ return mul(seq,overall_coeff);
+
+ if (level==-max_recursion_level)
+ throw(std::runtime_error("max recursion level reached"));
+
+ epvector *s = new epvector();
+ s->reserve(seq.size());
+
+ --level;
+ epvector::const_iterator i = seq.begin(), end = seq.end();
+ while (i != end) {
+ s->push_back(combine_ex_with_coeff_to_pair(i->rest.evalf(level),
+ i->coeff));
+ ++i;
+ }
+ return mul(s, overall_coeff.evalf(level));
+}
+
+ex mul::evalm() const
+{
+ // numeric*matrix
+ if (seq.size() == 1 && seq[0].coeff.is_equal(_ex1)
+ && is_a<matrix>(seq[0].rest))
+ 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 i = seq.begin(), end = seq.end();
+ while (i != end) {
+ const ex &m = recombine_pair_to_ex(*i).evalm();
+ s->push_back(split_ex_to_pair(m));
+ if (is_a<matrix>(m)) {
+ have_matrix = true;
+ the_matrix = s->end() - 1;
+ }
+ ++i;
+ }
+
+ 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::eval_ncmul(const exvector & v) const
+{
+ if (seq.empty())
+ return inherited::eval_ncmul(v);
+
+ // Find first noncommutative element and call its eval_ncmul()
+ epvector::const_iterator i = seq.begin(), end = seq.end();
+ while (i != end) {
+ if (i->rest.return_type() == return_types::noncommutative)
+ return i->rest.eval_ncmul(v);
+ ++i;
+ }
+ return inherited::eval_ncmul(v);
+}
+
+bool tryfactsubs(const ex & origfactor, const ex & patternfactor, int & nummatches, lst & repls)
+{
+ ex origbase;
+ int origexponent;
+ int origexpsign;
+
+ if (is_exactly_a<power>(origfactor) && origfactor.op(1).info(info_flags::integer)) {
+ origbase = origfactor.op(0);
+ int expon = ex_to<numeric>(origfactor.op(1)).to_int();
+ origexponent = expon > 0 ? expon : -expon;
+ origexpsign = expon > 0 ? 1 : -1;
+ } else {
+ origbase = origfactor;
+ origexponent = 1;
+ origexpsign = 1;
+ }
+
+ ex patternbase;
+ int patternexponent;
+ int patternexpsign;
+
+ if (is_exactly_a<power>(patternfactor) && patternfactor.op(1).info(info_flags::integer)) {
+ patternbase = patternfactor.op(0);
+ int expon = ex_to<numeric>(patternfactor.op(1)).to_int();
+ patternexponent = expon > 0 ? expon : -expon;
+ patternexpsign = expon > 0 ? 1 : -1;
+ } else {
+ patternbase = patternfactor;
+ patternexponent = 1;
+ patternexpsign = 1;
+ }
+
+ lst saverepls = repls;
+ if (origexponent < patternexponent || origexpsign != patternexpsign || !origbase.match(patternbase,saverepls))
+ return false;
+ repls = saverepls;
+
+ int newnummatches = origexponent / patternexponent;
+ if (newnummatches < nummatches)
+ nummatches = newnummatches;
+ return true;
+}
+
+ex mul::algebraic_subs_mul(const exmap & m, unsigned options) const
+{
+ std::vector<bool> subsed(seq.size(), false);
+ exvector subsresult(seq.size());
+
+ for (exmap::const_iterator it = m.begin(); it != m.end(); ++it) {
+
+ if (is_exactly_a<mul>(it->first)) {
+
+ int nummatches = std::numeric_limits<int>::max();
+ std::vector<bool> currsubsed(seq.size(), false);
+ bool succeed = true;
+ lst repls;
+
+ for (size_t j=0; j<it->first.nops(); j++) {
+ bool found=false;
+ for (size_t k=0; k<nops(); k++) {
+ if (currsubsed[k] || subsed[k])
+ continue;
+ if (tryfactsubs(op(k), it->first.op(j), nummatches, repls)) {
+ currsubsed[k] = true;
+ found = true;
+ break;
+ }
+ }
+ if (!found) {
+ succeed = false;
+ break;
+ }
+ }
+ if (!succeed)
+ continue;
+
+ bool foundfirstsubsedfactor = false;
+ for (size_t j=0; j<subsed.size(); j++) {
+ if (currsubsed[j]) {
+ if (foundfirstsubsedfactor)
+ subsresult[j] = op(j);
+ else {
+ foundfirstsubsedfactor = true;
+ subsresult[j] = op(j) * power(it->second.subs(ex(repls), subs_options::no_pattern) / it->first.subs(ex(repls), subs_options::no_pattern), nummatches);
+ }
+ subsed[j] = true;
+ }
+ }
+
+ } else {
+
+ int nummatches = std::numeric_limits<int>::max();
+ lst repls;
+
+ for (size_t j=0; j<this->nops(); j++) {
+ if (!subsed[j] && tryfactsubs(op(j), it->first, nummatches, repls)) {
+ subsed[j] = true;
+ subsresult[j] = op(j) * power(it->second.subs(ex(repls), subs_options::no_pattern) / it->first.subs(ex(repls), subs_options::no_pattern), nummatches);
+ }
+ }
+ }
+ }
+
+ bool subsfound = false;
+ for (size_t i=0; i<subsed.size(); i++) {
+ if (subsed[i]) {
+ subsfound = true;
+ break;
+ }
+ }
+ if (!subsfound)
+ return subs_one_level(m, options | subs_options::algebraic);
+
+ exvector ev; ev.reserve(nops());
+ for (size_t i=0; i<nops(); i++) {
+ if (subsed[i])
+ ev.push_back(subsresult[i]);
+ else
+ ev.push_back(op(i));
+ }
+
+ return (new mul(ev))->setflag(status_flags::dynallocated);