调用函数返回数组时Fortran错误256

时间:2018-02-09 20:02:30

标签: arrays matrix fortran gaussian

我正在编写一个程序来获取2D数组,每个方程有3个系数和1个解,然后求解变量X,Y和Z.我的问题是基本的行函数交换,比例和pivot,它们都会影响矩阵中的给定行。现在,我在函数完成时使用函数替换输入表达式矩阵,每当我调用每个行函数时就会发生错误。 我正在使用Plato IDE和FTN95编译器

Compiling and linking file: src.f95
C:\Users\vv3383my\Documents\School\Programs\Simultaneous Expression Solver\bin\src.F95(59) : error 256 - INTEGER expression expected in array bounds subscript
C:\Users\vv3383my\Documents\School\Programs\Simultaneous Expression Solver\bin\src.F95(62) : error 256 - INTEGER expression expected in array bounds subscript
C:\Users\vv3383my\Documents\School\Programs\Simultaneous Expression Solver\bin\src.F95(65) : error 256 - INTEGER expression expected in array bounds subscript
C:\Users\vv3383my\Documents\School\Programs\Simultaneous Expression Solver\bin\src.F95(67) : error 256 - INTEGER expression expected in array bounds subscript
C:\Users\vv3383my\Documents\School\Programs\Simultaneous Expression Solver\bin\src.F95(69) : error 256 - INTEGER expression expected in array bounds subscript
C:\Users\vv3383my\Documents\School\Programs\Simultaneous Expression Solver\bin\src.F95(71) : error 256 - INTEGER expression expected in array bounds subscript
C:\Users\vv3383my\Documents\School\Programs\Simultaneous Expression Solver\bin\src.F95(73) : error 256 - INTEGER expression expected in array bounds subscript
Compilation failed.

我的代码如下:

program expSolver

!declare variables
  implicit none
  integer, parameter :: n=10
  integer, parameter :: expCount=3
  real, dimension(n,4) :: exp
  real :: sol(expCount)
  real :: rowEchelon
  !basically a for loop
  !do i=1,n
  !  print*, i
  !enddo

  exp(1,1)=2
  exp(1,2)=12
  exp(1,3)=17
  exp(1,4)=-32
  exp(2,1)=-3
  exp(2,2)=-7
  exp(2,3)=-8
  exp(2,4)=6
  exp(3,1)=1
  exp(3,2)=4
  exp(3,3)=6
  exp(3,4)=-7

  call printArr(exp, expCount, n)
  sol(3) = rowEchelon(exp, expCount, n)
  print*,sol(3)
end program

subroutine printArr(exp, expCount, n)
  integer :: expCount, n
  real exp(n,4)
  integer :: i
  do i=1,expCount
    write(*,*) exp(i,1), exp(i,2), exp(i,3), "|", exp(i,4)
  enddo
  return
end
!Puts augmented matrix into row echelon for and returns for z
real function rowEchelon(exp, expCount, n)
  implicit none
  real :: rowEchelon
  integer :: x=-1
  integer :: expCount, n
  integer :: i
  real, dimension(n,4) :: exp
  real, dimension(n,4) :: swapRow, scaleRow, pivotRow
  print*, "Putting matrix into row echelon form..."
  do i=1, expCount
    if(exp(i,1) == 1.0)then
      x=i
    endif
  enddo
  if(.NOT.(x == -1))then
    print*, "swap"
    exp = swapRow(exp, n, 1, x)
  else
    print*, "scale"
    exp = scaleRow(exp, n, 1, real(1/exp(1,1)))
  endif
  print*, "pivot"
  exp = pivotRow(exp, n, 2, 1, real(-(exp(2,1))))
  print*, "pivot"  
  exp = pivotRow(exp, n, 3, 1, real(-(exp(3,1))))
  print*, "scale"
  exp = scaleRow(exp, n, 2, real(1/exp(2,2)))
  print*, "pivot"
  exp = pivotRow(exp, n, 3, 2, real(-(exp(3,2))))
  print*, "scale"
  exp = scaleRow(exp, n, 3, real(1/exp(3,3)))
  rowEchelon = exp(3,4)
  return
end
!Matrix function swap row take the index of 2 rows and swaps their contents
function swapRow(exp, n, index1, index2)
  implicit none
  integer :: n, index1, index2, i
  real, dimension(n,4) :: swapRow, exp
  swapRow = exp
  do i=1, 4
    swapRow(index1, i) = exp(index2, i)
    swapRow(index2, i) = exp(index1, i)
  enddo
  call printArr(swapRow)
  return
end function swapRow
!Matrix function scale row multiplies all values in a row by a given factor
function scaleRow(exp, n, index, fac) 
  integer :: n, index, i
  real :: fac
  real :: exp(n, 4)
  real, dimension(n, 4) :: scaleRow
  scaleRow = exp
  do i=1, 4
    scaleRow(index, i) = exp(index, i) * fac
  enddo
  call printArr(scaleRow)
  return
end
!Matrix function pivot row multiplies a row at index 2 by a given factor fac then adds to another row at index 1
function pivotRow(exp, n, index1, index2, fac)
  integer :: n, index1, index2, i
  real :: fac
  real :: exp(n, 4), pivotRow(n, 4)
  pivotRow = exp
  do i=1, 4
    pivotRow(index1, i) = exp(index1, i) + (exp(index2, i) * fac)
  enddo
  call printArr(pivotRow)
  return
end

我遇到问题的区域在函数rowEchelon中:

!Puts augmented matrix into row echelon for and returns for z
real function rowEchelon(exp, expCount, n)
  implicit none
  real :: rowEchelon
  integer :: x=-1
  integer :: expCount, n
  integer :: i
  real, dimension(n,4) :: exp
  real, dimension(n,4) :: swapRow, scaleRow, pivotRow
  print*, "Putting matrix into row echelon form..."
  do i=1, expCount
    if(exp(i,1) == 1.0)then
      x=i
    endif
  enddo
  if(.NOT.(x == -1))then
    print*, "swap"
    exp = swapRow(exp, n, 1, x)
  else
    print*, "scale"
    exp = scaleRow(exp, n, 1, real(1/exp(1,1)))
  endif
  print*, "pivot"
  exp = pivotRow(exp, n, 2, 1, real(-(exp(2,1))))
  print*, "pivot"  
  exp = pivotRow(exp, n, 3, 1, real(-(exp(3,1))))
  print*, "scale"
  exp = scaleRow(exp, n, 2, real(1/exp(2,2)))
  print*, "pivot"
  exp = pivotRow(exp, n, 3, 2, real(-(exp(3,2))))
  print*, "scale"
  exp = scaleRow(exp, n, 3, real(1/exp(3,3)))
  rowEchelon = exp(3,4)
  return
end

行函数本身在这里:

矩阵函数交换行取2行索引并交换其内容

function swapRow(exp, n, index1, index2)
  implicit none
  integer :: n, index1, index2, i
  real, dimension(n,4) :: swapRow, exp
  swapRow = exp
  do i=1, 4
    swapRow(index1, i) = exp(index2, i)
    swapRow(index2, i) = exp(index1, i)
  enddo
  call printArr(swapRow)
  return
end function swapRow

矩阵函数比例行将给定因子

中的所有值相乘
function scaleRow(exp, n, index, fac) 
  integer :: n, index, i
  real :: fac
  real :: exp(n, 4)
  real, dimension(n, 4) :: scaleRow
  scaleRow = exp
  do i=1, 4
    scaleRow(index, i) = exp(index, i) * fac
  enddo
  call printArr(scaleRow)
  return
end

矩阵函数轴行将索引2处的行乘以给定因子fac然后添加到索引1处的另一行

function pivotRow(exp, n, index1, index2, fac)
  integer :: n, index1, index2, i
  real :: fac
  real :: exp(n, 4), pivotRow(n, 4)
  pivotRow = exp
  do i=1, 4
    pivotRow(index1, i) = exp(index1, i) + (exp(index2, i) * fac)
  enddo
  call printArr(pivotRow)
  return
end

这是我第一次在Fortran编程,所以我可能会遗漏一些明显的东西,虽然我确实让程序在Java中运行,所以我知道我的数学是正确的。此外,我知道我的程序不允许更多的表达式,并且所有内容都是硬编码的,我只是想确保它能够首先解决这个简单的矩阵。

0 个答案:

没有答案