-
- epvector::const_iterator last = seq.end();
- epvector::const_iterator cit = seq.begin();
- while (cit!=last) {
- const ex &subsed_ex=(*cit).rest.subs(ls,lr,no_pattern);
- if (!are_ex_trivially_equal((*cit).rest,subsed_ex)) {
+
+ // The substitution is "complex" when any of the objects to be substituted
+ // is a product or power. In this case we have to recombine the pairs
+ // because the numeric coefficients may be part of the search pattern.
+ bool complex_subs = false;
+ for (unsigned i=0; i<ls.nops(); i++)
+ if (is_ex_exactly_of_type(ls.op(i), mul) || is_ex_exactly_of_type(ls.op(i), power)) {
+ complex_subs = true;
+ break;
+ }
+
+ if (complex_subs) {
+
+ // Substitute in the recombined pairs
+ epvector::const_iterator cit = seq.begin(), last = seq.end();
+ while (cit != last) {
+
+ const ex &orig_ex = recombine_pair_to_ex(*cit);
+ const ex &subsed_ex = orig_ex.subs(ls, lr, no_pattern);
+ if (!are_ex_trivially_equal(orig_ex, subsed_ex)) {
+
+ // Something changed, copy seq, subs and return it
+ epvector *s = new epvector;
+ s->reserve(seq.size());
+
+ // Copy parts of seq which are known not to have changed
+ s->insert(s->begin(), seq.begin(), cit);
+
+ // Copy first changed element
+ s->push_back(split_ex_to_pair(subsed_ex));
+ ++cit;
+
+ // Copy rest
+ while (cit != last) {
+ s->push_back(split_ex_to_pair(recombine_pair_to_ex(*cit).subs(ls, lr, no_pattern)));
+ ++cit;
+ }
+ return s;
+ }
+
+ ++cit;
+ }
+
+ } else {
+
+ // Substitute only in the "rest" part of the pairs
+ epvector::const_iterator cit = seq.begin(), last = seq.end();
+ while (cit != last) {
+
+ const ex &subsed_ex = cit->rest.subs(ls, lr, no_pattern);
+ if (!are_ex_trivially_equal(cit->rest, subsed_ex)) {