首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Fortran中将矩阵用作函数中的参数和子例程中的输出

在Fortran中将矩阵用作函数中的参数和子例程中的输出
EN

Stack Overflow用户
提问于 2021-02-16 03:27:59
回答 2查看 122关注 0票数 0

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

解线性方程组的程序(部分旋转高斯消去法)

代码:

代码语言:javascript
运行
复制
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

错误:

代码语言:javascript
运行
复制
   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

谢谢!

EN

Stack Overflow用户

发布于 2021-02-16 07:04:14

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

  1. 始终使用implicit none,至少在主程序中使用一次,并且不要忘记将-warn标志传递给compiler.
  2. Either使用函数和子例程的代码,然后将use <module>添加到主程序中,或者像我下面所做的那样,简单地使用contains并将它们包含在主程序中。
  3. interchanged数组已经在主程序中分配,您不需要在interchanger子例程中重新分配它,只需将其作为假定形式的code未使用变量传递即可;为了可读性,最好将alpha, maximum, k, inte.
  4. Define function.
  5. Function类型的interface.
  6. The a写在函数名前面;除非您正在使用显式的f_maxfinder f_maxfinder过程接受real输入,否则请参阅f_maxfinder的定义,不要在main程序中再次声明该函数,您不需要它并在function/subroutine.

中添加任何缺少的变量声明

代码语言:javascript
运行
复制
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

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

代码语言:javascript
运行
复制
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.
票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66214235

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档