I guess solve() would do it, although this has the potential to make
the pattern matcher very slow, and also make it return results that
are all but useless (e.g., I wouldn't want the pattern matcher to
return the cubic formula because it technically makes the pattern
match some expression that looks nothing like a cubic).

There's another related point that should be brought up here, which is
that you should make a clear separation early on in the pattern
matcher between the pure structural matching and the different levels
of "mathematical" matching.

My main experience with the pattern matcher in SymPy is with dsolve(),
which basically works by matching the input ODE to a pattern and
plugging the values into a solution. For example, a first-order linear
ODE looks like f(x).diff(x) + P(x)*f(x) + Q(x), and the solution is an
expression that contains P(x) and Q(x) (there are a few solving
algorithms which are a bit more complicated than that, but they still
require pattern matching).

The main issue here is getting the input expression to "look" like the
pattern. This is currently done with preprocessing (mainly collect(),
because f(x) + x*f(x) won't match a*f(x) but (1 + x)*f(x) will). The
preprocessing is done manually and is hard-coded for each ODE (for
instance 
https://github.com/sympy/sympy/blob/e6fc53f27ee872b27bc79b96529fc4bf34d4f023/sympy/solvers/ode.py#L928-L929).

The most complicated example here is the separable ODE (any ODE that
can be written as dy/dx = P(y)*Q(x)). The separatevars() function was
written entirely for the purpose of handling all the possible ways an
expression can be split into a product with separated variables.

The point here, though, is that this would be useful, from an API
point of view, if this could be done automatically, at least to some
degree. In other words I should be able to just write a pattern like
a*x**2 + b*x + c and it should match things like y*x**2 + x**2 + 2*x -
1. But it should be implemented in a completely separate layer, so
that it is used only if explicitly needed. Otherwise, it will
unnecessarily slow down the pattern matching that doesn't need it, and
will make the matching code too complicated to maintain.

Aaron Meurer


On Sun, Nov 30, 2014 at 12:06 AM, Richard Fateman <[email protected]> wrote:
>
> Actually, my pattern matcher in Maxima DOES some "solving"... in particular
> the example I gave..
>
> matchdeclare(a,true);
> defrule(r1,f(a+1), g(a));
>
> r1(f(4))  returns g(3)
>
> but it only solves simple linear forms.
>
> I think the lisp code for my matcher is fairly clear.  matcom.lisp  is the
> match "compiler" and matrun.lisp  is its
> "runtime" support.
>
> RJF
>
>
>
> On Saturday, November 29, 2014 9:58:10 PM UTC-8, Richard Fateman wrote:
>>
>>
>>
>> On Saturday, November 29, 2014 8:29:56 AM UTC-8, Aaron Meurer wrote:
>>>
>>>
>>> <big snip>
>>
>>
>>>
>>> .
>>>
>>> Another question is, how can you teach the pattern matcher that a
>>> function maps to an identity, like cos(a)*x can match x with a = 0, or
>>> x**a*y can match y with a = 0?
>>
>>
>> You can do this (trivially)  but no one does because it is too  expensive.
>> Simpler case
>> x is a pattern variable.
>> pattern is  f(x+1)
>> expression is f(4)
>> method.  f matches. Now to match x+1 to 4,  you call your solve program,
>> and find out that x=3.
>> So "all" you need is solve.
>>
>> Your example requires solving cos(a)=1 for a.  Unfortunately there are an
>> infinite number of solutions, not just a=0.
>> <snip>
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> 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/36c7ae5f-3475-4c5e-b399-6a84c2703919%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 [email protected].
To post to this group, send email to [email protected].
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%3D6L_SCebgfvjcL-W1UTbzdxLLChSPf763d%3D1F0rh1Zqy2g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to