Skip to content
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

Need Help in code generation. #33

Open
ashishkg0022 opened this Issue Jul 25, 2018 · 6 comments

Comments

Projects
None yet
2 participants
@ashishkg0022
Copy link

ashishkg0022 commented Jul 25, 2018

I could not find any documentation related to code generation.
Last year @wheerd had written a code generation script for rubi in sympy.
https://gist.github.com/wheerd/13ac5a0e9b560db6201d96136fa0bbcc

But code generated was huge so, It cannot be used. This year I have removed the repeated definition of constraints. So code generation should work probably.

However, I am unable to write the code generation script. Here is the new structure of rubi in sympy. Can someone update the gist accordingly?

rules = [] #list to keep track which rules has been applied
def cons_f1(m, x):
    return FreeQ(m, x)
cons1 = CustomConstraint(cons_f1)

def cons_f2(m):
    return NonzeroQ(m + S(1))
cons2 = CustomConstraint(cons_f2)

pattern1 = Pattern(Integral(x_**WC('m', S(1)), x_), cons1, cons2)
def replacement1(m, x):
    rules.append(1)
    return Simp(x**(m + S(1))/(m + S(1)), x)
rule1 = ReplacementRule(pattern1, replacement1)
@ashishkg0022

This comment has been minimized.

Copy link
Author

ashishkg0022 commented Jul 25, 2018

https://github.com/ashishkg0022/sympy/tree/rubi_progress/sympy/integrals/rubi
Here is the rubi module
rubi.py contains the manytoonereplacer
rules contain all rules
constraints.py contain constraints.

There are around 7000 rules.

@ashishkg0022

This comment has been minimized.

Copy link
Author

ashishkg0022 commented Jul 25, 2018

For few rules I tried this

import re
import os

from sympy.core.expr import Basic
from sympy.integrals.rubi.rubi import rubi_object
from sympy.integrals.rubi.rubi import *
from sympy import *
from sympy.core.singleton import Singleton

from matchpy.matching.code_generation import CodeGenerator
rubi = rubi_object()[0]
GENERATED_TEMPLATE = '''
# -*- coding: utf-8 -*-
from matchpy import *
from sympy import *
from sympy.integrals.rubi.utility_function import *
from sympy.integrals.rubi.constraints import *
from sympy.integrals.rubi.symbol import *
{}
{}
'''.strip()

generator = CodeGenerator(rubi.matcher)
global_code, code = generator.generate_code()
code = GENERATED_TEMPLATE.format(global_code, code)

with open('generated.py', 'w', encoding='utf-8') as f:
    f.write(code)

from generated import match_root

x = symbols('x')
for r, _ in match_root(Integral(x, x)):
print(r)

But this gives an key_error

~/generated.py in match_root(subject)
  27036                                                                                 yield 6, tmp_subst
  27037                                                 subjects2.appendleft(tmp120)
> 27038                                 if 'i1' in subst1 and CustomConstraint(cons_f16)(subst1):
  27039                                         # State 6226
  27040                                         if len(subjects2) >= 1:

~/Envs/sym/lib/python3.6/site-packages/matchpy/expressions/constraints.py in __call__(self, match)
    186 
    187     def __call__(self, match: substitution.Substitution) -> bool:
--> 188         args = dict((name, match[var_name]) for name, var_name in self._variables.items())
    189 
    190         return self.constraint(**args)

~/Envs/sym/lib/python3.6/site-packages/matchpy/expressions/constraints.py in <genexpr>(.0)
    186 
    187     def __call__(self, match: substitution.Substitution) -> bool:
--> 188         args = dict((name, match[var_name]) for name, var_name in self._variables.items())
    189 
    190         return self.constraint(**args)

KeyError: 'u'

What can be the cause of this ?

@ashishkg0022

This comment has been minimized.

Copy link
Author

ashishkg0022 commented Jul 28, 2018

Here is some progress sympy/sympy#14988
I got the code generator working. But it is able to match few expression and unable to match many.

@wheerd

This comment has been minimized.

Copy link
Collaborator

wheerd commented Jul 29, 2018

The cause for the error is likely that you are using a variable u in your constraint which is not matched.
I don't have time to help you debug that though. You might also want to look into overriding constraint_repr of the code generator for your custom constraint classes.

@ashishkg0022

This comment has been minimized.

Copy link
Author

ashishkg0022 commented Jul 29, 2018

Actually that problem is solved. We have faced a new issue, in which it is able to match some expressions and unable for some. Progress can be seen here sympy/sympy#14988

@ashishkg0022

This comment has been minimized.

Copy link
Author

ashishkg0022 commented Aug 1, 2018

@wheerd we are observing that, in code_generation matching, if any constraint returns False, matching is not continued any further. Probably this is the only issue. If this is fixed, our work is done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.