Fortran95中的决定因素

时间:2016-03-12 15:51:43

标签: recursion matrix fortran gfortran determinants

fortran中的这段代码使用laplacian公式(由未成年人扩展)计算nxn矩阵的行列式。我完全理解这个过程是如何运作的。

但是有人可以让我深入了解下面的代码如何操作,比如给定的迭代,代码的这一部分包含递归函数行列式(矩阵) - 假设读入并传递了一些nxn矩阵并且另一个函数打电话给辅助因子。我理解的代码有一些方面,但它的递归令我深感困惑。我试图用3x3矩阵逐步完成,但无济于事。

! Expansion of determinants using Laplace formula
recursive function determinant(matrix) result(laplace_det)
real, dimension(:,:) :: matrix
integer :: msize(2), i, n
real :: laplace_det, det
real, dimension(:,:), allocatable :: cf

msize = shape(matrix) 
n = msize(1)          

if (n .eq. 1) then  
  det = matrix(1,1)
else
  det = 0    
  do i=1, n  
    allocate(cf(n-1, n-1))     
    cf = cofactor(matrix, i, 1)
    det = det + ((-1)**(i+1))* matrix(i,1) * determinant(cf)
    deallocate(cf)
  end do        
end if
laplace_det = det
end function determinant       

 function cofactor(matrix, mI, mJ)
  real, dimension(:,:) :: matrix
  integer :: mI, mJ
  integer :: msize(2), i, j, k, l, n
  real, dimension(:,:), allocatable :: cofactor
  msize = shape(matrix)
  n = msize(1)

  allocate(cofactor(n-1, n-1))
  l=0
  k = 1
  do i=1, n
   if (i .ne. mI) then
     l = 1
     do j=1, n
       if (j .ne. mJ) then
         cofactor(k,l) = matrix(i,j)
         l = l+ 1
       end if
     end do
     k = k+ 1
   end if
  end do
return
end function cofactor

我正在努力解决的主要部分是这两个调用以及各个辅助因子计算的操作。

cf = cofactor(matrix, i, 1)
det = det + ((-1)**(i+1))* matrix(i,1) * determinant(cf)

非常感谢任何解释的输入(就像我说过一次迭代的例子)。这是我在堆栈溢出中的第一篇文章,因为我的大多数问题都存在于mathstack中(正如你可以从问题的数学性质来看)。虽然我有编程经验,但递归的概念(特别是在这个例子中)实际上令人难以置信。

如果需要任何编辑,请继续,我不熟悉堆栈溢出的礼仪。

1 个答案:

答案 0 :(得分:0)

让我们假设我们将以下3x3矩阵传递给determinant()

2 9 4
7 5 3
6 1 8

在例程中,对i = 1,2,3迭代执行以下两行:

cf = cofactor(matrix, i, 1)
det = det + ((-1)**(i+1))* matrix(i,1) * determinant(cf)

对应于相对于第一列的拉普拉斯展开。更具体地说,通过移除matrix的第cofactor()行和第1列,将上述3x3 i传递给matrix以获得2x2子矩阵。然后将获得的2×2子矩阵(cf)传递到下一行中的determinant()以计算对应于该子矩阵的辅因子。因此,在第一次迭代中,我们试图计算

enter image description here

请注意,右侧的三个决定因素尚未通过后续的determinant()调用来计算。让我们考虑一个这样的后续呼叫,例如为i=1。我们传递了以下子矩阵(存储在cf

5 3
1 8

determinant()。然后,再次重复与上述相同的过程,并且与父3×3矩阵的拉普拉斯展开无关。也就是说,determinant()现在迭代i=1,2并尝试计算

enter image description here

请注意,此后续通话中的i与之前通话的i不同;它们都是生活在例行程序中的局部变量,彼此完全独立。另请注意,虚拟数组参数的索引(如matrix(:,:))始终从Fortran中的1开始(除非另有说明)。重复这种操作,直到子矩阵的大小变为1

但在实践中,我认为理解这种代码的最简单方法是编写中间数据并跟踪实际的数据/例程流。例如,我们可以插入很多print语句作为

module mymod
    implicit none
contains

recursive function determinant(matrix) result(laplace_det)
    real :: matrix(:,:)
    integer :: i, n, p, q
    real :: laplace_det, det
    real, allocatable :: cf(:,:)

    n = size(matrix, 1) 

    !***** output *****   
    print "(a)", "Entering determinant() with matrix = "
    do p = 1, n
        print "(4x,100(f3.1,x))", ( matrix( p, q ), q=1,n )
    enddo

    if (n == 1) then  
        det = matrix(1,1)
    else
        det = 0
        do i = 1, n  
            allocate( cf(n-1, n-1) )
            cf = cofactor( matrix, i, 1 )

            !***** output *****
            print "(4x,a,i0,a,i0,a)", "Getting a ", &
                    n-1, "-by-", n-1, " sub-matrix from cofactor():"
            do p = 1, n-1
                print "(8x, 100(f3.1,x))", ( cf( p, q ), q=1,n-1 )
            enddo

            print "(4x,a)", "and passing it to determinant()."

            det = det + ((-1)**(i+1))* matrix( i, 1 ) * determinant( cf )
            deallocate(cf)
        end do
    end if

    laplace_det = det

    !***** output *****
    print *, "  ---> Returning det = ", det
end function

function cofactor(matrix, mI, mJ)
    .... (same as the original code)
end function

end module

program main
    use mymod
    implicit none
    real :: a(3,3), det

    a( 1, : ) = [ 2.0, 9.0, 4.0 ]
    a( 2, : ) = [ 7.0, 5.0, 3.0 ]
    a( 3, : ) = [ 6.0, 1.0, 8.0 ]

    det = determinant( a )
    print "(a, es30.20)", "Final det = ", det
end program

然后输出清楚地显示数据的处理方式:

Entering determinant() with matrix = 
    2.0 9.0 4.0
    7.0 5.0 3.0
    6.0 1.0 8.0
    Getting a 2-by-2 sub-matrix from cofactor():
        5.0 3.0
        1.0 8.0
    and passing it to determinant().
Entering determinant() with matrix = 
    5.0 3.0
    1.0 8.0
    Getting a 1-by-1 sub-matrix from cofactor():
        8.0
    and passing it to determinant().
Entering determinant() with matrix = 
    8.0
   ---> Returning det =    8.0000000    
    Getting a 1-by-1 sub-matrix from cofactor():
        3.0
    and passing it to determinant().
Entering determinant() with matrix = 
    3.0
   ---> Returning det =    3.0000000    
   ---> Returning det =    37.000000    
    Getting a 2-by-2 sub-matrix from cofactor():
        9.0 4.0
        1.0 8.0
    and passing it to determinant().
Entering determinant() with matrix = 
    9.0 4.0
    1.0 8.0
    Getting a 1-by-1 sub-matrix from cofactor():
        8.0
    and passing it to determinant().
Entering determinant() with matrix = 
    8.0
   ---> Returning det =    8.0000000    
    Getting a 1-by-1 sub-matrix from cofactor():
        4.0
    and passing it to determinant().
Entering determinant() with matrix = 
    4.0
   ---> Returning det =    4.0000000    
   ---> Returning det =    68.000000    
    Getting a 2-by-2 sub-matrix from cofactor():
        9.0 4.0
        5.0 3.0
    and passing it to determinant().
Entering determinant() with matrix = 
    9.0 4.0
    5.0 3.0
    Getting a 1-by-1 sub-matrix from cofactor():
        3.0
    and passing it to determinant().
Entering determinant() with matrix = 
    3.0
   ---> Returning det =    3.0000000    
    Getting a 1-by-1 sub-matrix from cofactor():
        4.0
    and passing it to determinant().
Entering determinant() with matrix = 
    4.0
   ---> Returning det =    4.0000000    
   ---> Returning det =    7.0000000    
   ---> Returning det =   -360.00000    
Final det =    -3.60000000000000000000E+02

您可以插入更多打印行,直到整个机制变得清晰。

BTW,the Rossetta page中的代码通过直接创建子矩阵作为本地数组,似乎比OP的代码简单得多。代码的简化版本为

recursive function det_rosetta( mat, n ) result( accum )
    integer :: n
    real    :: mat(n, n)
    real    :: submat(n-1, n-1), accum
    integer :: i, sgn

    if ( n == 1 ) then
        accum = mat(1,1)
    else
        accum = 0.0
        sgn = 1
        do i = 1, n
            submat( 1:n-1, 1:i-1 ) = mat( 2:n, 1:i-1 )
            submat( 1:n-1, i:n-1 ) = mat( 2:n, i+1:n )

            accum = accum + sgn * mat(1, i) * det_rosetta( submat, n-1 )
            sgn = - sgn
        enddo
    endif
end function

请注意,Laplace扩展是在第一行进行的,而submat是使用数组部分进行的。作业也可以简单地写成

submat( :, :i-1 ) = mat( 2:, :i-1 )
submat( :, i: )   = mat( 2:, i+1: )

其中省略了数组部分的上限和下限(然后,默认使用上限和下限的声明值)。后一种形式用于Rosetta页面。