9

I have a simple fortran function that computes the Kronecker product:

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

It's inside a module, but when I compile it with gfortran -static -ffree-form -std=f2003 -Wall, I get these errors:

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

Is this error occurring because you're supposed to know the size of the array to be returned beforehand?

4

3 回答 3

6

这正是错误告诉您的内容: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 上使用上面这个特定的显式声明确实为我编译。

于 2013-09-12T16:53:47.820 回答
1

具有 ALLOCATABLE 属性的延迟形状数组称为可分配数组。它的边界和形状是在通过 ALLOCATE 语句为其分配存储空间时确定的。

尝试这个

真实的,意图(in),可分配的,维度(:, :: A(:, :), B(:, :)

于 2018-10-25T17:02:41.020 回答
1

您只需要将可分配数组定义为可分配的,即将 kron 定义替换为;

real, allocatable, dimension(:,:) :: kron

这在 4.6.3 中也可以正常编译,并在以下位置定义: https ://docs.roguewave.com/codedynamics/2017.0/html/index.html#page/TotalViewLH/totalviewlhug-examineing-data.09.10.html

希望这可以为您节省一些精力,特别是考虑到这里不需要定义下限!

于 2019-09-22T04:32:32.880 回答