Skip to content
DeepForgeby svx
All problems
MediumLinear Algebrala-123

Companion Matrix

Problem

Build the companion matrix of a monic polynomial from coefficients coeffs = [c0, c1, ..., c_{n-1}], representing x^n + c_{n-1}*x^(n-1) + ... + c0.

The last column is [-c0, -c1, ..., -c_{n-1}] and the subdiagonal is 1. The eigenvalues are the polynomial roots.

Hint

Put the negated coefficients in the last column and ones directly below the diagonal.

Test cases

case 1
in: [[2,1]]
expected: [[0,-2],[1,-1]]
case 2
in: [[0,0,1]]
expected: [[0,0,0],[1,0,0],[0,1,-1]]
case 3
in: [[1]]
expected: [[-1]]
case 4
in: [[-2,0,1]]
expected: [[0,0,2],[1,0,0],[0,1,-1]]
Starter codePython
def companion_matrix(coeffs):
    # Your code here
    pass

4 test cases · run your code against them in the editor