matrix multiplication



On Fri, 2007-03-23 at 13:22 +0000, Jaime E. Villate wrote:
> try this:
> 
> cross (v1, v2) :=
>     block(M:transpose(matrix([[1, 0, 0], [0, 1, 0], [0, 0,1]])),
>     if length(transpose(v1)[1])=1 then v1: transpose(v1),
>     if length(transpose(v2)[1])=1 then v2: transpose(v2),
>     M: addcol(M,v1), M: addcol(M,v2), determinant(M))$

That function has the problem that cross(v,v) will give 0, rather
than [0,0,0], because determinant returns 0 when two of the columns
are equal. A better definition for cross would be the following:

(%i1) cross (v1, v2) := block([M],
         if length(transpose(v1)[1])=1 then v1: transpose(v1),
         if length(transpose(v2)[1])=1 then v2: transpose(v2),
         M: addcol(v1,v2),
         makelist((-1)^(i-1)*determinant(submatrix(i,M)),i,1,3))$

let's test it:

(%i2) a: [1, 4, 2]$
(%i3) b: matrix([3,1,4])$
(%i4) c: transpose(matrix([2,3,1]))$
(%i5) cross(c,a);
(%o5  [2, - 3, 5]
(%i6) cross(a,c);
(%o6) [- 2, 3, - 5]
(%i7) cross(a,a);
(%o7) [0, 0, 0]
(%i8) cross(b,b);
(%o8) [0, 0, 0]

Cheers,
Jaime