Subject: I really don't understand rules and patterns
From: Robert Dodier
Date: Thu, 23 May 2013 05:28:21 +0000 (UTC)
On 2013-04-02, Daniel Lakeland <dlakelan at street-artists.org> wrote:
> What is going on here? I want to find sums involving x,y, or z, and an
> index variable, i, j, k and a symbol dq and do something to them. I have
> rules like:
>
> matchdeclare(var,lambda([x],member(x,'[x,y,z])),
> indx,lambda([x],member(x,'[i,j,k])),
> int,integerp);
>
> defrule(recenteridxsum,var+int*dq+indx*dq,[var+indx*dq,int]);
>
> (%i179) recenteridxsum(x+dq*i+dq);
> (%o179) false
>
> This would seem to be wrong. I don't know why it does this though.
I looked at the code generated for #'$recenteridxsum and from what I can
tell, it can't work as intended -- that's a bug. There are calls to
ratcoef and ratsimp which are going to rearrange things. I tried to
puzzle out a way to declare the rule such that the generated code could
work ... no luck.
A different way is to try to match a sum of simple variables, each of
which matches one term of interest.
(%i1) matchdeclare (ii, lambda ([e], member(e, '[i,j,k])));
(%o1) done
(%i2) matchdeclare (nn, integerp);
(%o2) done
(%i3) defrule(rdqi, ii*dq, FOO(ii));
(%o3) rdqi : dq ii -> FOO(ii)
(%i4) defrule(rdqn, nn*dq, BAR(nn));
(%o4) rdqn : dq nn -> BAR(nn)
(%i5) matchdeclare (xx, lambda ([e], member (e, '[x,y,z])));
(%o5) done
(%i6) matchdeclare (dqi, lambda ([e], rdqi(e) # false));
(%o6) done
(%i7) matchdeclare (dqn, lambda ([e], rdqn(e) # false));
(%o7) done
(%i8) defrule (r1, xx + dqi + dqn, BAZ(xx,rdqi(dqi),rdqn(dqn)));
(%o8) r1 : xx + dqn + dqi -> BAZ(xx, rdqi(dqi), rdqn(dqn))
(%i9) r1(z+10*dq+j*dq);
(%o9) BAZ(z, FOO(j), BAR(10))
(%i10) r1(z-10*dq-j*dq);
(%o10) false
(%i11) r1(z-10*dq+j*dq);
(%o11) BAZ(z, FOO(j), BAR(- 10))
(%i12) r1(x+i*dq+dq);
(%o12) BAZ(x, FOO(i), BAR(1))
(%i13) r1(x+i*dq);
(%o13) BAZ(x, FOO(i), BAR(0))
Hope it's not too late to be useful.
best
Robert Dodier