1

After a few years of not using Fortran I'm having some troubles with matmul. Suppose I have two matrix: A_{N,K} and B_{J,K} . I want to create a subroutine that takes a row of A and a row of B, multiplies that and creates the scalar C. This is what I wrote:

subroutine test_matmul(A, B, N, K, J, row_a, row_b, C)
  integer, intent(in)                                   :: N, K, J, row_a, row_b
  double precision, dimension(N,K), intent(in)          :: A
  double precision, dimension(J,K), intent(in)          :: B
  double precision, intent(out)                         :: C

  C = matmul(A(row_a,:), B(row_b,:))

end subroutine test_matmul

Alas, when I try to compile this I get the following error:

  C = matmul(A(row_a,:), B(row_b,:))
                         1
Error: 'matrix_b' argument of 'matmul' intrinsic at (1) must be of rank 2

What am I doing wrong?

Ignacio
  • 7,646
  • 16
  • 60
  • 113

1 Answers1

3

The error is clear matmul multiplies two matrices and you are trying to pass two vectors. If you convert the vectors to matrices, matmul will through another error about matrices dimensions mismatch.

You should try using dot_product(vector_a, vector_b) instead of matmul.

Mahmoud Fayez
  • 3,398
  • 2
  • 19
  • 36