错误:语句函数是递归的

时间:2012-12-29 03:32:56

标签: recursion module fortran fortran90 subroutine

这是试图解决3 * 3的线性方程并打印出结果,但它在注释行中出现了问题:

我在程序之外定义了LinearSolution模块,我应该在里面定义它吗?有什么区别?

为什么它说该语句是递归的,你知道,当我使用这些语句作为普通子程序而不是模块子程序时,它们被证实是正常的。

module LinearSolution
    type LAE
        integer::N
        double precision,dimension(:,:),allocatable::A
        double precision,dimension(  :),allocatable::B
    contains
        procedure,nopass::RowReduction
    end type LAE
contains
    subroutine RowReduction
        double precision::C
        do k=1,N
            do i=k+1,N
                if(A(k,k)/=0) then
                    C=A(i,k)/A(k,k)
                    B(i)=B(i)-B(k)*C       !error: Statement Function is recursive
                    do j=k+1,N
                        A(i,j)=A(i,j)-A(k,j)*C   !error: Statement Function is recursive
                    end do
                end if
            end do
        end do

        do k=N,1,-1
            do i=k-1,1,-1
                if(A(k,k)/=0) then
                    C=A(i,k)/A(k,k)
                    B(i)=B(i)-B(k)*C  !error: Statement Function is recursive
                end if
            end do
        end do

        do k=1,N
            if(A(k,k)/=0) then
                B(k)=B(k)/A(k,k)  !error: Statement Function is recursive
            end if
        end do
    end subroutine RowReduction
end module LinearSolution

program TestLAE
    use LinearSolution  !fatal error: cant open module file LinearSolution.mod for reading
    type(LAE)::LAE1
    LAE1%N=3
    allocate(LAE1%B(1:N))
    allocate(LAE1%A(1:N,1:N))
    LAE1%B=(/1,1,1/)
    LAE1%A=(/2,0,0,0,2,0,0,0,2/)
    call LAE1%RowReduction
    print*, LAE1%B(1),LAE1%B(2),LAE1%B(3)
end program

1 个答案:

答案 0 :(得分:5)

通常情况如此,implicit none是你的朋友。

让我们一次一个地接受错误:

B(i)=B(i)-B(k)*C       !error: Statement Function is recursive

编译器在此上下文中无法识别B;这里没有声明B的变量(*),所以它能做的最好就是假设它是一个实值statement function,将B定义为I的函数。声明函数节省空间但令人困惑的方式定义函数内联,但除此之外,它们不能递归;在这里,您将根据B(i)来定义B(i),这显然会失败。

(*)但是!你哭。 B是我的类型LAE中的数组字段!是的,但我们不是在LAE的背景下;事实上,在这个函数的上下文中,没有LAE类型的变量甚至可以使用B值。这是因为程序定义为nopass;你需要有一个变量,它是正在操作的对象LAE,以便我们可以访问这些字段。看起来像这样:

    type LAE
        !...
    contains
        procedure::RowReduction
    end type LAE

contains
    subroutine RowReduction(self)
        class(LAE), intent(InOut) :: self
        double precision::C
        integer :: i, j, k
        do k= 1, self%N
            do i= k+1, self%N
                if( self%A(k,k) /= 0 ) then
        !....

请注意,我们必须将self定义为class(LAE)而不是类型; class是类型的超集,在处理包含具有(重新)可分配组件的对象的可扩展对象时是必需的。另请注意,我们添加了隐式none,它会立即告诉您B未定义,因此指定了整数索引i,j和k。

NAB被正确引用为self的字段后,其余大部分程序都是正确的。请注意,您必须reshape LAE1%A数组:

LAE1%A=reshape((/2,0,0,0,2,0,0,0,2/), (/N, N/))

但是其他方面看起来很好。

module LinearSolution
implicit none

    type LAE
        integer::N
        double precision,dimension(:,:),allocatable::A
        double precision,dimension(  :),allocatable::B
    contains
        procedure::RowReduction
    end type LAE

contains
    subroutine RowReduction(self)
        class(LAE), intent(InOut) :: self
        double precision::C
        integer :: i, j, k
        do k= 1, self%N
            do i= k+1, self%N
                if( self%A(k,k) /= 0 ) then
                    C = self%A(i,k) / self%A(k,k)
                    self%B(i) = self%B(i)- self%B(k)*C
                    do j=k+1, self%N
                         self%A(i,j) = self%A(i,j) - self%A(k,j)*C
                    end do
                end if
            end do
        end do

        do k = self%N,1,-1
            do i=k-1,1,-1
                if( self%A(k,k)/=0) then
                    C= self%A(i,k)/ self%A(k,k)
                    self%B(i)= self%B(i)- self%B(k)*C
                end if
            end do
        end do

        do k=1, self%N
            if( self%A(k,k)/=0 ) then
                self%B(k) = self%B(k) / self%A(k,k)
            end if
        end do
    end subroutine RowReduction
end module LinearSolution

program TestLAE
    use LinearSolution
    implicit none

    integer, parameter :: N = 3
    type(LAE)::LAE1
    LAE1%N=N
    allocate(LAE1%B(1:N))
    allocate(LAE1%A(1:N,1:N))
    LAE1%B=(/1,1,1/)
    LAE1%A=reshape((/2,0,0,0,2,0,0,0,2/), (/N, N/))
    call LAE1%RowReduction
    print*, LAE1%B(1),LAE1%B(2),LAE1%B(3)
end program

跑步给出:

$ gfortran -o lae lae.f90 -Wall -std=f2003
$ ./lae
  0.50000000000000000       0.50000000000000000       0.50000000000000000     
相关问题