Difference between revisions of "MatrixMultiply"

 
(2 intermediate revisions by 2 users not shown)
Line 2: Line 2:
 
[[Category:Doc Status D]] <!-- For Lumina use, do not change -->
 
[[Category:Doc Status D]] <!-- For Lumina use, do not change -->
 
   
 
   
= MatrixMultiply( A, aRow,aCol,  B, bRow, bCol ) =
+
== MatrixMultiply(A, aRow, aCol,  B, bRow, bCol) ==
 +
Performs the matrix multiplication of matrix «A» by matrix «B». 
  
Performs the matrix multiplication of matrix ''A'' by matrix ''B''.  Matrix ''A'' should be indexed by ''aRow'' and ''aCol'', and Matrix ''B'' by ''bRow'' and ''bCol''.  The result is indexed by ''aRow'' and ''bCol''.  The indexes ''aCol'' and ''bRow'' must have the same number of elements.
+
Matrix «A» should be indexed by «aRow» and «aCol», and Matrix «B» by «bRow» and «bCol».  The result is indexed by «aRow» and «bCol».  The indexes «aCol» and «bRow» must have the same number of elements.
  
The indexes ''aRow'' and ''bCol'' should be distinct indexes in order to obtain a 2-D matrix result.  If ''aRow'' and ''bCol'' are the same index, then the trace of ''AB'' is obtained (the trace is the diagonal elements).
+
The indexes «aRow» and «bCol» should be distinct indexes in order to obtain a 2-D matrix result.  If «aRow» and «bCol» are the same index, then the trace of '''AB''' is obtained (the trace is the diagonal elements).
  
= Details =
+
== Details ==
 +
When «aCol» and «bRow» are the same index, [[MatrixMultiply]] is equivalent to
 +
:<code>Sum(A*B, aCol)</code> 
  
When ''aCol'' and ''bRow'' are the same index, MatrixMultiply is equivalent to ''[[Sum]](A*B,aCol)''.  When all four indexes are distinct, it is equivalent to ''[[Sum]](A*B[bRow=aCol],aCol)''. MatrixMultiply is particular convenient with ''aRow'' and ''bRow'' may be the same index (or when ''aCol'' and ''bCol'' may be the same), in which case the use of [[Sum]] to perform the multiplication becomes awkward.
+
When all four indexes are distinct, it is equivalent to  
 +
:<code>Sum(A*B[bRow = aCol], aCol)</code>  
  
= Matrix-Vector multiplication =
+
[[MatrixMultiply]] is particular convenient with «aRow» and «bRow» may be the same index (or when «aCol» and «bCol» may be the same), in which case the use of [[Sum]] to perform the multiplication becomes awkward.
  
 +
== Matrix-Vector multiplication ==
 
MatrixMultiply assumes that both matrixes are 2-D, which is not the case when multiplying a matrix and a vector.  The index parameters are not optional (but will be made so in a future release).
 
MatrixMultiply assumes that both matrixes are 2-D, which is not the case when multiplying a matrix and a vector.  The index parameters are not optional (but will be made so in a future release).
  
However, multiplying matricies and vectors is easily accomplished using [[Sum]], and the awkward case mentioned above in ''Details'' does not occur.  To multiply matrix A with vector x (where the vector is dimensioned by ''J''), you need to identify which dimension of the matrix corresponds to the vector's index.  For example, in the matrix product ''Ax'' where ''x'' is a row vector, the column dimension of ''A'' corresponds to ''x'' 's index.  If ''x'' is a column vector, then it would be ''A'' 's rows.  For illustration, suppose that ''aCol'' corresponds to ''x'' 's index, then the matrix-vector product is:
+
However, multiplying matricies and vectors is easily accomplished using [[Sum]], and the awkward case mentioned above in ''Details'' does not occur.  To multiply matrix «A» with vector ''x'' (where the vector is dimensioned by ''J''), you need to identify which dimension of the matrix corresponds to the vector's index.  For example, in the matrix product '''Ax''' where ''x'' is a row vector, the column dimension of «A» corresponds to ''x'' 's index.  If ''x'' is a column vector, then it would be «A» 's rows.  For illustration, suppose that «aCol» corresponds to ''x'' 's index, then the matrix-vector product is:
[[Sum]](A * x[@J=@aCol], aCol )
+
:<code>Sum(A*x[@J = @aCol], aCol)</code>
  
The dual, where ''A'''s rows correspond to ''x'''s index, the vector-matrix product is:
+
The dual, where «A»'s rows correspond to ''x'''s index, the vector-matrix product is:
[[Sum]](A * x[@J=@aRow]], aRow )
+
:<code>Sum(A*x[@J = @aRow]], aRow)</code>
  
The following [[User-Defined Functions|user-defined function]] encapsulates this as a single function with a usage fairly similar to MatrixMultiply:
+
The following [[User-Defined Functions|user-defined function]] encapsulates this as a single function with a usage fairly similar to [[MatrixMultiply]]:
  
Function MatrixVectorMultiply( M : Array[I,J] ; I,J : Index ; x : Array[J2] ; J2 : Index ) :=
+
:<code>Function MatrixVectorMultiply(M: Array[I, J]; I, J: Index; x: Array[J2]; J2: Index) :=</code>
    if size(J) <> size(J2) then error("indexes J and J2 must be the same length in MatrixVectorMultiply");
+
::<code>if size(J) <> size(J2) then error("indexes J and J2 must be the same length in MatrixVectorMultiply");</code>
    Sum( M * x[@J2=@J], J )
+
::<code>Sum(M*x[@J2 = @J], J)</code>
  
The generality of this function may not be apparent at firstWhen you multiply a matrix and a vector, there are four cases -- the following table shows how all are covered by this function.  Suppose ''M'' is indexed by ''Row'' and ''Col'', and ''x by ''J'', then:
+
It should be evident that this function evaluates <code>Mx</code>, where <code>x</code> is a row vectorYou can also use it for the matrix product <code>xM</code> where <code>x</code> is a column vector just by transposing the order of the index parameters for <code>M</code>, e.g.:
{| border="1"
+
:<code>MatrixVectorMultiply(M, R, C, X, J)     { matrix product M x }</code>
! Matrix product || x is a || Function call
+
:<code>MatrixVectorMultiply(M, C, R, X, J)     { matrix product x M }</code>
|-
 
| M x || row vector || MatrixVectorMultiply(M,R,C,x,J )
 
|-
 
|M x || column vector || MatrixVectorMultiply(M,C,R,x,J)       
 
|-
 
|x M || row vector || MatrixVectorMultiply(M,C,R,x,J)
 
|-
 
|x M || column vector || MatrixVectorMultiply(M,R,C,x,J)
 
|}
 
 
  
== Treating vectors as 2-D matricies ==
+
=== Treating vectors as 2-D matricies ===
 +
An alternative approach sometimes used is to treat vectors as 2-D matrices, where on of those dimensions has only one element. To do this, you define an index with one element:
 +
:<code>Index One := [1]</code>
  
An alternative approach sometimes used is to treat vectors as 2-D matrices, where on of those dimensions has only one element. To do this, you define an index with one element:
+
With this index defined, you can use [[MatrixMultiply]] directly for Matrix-Vector products, e.g.:
Index One := [1]
+
:<code>MatrixMultiply(A, aRow, bCol, x, J, One)</code>
  
With this index defined, you can use '''MatrixMultiply'' directly for Matrix-Vector products, e.g.:
+
The result has the <code>One</code> index.  You can continue carrying the <code>One</code> index along through your expression, then as a final step, [[Subscript]] it away using <code>Result[One = 1]</code>.
MatrixMultiply(A,aRow,bCol,x,J,One)
 
 
 
The result has the One index.  You can continue carrying the One index along through your expression, then as a final step, [[Subscript]] it away using ''Result[One=1]''.
 
 
 
= See Also =
 
  
 +
== See Also ==
 +
* [[DotProduct]]
 +
* [[Determinant]]
 +
* [[Decompose]]
 
* [[Transpose]]
 
* [[Transpose]]
 
* [[Sum]]
 
* [[Sum]]
 +
* [[Matrix functions]]
 +
* [[:Category:Matrix Functions]]

Latest revision as of 01:36, 22 January 2016


MatrixMultiply(A, aRow, aCol, B, bRow, bCol)

Performs the matrix multiplication of matrix «A» by matrix «B».

Matrix «A» should be indexed by «aRow» and «aCol», and Matrix «B» by «bRow» and «bCol». The result is indexed by «aRow» and «bCol». The indexes «aCol» and «bRow» must have the same number of elements.

The indexes «aRow» and «bCol» should be distinct indexes in order to obtain a 2-D matrix result. If «aRow» and «bCol» are the same index, then the trace of AB is obtained (the trace is the diagonal elements).

Details

When «aCol» and «bRow» are the same index, MatrixMultiply is equivalent to

Sum(A*B, aCol)

When all four indexes are distinct, it is equivalent to

Sum(A*B[bRow = aCol], aCol)

MatrixMultiply is particular convenient with «aRow» and «bRow» may be the same index (or when «aCol» and «bCol» may be the same), in which case the use of Sum to perform the multiplication becomes awkward.

Matrix-Vector multiplication

MatrixMultiply assumes that both matrixes are 2-D, which is not the case when multiplying a matrix and a vector. The index parameters are not optional (but will be made so in a future release).

However, multiplying matricies and vectors is easily accomplished using Sum, and the awkward case mentioned above in Details does not occur. To multiply matrix «A» with vector x (where the vector is dimensioned by J), you need to identify which dimension of the matrix corresponds to the vector's index. For example, in the matrix product Ax where x is a row vector, the column dimension of «A» corresponds to x 's index. If x is a column vector, then it would be «A» 's rows. For illustration, suppose that «aCol» corresponds to x 's index, then the matrix-vector product is:

Sum(A*x[@J = @aCol], aCol)

The dual, where «A»'s rows correspond to x's index, the vector-matrix product is:

Sum(A*x[@J = @aRow]], aRow)

The following user-defined function encapsulates this as a single function with a usage fairly similar to MatrixMultiply:

Function MatrixVectorMultiply(M: Array[I, J]; I, J: Index; x: Array[J2]; J2: Index) :=
if size(J) <> size(J2) then error("indexes J and J2 must be the same length in MatrixVectorMultiply");
Sum(M*x[@J2 = @J], J)

It should be evident that this function evaluates Mx, where x is a row vector. You can also use it for the matrix product xM where x is a column vector just by transposing the order of the index parameters for M, e.g.:

MatrixVectorMultiply(M, R, C, X, J) { matrix product M x }
MatrixVectorMultiply(M, C, R, X, J) { matrix product x M }

Treating vectors as 2-D matricies

An alternative approach sometimes used is to treat vectors as 2-D matrices, where on of those dimensions has only one element. To do this, you define an index with one element:

Index One := [1]

With this index defined, you can use MatrixMultiply directly for Matrix-Vector products, e.g.:

MatrixMultiply(A, aRow, bCol, x, J, One)

The result has the One index. You can continue carrying the One index along through your expression, then as a final step, Subscript it away using Result[One = 1].

See Also

Comments


You are not allowed to post comments.