Fortran array rank for matmul intrinsic

前端 未结 2 1152
轻奢々
轻奢々 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.

    0 讨论(0)
  • 2020-12-22 08:32

    You can use RESHAPE to get them into a form MATMUL will like:

    Program scratch
      real(kind=8) :: A(10)=(/0,1,2,3,4,5,6,7,8,9/)
      real(kind=8) :: B(10)=(/0,1,2,3,4,5,6,7,8,9/)
      real(kind=8) :: C(10,10)
      print *,rank(A),rank(B)
      C = matmul( RESHAPE(A,(/10,1/)), RESHAPE(B,(/1,10/)) )
      WRITE(*,"(10F7.2)") C
    End Program scratch
    
    0 讨论(0)
提交回复
热议问题