Non-exclusive pattern matching? e.g. distributiveness of a function, was: Predictable pattern matching?
Subject: Non-exclusive pattern matching? e.g. distributiveness of a function, was: Predictable pattern matching?
From: Martin Schönecker
Date: Tue, 23 Dec 2008 21:36:19 +0100
Thank you for the detailed description, the definition of a predicate
function was new to me and should be useful. Actually, I think I have
to learn a lot more about Maxima - and maybe forget about Mathematica
while typing in Maxima?
How could I convert, for example, the following Mathematica function
definition, which uses patterns as arguments, into Maxima? It is a
definition of an integration function including (1) distribution of the
function over sums in the first argument, (2) extraction of constants
out of the integral sign, integration of (3) monomials and (4) the constant.
(1) int[a_ + b_, x] := int[a, x] + int[b, x]
(2) int[c_ f_, x] := c int[f, x] /; FreeQ[c, x]
(3) int[x^n_, x] := 1/(n + 1) * x^(n + 1) /; n != -1
(4) int[c_, x] := c x /; FreeQ[c, x]
which results in a useful function,
In[20]:= int[y+const* x^2,x]
Out[20]= (const x^3)/3+x y
(It is useful because sometimes it is faster than the built-in
Integrate[] function)
For the distribution, the sum of any type of expression should be
allowed, so I think non-mutually exclusive patters may/should be used.
Trying to implement the property of distributiveness in Maxima, the
naive attempt
int(a+b, x) := int(a, x) + int(b, x);
yields an error, probably because the + is disturbing in the left hand
side of the definition.
I had no success trying to define the property with a rule,
(%i1) matchdeclare(
[a1, a2], all,
s1, symbolp
)$
(%i2) defrule(
distributeint_r,
int(a1+a2, s1),
int(a1, s1) + int(a2, s1)
);
a2+a1 partitions `sum'
(%o2) distributeint_r:int(a2+a1,s1)->int(a2,s1)+int(a1,s1)
(%i3) int(y + x^2, x);
apply1(%, distributeint_r);
(%o3) int(y+x^2,x)
(%o4) int(y+x^2,x)
Any hints would be appreciated, as well hints to documentation
(currently, I use the Maxima manual - thank you Robert - and MBE by
E.L. Woollett)
Regards,
Martin