Companion matrix of a polynomial

### 14.2.11 Companion matrix of a polynomial

The companion command finds a matrix given its characteristic polynomial; specifically, if the polynomial is P(x)=xn+an−1xn−1+⋯+a−1x+a0, this matrix is equal to the identity matrix of size n−1 bordered with [0,0..,0,−a0] as first row, and with [−a0,−a1,…,−an−1] as last column.

- companion takes two arguments:
  - P, a unitary polynomial.
  - x, the name of its variable.
- companion(P,x) returns the matrix whose characteristic polynomial is P.

#### Examples

companion(x^2+5x-7,x)

⎡ ⎢ ⎣ |
0 | 7
1 | −5
| ⎤ ⎥ ⎦

companion(x^4+3x^3+2x^2+4x-1,x)

⎡ ⎢ ⎢ ⎢ ⎣ |
0 | 0 | 0 | 1
1 | 0 | 0 | −4
0 | 1 | 0 | −2
0 | 0 | 1 | −3
| ⎤ ⎥ ⎥ ⎥ ⎦