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

标签 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

最佳答案

在此实现中,您可以将索引 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.

关于matrix - 解释寻找矩阵行列式的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53366677/

相关文章:

c++ - Cholesky 分解 ScaLapack 错误

java - 使用 Colt 库的简单矩阵运算(2D 和 3D)

c - 在 C : How to detect line ending

matrix - 在 Fortran 中使用 ZGETRI 时出现错误的逆矩阵

fortran - 为什么 'use mpi' 使用 mpif90 失败

java - 正在寻找计算大型矩阵并输出它们的最快方法?

python - 将 FORTRAN90 文件生成的数据读入 NUMPY 数组

javascript - 在javascript中乘以4x3仿射变换矩阵

matlab - 在matlab中进行三角矩阵向量乘法的最快方法?

matlab - 计算矩阵中每列或行的 fft