matrix multiplication



On Fri, 2007-03-23 at 06:32 -0500, Ryan Krauss wrote:
> cross (v1, v2) :=block(M:matrix([[1, 0, 0], [0, 1, 0], [0, 0,1]]),
> M:addrow(M, v1), M:addrow(M, v2), determinant(M))$

very nice trick.

> ...I would like to make it so that it can take row or
> column vectors for inputs v1 and v2.  I think that would work by
> checking the number of rows and columns of v1 and v2 (if they are
> matrices) and transposing if numrows > numcols...

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))$

If v1 is either a list or a row vector, transpose(v1)[1] is a list
with only one element. If v1 is a column vector, transpose(v1)[1] is
a list with more than 1 element.

Regards,
Jaime Villate