+ 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);