# [Maxima] matrix multiplication

Ryan Krauss ryanlists at gmail.com
Fri Mar 23 10:26:00 CDT 2007

```Thanks for your help Jaime.  This is good work.  I can't quite make
the final thing work where I get [0,0,0] for a vector crossed with
itself.  I must be misunderstanding what you final function is.  Here
is my .mac file:

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),
makelist((-1)^(i-1)*determinant(submatrix(i,M)),i,1,3))\$
crossmat (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),
cross2(v1,v2) := determinant(crossmat(v1,v2))\$

I created crossmat to help me debug.  cross2 works but returns the
scalar 0 for A x A.  My cross gives an error coming from the submatrix
determinant:
i:matrix([1],[0],[0]);
j:matrix([0],[1],[0]);
cross(i,j);
`determinant' called on a non-square matrix.
#0: cross(v1=matrix([1],[0],[0]),v2=matrix([0],[1],[0]))(rotation_matrices.mac
line 6)
-- an error.  To debug this try debugmode(true);

(%i5) M:crossmat(i,j);
(%o5) matrix([[1,0,0],1,0],[[0,1,0],0,1],[[0,0,1],0,0])
(%i6) B:submatrix(1,M);
(%o6) matrix([[0,1,0],0,1],[[0,0,1],0,0])
(%i7) determinant(B);
`determinant' called on a non-square matrix.
-- an error.  To debug this try debugmode(true);

What did I do wrong in the final function?

Thanks again,

Ryan

On 3/23/07, Jaime E. Villate <villate at fe.up.pt> wrote:
> 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),
>
> 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),
>          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
>
>
>
```