# Help with coding a matrix

I have a $$n \times n$$ matrix $$A$$ with a full set of eigenvalues $$\lambda$$ including repetitions.

I want to create the following $$i \times i$$ matrix:

$$\left(\sum_{a=2}^i (a-1) |a-1⟩⟨a| \right) + \sum_{j=1}^i d_j \sum_{b=j}^i |b⟩⟨b-j+1|$$

where $$|1⟩,...,|i⟩$$ is the standard basis and $$d_j (\lambda) = \sum_{c=1}^n \lambda_c^j$$.

Any help with coding this matrix in Mathematica would be greatly appreciated.

• You must give an explicit mathematical formula for $|b \rangle \langle b-j+1|$ and other terms. – David G. Stork Jun 16 at 15:45
• @DavidG.Stork I added a missing definition and believe I have defined all the other terms. Please let me know if any definition is unclear. – jacobi16 Jun 16 at 15:56
• The first term $\delta_{i,0}$ is a scalar, not a matrix, and does not fit into the formula. Does it mean that for $i=0$ you want to get the scalar 1 as the answer? – Roman Jun 16 at 16:43
• Does $d_j(\lambda)$ depend on $j$ at all? – Roman Jun 16 at 17:13
• @Roman Thanks for your comments. I had mistakenly written the superscript of $x$ as $q$ instead of $j$. $\delta_{i,0}$ would be the identity matrix when $i=0$, which makes this term irrelevant now that I think about it. Sorry for the mistake with the definition of $d_j$ and thanks for spotting out the unnecessary $\delta_{i,0}$ factor. I have edited my question to reflect these changes. – jacobi16 Jun 16 at 21:47

You can define the matrix with

M = {{d}};
M[i_Integer /; i >= 2] := SparseArray[{Band[{1, 2}] -> Range[i - 1],
{a_, b_} /; a >= b -> d[a - b + 1]},
{i, i}]


With the $$d$$-elements

λ = RandomReal[{0, 1}, 10];
d[j_] = Total[λ^j];


(replace the $$\lambda$$-vector by your eigenvalues).

M // MatrixForm You can also use a combination of ToeplitzMatrix, DiagonalMatrix, LowerTriangularize and SparseArray:

ClearAll[mat]
mat[n_] := Module[{dd = Array[d, n]},
LowerTriangularize[ToeplitzMatrix[dd, SparseArray]] +
DiagonalMatrix[SparseArray@Range[n - 1], 1]]

mat // MatrixForm // TeXForm


$$\left( \begin{array}{ccccc} d(1) & 1 & 0 & 0 & 0 \\ d(2) & d(1) & 2 & 0 & 0 \\ d(3) & d(2) & d(1) & 3 & 0 \\ d(4) & d(3) & d(2) & d(1) & 4 \\ d(5) & d(4) & d(3) & d(2) & d(1) \\ \end{array} \right)$$

• This can be simplified quite a bit, and can be arranged such that the output is a SparseArray[]: mat[n_Integer /; n > 1] := ToeplitzMatrix[Array[d, n], PadRight[{d}, n], SparseArray] + DiagonalMatrix[Range[n - 1], 1, n, SparseArray] – J. M. will be back soon Nov 18 at 8:55