Intel® Fortran Compiler Classic and Intel® Fortran Compiler Developer Guide and Reference

ID 767251
Date 3/22/2024
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

MATMUL

Transformational Intrinsic Function (Generic): Performs matrix multiplication of numeric or logical matrices.

result = MATMUL (matrix_a,matrix_b)

matrix_a

(Input) Must be an array of rank one or two. It must be of numeric (integer, real, or complex) or logical type.

matrix_b

(Input) Must be an array of rank one or two. It must be of numeric type if matrix_a is of numeric type or logical type if matrix_a is logical type.

At least one argument must be of rank two. The size of the first (or only) dimension of matrix_b must equal the size of the last (or only) dimension of matrix_a.

Results

The result is an array whose type depends on the data type of the arguments, according to the rules described in Data Type of Numeric Expressions. The rank and shape of the result depends on the rank and shapes of the arguments, as follows:

  • If matrix_a has shape (n, m) and matrix_b has shape (m, k), the result is a rank-two array with shape (n, k).

  • If matrix_a has shape (m) and matrix_b has shape (m, k), the result is a rank-one array with shape (k).

  • If matrix_a has shape (n, m) and matrix_b has shape (m), the result is a rank-one array with shape (n).

If the arguments are of numeric type, element (i, j) of the result has the value SUM((row i of matrix_a) * (column j of matrix_b)). If the arguments are of logical type, element (i, j) of the result has the value ANY((row i of matrix_a) .AND. (column j of matrix_b)).

Example

A is matrix

  [ 2  3  4 ]
  [ 3  4  5 ],

B is matrix

  [ 2  3 ]
  [ 3  4 ]
  [ 4  5 ],

X is vector (1, 2), and Y is vector (1, 2, 3).

The result of MATMUL (A, B) is the matrix-matrix product AB with the value

  [ 29  38 ]
  [ 38  50 ].

The result of MATMUL (X, A) is the vector-matrix product XA with the value (8, 11, 14).

The result of MATMUL (A, Y) is the matrix-vector product AY with the value (20, 26).

The following shows another example:

 INTEGER a(2,3), b(3,2), c(2), d(3), e(2,2), f(3), g(2)
 a = RESHAPE((/1, 2, 3, 4, 5, 6/), (/2, 3/))
 !  a is   1 3 5
 !         2 4 6
 b = RESHAPE((/1, 2, 3, 4, 5, 6/), (/3, 2/))
 !  b is   1 4
 !         2 5
 !         3 6
 c = (/1, 2/)      ! c is [1 2]
 d = (/1, 2, 3/)   ! d is [1 2 3]

 e = MATMUL(a, b)    ! returns 22 49
                     !         28 64

 f = MATMUL(c,a)   ! returns [5 11 17]
 g = MATMUL(a,d)   ! returns [22 28]
 WRITE(*,*) e, f, g
 END

See Also