Matmul

Function Type:

Matmul is a transformational function that works on arrays. Please note that the arrays passed to the function must be of the same type ( INTEGER, REAL, LOGICAL or COMPLEX ).

Function Purpose:

The function takes two matrices ( of rank one or two ) and preforms matrix multiplication on them.

Required Arguments:

matmul ( matrixA, matrixB )

matrixA

MatrixA can be an array of either rank one or two of a numerical or logical type.

matrixB

First of all, matrixB can be of a different numeric type than matrixA ( matrixA is real, matrixB can be integer ). However, if matrixA is of type logical then matrixB must be of type logical as well. Next, the last dimension of matrixA must equal the first dimension of matrixB. If matrixA is of rank two, then matrixB can either have rank one or two. Also, note that if matrixA is of rank one, then matrixB must be of rank two.

Optional Arguments:

None.

Results:

Don't worry if you have not had Linear Algebra yet ( Math 220 for most Penn State Students ), the following examples are written to provide an explanation of how matrix multiplication is carried out.

Say we have the following matrices x(3,3), y(2,3) and z(3) in Fortran

	x = 1 2 3       y = 3 2 1     Z = -2  
	    3 2 1          -2 4 1          3
 	   -4 3 1                          0

Remember when multiplying two matrices together with matmul ( or anytime matrix multiplication is carried out ) the last dimension of the first array must be equal to the first dimension of the second array. Note that the resultant matrix from any multiplication will then have dimensions that are equal to the first dimension of the first array and a second dimension equal to the second dimension of the second array. Based on that, the only valid lines of code in Fortran (aside from the obvious "matmul(x,x)") are:
		matmul(y,x)
		matmul(x,z)
		matmul(y,z)
with resultant arrays of dimension (2,3), (3,3), (3,1) and (2,1). The reason why these are the only valid matrix multiplication are because of the way the multiplication is carried out. This is how matmul(y,x) works. The first value ( position 1,1 ) of the resultant matrix (which is 2,3) is generated by adding three numbers. The first number in the sum is found by multiplying the first number in row one of array y by the first number in the first column of array x. The second number in the sum is then found by multiplying the second number of row one of matrix y by the second number of column one of matrix x. The last of the numbers in the sum is finally found by multiplying the last number in the first row of y by the last number in column one of x. In this case you would get 3 + 6 - 4 which equals five.

The first number in row two of the resultant matrix is found in a similar way. All that has to be done is multiply all the numbers of row two of y by the numbers of column one of x in the same manner that was done to get the previous value. So, at this point one should have the following values in the resultant matrix:
		( 3 + 6 - 4 )   ?     ?       
		(-2 + 12 -4 )   ?     ?
The last four values of the matrix can then be found by multiplying the same two rows of matrix y by different columns in x. For instance, the two missing values in the second column of the resultant are found by multiplying those two rows by the second column of x and the last two are found by multiplying those rows of y by the third column of x. This means that the resultant of this matrix multiplication will equal:
	  (3+6-4)   ((3*2)+(2*2)+(1*3))   ((3*3)+(1*2)+(1*1))
	  (-2+12-4) ((-2*2)+(4*2)+(3*1))  ((-2*3)+(4*1)+(1*1))

		5 13 12
		6  7 -1
The results of the other two matrix multiplications are as follows:
		matmul(x,y) = an column array with values of 4,0,17
		matmul(y,z) = an column array with values of 0,16

For additional examples an information see:

lecture thirty six

examples matprod.f

Up one level / Home


Written by Jason Wehr : jcw142@psu.edu and Maintained by John Mahaffy : jhm@cac.psu.edu