HPAC / matchpy

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

Unable to `.add()` rules in ManyToOneReplacer #4

Closed arihantparsoya closed 7 years ago

arihantparsoya commented 7 years ago

I am receiving the following error while adding rule to ManyToOneReplacer:

>>> pattern1 = Pattern(Int(Mul(a_, Pow(x_, -1)), x), FreeQ((a,), x))
>>> rule1 = ReplacementRule(pattern1, lambda a, x: Mul(a, Log(x)))
>>> subject1 = Int(Mul(a, Pow(x, -1)), x)
>>>
>>> pattern2 = Pattern(Int(Pow(x_, m_), x), FreeQ((m,), x), NonzeroQ(Add(m_, one), (m,)))
>>> rule2 = ReplacementRule(pattern2, lambda m, x: Mul(Pow(x, Add(m, one)), Pow(Add(m, one), m_one)))
>>> subject2 = Int(Pow(x, one), x)
>>>
>>> replace_all(subject1, [rule1])
Mul(Log(VariableSymbol('x')), VariableSymbol('a'))
>>> replace_all(subject2, [rule2])
Mul(Pow(Add(ConstantSymbol('1'), ConstantSymbol('1')), ConstantSymbol('-1')), Pow(VariableSymbol('x'), Add(ConstantSymbol('1'), ConstantSymbol('1'))))
>>> rubi = ManyToOneReplacer(rule1)
>>> rubi.add(rule2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/parsoyaarihant/anaconda/lib/python3.6/site-packages/matchpy-0.3-py3.6.egg/matchpy/matching/many_to_one.py", line 329, in add
AttributeError: 'ReplacementRule' object has no attribute 'expression'
wheerd commented 7 years ago

Sorry about that, I forgot to override the add method as well. You can work around it by using. add(*rule) for the moment. I will fix it once I get home.

⁣Sent from BlueMail ​

On 26 May 2017, 14:16, at 14:16, Arihant Parsoya notifications@github.com wrote:

I am receiving the following error while adding rule to ManyToOneReplacer:

>>> pattern1 = Pattern(Int(Mul(a_, Pow(x_, -1)), x), FreeQ((a,), x))
>>> rule1 = ReplacementRule(pattern1, lambda a, x: Mul(a, Log(x)))
>>> subject1 = Int(Mul(a, Pow(x, -1)), x)
>>>
>>> pattern2 = Pattern(Int(Pow(x_, m_), x), FreeQ((m,), x),
NonzeroQ(Add(m_, one), (m,)))
>>> rule2 = ReplacementRule(pattern2, lambda m, x: Mul(Pow(x, Add(m,
one)), Pow(Add(m, one), m_one)))
>>> subject2 = Int(Pow(x, one), x)
>>>
>>> replace_all(subject1, [rule1])
Mul(Log(VariableSymbol('x')), VariableSymbol('a'))
>>> replace_all(subject2, [rule2])
Mul(Pow(Add(ConstantSymbol('1'), ConstantSymbol('1')),
ConstantSymbol('-1')), Pow(VariableSymbol('x'),
Add(ConstantSymbol('1'), ConstantSymbol('1'))))
>>> rubi = ManyToOneReplacer(rule1)
>>> rubi.add(rule2)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
File
"/Users/parsoyaarihant/anaconda/lib/python3.6/site-packages/matchpy-0.3-py3.6.egg/matchpy/matching/many_to_one.py",
line 329, in add
AttributeError: 'ReplacementRule' object has no attribute 'expression'

-- You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub: https://github.com/HPAC/matchpy/issues/4

arihantparsoya commented 7 years ago

Thanks, I am facing another issue with ManyToOneReplacer:

>>> pattern2 = Pattern(Int(Pow(x_, m_), x), FreeQ((m,), x), NonzeroQ(Add(m_, one), (m,)))
>>> rule2 = ReplacementRule(pattern2, lambda m, x: Mul(Pow(x, Add(m, one)), Pow(Add(m, one), m_one)))
>>> subject2 = Int(Pow(x, one), x)
>>> replace_all(subject2, [rule2])
Mul(Pow(Add(ConstantSymbol('1'), ConstantSymbol('1')), ConstantSymbol('-1')), Pow(VariableSymbol('x'), Add(ConstantSymbol('1'), ConstantSymbol('1'))))
>>> rubi = ManyToOneReplacer(rule2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/parsoyaarihant/anaconda/lib/python3.6/site-packages/matchpy-0.3-py3.6.egg/matchpy/matching/many_to_one.py", line 720, in __init__
  File "/Users/parsoyaarihant/anaconda/lib/python3.6/site-packages/matchpy-0.3-py3.6.egg/matchpy/matching/many_to_one.py", line 330, in add
  File "/Users/parsoyaarihant/anaconda/lib/python3.6/site-packages/matchpy-0.3-py3.6.egg/matchpy/matching/many_to_one.py", line 346, in _internal_add
  File "/Users/parsoyaarihant/anaconda/lib/python3.6/site-packages/matchpy-0.3-py3.6.egg/matchpy/matching/many_to_one.py", line 346, in <listcomp>
  File "/Users/parsoyaarihant/Rubi-MatchPy/Rubi/constraint.py", line 43, in with_renamed_vars
    copy = NonzeroQ()
TypeError: __init__() missing 2 required positional arguments: 'expr' and 'vars'

rule2 is being evaluated my replace_all but not by ManyToOneReplacer.

arihantparsoya commented 7 years ago

This is happening due to presence of NonzeroQ:

class NonzeroQ(Constraint):
    def __init__(self, expr, vars):
        self.expr = expr
        self.vars = frozenset(v.name for v in vars)

    def __call__(self, substitution):
        return sympify(str(substitute(self.expr, substitution))) != 0

    @property
    def variables(self):
        return self.vars

    def with_renamed_vars(self, renaming):
        copy = NonzeroQ()
        copy.vars = frozenset(renaming.get(v, v) for v in self.vars)
        return copy

    def __eq__(self, other):
        return isinstance(other, NonzeroQ) and other.vars == self.vars and other.expr == self.expr

    def __hash__(self):
        return hash(self.vars)
wheerd commented 7 years ago

This is happening because in with_renamed_vars you use NonzeroQ without passing expr and vars.

On 27 May 2017, 06:58, at 06:58, Arihant Parsoya notifications@github.com wrote:

This is happening due to presence of NonzeroQ:

class NonzeroQ(Constraint):
   def __init__(self, expr, vars):
       self.expr = expr
       self.vars = frozenset(v.name for v in vars)

   def __call__(self, substitution):
       return sympify(str(substitute(self.expr, substitution))) != 0

   @property
   def variables(self):
       return self.vars

   def with_renamed_vars(self, renaming):
       copy = NonzeroQ()
       copy.vars = frozenset(renaming.get(v, v) for v in self.vars)
       return copy

   def __eq__(self, other):
return isinstance(other, NonzeroQ) and other.vars == self.vars and
other.expr == self.expr

   def __hash__(self):
       return hash(self.vars)

-- You are receiving this because you commented. Reply to this email directly or view it on GitHub: https://github.com/HPAC/matchpy/issues/4#issuecomment-304427779

wheerd commented 7 years ago

New version 0.3.1 with this fix is up on PyPI.

arihantparsoya commented 7 years ago

Thanks