Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
470 views
in Technique[技术] by (71.8m points)

gfortran - Fortran array rank for matmul intrinsic

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 OR 2. However the following snippet wont compile:

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(A,B)  
End Program scratch

gfortran gives the error:

$gfortran scratch.f90 
scratch.f90:6:13:

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

My gfortran is 5.4.0 (compatible with the link above). Am I doing something really stupid?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...