-
Notifications
You must be signed in to change notification settings - Fork 25
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
Multiple pattern matched in ManyToOneReplacer #30
Comments
You need to make sure that the rules lead to replacements that are not matched by the same rule again and again. Otherwise, you get an infinite loop. Probably some of the constraints are not working correctly. But it is hard to tell without a concrete example. |
rubi_integrate(x**4*sqrt(a + b*x**2),x) It results into a recursion error. Should I show the complete error message ? |
That would be great.
Sent from Blue
…On Mar 20, 2018, 07:13, at 07:13, Ashish Kumar Gaurav ***@***.***> wrote:
```python
rubi_integrate(x**4*sqrt(a + b*x**2),x)
````
It results into a recursion error. Should I show the complete error
message ?
--
You are receiving this because you commented.
Reply to this email directly or view it on GitHub:
#30 (comment)
|
|
That is nothing that can be changed in MatchPy. It seems that the replacement functions are calling the integration replacement recursively. That can lead to infinite recursion if the new term is not simpler than the original one.
Sent from Blue
…On Mar 20, 2018, 07:31, at 07:31, Ashish Kumar Gaurav ***@***.***> wrote:
```
RecursionError Traceback (most recent call
last)
<ipython-input-5-defee0be9862> in <module>()
----> 1 rubi_integrate(x**4*sqrt(a + b*x**2),x)
~/sympy/sympy/integrals/rubi/rubi.py in rubi_integrate(expr, var,
showsteps)
203 return S(expr)*var
204
--> 205 result = rubi.replace(Integral(expr, var))
206
207 return result
~/matchpy/matchpy/matching/many_to_one.py in replace(self, expression,
max_count)
795 try:
796 replacement, subst =
next(iter(self.matcher.match(subexpr)))
--> 797 result = replacement(**subst)
798 expression = functions.replace(expression, pos,
result)
799 replaced = True
~/sympy/sympy/integrals/rubi/rules/binomial_products.py in <lambda>(b,
n, a, p, m, x)
412 k = GCD(m + S(1), n)
413 return Subst(Int(x**(S(-1) + (m + S(1))/k)*(a +
b*x**(n/k))**p, x), x, x**k)/k
--> 414 rule79 = ReplacementRule(pattern79, lambda b, n, a, p, m, x
: With79(b, n, a, p, m, x))
415 rubi.add(rule79)
416
~/sympy/sympy/integrals/rubi/rules/binomial_products.py in With79(b, n,
a, p, m, x)
411 def With79(b, n, a, p, m, x):
412 k = GCD(m + S(1), n)
--> 413 return Subst(Int(x**(S(-1) + (m + S(1))/k)*(a +
b*x**(n/k))**p, x), x, x**k)/k
414 rule79 = ReplacementRule(pattern79, lambda b, n, a, p, m, x :
With79(b, n, a, p, m, x))
415 rubi.add(rule79)
~/sympy/sympy/integrals/rubi/utility_function.py in Int(expr, var)
160 def Int(expr, var):
161 from sympy.integrals.rubi.rubi import rubi_integrate
--> 162 return rubi_integrate(expr, var)
163
164 def Set(expr, value):
... last 5 frames repeated, from the frame below ...
~/sympy/sympy/integrals/rubi/rubi.py in rubi_integrate(expr, var,
showsteps)
203 return S(expr)*var
204
--> 205 result = rubi.replace(Integral(expr, var))
206
207 return result
RecursionError: maximum recursion depth exceeded
```
--
You are receiving this because you commented.
Reply to this email directly or view it on GitHub:
#30 (comment)
|
Last year you suggested to use multilple macthers for cases to handle multiple rules
Can you explain it ? |
I mean that when you have rules like |
Mathematica attempts to rank the specificity of downvalues. For example, f(a) would have higher specificity than f(x_), thus it would be checked first when evaluating f. The specificity function is not perfect and seems to change between versions, but if MMA gets the order wrong then it can be overridden. Interestingly for RUBI this ordering does not seem to matter much. For many of the rules, they can simply be checked in the order in which they are defined and the integration rules work just fine. |
But that is what I mean. The order of the rules matters. Many-to-one matching means that all patterns are matched simultaneously. Hence it is not deterministic in which order matches are found.
Sent from Blue
…On Mar 21, 2018, 07:41, at 07:41, Cory Walker ***@***.***> wrote:
Mathematica attempts to rank the specificity of downvalues. For
example, f(a) would have higher specificity than f(x_), thus it would
be checked first when evaluating f. The specificity function is not
perfect and seems to change between versions, but if MMA gets the order
wrong then it can be overridden.
Interestingly for RUBI this ordering does not seem to matter much. For
many of the rules, they can simply be checked in the order in which
they are defined and the integration rules work just fine.
--
You are receiving this because you commented.
Reply to this email directly or view it on GitHub:
#30 (comment)
|
@wheerd , For example I have these list of rules:
I want to match in the given fix order ( 1 -> 2 -> 3). Is there any way for this ? Or can you give an example using multiple matcher to perform this ? |
Our many-to-one matching algorithms don't support this. How to do this depends on the actual problem. If you only have three rules that you want to match one after another, there is no reason to use many-to-one matching at all: You could simply do this "manually". Can you provide more information about the actual problem? What kind of dependencies between the rules do you have? |
Actually I have a lot of rules (~10, 000) . When an expression matches to multiple(2-3) rules, we want to follow a order.(order is known to us). If you want , I can make it more clear through code. |
https://github.com/ashishkg0022/rubi-structure/tree/master/way_2 In rules lets say, if rule12 and rule28 matches to an expression. We want rule12 to work first. |
You want to use a many-to-one matcher whenever you have multiple rules and want to know which ones match. Think of a many-to-one matcher as parallel matching. Matching rules are returned in an arbitrary order, it's not possible to provide priorities (it might be possible for you to use the priorities to speed up matching a bit, but let's talk about that later). The way I understand it, you have a tree (or forest) of dependencies (in addition to priorities, in case there are multiple successors). Something like:
|
Yes. Is there any way to do it ? |
Also |
This I would recommend you to implement this yourself. I don't think this functionality belongs into MatchPy, because it's quite specific to your problem. The way I understand it, you need a tree structure that represents the dependencies between rules. If a node only has one successor, you can use one-to-one matching, otherwise you would want to construct a many-to-one matcher for each node in the tree. Do you have the dependencies and priorities in a format that allows to construct the tree programmatically? For 10k rules, you probably don't want to do that by hand. Regarding that optimization that I mentioned earlier: All matching functions are generators, so they produce matches one after another (but in an arbitrary order). As soon as the matcher returns a match for the rule with the highest priority, there is no need to continue matching (what do you do if there are multiple matches for one rule?), so you can stop. |
Rubi contains a lot of potential structure and matchpy is overkill for this problem, not only because parallel matching is overkill, but also because there are practically no sequential variables in the Rubi ruleset. |
I don't know the Rubi rules well enough to say anything about many-to-one matching, but the fact that MatchPy supports sequence variables should not affect matching if you don't use them. From the algorithms perspective, sequence variables are almost the same as supporting associativity, so if you implement algorithms for one, you basically get the other for free. |
Would it be possible to warn if we define replacements that lead to non determinism? I am trying to prevent this, but it would be nice if I had some automated checks for it since I have to be careful about it. |
Can you provide examples of cases where you would want to get such warnings?
|
This problem is much more complicated than it sounds, as it requires looking for expressions that are matched by multiple rules. This might very well be an undecidable problem. |
Any cases it could cover would be helpful. Even if it started with only the trivial cases you pointed out. I am hitting these as I am defining indexing and shape rules for arrays: https://github.com/Quansight-Labs/uarray/blob/master/uarray/uarray.py based on A Mathematics of Arrays. It would just be helpful as another sanity check that I am defining reasonable replacement rules. What about rules like |
I'm somewhat skeptical about this. If we produce warnings in the trivial cases, what do we do for those cases where we can't say anything? If we don't produce any warnings, that might give a false sense of safety. Conversely, always producing warnings in those cases might be irritating. Apart from that, I'm not enough of an expert in term rewriting systems to judge if it always makes sense to produce those warnings. There might be cases where there is nothing wrong with having a nondeterministic system.
No, the order is arbitrary. |
How MatchPy manages patterns that match multiple rules. In rubi module of sympy , it results in recursion errors. Is there any way to handle the recursion errors ?
The text was updated successfully, but these errors were encountered: