Ask Your Question
1

Is there a simple and effective method in Python to determine if the determinant is congruent to 0 (modulo a large prime)?

asked 2023-07-05 10:37:01 +0000

devzero gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-07-05 10:58:01 +0000

pufferfish gravatar image

Yes, there is a simple and effective method in Python to determine if the determinant is congruent to 0 (modulo a large prime). One way to do this is to use the Gauss-Jordan elimination method and keep track of the total number of row swaps performed during the process. If the number of row swaps is odd, then the determinant is congruent to the negative of the original determinant (modulo the prime), and if the number of row swaps is even, then the determinant is congruent to the original determinant (modulo the prime).

Here's an example implementation:

def determinant_mod_p(matrix, p):
    # perform Gauss-Jordan elimination
    n = len(matrix)
    swaps = 0
    for i in range(n):
        # find pivot element
        max_row = i
        for j in range(i+1, n):
            if abs(matrix[j][i]) > abs(matrix[max_row][i]):
                max_row = j
        if max_row != i:
            matrix[i], matrix[max_row] = matrix[max_row], matrix[i]
            swaps += 1
        # row reduction
        for j in range(i+1, n):
            factor = matrix[j][i] * pow(matrix[i][i], p-2, p) % p
            for k in range(i, n):
                matrix[j][k] = (matrix[j][k] - factor * matrix[i][k]) % p

    # compute determinant modulo p
    det = 1
    for i in range(n):
        det = (det * matrix[i][i]) % p

    # apply sign change if necessary
    if swaps % 2 == 1:
        det = (-det) % p

    return det

This function takes as input a matrix (as a list of lists) and a prime number p. It then performs Gauss-Jordan elimination to compute the determinant of the matrix modulo p, and returns it as an integer. Note that we use the "pow" function with a third argument in order to compute the modular inverse of the pivot element with respect to p (rather than directly computing the inverse using the extended Euclidean algorithm).

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-05 10:37:01 +0000

Seen: 8 times

Last updated: Jul 05 '23