Matrix 解释求矩阵行列式的程序

Matrix 解释求矩阵行列式的程序,matrix,fortran,Matrix,Fortran,这是我找到的用于计算(nxn)矩阵行列式的示例代码,它工作得很好,但我很难理解转换为三角形部分的过程。有人能解释一下“上三角部分的转换”中发生了什么吗 我自己计算行列式或进行任何上三角形式的转换都没有问题,但我不知道这一切在这个程序中是如何转换的 ii)整数(i,j,k,l)发生了什么?具体来说,k和l在做什么?IF构造内部发生了什么?对于矩阵a,我知道类似于a(I,j)的东西表示它在矩阵中的位置,这就是我过去使用过的所有矩阵程序所需要的 ============================

这是我找到的用于计算(nxn)矩阵行列式的示例代码,它工作得很好,但我很难理解转换为三角形部分的过程。有人能解释一下“上三角部分的转换”中发生了什么吗

我自己计算行列式或进行任何上三角形式的转换都没有问题,但我不知道这一切在这个程序中是如何转换的

ii)整数(i,j,k,l)发生了什么?具体来说,k和l在做什么?IF构造内部发生了什么?对于矩阵a,我知道类似于a(I,j)的东西表示它在矩阵中的位置,这就是我过去使用过的所有矩阵程序所需要的

========================================================================

    !Function to find the determinant of a square matrix
    !Description: The subroutine is based on two key points:
    !1] A determinant is unaltered when row operations are performed: Hence, 
    using this principle,
    !row operations (column operations would work as well) are used
    !to convert the matrix into upper traingular form
    !2]The determinant of a triangular matrix is obtained by finding the 
    product of the diagonal elements

    REAL FUNCTION FindDet(matrix, n)
        IMPLICIT NONE
        REAL, DIMENSION(n,n) :: matrix
        INTEGER, INTENT(IN) :: n
        REAL :: m, temp
        INTEGER :: i, j, k, l
        LOGICAL :: DetExists = .TRUE.

        l = 1
        !Convert to upper triangular form
        DO k = 1, n-1
            IF (matrix(k,k) == 0) THEN
                DetExists = .FALSE.
                DO i = k+1, n
                    IF (matrix(i,k) /= 0) THEN
                        DO j = 1, n
                            temp = matrix(i,j)
                            matrix(i,j)= matrix(k,j)
                            matrix(k,j) = temp
                        END DO
                        DetExists = .TRUE.
                        l=-l
                        EXIT
                    ENDIF
                END DO
                IF (DetExists .EQV. .FALSE.) THEN
                    FindDet = 0
                    return
                END IF
            ENDIF
            DO j = k+1, n
                m = matrix(j,k)/matrix(k,k)
                DO i = k+1, n
                    matrix(j,i) = matrix(j,i) - m*matrix(k,i)
                END DO
            END DO
        END DO

        !Calculate determinant by finding product of diagonal elements
        FindDet = l
        DO i = 1, n
            FindDet = FindDet * matrix(i,i)
        END DO

    END FUNCTION FindDet

在此实现中,您可以将索引
i、j、k、l
解释为以下内容:

  • i
    j
    :将互换使用(在这方面,代码不一致)来表示矩阵元素的行和列坐标
  • k
    :将迭代矩阵的“维度”,而不表示协调位置。或者,在不同的抽象中,迭代对角线
  • l
    :将为+1或-1,在算法执行线路切换时交替其值。它考虑到切换矩阵的任意两行是其行列式的符号
因此,守则的解释是:

At each iteration over the dimension of the matrix:
  First, check if this diagonal element is zero. If it is zero:
    ALARM: maybe the matrix is degenerate.
    Let's find it out. Iterate downwards over the rest of this row, trying to find a non-zero element.
      If you find a row with a non-zero element in this column, if was a false alarm. Perform row-switch and invert the sign of the determinant. Go on.
      If there were all zeroes, then the matrix is degenerate. There is nothing else to do, determinant is zero.
  Going on. For each row below this diagonal:
    Perform sums and subtractions on the rest of the rows, constructing the diagonal matrix.
Finally, calculate the determinant by multiplying all the elements on the diagonal, taking the sign changes into acount.