使用矩阵作为函数的参数和Fortran子程序的输出



我试图创建一个程序,要求我使用矩阵作为函数和子程序的输入,还要求我将矩阵作为Fortran中的子程序输出。但是,我在这样做时遇到了多个错误。我不能理解这些错误的来源,因此如何修复它们。我对逻辑很有信心,但我似乎在处理矩阵时犯了错误。

求解线性方程组的程序(部分轴向高斯消去法)

代码:

program solving_equations
implicit none
real, allocatable :: a(:,:),interchanged(:,:)
real, allocatable :: x(:)
real addition,multiplying_term,alpha,maximum
integer i,j,row,rth_ele,f_maxfinder,k,n,s,inte
read(*,*)n
allocate(  a( n,(n+1) )  )
allocate( x(n) )
allocate(  interchanged( n,(n+1) )  )
do i=1,n
read(*,*)( a(i,j),j=1,(n+1) )
end do
do rth_ele= 1,(n-1) 
row=f_maxfinder( a , n , rth_ele )
if (row==rth_ele) then
continue
else
call interchanger(a,rth_ele,row,n,interchanged)
a = interchanged

end if      

do i= (rth_ele+1) , n
! once i is fixed, multiplying term is fixed too
multiplying_term=( a(i,rth_ele)/a(rth_ele,rth_ele) )

do j=1,(n+1)
a(i,j)=a(i,j)-a(rth_ele,j)*multiplying_term
end do
end do   
end do      
x(n)=a(n,n+1)/a(n,n)
do i=(n-1),1,-1
addition=0.0

do s=n , (i+1) , -1
addition=addition+a(i,s)*x(s)
end do
x(i)= (   ( a(i,n+1)- addition  )/a(i,i)  )
end do
do i=1,n
print*,x(i)
end do
endprogram solving_equations
!=================
function f_maxfinder(a,n,rth_ele)
integer inte,f_maxfinder
real maximum
maximum=a(rth_ele,rth_ele)
do inte=n,nint(rth_ele+1),-1
if(  a(inte,rth_ele) > maximum  ) then
maximum = a(inte,rth_ele)
f_maxfinder=inte

else
continue

end if
end do
end  

subroutine interchanger( a,rth_ele,row,n,interchanged )
integer i
real alpha
real, allocatable :: interchanged(:,:)
allocate(  interchanged( n,(n+1) )  )
do i=1,n+1
alpha=a(row,i)
a(row,i)=a(rth_ele,i)
a(rth_ele,i)=alpha
end do
do i=1,n
do j=1,(n+1)
interchanged(i,j)=a(i,j)
end do
end do
end

错误:

row=f_maxfinder( a , n , rth_ele )
1
Warning: Rank mismatch in argument 'a' at (1) (scalar and rank-2)
a(row,i)=a(rth_ele,i)
Error: The function result on the lhs of the assignment at (1) must have the pointer attribute.
a(rth_ele,i)=alpha
Error: The function result on the lhs of the assignment at (1) must have the pointer attribute.
call interchanger(a,rth_ele,row,n,interchanged)
1
Error: Explicit interface required for 'interchanger' at (1): allocatable argument

谢谢!

  1. 您在f_maxfinder中缺少a作为数组的声明。implicit none是你的朋友——一定要一直使用它。
  2. interchanger有一个虚拟参数interchanged,它是一个可分配的假定形状数组。这需要在调用者中可见interchanger的显式接口。(关于这方面的更多信息,请参阅我的帖子https://stevelionel.com/drfortran/2012/01/05/doctor-fortran-gets-explicit-again/。

接口问题可以通过将子程序放在一个模块中,并在主程序中添加该模块的use来解决。

顺便说一下,没有必要使af_maxfinder中可分配,因为您没有分配或释放它。它仍然是一个假定形状的数组,因此仍然需要显式接口。

这是一个考虑了@SteveLionel的建议和以下注释的工作示例:

  1. 始终使用implicit none,至少在主程序中使用一次,不要忘记将-warn标志传递给编译器。
  2. 要么为函数和子程序使用module,然后将use <module>添加到主程序中,要么简单地使用contains并将它们包含在主程序中,就像我下面所做的那样。
  3. interchanged数组已经在主程序中分配了,你不需要在interchanger子程序中重新分配它,只需要将它作为一个假设形状的数组传递。
  4. 删除未使用的变量;alpha, maximum, k, inte
  5. f_maxfinder函数中定义a
  6. 为了可读性,函数类型最好写在函数名前面;查看f_maxfinder的定义,不要在主程序中再次声明该函数,除非你使用显式接口。
  7. nint过程接受real输入,这里不需要。
  8. 最后在你的函数/子程序中添加任何缺失的变量声明。
program solving_equations
implicit none
real, allocatable :: a(:,:), interchanged(:,:), x(:)
real :: addition, multiplying_term
integer :: i, j, row, rth_ele, n, s
read (*,*) n
allocate ( a( n,(n+1) ) )
allocate ( x( n ) )
allocate ( interchanged( n,(n+1) ) )
do i = 1,n
do j = 1,(n+1)
read (*,*) a(i,j)
end do
end do
do rth_ele = 1,(n-1)
row = f_maxfinder( a , n , rth_ele )
if (row == rth_ele) then
continue
else
call interchanger(a, rth_ele, row, n, interchanged)
a = interchanged
end if
do i = (rth_ele+1) , n
! once i is fixed, multiplying term is fixed too
multiplying_term = a(i,rth_ele) / a(rth_ele,rth_ele)
do j = 1,(n+1)
a(i,j) = a(i,j) - a(rth_ele,j) * multiplying_term
end do
end do
end do
x(n) = a(n,n+1) / a(n,n)
do i = (n-1),1,-1
addition = 0.0
do s = n,(i+1),-1
addition = addition + a(i,s) * x(s)
end do
x(i)= (a(i,n+1) - addition) / a(i,i) 
end do
do i = 1,n
print *, x(i)
end do
contains
integer function f_maxfinder(a, n, rth_ele)
integer :: n, rth_ele, inte
real :: maximum, a(:,:)
maximum = a(rth_ele,rth_ele)
do inte = n,rth_ele+1,-1
if (a(inte,rth_ele) > maximum) then
maximum = a(inte,rth_ele)
f_maxfinder = inte
else
continue
end if
end do
end
subroutine interchanger( a, rth_ele, row, n, interchanged )
integer :: i, rth_ele, row, n
real :: alpha, a(:,:), interchanged(:,:)
do i = 1,n+1
alpha = a(row,i)
a(row,i) = a(rth_ele,i)
a(rth_ele,i) = alpha
end do
do i = 1,n
do j = 1,(n+1)
interchanged(i,j) = a(i,j)
end do
end do
end
end program solving_equations

输入一个3 × 4的示例数组,您将得到以下输出(检查结果,您知道您的算法):

3
4
3
6
3
7
4
6
7
4
4
2
0
2.05263186
-2.15789509
0.210526198
Process returned 0 (0x0)   execution time : 1.051 s
Press any key to continue.

相关内容

  • 没有找到相关文章

最新更新