SSpectrumGorse
Workflows

Coding workflows · Integer characteristic polynomials

03
Built around your work

A concrete starting point

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.

01

Open the context

Choose the project folder and name the files or behavior involved.

02

Define the change

Describe the input, expected output and boundaries that must stay intact.

03

Inspect the work

Review the proposed edits and any commands before relying on their result.

04

Check the outcome

Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.

Integer characteristic polynomials

A concrete starting point
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 coefficients

The 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.

AI coding workspace

Your next change starts here.

Get the Windows app ↗