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

2024-01-20

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

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

Code:

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

Errors:

   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

Thanks!


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

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

顺便说一句,没有必要做a可分配于f_maxfinder,因为您没有分配或取消分配它。它仍然是一个假定形状的数组,因此仍然需要显式接口。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Fortran 中使用矩阵作为函数中的参数和子例程中的输出 的相关文章

  • 将数组中的所有值作为参数传递给函数

    我有一个值数组 a b c d 我需要将它们作为参数传递给函数 window myFunction a b c d 如果我可以将数组 对象传递到函数中 那么这会更容易 但这些函数是由其他人编写的或已经存在 我无法更改它们 它们需要作为单独的
  • 指定 gfortran 应该在其中查找模块的目录

    我目前基于模块来编译程序 例如主程序foo这取决于模块bar 如下 gfortran c bar f90 gfortran o foo exe foo f90 bar o 当foo f90 and bar f90位于同一目录中 如何指定 g
  • 在 JavaScript 中引用 C# 变量

    我已经阅读了很多线程 但我不明白为什么这不起作用 我正在创建一个将用作导航栏的 SharePoint Web 部件 一切都很顺利 直到我尝试在 JS 代码中引用 C 变量 这是来自 VisualWebPart1UserControl asc
  • python 中的基本矩阵转置

    我尝试了 python 中矩阵转置的最基本方法 但是 我没有得到所需的结果 接下来是代码 A 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 print A def TS A B A for i in range len A
  • 仅从 tsv 中的列索引生成“特殊”字典结构

    想象一下这样一个制表符分隔的文件 9606 1 GO 0002576 TAS platelet degranulation Process 9606 1 GO 0003674 ND molecular function z Function
  • Excel - 查找列中不是错误或空白的最后一个值

    我需要在 Excel 电子表格的一列中找到最后一个非错误 非空白值 该列可以有多个 N A 实例 它们与实际值交替 有多种解决方案可用于查找最后一个非空单元格 但这些解决方案不考虑错误 特别是如果最后一个非空单元格出现错误 在这种情况下 解
  • 将数组传递给函数名称冲突

    Specs GNU bash 版本 3 1 17 无法升级 Premise 我一直在摆弄数组 我想知道是否有任何方法可以让函数的本地变量与所述函数外部的数组同名 Example 在下面的示例中 我将尝试显示该问题 Working bin b
  • 根据Python中两行之间的匹配创建一个带有[0,1]的新列

    我正在尝试将多个列表或数据帧与一个大型基础数据帧进行比较 然后对于任何匹配 我想附加一个存储 1 匹配或 0 不匹配的列 df pd DataFrame Name A B C D ID 5 6 6 7 8 9 7 list1 5 6 8 9
  • 如何确定函数是否不返回任何内容?

    有没有办法在 PHP 中使用反射或其他方法来做到这一点 function a return null function b a a null b b null 如果您没有显式返回某些内容 则函数将返回null默认情况下 这就是 PHP 中函
  • python:函数中的变量,点前面是函数名

    我需要理解这个概念 其中我们可以在函数定义中的变量名中使用点 这里没有类定义 也没有模块 Python 不应该接受包含点的变量名 def f x f author sunder f language Python print x f aut
  • 有没有办法从画布上清除一个元素而不消除其他元素?

    我正在使用画布构建页面加载器 并使用 es6 类 虽然目前我无法使其正常工作 原因之一是我找不到清除画布的方法进展 到目前为止 这是我的代码 class Loader constructor width height this width
  • 如何显示 zsh 函数定义(如 bash“type myfunc”)?

    如何在 zsh 中显示函数的定义 type foo没有给出定义 在bash中 bash function foo echo hello bash foo hello bash type foo foo is a function foo e
  • C语言声明数组没有初始大小

    编写一个程序来操纵温度详细信息 如下所示 输入要计算的天数 主功能 输入摄氏度温度 输入功能 将温度从摄氏度转换为华氏度 独立功能 查找华氏度的平均温度 我怎样才能在没有数组初始大小的情况下制作这个程序 include
  • Fortran 在 gdb 中打印可分配数组

    我正在向开源科学代码添加一些功能 我使用很多可分配项 但在正确打印它们时遇到一些问题 例如 我声明并分配 然后使用 real dp allocatable psi n phi some other stuff here allocate p
  • 在查询中实现函数调用(分组运行总计)

    我有一个函数叫做fxGroupedRunningTotal fxGRT 和查询 总计 我想在 Totals 中调用 fxGRT 以便获得一个显示分组运行总计的列 我只能通过导入总计查询来测试 fxGRT 使用总计并调用 fxGRT 的查询
  • 如何从连接矩阵绘制图像?

    我想编写一个脚本来从连接矩阵创建图像 基本上 只要矩阵中有 1 我就希望该区域在图像中被着色 对于例如 我使用 Photoshop 创建了这张图像 但我有一个很大的数据集 所以我必须自动化这个过程 如果有人能指出我正确的方向 那将非常有帮助
  • f951 错误:无法识别的命令行选项

    我在linux上 正在编译以下内容 mpif90 shared source F90 object1 o object2 o L some path Qoption link rpath some path I some path lhdf
  • 如何通过函数注释指示函数需要函数作为参数,或返回函数?

    您可以使用函数注释 http www python org dev peps pep 3107 在python 3中指示参数和返回值的类型 如下所示 def myfunction name str age int gt str return
  • 如何返回包含最大值标签的向量

    我有一个 4 列数组 我想获得一个向量 其中每行包含包含该行最大值的列的标签 我可以在循环中执行此操作 但我想使用矩阵函数来提高速度 我怎样才能在不编写自己的库函数的情况下做到这一点 有一个函数可以做到这一点 如果x是你的矩阵 尝试max
  • Delphi - 如果没有创建类,为什么这个函数可以工作?

    考虑这个类 unit Unit2 interface type TTeste class private texto string public function soma a b integer string end implementa

随机推荐