+/** Checks whether e matches to the pattern pat and the (possibly to be updated)
+ * list of replacements repls. This matching is in the sense of algebraic
+ * substitutions. Matching starts with pat.op(factor) of the pattern because
+ * the factors before this one have already been matched. The (possibly
+ * updated) number of matches is in nummatches. subsed[i] is true for factors
+ * that already have been replaced by previous substitutions and matched[i]
+ * is true for factors that have been matched by the current match.
+ */
+bool algebraic_match_mul_with_mul(const mul &e, const ex &pat, exmap& repls,
+ int factor, int &nummatches, const std::vector<bool> &subsed,
+ std::vector<bool> &matched)
+{
+ GINAC_ASSERT(subsed.size() == e.nops());
+ GINAC_ASSERT(matched.size() == e.nops());
+
+ if (factor == (int)pat.nops())
+ return true;
+
+ for (size_t i=0; i<e.nops(); ++i) {
+ if(subsed[i] || matched[i])
+ continue;
+ exmap newrepls = repls;
+ int newnummatches = nummatches;
+ if (tryfactsubs(e.op(i), pat.op(factor), newnummatches, newrepls)) {
+ matched[i] = true;
+ if (algebraic_match_mul_with_mul(e, pat, newrepls, factor+1,
+ newnummatches, subsed, matched)) {
+ repls = newrepls;
+ nummatches = newnummatches;
+ return true;
+ }
+ else
+ matched[i] = false;
+ }
+ }
+
+ return false;
+}
+
+bool mul::has(const ex & pattern, unsigned options) const
+{
+ if(!(options & has_options::algebraic))
+ return basic::has(pattern,options);
+ if(is_a<mul>(pattern)) {
+ exmap repls;
+ int nummatches = std::numeric_limits<int>::max();
+ std::vector<bool> subsed(nops(), false);
+ std::vector<bool> matched(nops(), false);
+ if(algebraic_match_mul_with_mul(*this, pattern, repls, 0, nummatches,
+ subsed, matched))
+ return true;
+ }
+ return basic::has(pattern, options);
+}
+