Open the context
Choose the project folder and name the files or behavior involved.
Return descending coefficients of det(xI−A) for a square integer matrix of order zero through six with entries bounded by 1000 in magnitude. Test the empty matrix and repeated eigenvalues, rejecting ragged or Boolean entries without approximating roots.
Choose the project folder and name the files or behavior involved.
Describe the input, expected output and boundaries that must stay intact.
Review the proposed edits and any commands before relying on their result.
Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.
def characteristic_polynomial(matrix):
if type(matrix) not in (list, tuple) or len(matrix) > 6:
raise ValueError("charpoly.size")
n = len(matrix)
if any(type(row) not in (list, tuple) or len(row) != n or any(type(v) is not int or abs(v) > 1000 for v in row) for row in matrix):
raise ValueError("charpoly.entry")
block = [[int(i == j) for j in range(n)] for i in range(n)]
coefficients = [1]
for degree in range(1, n+1):
product = [[sum(matrix[i][k]*block[k][j] for k in range(n)) for j in range(n)] for i in range(n)]
trace = sum(product[i][i] for i in range(n))
coefficient = -trace // degree
coefficients.append(coefficient)
block = [[product[i][j] + (coefficient if i == j else 0) for j in range(n)] for i in range(n)]
return coefficientsThe example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.