From: jpg@spectre.unm.edu (Jeffrey P. Golden)
Subject: an example of comparative CAS programming
Date: Sun, 22 Nov 92 00:35:09 GMT
Organization: Dept. of Math & Stat, University of New Mexico, Albuquerque

Reply-To: jpg@macsyma.com

Given all the recent messages about comparative programming in 
the various CAS, I thought the following might be elucidating.

Barry Simon, "Symbolic Math: Problems and Solutions", 
Notices of the AMS, Vol. 39, No. 7, pp. 700-710, Sept. 1992 
has the following problem:

  9.  Rule based algebra
  Consider the Clifford algebra in 10 variables, that is the 
  complex algebra with ten generators, s0,...,s9 obeying
          si sj + sj si = 0 if i is different from j
          si si = 1
  That is multiplication is NOT commutative (but is assumed
  associative).  Compute (s0+s1+....+s9)^5
  NOTE: This must be done with general methods - it is cheating 
  to use the invariance of the Clifford algebra under orthogonal 
  transformations.

The article gives the following solutions provided by the 
respective CAS vendors:

-------------------------------------------------------------------------

Derive Problem 9.  A solution was provided with 55 lines of function
definition of which the following is typical:
ADD_AUX(u,v,j,k):=~
 IF(j=0,HEAD(v,k),~
    IF(k=0,HEAD(u,j),~
       IF(SIMILAR(ELEMENT(u,j),ELEMENT(v,k)),~
          APPEND(ADD_AUX(u,v,j-1,k-1),~
  ADDSIMILAR(ELEMENT(ELEMENT(u,j),1)+ELEMENT(ELEMENT(v,k),1),ELEMENT(u,j))),~
          IF(BEFORE(ELEMENT(u,j),ELEMENT(v,k)),~
             APPEND(ADD_AUX(u,v,j,k-1),[ELEMENT(v,k)]),~
             APPEND(ADD_AUX(u,v,j-1,k),[ELEMENT(u,j)])))))
Anyone who thinks you can't program with just an IF statement
should look at this.  It's awkward but certainly doable!  The
answer, by the way is 100 times the sum of the sigmas.

Maple Problem 9: Here's the code to set up the rule based algebra:
   s.(0..9):
   readlib(commutat):
   for i from 0 to 9 do
    for j from i+1 to 9 do
     &*(s.j,s.i) := -&*(s.i,s.j);
    od;
    &*(s.i,s.i) := 1;
   od:

   &^ := proc(a,n) local t;
   t := 1;
   to n do
    if type(t,constant) then t := t*a
    else t := expand(t&*a)
    fi
   od;
   t
   end:

   q9 := sum('s.k',k=0..9);
   a9:=q9&^5;

Mathematica Problem 9
  (* rulesfor  Clifford algebra *)
  ncm[s[i_],s[j_]] := -ncm[s[j], s[i]]/;i > j
  ncm[s[i_], s[i_]] := 1
  (* rules for non-commutative multiplication *)
  (* - behavior of multiplication of integers *)
  ncm[a_?NumberQ, b_?NumberQ, c___] := ncm[a b, c]
  (* - distributivity of addition *)
  ncm[m___, a_Plus,b_Plus, n___] :=
          ncm[m, Distribute[tmp[a,b],Plus]/.tmp -> ncm, n]
  (*  - associativity *)
  ncm[a___, ncm[r___], b___] := ncm[a, r, b]
  (* here's the expression *)
  expr = Plus @@ Map[s, Range[1,9]]
  (* and here's the power.  Note the output form involves ncm; this could 
   be formatted to look cleaner for one's individual purposes. *)
  expr^5/.
        x_^n_ :> Fold[ncm, x, Table[x, {n - 1}]]

Reduce Problem 9: Shows simple elegance of the Reduce language.
   operator s;
   noncom s;
   for all i,j such that i>j let s(i)*s(j)=-s(j)*s(i);
   for all i let s(i)*s(i) = 1;
   xxx := for i:=0:9 sum s(i);
   xxx^2;
   xxx*ws^2;

-------------------------------------------------------------------------

Macsyma (from Macsyma Inc.) has a built in package ATENSOR for 
working with Clifford algebras, but here's an alternative solution 
done with Macsyma 417, going back to basic principles and using 
pattern matching:


(c1) matchdeclare([i,j],integerp)$

(c2) tellsimpafter(s[i]^^2,1)$

(c3) tellsimpafter(s[i].s[j],-s[j].s[i],i>j)$

(c4) /* optional: */  compile_rule(all)$

(c5) factor(expand(sum(s[i],i,0,9)^^5));

(d5)    100 (s  + s  + s  + s  + s  + s  + s  + s  + s  + s )
              9    8    7    6    5    4    3    2    1    0

-------------------------------------------------------------------------


You should of course make your own judgments but I don't find the 
Derive, Maple, or Mathematica solutions to be elegant.  

One easily sees the price one pays with Maple due to its lack of 
pattern matching - notice all the enumerated assignments that are 
being made!  I don't know if a better solution is possible with 
Maple VR2.

And notice all the extra stuff that's required in the Mathematica 
solution!

I think the Reduce solution would be the most elegant if it weren't 
for the     xxx^2;  xxx*ws^2;   .  Does the simpler  xxx^5; 
not work, or is this circumlocution done just for the sake of 
efficiency?

But I also think the Macsyma solution is pretty elegant too.


From: Jeffrey P. Golden <jpg@macsyma.com>
Organization: Macsyma Inc.
Reply-To: jpg@macsyma.com
Newsgroups: sci.math.symbolic