HPAC / matchpy

A library for pattern matching on symbolic expressions in Python.
MIT License
164 stars 25 forks source link

Bug in `ManyToOneReplacer` #9

Closed arihantparsoya closed 7 years ago

arihantparsoya commented 7 years ago

is_match is returning True but ManyToOneReplacer is not able to match the subject:

>>> a_, b_, c_, d_, e_, f_, g_, h_ = map(Wildcard.dot, 'abcdefgh')
>>> n_, m_ = map(Wildcard.dot, 'nm')
>>> x_, u_ = map(Wildcard.symbol, 'xu')
>>> pattern533 = Pattern(Int(Mul(Add(a_, Mul(b_, x_)), Pow(x_, n_)), x_))
>>> rule533 = ReplacementRule(pattern533, lambda x, a, n, b : a)
>>> rubi = ManyToOneReplacer()
>>> rubi.add(rule533)
>>> sub = Int(Mul(Add(Mul(b, x), a), Pow(x, Integer(3))), x)
>>> is_match(sub, pattern533)
True
>>> rubi.replace(sub)
Int(Mul(Add(Mul(VariableSymbol('b'), VariableSymbol('x')), VariableSymbol('a')), Pow(VariableSymbol('x'), Integer('3'))), VariableSymbol('x'))

Operators are:

class Int(Operation):
    name = "Int"
    arity = Arity.binary

class Mul(Operation):
    name = "Mul"
    arity = Arity.variadic
    associative = True
    commutative = True
    one_identity = True

class Add(Operation):
    name = "Add"
    arity = Arity.variadic
    associative = True
    commutative = True
    one_identity = True

class Pow(Operation):
    name = "Pow"
    arity = Arity.binary
wheerd commented 7 years ago

This one looks like it will be tricky to track down. I think it happens because of the many nested commutative expressions and some missing case here. I will try to fix it as soon as possible.

arihantparsoya commented 7 years ago

Thanks !

wheerd commented 7 years ago

I published the new version to PyPI. There is also a new function rename_variables in matchpy.expressions.functions that you can use instead of Expression.with_renamed_vars. The advantage of that is you can use it with mixed expressions, i.e. expressions that contain objects which are not an Expression - e.g. an int.