数组值函数

时间:2014-03-20 00:07:49

标签: fortran fortran90 fortran95

我是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

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

  

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

对我而言,似乎编译器会将变量mulMatrix视为通常的数组,因此我会抱怨它,因为我会使用它,因为它有四个维度,但事实并非如此。但是,如何让编译器将此变量视为具有四个参数而不是数组访问的函数调用? 任何帮助都会很感激。

1 个答案:

答案 0 :(得分:2)

正如IRO-bot已经提到的,使用模块。这是一个非常好的练习,因为你正在学习一门新语言。使用模块,您只需使用模块而不是单独声明功能。不使用关键字作为变量名称(例如:结果),使用其他东西也是一种很好的做法。我认为你很好理解将可分配数组作为fonction返回值返回的技巧,在fortran 2003中引入。

你的程序看起来像这样,(函数包含在程序文件中而不是单独的模块中,结果是一样的)

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将它们放入函数中。