fortran - "array cannot have a deferred shape"在fortran中是什么意思?

标签 fortran gfortran fortran2003

我有一个简单的 Fortran 函数来计算克罗内克乘积:

function kron(A, B)
    implicit none
    real, intent(in) :: A(:, :), B(:, :)
    integer :: i, j, ma, na, mb, nb
    real, dimension(:, :) :: kron

    ma = ubound(A, 1)
    na = ubound(A, 2)
    mb = ubound(b, 1)
    nb = ubound(b, 2)
    forall(i=1:ma, j=1:na)
        kron(mb*(i-1)+1:mb*i, nb*(j-1)+1:nb*j) = A(i,j)*B
    end forall
end function kron

它位于模块内部,但是当我使用 gfortran -static -ffree-form -std=f2003 -Wall 编译它时,出现以下错误:

function kron(A, B)
                1
Error: Array 'kron' at (1) cannot have a deferred shape

发生此错误是因为您应该事先知道要返回的数组的大小吗?

最佳答案

这正是错误告诉您的:kron 必须具有明确的形状。如果您不想预先传递数组大小,则必须将 kron 定义为

real, dimension(lbound(a,dim=1):ubound(a,dim=1),&
                lbound(a,dim=2):ubound(a,dim=2)) :: kron

使用上面这个特定的显式声明确实可以在 gfortran 4.6.3 上进行编译。

关于fortran - "array cannot have a deferred shape"在fortran中是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18769827/

相关文章:

c - 从 Fortran 调用的 C 函数中缺少参数

fortran - Fortran 中函数返回值的直接索引

fortran - 从整数转换为实数(dp)时自动分配失败

fortran - 带有 BLAS 的 OpenMP

matlab - 在 Fortran 中实现匿名函数

fortran - 有没有办法在 Fortran 中关联数组切片索引?

fortran - 为什么SCons构建的成功取决于variant_dir名称?

fortran - 如何将输出写入 Fortran 中的字符串?

python - 使用 CFFI 在 Fortran 中使用 Python 函数。 cffi 构建中的警告。我无法从控制台或文件中打印得到结果,但运行时没有错误