HPAC / matchpy

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

Using MatchPy lambda constraint with SymPy #19

Closed arihantparsoya closed 7 years ago

arihantparsoya commented 7 years ago

I tried using Pattern constraint using lambda and it is giving errors:

Rule

pattern1 = Pattern(Integral(x_**m_, x_), lambda x, m: FreeQ(m, x))
rule1 = ReplacementRule(pattern1, lambda x, m: x**(m + 1)/(m + 1))

Error:

Traceback (most recent call last):
  File "t.py", line 4, in <module>
    print(rubi_integrate(expr, x))
  File "/Users/parsoyaarihant/sympy/sympy/integrals/rubi/rubi.py", line 46, in rubi_integrate
    result = replace_all(Integral(expr, var), [rubi])
  File "/Users/parsoyaarihant/anaconda/lib/python3.6/site-packages/matchpy-0.5.dev51+ge514171-py3.6.egg/matchpy/functions.py", line 251, in replace_all
  File "/Users/parsoyaarihant/anaconda/lib/python3.6/site-packages/matchpy-0.5.dev51+ge514171-py3.6.egg/matchpy/matching/one_to_one.py", line 43, in match
  File "/Users/parsoyaarihant/anaconda/lib/python3.6/site-packages/matchpy-0.5.dev51+ge514171-py3.6.egg/matchpy/matching/one_to_one.py", line 43, in <listcomp>
AttributeError: 'function' object has no attribute 'variables'
wheerd commented 7 years ago

That is because all constraints need to be subclasses of Constraint to provide some additional features. You can wrap your lambda in a CustomConstraint:

CustomConstraint(lambda x, m: FreeQ(m, x))

I do recommend you to make FreeQ a Constrait subclass instead though.

arihantparsoya commented 7 years ago

Thanks