Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Fortran 数组值函数_Fortran_Fortran90_Fortran95 - Fatal编程技术网

Fortran 数组值函数

Fortran 数组值函数,fortran,fortran90,fortran95,Fortran,Fortran90,Fortran95,我是Fortran编程领域的新手,在测试了一些程序之后,我已经对如何编写程序有了一种感觉。现在我正在尝试一个更难的程序,我遇到了一个我自己无法解决的问题。我已经在谷歌上搜索过这个问题,但是我找不到一个合适的答案。。。 所以我想也许有一两个Fortran程序员可以帮我解决这个问题 这个程序非常简单,它只是将两个矩阵相乘。我试图编写一个函数来执行任务并将结果矩阵返回给调用程序。 为了使程序更具动态性,我使用了动态数组,用户可以在其中指定矩阵应有的维度。 程序如下所示: program matrixM

我是Fortran编程领域的新手,在测试了一些程序之后,我已经对如何编写程序有了一种感觉。现在我正在尝试一个更难的程序,我遇到了一个我自己无法解决的问题。我已经在谷歌上搜索过这个问题,但是我找不到一个合适的答案。。。 所以我想也许有一两个Fortran程序员可以帮我解决这个问题

这个程序非常简单,它只是将两个矩阵相乘。我试图编写一个函数来执行任务并将结果矩阵返回给调用程序。 为了使程序更具动态性,我使用了动态数组,用户可以在其中指定矩阵应有的维度。 程序如下所示:

program matrixMul
implicit none
integer, parameter :: ikind=selected_int_kind(18)
integer(kind=ikind), allocatable, dimension(:,:) :: m1, m2, result, mulMatrix
integer :: rows, cols, i, j

print *, 'Please enter the number of rows the matrix should have: '
read *, rows

print *, 'Please enter the number of columns the matrix should have: '
read *, cols

!allocate sufficient memory
allocate(m1(rows, cols), m2(cols, rows))

!fill matrixes with numbers entered by the user
call fillMatrix(m1, rows, cols)
call fillMatrix(m2, cols, rows)

result = mulMatrix(m1, m2, rows, cols)

!prints the result matrix to the screen
call printMatrix(result, rows, cols)

!deallocate memory
deallocate(m1, m2, mulMatrix, result)
end program matrixMul
function mulMatrix(m1, m2, r, c) result(mulMat)
implicit none
integer, parameter :: ikind=selected_int_kind(18)
integer(kind=ikind), dimension(r, c) :: m1
integer(kind=ikind), dimension(c, r) :: m2
integer(kind=ikind), allocatable, dimension(:,:) :: mulMat
integer r, c, i, j, k

allocate(mulMat(r,r))

!code which performs calculation is omitted

end function mulMatrix
其中执行实际乘法的函数如下所示:

program matrixMul
implicit none
integer, parameter :: ikind=selected_int_kind(18)
integer(kind=ikind), allocatable, dimension(:,:) :: m1, m2, result, mulMatrix
integer :: rows, cols, i, j

print *, 'Please enter the number of rows the matrix should have: '
read *, rows

print *, 'Please enter the number of columns the matrix should have: '
read *, cols

!allocate sufficient memory
allocate(m1(rows, cols), m2(cols, rows))

!fill matrixes with numbers entered by the user
call fillMatrix(m1, rows, cols)
call fillMatrix(m2, cols, rows)

result = mulMatrix(m1, m2, rows, cols)

!prints the result matrix to the screen
call printMatrix(result, rows, cols)

!deallocate memory
deallocate(m1, m2, mulMatrix, result)
end program matrixMul
function mulMatrix(m1, m2, r, c) result(mulMat)
implicit none
integer, parameter :: ikind=selected_int_kind(18)
integer(kind=ikind), dimension(r, c) :: m1
integer(kind=ikind), dimension(c, r) :: m2
integer(kind=ikind), allocatable, dimension(:,:) :: mulMat
integer r, c, i, j, k

allocate(mulMat(r,r))

!code which performs calculation is omitted

end function mulMatrix
我的编译器报告以下错误:

错误:(1)处的数组索引是秩为2的数组

对我来说,编译器似乎会把变量mulMatrix当作一个普通数组来处理,因此会抱怨它,因为我使用它,因为它有四个维度,而事实并非如此。但是我怎样才能让编译器把这个变量看作是一个带有四个参数的函数调用,而不是数组访问?
如有任何帮助,将不胜感激。

如前所述,请使用模块。这将是一个非常好的实践,因为你正在学习一门新的语言。对于模块,您只需要使用模块,而不是单独声明函数。最好不要使用关键字作为变量名(例如:result),而是使用其他名称。我想您很了解将可分配数组作为函数返回值返回的技巧,这是在fortran 2003中引入的

您的程序可以如下所示(函数包含在程序文件中,而不是单独的模块中,结果相同)

另一个想法是:如果执行线性代数中定义的矩阵乘法,则生成的矩阵将是行x行,请参见打印结果的调用。
鱼子酱将不添加矩阵的大小作为参数(参见M.S.B.注释),并使用Intrincic函数LBOUND和UBOUND或size将它们放入函数中。

从主程序中删除
mulMatrix
数组的声明并在模块内定义函数将解决您遇到的问题。将任何外部过程(函数和子例程)放入模块中,并通过
use somemodule
访问它们,提供了一个明确的接口,并允许编译器检查参数类型不匹配、名称冲突等。同上上述注释。示例:。另外,您还可以选择省略参数
r
c
,用
dimension(:,:)
声明
m1
m2
,用
size
内在函数确定函数中的维度。我将提到内在
matmul
,以防您还没有遇到它。