该代码在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)任何对解释的输入都会非常感谢(就像我说过的一个迭代示例)。这是我在堆栈溢出中的第一篇文章,因为我的大部分问题都在数学堆中(您可能从问题的数学性质可以看出这一点)。虽然我有编程经验,但递归的概念(尤其是在本例中)确实让我心烦意乱。
如果需要编辑,请继续,我不熟悉堆叠溢出的礼仪。
发布于 2016-03-13 19:53:13
假设我们将以下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)它对应于相对于第一列的拉普拉斯展开。更具体地说,将上面的3x3 matrix传递给cofactor(),通过删除i-th行和matrix的第1列获得2x2子矩阵。得到的2x2子矩阵(cf)然后传递给下一行的determinant(),以计算与该子矩阵对应的余因子。所以,在第一次迭代中,我们试图计算

请注意,右侧的三个行列式尚未通过行列式()的后续调用来计算。让我们考虑一个这样的后续调用,例如i=1。我们正在传递以下子矩阵(存储在cf中)
5 3
1 8敬determinant()。然后,重复上述相同的过程,不依赖于父3x3矩阵的Laplace展开。也就是说,行列式()现在在i=1,2上迭代并尝试计算

请注意,此后续调用中的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您可以插入更多的打印行,直到整个机制变得清晰。
顺便说一句,罗塞塔网页中的代码似乎比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页面中使用。
https://stackoverflow.com/questions/35959828
复制相似问题