本文介绍了matmul 内在函数的 Fortran 数组排名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下链接 https://gcc.gnu.org/onlinedocs/gcc-5.4.0/gfortran/MATMUL.html 明确指出 gfortran 期望输入到 matmul 的矩阵的等级为 1 或 2.但是以下代码段无法编译:

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 给出错误:

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

我的 gfortran 是 5.4.0(与上面的链接兼容).我做的事情真的很愚蠢吗?

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

推荐答案

您可以使用 RESHAPE 将它们变成 MATMUL 喜欢的形式:

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

这篇关于matmul 内在函数的 Fortran 数组排名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-30 22:02