I am trying to write a script on python to determine a matrix determinant using Gauss method. It works correctly, but the precision isn't enough for me. My code is:
import scipy.linalg as sla
import numpy as np
def my_det(X):
n = len(X)
s = 0
if n != len(X[0]):
return ValueError
for i in range(0, n):
maxElement = abs(X[i][i])
maxRow = i
for k in range(i+1, n):
if abs(X[k][i]) > maxElement:
maxElement = abs(X[k][i])
maxRow = k
if maxRow != i:
s += 1
for k in range(i, n):
X[i][k], X[maxRow][k] = X[maxRow][k], X[i][k]
for k in range(i+1, n):
c = -X[k][i]/X[i][i]
for j in range(i, n):
if i == j:
X[k][j] = 0
else:
X[k][j] += c * X[i][j]
det = (-1)**s
for i in range(n):
det *= X[i][i]
return det
And I have tests for this code:
for x in range(10):
X = np.random.rand(3,3)
if np.abs(my_det(X) - sla.det(X)) > 1e-6:
print('FAILED')
My function fails all tests. I tried Decimals, but it didn't help. What's wrong?
The reason why the code fails the test condition, abs(my_det(X) -
sla.det(X)) < 1e-6, is not due to lack of precision but rather the change
in sign brought about the unintended side-effect of my_det mutating X:
X[i][k], X[maxRow][k] = X[maxRow][k], X[i][k]
This row swapping changes the sign of the determinant.
The code uses s to adjust for the change in sign, but X itself is altered
in a way which changes the sign of the determinant.
So the X passed to my_det is not the same as the X which is subsequently passed to sla.det. Here is an example where the alteration of X changes the sign of the determinant:
In [55]: X = np.random.rand(3, 3); X
Out[55]:
array([[ 0.38062719, 0.41892961, 0.88277747],
[ 0.39881724, 0.00188804, 0.79258322],
[ 0.40195279, 0.3950311 , 0.32771527]])
In [56]: my_det(X)
Out[56]: 0.098180005266934267
In [57]: X
Out[57]:
array([[ 0.40195279, 0.3950311 , 0.32771527],
[ 0. , -0.39006151, 0.46742438],
[ 0. , 0. , 0.62620267]])
In [58]: sla.det(X)
Out[58]: -0.09818000526693427
You can fix the problem by making a copy of X inside my_det:
def my_det(X):
X = np.array(X, copy=True) # copy=True is the default; shown here for emphasis
...
Thus, subsequent changes to X within my_det no longer affect the X outside of
my_det.
import scipy.linalg as sla
import numpy as np
def my_det(X):
X = np.array(X, dtype='float64', copy=True)
n = len(X)
s = 0
if n != len(X[0]):
return ValueError
for i in range(0, n):
maxElement = abs(X[i, i])
maxRow = i
for k in range(i + 1, n):
if abs(X[k, i]) > maxElement:
maxElement = abs(X[k, i])
maxRow = k
if maxRow != i:
s += 1
for k in range(i, n):
X[i, k], X[maxRow, k] = X[maxRow, k], X[i, k]
for k in range(i + 1, n):
c = -X[k, i] / X[i, i]
for j in range(i, n):
if i == j:
X[k, j] = 0
else:
X[k, j] += c * X[i, j]
det = (-1)**s
for i in range(n):
det *= X[i, i]
return det
for i in range(10):
X = np.random.rand(3, 3)
diff = abs(my_det(X) - sla.det(X))
if diff > 1e-6:
print('{} FAILED: {:0.8f}'.format(i, diff))
Also note that dtype matters:
In [88]: my_det(np.arange(9).reshape(3,3))
Out[88]: 6
while the correct answer is
In [89]: my_det(np.arange(9).reshape(3,3).astype(float))
Out[89]: 0.0
Since my_det uses division (in c = -X[k, i] / X[i, i]), we need X to have floating point dtype so that the / performs floating point division, not integer division.
So to fix this, use X = np.asarray(X, dtype='float64') to ensure that X has dtype float64:
def my_det(X):
X = np.array(X, dtype='float64', copy=True)
...
With this change,
In [91]: my_det(np.arange(9).reshape(3,3))
Out[91]: 0.0
now gives the correct answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With