Are you sure your minor returns a new object, not a reference to your original matrix object? I used your exact determinant method and implemented a minor method for your class, and it works great for me.
Below is a short-term implementation of your matrix class, since I do not have your implementation. For brevity, I decided to implement it only for square matrices, which in this case does not matter, since we are dealing with determinants. Pay attention to the det method, which matches yours, and the minor method (other methods are designed to facilitate implementation and testing):
class matrix: def __init__(self, n): self.data = [0.0 for i in range(n*n)] self.dim = n @classmethod def rand(self, n): import random a = matrix(n) for i in range(n): for j in range(n): a[i,j] = random.random() return a @classmethod def eye(self, n): a = matrix(n) for i in range(n): a[i,i] = 1.0 return a def __repr__(self): n = self.dim for i in range(n): print str(self.data[i*n: i*n+n]) return '' def __getitem__(self,(i,j)): assert i < self.dim and j < self.dim return self.data[self.dim*i + j] def __setitem__(self, (i, j), val): assert i < self.dim and j < self.dim self.data[self.dim*i + j] = float(val)
Now for testing
import numpy as np a = matrix(3)
The remainder in the case of numpy is that it calculates the determinant by the (Gaussian) method, not the Laplace extension. You can also compare the results on random matrices to see that the difference between your determinant function and numpy does not exceed the precision of the float :
import numpy as np a = 10*matrix.rand(4) A = np.array( a.data ).reshape([4,4]) print (np.linalg.det(A) - a.det())/a.det()
source share