Coder Social home page Coder Social logo

using `int` as Wildcard about matchpy HOT 9 CLOSED

hpac avatar hpac commented on June 1, 2024
using `int` as Wildcard

from matchpy.

Comments (9)

wheerd avatar wheerd commented on June 1, 2024

I am not sure why you would want to have a constant wildcard. The whole point of wildcards is that they do match anything, not just a constant value. You can use any Symbol or ConstantSymbol in a pattern as well. Your code works fine for me, too. A traceback or some code of what you are trying to use the ConstantWild for would be helpful. If you want a wildcard, that can only match constants, you can use the SymbolWildcard class or just a = Wildcard.symbol('a', ConstantSymbol).

from matchpy.

arihantparsoya avatar arihantparsoya commented on June 1, 2024

When I have a pattern(Which uses ConstantSymbol):

And(FreeQ(m_, x_), NonzeroQ(Add(ConstantSymbol(1), m_)))

and I want to substitute variables. I am receiving errors:

  File "/users/parsoyaarihant/sympy/sympy/rubi/tests/test_rubi.py", line 8, in test_rubi_integrate
    print(rubi_integrate(expr, x))
  File "/Users/parsoyaarihant/sympy/sympy/rubi/rubi.py", line 17, in rubi_integrate
    rubi = rubi_object()
  File "/Users/parsoyaarihant/sympy/sympy/rubi/patterns.py", line 40, in rubi_object
    rubi.add(rule2)
  File "/Users/parsoyaarihant/anaconda/lib/python3.6/site-packages/matchpy-0.3.1-py3.6.egg/matchpy/matching/many_to_one.py", line 730, in add
    self.matcher.add(rule.pattern, rule.replacement)
  File "/Users/parsoyaarihant/anaconda/lib/python3.6/site-packages/matchpy-0.3.1-py3.6.egg/matchpy/matching/many_to_one.py", line 330, in add
    self._internal_add(pattern, label, renaming)
  File "/Users/parsoyaarihant/anaconda/lib/python3.6/site-packages/matchpy-0.3.1-py3.6.egg/matchpy/matching/many_to_one.py", line 346, in _internal_add
    renamed_constraints = [c.with_renamed_vars(renaming) for c in pattern.local_constraints]
  File "/Users/parsoyaarihant/anaconda/lib/python3.6/site-packages/matchpy-0.3.1-py3.6.egg/matchpy/matching/many_to_one.py", line 346, in <listcomp>
    renamed_constraints = [c.with_renamed_vars(renaming) for c in pattern.local_constraints]
  File "/Users/parsoyaarihant/sympy/sympy/rubi/constraint.py", line 22, in with_renamed_vars
    copy = cons(self.expr.with_renamed_vars(renaming), [])
  File "/Users/parsoyaarihant/anaconda/lib/python3.6/site-packages/matchpy-0.3.1-py3.6.egg/matchpy/expressions/expressions.py", line 592, in with_renamed_vars
    variable_name=renaming.get(self.variable_name, self.variable_name)
  File "/Users/parsoyaarihant/anaconda/lib/python3.6/site-packages/matchpy-0.3.1-py3.6.egg/matchpy/expressions/expressions.py", line 591, in <genexpr>
    *(o.with_renamed_vars(renaming) for o in self.operands),
  File "/Users/parsoyaarihant/anaconda/lib/python3.6/site-packages/matchpy-0.3.1-py3.6.egg/matchpy/expressions/expressions.py", line 592, in with_renamed_vars
    variable_name=renaming.get(self.variable_name, self.variable_name)
  File "/Users/parsoyaarihant/anaconda/lib/python3.6/site-packages/matchpy-0.3.1-py3.6.egg/matchpy/expressions/expressions.py", line 591, in <genexpr>
    *(o.with_renamed_vars(renaming) for o in self.operands),
  File "/Users/parsoyaarihant/anaconda/lib/python3.6/site-packages/matchpy-0.3.1-py3.6.egg/matchpy/expressions/expressions.py", line 592, in with_renamed_vars
    variable_name=renaming.get(self.variable_name, self.variable_name)
  File "/Users/parsoyaarihant/anaconda/lib/python3.6/site-packages/matchpy-0.3.1-py3.6.egg/matchpy/expressions/expressions.py", line 591, in <genexpr>
    *(o.with_renamed_vars(renaming) for o in self.operands),
  File "/Users/parsoyaarihant/anaconda/lib/python3.6/site-packages/matchpy-0.3.1-py3.6.egg/matchpy/expressions/expressions.py", line 667, in with_renamed_vars
    return type(self)(self.name, variable_name=renaming.get(self.variable_name, self.variable_name))
TypeError: __init__() got an unexpected keyword argument 'variable_name'

This does not happen if I use a Wildcard.dot object instead of ConstantSymbol.

from matchpy.

arihantparsoya avatar arihantparsoya commented on June 1, 2024

On side note, the documentation website for MatchPy doesn't seems to be working properly. I dont see any documentation on this website:

http://matchpy.readthedocs.io/en/latest/api/matchpy.expressions.expressions.html

from matchpy.

wheerd avatar wheerd commented on June 1, 2024

Oh, thanks for the hint. I thought I had fixed the doc building process, but apparently it was still broken.
Now it should be fixed, you might need to refresh the page to see the new version.

from matchpy.

wheerd avatar wheerd commented on June 1, 2024

The problem is that your ConstantSymbol inherits from Symbol, but changes the __init__ signature.
The __init__ method needs to accept the variable_name keyword:

class ConstantSymbol(Symbol):
    ...
    def __init__(self, name: str, variable_name=None) -> None:
        ...

from matchpy.

arihantparsoya avatar arihantparsoya commented on June 1, 2024

This is the code for ConstantSymbol(updated) but I am still getting the same error:

class ConstantSymbol(Symbol):
    def __init__(self, value):
        super(self.__class__, self).__init__(name=str(value), variable_name=None)
        self.value = value

from matchpy.

wheerd avatar wheerd commented on June 1, 2024

Try this instead:

class ConstantSymbol(Symbol):
    def __init__(self, value, variable_name=None):
        super(self.__class__, self).__init__(name=str(value), variable_name=variable_name)
        self.value = value

from matchpy.

arihantparsoya avatar arihantparsoya commented on June 1, 2024

It works. Thanks!

Can you explain what was wrong with the previous implementation?

from matchpy.

wheerd avatar wheerd commented on June 1, 2024

When renaming the variables in the expression, the new Expression is created with type(expr)(..., variable_name='new_name'). In this example, the new symbol is created with ConstantSymbol(..., variable_name='new_name'), but the __init__ does not accept variable_name. Now that I think about it, you should probably override with_renamed_vars in your ConstantSymbol class, so that the value property is set correctly.

from matchpy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.