Skip to content

Instantly share code, notes, and snippets.

@rodrigoSaladoAnaya
Last active May 12, 2023 08:20
Show Gist options
  • Save rodrigoSaladoAnaya/9609ab2df9766f6af26ec2b0835240c0 to your computer and use it in GitHub Desktop.
Save rodrigoSaladoAnaya/9609ab2df9766f6af26ec2b0835240c0 to your computer and use it in GitHub Desktop.
Calculate the determinant by Gaussian method (Python - Lab)
import numpy as np
def mcd(x, y):
while y != 0:
x, y = y, x % y
return x
def equiv(A, m, n):
x, y = A[m, m], A[n, m]
if x == 0 or y == 0:
return 1
z = mcd(x, y)
x /= z
y /= z
if x != y:
A[m] *= y
A[n] *= x
A[n] -= A[m]
return 1 if x == y else x * y
def det(A):
q, d, n = 1, 1, len(A)
for i in range(n):
for j in range(n - i - 1):
q *= equiv(A, i, j + i + 1)
d *= A[i, i]
return d / q
a = np.array([
[2, 0, 2, 4],
[3, 3, 1, 2],
[0, 1, 3, 1],
[4, 1, 7, 1],
], dtype=float)
print(a)
print(np.linalg.det(a))
print("......")
r = det(a)
print(a)
print(r)
print("......")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment