I want to calculate the determinant of some specific matrix. I have simplified my problem and try to demonstrate the problem with the simplest code below.
My problem is :
- Every element in my matrix is a polynomial.
- But the determinant of the matrix is not a polynomial.
I suspect that it should be caused by precision or something else, I would like to know if there is other way to avoid this problem. I hope the code below can successfully demonstrate my issue, thanks everyone.
import sympy as sp
class Matrix:
def __init__(self, N):
self.E = sp.symbols('E') # the matrix determinant will be represented as polynomial in E
self.elements = [] # list of matrix elements
for i in range(2*N):
if i >= 2:
self.elements.append(self.recursion(s=i, X=self.elements[i-1], Y=self.elements[i-2]))
else:
self.elements.append(self.recursion(s=i))
self.m = sp.Matrix([[self.elements[i+j] for j in range(N)] for i in range(N)])
def recursion(self, s, X=None, Y=None):
if s == 0:
return 1
elif s == 1:
return self.E
else:
return s*self.E*X + 0.5*Y # if change to s*self.E*X + Y will be no problem
N = 4 # N=1,2,3 works fine
m = Matrix(N).m
det = m.det()
print("Is the determinant polynomial : ", det.is_polynomial()) # will get False
for element in m:
print("Is this element polynomial : ", element.is_polynomial()) # will get True
print("The determinant looks like : \n", det)