Numpy zero determinant precision | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Numpy zero determinant precision

When computing determinant using numpy.linalg.det() and expecting zero result, result is different. >> import numpy as np >> sigma = np.matrix('1 2 3; 4 5 6; 7 8 9', dtype=np.float64) >> print(np.linalg.det(sigma)) -9.51619735393e-16 Is there some way to change algorithm for small matrices, which is more precise ? Or how can it be solved in more precise way ?

11th Mar 2017, 10:18 AM
Michal Dohnal
Michal Dohnal - avatar
1 Answer
0
the result of np. linalg.det(sigma) is a 'numerical zero', due to the np.float number format. Even if you change dtype to np.int16, the result will be a floating number. If you want to work only with integers, you can rewrite a det function. Here is a recursive one for very small matrixes : import numpy as np def det(A) : n, p=np.shape(A) if n== 1: return A[0,0] else: S=0 for i in range(n): L = [x for x in range(n) if x ! = i] S += (-1)**i *A[0,i]*det(A[1:,L]) return S A=np.matrix(' .... ', dtype=np.int16) print(det(A),np.linalg.det(A)) #to compare results
15th Mar 2017, 9:40 PM
fbes