3

I want to get a matrix like this: enter image description here It has n*n dimensions but n is not an exact number. I have tried some functions like Table and Matrix, but they did not work while n was a symbol.

Append[Table[{0}, n - 1], alpha0]

Certainly, n should be a positive integer. Actually, what I want to get finally is the inverse matrix of the original matrix.

Super Loop
  • 135
  • 3
  • It is a method, and it may depend on the brain to find the common results, isn't it? Can Mathematica do it without my participation after I finish the code? – Super Loop Nov 08 '20 at 07:08
  • 2
    NRoots`CompanionMatrix[Append[{a0, a1, a2, a3, a4}, 1]] or NRoots`CompanionMatrix[CoefficientList[a0 + a1 x + a2 x^2 + a3 x^3 + a4 x^4 + x^5]] if starting from a monic polynomial. – Michael E2 Nov 08 '20 at 16:31

5 Answers5

5

This type of n-by-n matrix can be created with SparseArray and Band.

n = 4;
a = SparseArray[{Band[{1, 2}] -> 1,
  Band[{n, 1}, Automatic, {0, 1}] ->
    ToExpression@Table["-\[Alpha]" <> ToString[i - 1], {i, n}]},
  {n, n}];

Then,

MatrixForm[a]

$$\left ( \begin {array} {cccc} 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & \ 1 \\ - \text {$\alpha $0} & - \text {$\alpha $1} & - \text {$\alpha $2} & - \text {$\alpha $3} \\\end {array} \right)$$

creidhne
  • 5,055
  • 4
  • 20
  • 28
4
ClearAll[mat1]

mat1[n_] := Array[If[# == n, -Subscript[α, #2], Boole[# == #2]] &, {n, n}, {1, 0}]

mat1[5] // MatrixForm // TeXForm

$\left( \begin{array}{ccccc} 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 & 1 \\ -\alpha _0 & -\alpha _1 & -\alpha _2 & -\alpha _3 & -\alpha _4 \\ \end{array} \right)$

ClearAll[mat2]
mat2[n_] := Array[-Subscript[α, #]&, n, 0,
   Join[Prepend[0] /@ IdentityMatrix[n - 1], {{##}}] &]

mat2[5] == mat1[5]

True
kglr
  • 394,356
  • 18
  • 477
  • 896
4

Yet another way to get the companion matrix:

coeff = Array[a, 4, 0];
mat = ReplacePart[
 DiagonalMatrix[ConstantArray[1, Length@coeff - 1], 1],
 -1 -> -coeff]

$$\left( \begin{array}{cccc} 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \\ -a(0) & -a(1) & -a(2) & -a(3) \\ \end{array} \right)$$

There is a formula for the inverse:

imat = ReplacePart[
  DiagonalMatrix[ConstantArray[1, Length@coeff - 1], -1],
  1 -> -RotateLeft@ReplacePart[coeff, 1 -> 1]/First[coeff]]

$$\left( \begin{array}{cccc} -\frac{a(1)}{a(0)} & -\frac{a(2)}{a(0)} & -\frac{a(3)}{a(0)} & -\frac{1}{a(0)} \\ 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ \end{array} \right)$$

Check:

mat.imat == IdentityMatrix[Length@coeff]
(*  True  *)

From my comment, an internal function:

NRoots`CompanionMatrix[Append[{a0, a1, a2, a3, a4}, 1]] 

Or if starting from a monic polynomial:

NRoots`CompanionMatrix[
 CoefficientList[a0 + a1 x + a2 x^2 + a3 x^3 + a4 x^4 + x^5]]
Michael E2
  • 235,386
  • 17
  • 334
  • 747
3
With[{n = 5}, 
  SparseArray[{{i_, j_} /; j == i + 1 -> 1, {n, j_} -> -Subscript[α, j - 1]}, {n, n}]];
% // MatrixForm
cvgmt
  • 72,231
  • 4
  • 75
  • 133
3

Table is easy to understand.

Table[Which[i - 1 == j, 1, j == #, - α[i - 1], True, 0], {j, 1, #}, {i, 1, #}] &@5

$$ \begin{bmatrix} 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 & 1 \\ -\alpha (0) & -\alpha (1) & -\alpha (2) & -\alpha (3) & -\alpha (4) \\ \end{bmatrix} $$

AsukaMinato
  • 9,758
  • 1
  • 14
  • 40