這是試圖解決的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
非常感謝你,這是非常明確的解釋! – northfly
好的調用,但我不同意這是編譯器可以做的最好的。實際的錯誤(未聲明的數組)比糟糕的語句函數要明顯得多。現代Fortran甚至允許聲明函數散佈在可執行文件之間嗎? – agentp
@george:我同意;我可以用gfortran 4.7.2重現錯誤,但ifort 13給了我一個未聲明的數組/函數錯誤'B',並假定'A'是一個外部函數名。語句函數只能在規範部分中使用,但當然會過時。 – sigma