I really don't understand rules and patterns



On 5/22/2013 10:28 PM, Robert Dodier wrote:
> 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);

Why don't you just pick out the coefficient of dq,   which is presumably 
(<integer>+ <some_index)  and the remainder which is what
you call var.  bothcoef, for example, does that.  Or you can use 
patterns, but see below.


It seems that what you are selecting is for in this particular command is
  some pattern of a sum which involves 3 terms, of which two are products.
Your example is a sum involving 3 terms of which only one is a product, 
so it doesn't match.


Unless your pattern is coincidentally presented in exactly the same 
order as the sum and the
same number of terms, it may not match.  That's why you

Maxima's defrule does not try many different possible ways of matching, 
commuting terms etc
within sums unless it can pick out coefficients by using constants e.g. 
dq  as anchors.

A pattern like   var + A*dq  should work   though,  and you can check 
separately
  that A looks like int+index.  A pattern  int+index is not good though, 
because   it won't match
just an index.  and int to 0.



I suggest that for clarity you use functions like lambda([s], 
member(s,[x,y,z]))  instead of re-using x.




>>
>> 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
>
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima