LF Fortran 95

MATMUL Function

Description

The MATMUL function multiplies two matrices.

Syntax

MATMUL (matrix_a, matrix_b)

Arguments

matrix_a is an INTENT(IN) array of type INTEGER, REAL, COMPLEX, or LOGICAL. It may be either rank one or two if matrix_b is of rank two, and must be of rank two if matrix_b is rank one.

matrix_b is an INTENT(IN) array of numerical type if matrix_a is of numerical type, and of LOGICAL type if matrix_a is of LOGICAL type. It may be of rank one or two if matrix_a is of rank two, and must be of rank two if matrix_a is of rank one.

The size of the first dimension must be the same as the size of the last dimension of matrix_a.

Result

If the arguments are of the same numeric type and kind, the result is of that type and kind. If their kind is different, the result kind is that with higher precision.

If the arguments are of different numeric types and neither is of type COMPLEX, the result is of type REAL.

If one or both of the arguments are of type COMPLEX, then the result is COMPLEX.

If the arguments are of type LOGICAL, the result is of type LOGICAL. If their kinds are the same, the result kind is that of the arguments. If their kind is different, the result kind is that of the argument with the greater kind parameter.

The value and shape of the result are as follows:

If matrix_a has shape (n, m) and matrix_b has shape (m, k), the result has shape (n, k). Element (i, j) of the result has the value SUM(matrix_a(i, :) * matrix_b(:, j)) if the arguments are of numeric type and has the value ANY(matrix_a(i, :) * matrix_b(:, j)) if the arguments are of type LOGICAL.

If matrix_a has shape (m) and matrix_b has shape (m, k), the result has shape (k). Element (j) of the result has the value SUM(matrix_a(:) * matrix_b(:, j)) if the arguments are of numeric type and has the value ANY(matrix_a(:) * matrix_b(:, j)) if the arguments are of type LOGICAL.

If matrix_a has shape (n, m) and matrix_b has shape (m), the result has shape (n). Element (i, j) of the result has the value SUM(matrix_a(i, :) * matrix_b(:)) if the arguments are of numeric type and has the value ANY(matrix_a(i, :) * matrix_b(:)) if the arguments are of type LOGICAL.

Example

integer :: a1(2,3),a5(5,2),b3(3),b2(2) complex :: c2(2) a1=reshape((/1,2,3,4,5,6/),shape(a1)) a5=reshape((/0,1,2,3,4,5,6,7,8,9/),shape(a5)) b2=(/1,2/) b3=(/1,2,3/) write(*,"(2i3)") a1 ! writes 1 2 ! 3 4 ! 5 6 write(*,*) matmul(a1,b3) ! writes 22 28 write(*,*) matmul(b2,a1) ! writes 5 11 17 write(*,"(5i3)") a5 ! writes 0 1 2 3 4 ! 5 6 7 8 9 write(*,"(5i3)") matmul(a5,a1) ! writes 10 13 16 19 22 ! 20 27 34 41 48 ! 30 41 52 63 74 c2=(/(-1.,1.),(1.,-1.)/) write(*,*) matmul(a5,c2) ! writes (5.,-5.) five times