Subject: Rules and simplification question (newbie)
From: Robert Dodier
Date: Tue, 23 May 2006 23:24:13 -0600
Hi Doug,
> I'm new to Maxima.
Terrific!
> I want to apply a rule such that the expression:
> %e^-x^2 is replaced by gp(x)
>
> When x is a variable it works fine, but when x
> is an expression such as r/t, the rule doesn't work:
Well, for the most part the pattern-matching stuff only works
with literal matches. (Rules for * and + will try, not very hard,
to match against arithmetically equivalent expressions.)
So that's why r^2/t^2 didn't work.
I'm guessing that you want to recognize stuff like
exp (-(1/2)*(x^2 - 1)) as a Gaussian density function.
So it seems useful to distinguish one variable (say x)
from the others, and treat any non-x stuff as a coefficient.
Here's a half-hearted attempt to get that idea working.
This stuff assumes the variable of interest is x.
matchdeclare (aa, nz_xcoefp, [bb, cc], freeof (x));
nz_xcoefp (e) := e # 0 and freeof (x, e);
defmatch (qxp, aa*x^2 + bb*x + cc);
matchdeclare (xx2, qxp);
defrule (r1, %e^(-(1/2)*xx2), gp3 (aa, bb, cc));
apply1 (exp (-(1/2)*(x^2/u^2 + v^2*x + sqrt(w))), r1);
=> gp3 (1/u^2, v^2, sqrt(w))
apply1 (exp (-(1/2)*((x - mu)/sigma)^2), r1);
=> gp3 (1/sigma^2, -2*mu/sigma^2, mu^2/sigma^2)
apply1 (1/3 * exp (-x^2/2) + 2/3 * exp (-x^2/4 + 1), r1);
=> gp3 (1, 0, 0) / 3 + 2*gp3 (1/2, 0, -2) / 3
You could have this function gp3 complete the square to
find expressions for mu and sigma.
The defmatch function qxp binds aa, bb, and cc upon
a successful match; that's how gp3 gets its arguments.
HTH
Robert Dodier