Fortran array rank for matmul intrinsic

前端 未结 2 1153
轻奢々
轻奢々 2020-12-22 08:11

The following link https://gcc.gnu.org/onlinedocs/gcc-5.4.0/gfortran/MATMUL.html clearly states that gfortran expects matrices input to matmul to be of rank 1

2条回答
  •  有刺的猬
    2020-12-22 08:30

    You must do this for a tensor product of two vectors

    Program scratch
      integer, parameter :: dp = kind(1.d0)
      real(dp) :: A(10,1)=reshape((/0,1,2,3,4,5,6,7,8,9/), (/ 10, 1 /))
      real(dp) :: B(1,10)=reshape((/0,1,2,3,4,5,6,7,8,9/), (/ 1, 10 /))
      real(dp) :: C(10,10)
      print *,rank(A),rank(B)
      C=matmul(A,B)
      print *, C
    End Program scratch
    

    If you do

       A(1,10)
       B(10,1)
    

    you will get a scalar product. With just two 1D arrays it is not clear which of the two products you want (although for a dot product there is a special function available).

    A or B can be a 1D array when you are multiplying a matrix by a vector.

提交回复
热议问题