It seems that the term 'pattern match' is overloaded here. The existing pattern matching in sympy appears to match at the mathematical level: >>> p = Wild('p') >>> print (x+1).match(x+p) {p_: 1}
That is, it returns the value that makes the patterned expression true. The code I've written is for matching the underlying expression tree, and is supposed to help with transforming the sympy expression trees into other formats. Example: e = x + 1 m = Match(e) v = AutoVar() if m(Add, v.e1, v.e2): # use v.e1 and v.e2 # Nested match if m(Add, (Mul, S.NegativeOne, v.e1), v.e2): # use v.e1 and v.e2 I don't see that these two ideas could be combined (unless I'm missing something ...) Perhaps it would be clearer to rename the second idea to something like 'expression_tree_match'? Mark On Sep 9, 10:32 am, Aaron Meurer <asmeu...@gmail.com> wrote: > Is this the code that lets you type (x + 1).match((Add, (Symbol, Number))) > (or something like that)? > > Could give a summary of the functionality? Perhaps it should be integrated > into the main matcher. > > Aaron > > > > On Friday, September 9, 2011, Mark Dewing wrote: > > Dimas, > > The pattern matching code I've been working on might be useful to > > compactly represent this sort of transformation. > > > The code is here: > > >https://github.com/markdewing/sympy/blob/derivation_modeling/sympy/pr... > > > Example of it in use is here (converting to a python tree - not the > > python AST, but a different one) > > >https://github.com/markdewing/sympy/blob/derivation_modeling/sympy/pr... > > > Everyone, > > I think the pattern-matching code could be ready to go into the main > > sympy branch, but where should it live - perhaps in sympy.util? (The > > rest of the code generation and derivation modeling work is probably > > not ready yet, but the pattern-matching piece could be useful on its > > own). > > > Mark > > > On Sep 8, 12:27 pm, Dimas Abreu Dutra <dimasadu...@gmail.com<javascript:;>> > > wrote: > > > On Thu, Sep 8, 2011 at 1:27 PM, Aaron Meurer > > > <asmeu...@gmail.com<javascript:;>> > > wrote: > > > > You need to fix some doctests. For example, the fix_missing_locations > > > > used in the convert() method docstring isn't imported. > > > > Weird, all my doctests are passing. > > > > > I think perhaps your convert methods should be class methods. > > > > Do you mean class methods as opposed to instance methods? The thing is > > that > > > the conversion needs to know the constants that should remain named and > > is > > > currently saving the recalled symbols and functions. These could be > > useful > > > to know what functions and symbols are being loaded so that the > > namespaces > > > can be appropriately populated. Those things are obviously instance > > related, > > > so that's why the conversions are not class methods. > > > > > I guess that could be a useful addition to sympy, and other people > > > > > might need it as well. I was thinking it could be included as a > > module > > > > > in the sympy.parsing package. I'm not sure about the name, though, > > > > > perhaps sympy_ast or ast_builder. I'm still working on it, though, so > > > > > maybe I could send a pull request when I'm done. > > > > > Yes, please do. > > > > Ok, will soon post a pull request with my code as a module name > > *ast_builder > > > *. It will be easier to comment and test then. Something that I also > > forgot > > > to mention is that this could be used for code generation, with very > > little > > > effort, using the unparse module. > > > >http://svn.python.org/view/python/trunk/Demo/parser/unparse.py > > > > ~Dimas > > > -- > > You received this message because you are subscribed to the Google Groups > > "sympy" group. > > To post to this group, send email to sympy@googlegroups.com <javascript:;> > > . > > To unsubscribe from this group, send email to > > sympy+unsubscr...@googlegroups.com <javascript:;>. > > 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 sympy@googlegroups.com. To unsubscribe from this group, send email to sympy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sympy?hl=en.