On Wed, Dec 2, 2015 at 2:23 PM, Francesco Bonazzi <franz.bona...@gmail.com> wrote: > > > On Wednesday, 2 December 2015 17:19:50 UTC+1, Sergey Kirpichev wrote: >> >> >> Pattern matching in Sympy is not a useful thing in that >> sense, AFAIK it doesn't handle subexpressions. > > > Current pattern matching could be modified to support subexpressions, the > only fear is that it would mess up all solvers based on pattern matching.
How might that happen? As far as I know, the main thing that uses the pattern matcher is the ode module (see https://github.com/sympy/sympy/blob/cbc73f198998c4d53b228511ed3a821fa4e1bb8f/sympy/solvers/ode.py#L693-L1280). Stuff like r = collect(reduced_eq, f(x), exact=True).match(a*df + b*f(x) + c*f(x)**n) (df = f(x).diff(x)). Subexpressions are irrelevant here because we have to match the whole expression. As far as I can tell, the replace() method using Wilds is not used anywhere in library code (at least based off of a quick search with git grep). In fact, it doesn't look like the pattern matcher deals with subexpressions at all. It only matches the entire expression. replace() uses bottom_up to iterate all subexpressions and replace the ones that match. To do what you want, you would need to remove the separation between pattern matching and expression traversal that currently exists. > > I have the impression that SymPy's pattern matcher works too much like an > equation solver and too little like a formula matcher. I agree that it's a good idea to have a purely structural pattern matcher, but you would also need something that uses mathematics and tries to rewrite expressions to fit the matcher. In the above example, collect() is used to pull out coefficients, so that even things like f(x) + x*f(x) would match b*f(x). That's a simple case. A more complicated case would be an Euler ODE which has terms like (x - a), (x - a)**2, (x - a)**3, ..., where a is a constant, which would be nice to match even if the polynomials are expanded (although I admit that at some point you want to stop doing naive pattern matching and start doing more advanced stuff using Lie symmetries). Aaron Meurer > > The issue of sublist matching is quite stringent, as lots of substitutions > could get way easier. > > -- > You received this message because you are subscribed to the Google Groups > "sympy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to sympy+unsubscr...@googlegroups.com. > To post to this group, send email to sympy@googlegroups.com. > Visit this group at http://groups.google.com/group/sympy. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sympy/27d75961-3108-437b-b620-96f7a2385907%40googlegroups.com. > > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sympy+unsubscr...@googlegroups.com. To post to this group, send email to sympy@googlegroups.com. Visit this group at http://groups.google.com/group/sympy. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAKgW%3D6KATj98ofr6%3Dvx0rnsDv2prg6VJk2z-PMPw8OYQ%2Bz-7Vw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.