What sort of “companion matrix” do you want? That term usually implies a matrix whose eigenvalues match the roots of a given polynomial, but in this case you aren’t supplying a polynomial so I’m not sure what you mean.
If you just want an n \times n matrix A with given n eigenvalues λ
(e.g. λ = randn(n)
), why not simply do X = randn(n, n)
followed by A = X * Diagonal(λ) / X
? (Or, if you want a Hermitian A, you could get a random orthogonal matrix Q by doing QR = qr(X); Q = QR.Q * Diagonal(sign.(diag(QR.R)))
and then do A = Hermitian(Q * Diagonal(λ) * Q')
.)
(If you then want a polynomial with those roots, given the matrix, you can compute the characteristic polynomial. Numerically, working with this polynomial directly is not usually a good idea, however.)