Fast method to check if a Matrix is singular? (non-invertible, det = 0)

后端 未结 3 1707
轮回少年
轮回少年 2020-12-29 13:28

What is the fastest algorithm (a link to C or C++ example would be cool) to check if a small square matrix (<16*16 elements) is singular (non-invertible, det = 0) ?

3条回答
  •  春和景丽
    2020-12-29 13:34

    The fastest way is probably to hard code a determinant function for each size matrix you expect to deal with.

    Here is some psuedo-code for N=3, but if you check out The Leibniz formula for determinants the pattern should be clear for all N.

    function is_singular3(matrix) {
        det = matrix[1][1]*matrix[2][2]*matrix[3][3]
            - matrix[1][1]*matrix[2][3]*matrix[3][2]
            - matrix[1][2]*matrix[2][1]*matrix[3][3]
            + matrix[1][2]*matrix[2][3]*matrix[3][1]
            + matrix[1][3]*matrix[2][1]*matrix[3][2]
            - matrix[1][3]*matrix[2][2]*matrix[3][1];
    
         if(det==0) return true
         else return false
    }
    

提交回复
热议问题