|
From: Richard F. <fa...@be...> - 2017-09-22 18:10:31
|
/*sdp is a program to simplify dot products according to
a multiplication program named dot2.
Comments? Corrections? Neat examples of use?
--
RJF
*/
sdp(z):= sdp1(z,[])$
sdp1(z,sofar):=if mapatom(z) or inpart(z,0)#"."
then
block([subparts: if mapatom(z) then z else map(sdp,z)],
if sofar#[] then
apply(".",reverse(cons(subparts,sofar)))
else subparts)
else
block([newhead:dot2(first(z),second(z))],
/*what can be the result of this product? a single item */
if mapatom(newhead) or
inpart(newhead,0)#"." then return (newhead.sdp1(rest(z),sofar)),
/* final case: something like. a.b or maybe a.b.c returned from dot2
*/
return (sdp1(rest(dotappend(newhead,rest(z,2))),
cons(first(newhead),sofar))))$
/* utility to program around simplification that "."(X) is just X */
dotappend(r,s):= block([a1:args(r),
a2:if mapatom(s) then [s] else args(s)],
apply(".", append(a1,a2)))$
/* define the dot product of exactly 2 items */
/*example*/
dot2(a,b):=m(a,b)$
sdp(a.b.c.d.e);
/*Using a global name "dot2" is not the most refined
programming technique, but it is simple, and works. Here we
redefine dot2*/
dot2(a,b):=q(a).r(b)$
sdp(a.b.c.d.e);
|