# Matrix definition using some rules for the elements

I'm gonna produce a matrix in which the matrix elements obey the following rules

$$\left = m^2 \delta_{m,n},$$

$$\left = (1-m) \delta_{m,m \pm 2}.$$

How can I produce such a matrix in Mathematica?

Since I have no idea about it, I could not make any code.

• You have two different definitions. Do you mean that the matrix you seek is the sum of the ones you've defined? – John Doty May 13 at 11:55
• Did you mean $\langle n\lvert M \rvert m \rangle = m^2 \delta_{m,n}+(1-m)(\delta_{m,n+2}+\delta_{m,n-2})$? As @JohnDoty says, your definitions as they are do not make sense. Also, please add the required ranges of the indices $n$ and $m$. – Roman May 13 at 12:20
• Try KroneckerDelta: for instance the first matrix could be: Range[4]^2 Array[KroneckerDelta, {4, 6}] – bill s May 13 at 12:21
• Your second definition still doesn't make sense. There is no $n$ on the left-hand side; so why is there one on the right? – Roman May 13 at 15:49

Assuming that you meant

$$\langle n\lvert M \rvert m \rangle = m^2 \delta_{m,n}+(1-m)(\delta_{m,n+2}+\delta_{m,n-2})$$

and ranges of $$n,m\in\{1,2,3,\ldots,a\}$$, you can define this matrix with

M[a_] := Table[m^2 KroneckerDelta[n, m] +
(1 - m) (KroneckerDelta[m, n + 2] + KroneckerDelta[m, n - 2]),
{n, a}, {m, a}]


or, more efficiently,

M[a_] := SparseArray[{{m_,m_} -> m^2, {n_,m_} /; Abs[n-m]==2 -> 1-m}, {a,a}]


or even

M[a_] := SparseArray[{Band[{1, 1}] -> Range[a]^2,
Band[{3, 1}] -> -Range[0, a - 3],
Band[{1, 3}] -> -Range[2, a - 1]}]


Test:

M[6] // MatrixForm
(*    {{ 1,  0, -2,  0,  0,  0},
{ 0,  4,  0, -3,  0,  0},
{ 0,  0,  9,  0, -4,  0},
{ 0, -1,  0, 16,  0, -5},
{ 0,  0, -2,  0, 25,  0},
{ 0,  0,  0, -3,  0, 36}}    *)


Considering that this matrix isn't Hermitian and that you are using Dirac-notation matrix elements, I suspect that there is something wrong in these definitions.

• It's perfectly possible to have non-Hermitian operators in quantum mechanics. They just don't represent observables, but combinations of non-Hermitian operators can be Hermitian. Ladder operators are a common example: en.wikipedia.org/wiki/Ladder_operator – Sjoerd Smit May 13 at 15:55
• Yes @SjoerdSmit it's possible but feels a bit unlikely here, given the diagonal. Just my suspicion. – Roman May 13 at 15:57