Home python How to check the degeneracy of the matrix

# How to check the degeneracy of the matrix

Author

Date

Category

Perfect steaming in the graph exists if and only if the determinant of the adjacency matrix of the graph is differ from zero. It is necessary to know whether the arrangement matrix is ​​degenerated. If you count the determinant using np.linalg.det (M), then due to rounding the answer may be incorrect. Question: How can I check the degeneracy of the matrix quickly using Python?

`np.linalg.det (m) `Looks like everything comes down to the note of the matrix to the triangular form, with large matrices the accuracy is highly falling due to multiple divisions

But the calculation of the determinant is a redundant operation for your task

Matrix is ​​degenerate if

``````a = b * const
``````

i.e. One column is equal to the second column, multiplied on some constant

so it can be repelled

``````for i in range (0, N-1):
For j in Range (I + 1, N):
IF Check ([Matrix [i], Matrix [J]):
Return True.
Return False
``````

How to check 2 columns among themselves – you can calculate the difference between columns dregated to the coefficient:

``````def check (col1, col2):
# Calculate conversion coefficient
CoeFF = COL  / COL 
# Check columns
For i in Range (Len (COL ):
IF ABS (COL1 [I] - COL2 [I] * COEFF) & GT; 1E-10:
Return False
Return True.
``````

Since it is necessary to work with floating semicolons, it is often possible that it cannot be obtained, so you can choose some limit near zero, for example 1E-10 or 1E-15 – here you need to see how much accuracy is needed

As a result, compared with the calculation of the determinant, the columns only are divided once to be multiplied by some number, so the accuracy floats weakly, in addition, already in the middle of the check, you can find the same columns and stop further analysis, because the matrix is ​​already degenerate

Here is the full code:

``````# check if you can submit one column as a product of another column on a constant
DEF Check (col1, col2):
# Calculate conversion coefficient
# In this case, you need to find nonzero values ​​otherwise
FIRST1 = -1.
FIRST2 = -1.
For i in Range (Len (col1)):
IF col1 [i]! = 0 and first1! = -1:
FIRST1 = I.
IF col2 [i]! = 0 and first2! = -1:
FIRST2 = I.
If first1 == 0 and first2 == 0:
Return True.
# Find the coefficient
First = Max (first1, first2)
coeff = col1 [first] / col2 [first]
# Check elements of columns - is it possible to imagine them in the form elem2 = elem1 * const
For i in Range (Len (col1)):
IF ABS (COL1 [I] - COL2 [I] * COEFF) & GT; 1E-10:
Return False
Return True.
# Check if the matrix is ​​degenerate
DEF is_invertible (Matrix):
N = Len (Matrix)
For i in Range (n - 1):
For j in Range (I + 1, N):
IF Check (Matrix [i], Matrix [J]):
Return True.
Return False
MATRIX1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Matrix2 = [[1, 2, 3], [4, 5, 6], [3, 6, 9]]
Print (Matrix1, IS_INvertible (Matrix1))
Print (Matrix2, IS_INvertible (Matrix2))
``````

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.