解释寻找矩阵行列式的程序

时间:2018-11-18 23:57:32

标签: matrix fortran

这是我发现的用于计算(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

1 个答案:

答案 0 :(得分:2)

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

  • ij:将可互换使用(此方面的代码不一致)表示矩阵元素的行和列坐标。
  • k:将遍历矩阵的“维度”,而不意味着协调的位置。或者,以另一种抽象方式,在对角线上进行迭代。
  • l:将为+1或-1,在算法执行线路切换时随时更改其值。它考虑了切换矩阵reverses的任意两行行列式的符号。

因此,代码的解释是:

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.