Pattern of (aa+nn*bb), matchdeclare, defmatch



Maybe you should describe your pattern better.  For example, here is a 
description that is probably NOT
what you mean:

the sum of one symbol squared and  3 times y squared.

Another that I think you don't mean..

Maybe you mean the sum of exactly two items, one of which is precisely 
x^2 and the other is <some constant> times a symbol squared??


I suspect that you mean.

matchdeclare(q, freeof(y))

defmatch(m3, x^2+a*y^2,x,y)

then m3(x^2+3*y^2,x,y)

gives q=3,x=x,y=y

as the match.

If you don't want to specify x and y beforehand, then you should 
reconsider your plan,

x^2+a*y^2  then matches, for example,   0.  by assigning x=0 y=0   and 
many other
possibilities.

So pick out x, pick out y.
RJF


On 11/23/2010 12:05 AM, Rhys Pratt wrote:
> I have tried to design a pattern to match such expressions as x^2+3*y^2 but failed.
> It's only a practice but I am really eager to learn why it failed.
>
> (%i1) matchdeclare([aa,bb], lambda([x], freeof("+",x)), nn, numberp);
> (%o1) done
>
> (%i2) defmatch(match1, aa);
> defmatch: evaluation of atomic pattern yields: aa
> (%o2) match1
>
> (%i3) defmatch(match2, nn*bb);
> (%o3) match2
>
> (%i4) defmatch(match3, aa+nn*bb);
> defmatch: nn*bb will be matched uniquely since sub-parts would otherwise be ambigious.
> (%o4) match3
>
> (%i5) match1(x^2);
> (%o5) [aa=x^2]
>
> (%i6) match1(3*y^2);
> (%o6) [aa=3*y^2]
>
> (%i7) match2(x^2);
> (%o7) false
>
> (%i8) match2(3*y^2);
> (%o8) [nn=3,bb=y^2]
>
> (%i9) match3(x^2+3*y^2);
> assignment: cannot assign to nn*bb
>   -- an error. To debug this try: debugmode(true);
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima