New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repeated definition of the same CustomConstraint #27

Closed
Upabjojr opened this Issue Sep 2, 2017 · 13 comments

Comments

Projects
None yet
4 participants
@Upabjojr

Upabjojr commented Sep 2, 2017

In the current RUBI rules patterns in SymPy, the same constraint is defined many times. For example: CustomConstraint(lambda a, x: FreeQ(a, x)) has its definition repeated more and more, like in:

https://github.com/sympy/sympy/blob/628286fb5cc2248002bb0b141a911757a2e81fa7/sympy/integrals/rubi/rules/miscellaneous_algebraic.py#L22

and in the next rule the same constraint is redefined:
https://github.com/sympy/sympy/blob/628286fb5cc2248002bb0b141a911757a2e81fa7/sympy/integrals/rubi/rules/miscellaneous_algebraic.py#L26

Python defines a new object for every new lambda definition, even if the functions return the same identical value.

Does this practice have some potential negative consequences, like:

  • the size of the generated decision tree?
  • the overall speed of MatchPy?
@wheerd

This comment has been minimized.

Collaborator

wheerd commented Sep 3, 2017

It will have a negative impact on the overall speed because equivalent constraints will be checked multiple times. It is easy to fix that though by creating a custom subclass of Constraint and overriding __eq__. I would at least do that for FreeQ. You could also create only one representation for each unique constraint during the transformation from the original Rubi rules.

@parsoyaarihant

This comment has been minimized.

parsoyaarihant commented Sep 3, 2017

You could also create only one representation for each unique constraint during the transformation from the original Rubi rules.

Can you give an example? I am unable to understand what exactly you are trying to say here.

@wheerd

This comment has been minimized.

Collaborator

wheerd commented Sep 3, 2017

Instead of

pattern456 = Pattern(..., CustomConstraint(lambda a, x: FreeQ(a, x)))

you would write

constraint123 = CustomConstraint(lambda a, x: FreeQ(a, x))
...
pattern456 = Pattern(..., constraint123)

Then you can reuse constraint123 for every occurence of that constraint.

@parsoyaarihant

This comment has been minimized.

parsoyaarihant commented Sep 3, 2017

Okay, I think I should also redefine cons() Constraint class which I was using previously. So there is no dependency on lambda functions at all.

Another question: Does lambda causes reduction of speed when loading rules?

@wheerd

This comment has been minimized.

Collaborator

wheerd commented Sep 3, 2017

I don't think so, but I am not quite sure what you mean. Getting rid of the lambdas also means you can pickle the matcher so you don't have to reconstruct it every time.

@Upabjojr

This comment has been minimized.

Upabjojr commented Sep 3, 2017

Getting rid of the lambdas also means you can pickle the matcher so you don't have to reconstruct it every time.

I like this idea.

@parsoyaarihant

This comment has been minimized.

parsoyaarihant commented Sep 17, 2017

lambda functions are also present in ReplacementRule hence the rule is unable to pickle.

    pickle.dump(a, fileObject)
AttributeError: Can't pickle local object 'linear_products.<locals>.<lambda>'
@parsoyaarihant

This comment has been minimized.

parsoyaarihant commented Sep 17, 2017

I was trying to pickle only this rule:

    pattern1 = Pattern(Integral(1/x_, x_))
    rule1 = ReplacementRule(pattern1, lambda x : log(x))
    rubi.add(rule1)
@wheerd

This comment has been minimized.

Collaborator

wheerd commented Sep 17, 2017

Yes, you can either use a proper function or use the dill package.

@parsoyaarihant

This comment has been minimized.

parsoyaarihant commented Sep 24, 2017

@wheerd can you give an example of using a function in ReplacementRule? I was unable to find this in the documentation.

@wheerd

This comment has been minimized.

Collaborator

wheerd commented Sep 25, 2017

Just use it instead of the lambda.

@parsoyaarihant

This comment has been minimized.

parsoyaarihant commented Sep 29, 2017

It does not work:

Code:

from sympy.integrals.rubi.symbol import WC
from sympy.integrals.rubi.utility_function import FreeQ, NonzeroQ
from matchpy import (Operation, CommutativeOperation, AssociativeOperation,
    ManyToOneReplacer, OneIdentityOperation, CustomConstraint, Pattern, ReplacementRule)
from matchpy.expressions.functions import register_operation_iterator, register_operation_factory
from sympy import Pow, Add, Integral, Basic, Mul, log, S
from sympy.abc import x, m

Operation.register(Integral)
register_operation_iterator(Integral, lambda a: (a._args[0],) + a._args[1], lambda a: len((a._args[0],) + a._args[1]))

Operation.register(Pow)
OneIdentityOperation.register(Pow)
register_operation_iterator(Pow, lambda a: a._args, lambda a: len(a._args))

Operation.register(Add)
OneIdentityOperation.register(Add)
CommutativeOperation.register(Add)
AssociativeOperation.register(Add)
register_operation_iterator(Add, lambda a: a._args, lambda a: len(a._args))

Operation.register(Mul)
OneIdentityOperation.register(Mul)
CommutativeOperation.register(Mul)
AssociativeOperation.register(Mul)
register_operation_iterator(Mul, lambda a: a._args, lambda a: len(a._args))

def sympy_op_factory(old_operation, new_operands, variable_name):
     return type(old_operation)(*new_operands)

register_operation_factory(Basic, sympy_op_factory)

x_ = WC('x')

rubi = ManyToOneReplacer()

pattern1 = Pattern(Integral(S(1)/x_, x_))
rule1 = ReplacementRule(pattern1, lambda x : log(x))
rubi.add(rule1)

pattern2 = Pattern(Integral(x_**WC('m', S(1)), x_), CustomConstraint(lambda m, x: FreeQ(m, x)), CustomConstraint(lambda m: NonzeroQ(m + S(1))))
rule2 = ReplacementRule(pattern2, x**(m + S(1))/(m + S(1)))
rubi.add(rule2)

subject = Integral(x, x)
print(rubi.replace(subject))

Output:

Traceback (most recent call last):
  File "t2.py", line 46, in <module>
    print(rubi.replace(subject))
  File "/Users/arihantparsoya/anaconda/lib/python3.6/site-packages/matchpy-0.5.dev74+gd9efe29-py3.6.egg/matchpy/matching/many_to_one.py", line 797, in replace
TypeError: 'Mul' object is not callable
@wheerd

This comment has been minimized.

Collaborator

wheerd commented Oct 2, 2017

I meant using a Python function:

def replacement(x, m):
    return x**(m + S(1))/(m + S(1))

rule = ReplacementRule(pattern, replacement)

@henrik227 henrik227 closed this Jun 13, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment