On Mon, Oct 22, 2012 at 9:52 AM, Matthew Rocklin <[email protected]> wrote:
> Hi Everyone,
>
> I'd like to know more about pattern matching within SymPy and how it can be
> applied beyond the Expr class.
>
> I see that Basic has a matches method that does exact matching (so for
> example requires the same number of args and same arg sequence between
> pattern and expression). I see that AssocOp(Expr) has a _matches_commutative
> method that (I assume) allows freer matching.
>
> Some questions:
> How well does pattern matching work in SymPy on non-commutative symbols?
> How well does pattern matching work in SymPy on non-Exprs. Could
> _matches_commutative be moved up out of Expr to work on Basics? This code
> appears pretty general. Union(Set) is commutative too.
> The Wild class is a Symbol. How should we construct Wild objects for other
> non-expr types? How could we match sets for example?
I only have experience with matching expressions, particularly in the
ode module with classify_ode. If you want to match some complicated
expressions, you will run into some limitations soon. One issue is
that you can't match "n" terms, so for example, I had to write a
custom matcher for a linear ODE with constant coefficients (an*y^(n) +
... + a1*y' + a0*y = g).
Another issue is that the pattern matcher will give non-deterministic
results unless you specify the exclude keyword on the Wild objects
very carefully.
A third issue is that automatic simplification (and in many cases,
even non-automatic simplification, i.e., expressions being written in
different equivalent forms) can move an expression into a form that
the pattern matcher won't recognize. For example:
In [20]: p = Wild('p', exclude=[x])
In [21]: q = Wild('q')
In [24]: (pi*(x + 1)).match(p*q)
Out[24]: {p: π, q: x + 1}
In [26]: (2*(x + 1)).match(p*q)
Out[26]: {p: 1, q: 2⋅x + 2}
Note that 2*(x + 1) is automatically converted to 2*x + 2.
Another good example is at http://code.google.com/p/sympy/issues/detail?id=1784.
The good news is that there are a lot of good algorithms that you can
use to write custom targeted matching solutions. For example, with
the above, you could use some of the gcd algorithms (I'm not sure
which exactly; terms_gcd I think) to pull out the coefficient, if
that's what you want.
The bad news is that it isn't automated in the pattern matcher.
Matching pure ast trees is easy enough, but you'll find that you
really want to match things on a mathematical basis to some level, and
for this, the power is there, but it's not automated.
>
> The code in _matches_commutative and mul.matches are difficult for me to
> understand. Are these implementing a known algorithm that I can look up
> somewhere. Are there supplemental docs or references?
I thought we tried to document this a while back when we were fixing
some bugs. Are the inline comments and docstrings not enough?
Aaron Meurer
>
> If anyone is knowledgeable or interested in extending pattern matching
> within SymPy I'd enjoy company on this project.
>
> -Matt
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/sympy?hl=en.
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sympy?hl=en.