数组值函数 [英] Array-valued function

查看:95
本文介绍了数组值函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new in the area of Fortran programming and after testing a few programs I already got a feeling of how to write programs. Now I was trying me on a bit harder program and I run into a problem I couldn't solve by myself. I already googled about the problem but I couldn't find an adequate answer ... So I thought maybe there are one or two Fortran programmers which can give me a hand in solving this problem.

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

The program is very simple it just multiplies two matrices with each other. I was trying to write a function which performs the task and returns the result matrix back to the invoker. To make the program a bit more dynamic I used dynamic arrays where the user can specify the dimension the matrices should have. The program looks like this:

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

执行实际乘法的函数如下所示:

Where the function which performs the actual multiplication looks like this:

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

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

My compiler reports the following error:

错误:(1)处的数组索引是等级2的数组

Error: Array index at (1) is an array of rank 2

对我来说,似乎编译器会将变量mulMatrix当作普通数组对待,因此抱怨它,因为我使用它,因为它有四个维度,事实并非如此.但是,如何使编译器将此变量视为具有四个参数而不是数组访问的函数调用? 任何帮助将不胜感激.

For me it seems as the compiler would treat the variable mulMatrix as an usual array and therefore complains about it because I use it as it would have four dimensions which isn't the case. But how can I make the compiler think of this variable as a function call with four parameters instead of an array access? Any help would be appreciate.

推荐答案

正如IRO-bot所述,请使用模块.因为您正在学习一种新语言,所以这将是一个非常好的做法.使用模块,您只需要使用模块,而不必单独声明功能.最好不要使用关键字作为变量名(例如:结果),而使用其他名称.我以为您很好理解了在Fortran 2003中引入的将可分配​​数组作为函数返回值返回的技巧.

As IRO-bot already mentioned, use module. This will be a very good practice since you are learning a new language. With a module, you will just have to use the module instead of declaring individually the functions. It is also a good practice to not use keywords as variable names (example: result), use something else. I supposed that you understand well the trick of returning allocatable arrays as fonction return value, introduced in fortran 2003.

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

Your program can look like this, (the functions are contained in the program file instead of in a separate module, the result is the same)

program matrixMul
implicit none
integer, parameter :: ikind=selected_int_kind(18)
integer(kind=ikind), allocatable, dimension(:,:) :: m1, m2, m3
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)
    m1 = 1
    m2 = 1

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

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

    !deallocate memory
    deallocate(m1, m2, m3)

contains
    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
end program matrixMul

另一种想法是:如果执行线性代数中定义的矩阵乘法,则所得矩阵将为行x行,请参见调用以打印结果. 鱼子酱将不添加矩阵的大小作为参数(请参见M. S. B.评论),并使用内函数LBOUND和UBOUND或SIZE来使它们进入函数.

Another think is: if you are performing the matrix multiplication as defined in linear algebra, the resulting matrix will be rows x rows, see the call to print the result. A caviar will be to not add the size of the matrices as parameters (see M. S. B. comment) and use the intrincsic function LBOUND and UBOUND or SIZE to get them in the function.

这篇关于数组值函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆