# Constructing a special square matrix

I am trying to define an $$n \times n$$ matrix for even $$n$$ using

d1 =
Table[
Which[
i < j, 0, i == j, i!/(2^i 0! i!),
EvenQ[i - j] && j == 0, (i)!/(2^i (i/2)! j!),
EvenQ[i - j] && OddQ[i], (i)!/(2^i ((i + 1)/2 - 1)! j!),
EvenQ[i - j] && EvenQ[i], (i)!/(2^i (i/2 - 1)! j!),
True, 0],
{i, 0, n}, {j, 0, n}]


Is my code correct? Will it be correct when $$n$$ is odd? • Probably you don't need a separate case for j==0. Also, is there are additional j dependencies in the other two cases (I don't know, because I don't know exactly what you are trying to do). What happens when n is odd? – mjw Mar 1 '19 at 20:45
• Dear mjw, I added both cases even and odd...thanks – user62716 Mar 1 '19 at 20:54
• Okay, so i is the row, and j is the column, right? The term's dependence on j (other than whether it zero or not) is the j! in the denominator, yes? – mjw Mar 1 '19 at 21:01
• How should we interpret the image you show? Is it the output are now getting or the output you want to get? – m_goldberg Mar 2 '19 at 0:34
• I suggest using SparseArray and Band. – Αλέξανδρος Ζεγγ Mar 3 '19 at 3:02

This works for both even and odd $$N$$=n. Notice that the generated matrix is actually $$(N+1)\times(N+1)$$ as the indices run from 0 to $$N$$. You can see this in your images, which show an even-sized matrix for odd $$N$$ and vice-versa.

M[n_Integer /; n >= 0] :=
SparseArray[{{i_,j_} /; EvenQ[i-j] && i>=j -> (i-1)!/(2^(i-1)((i-j)/2)!(j-1)!)},
{n + 1, n + 1}]


If you need a non-sparse matrix, use Normal.

I'm not sure why the bottom-right matrix element in your odd-$$N$$ image is zero. Shouldn't it be $$2^{-N}$$ as it lies on the diagonal?

• Or use // MatrixForm to view the matrix. – mjw Mar 3 '19 at 3:54
• Dear all, many thanks for all comments, I will check and back...best regards – user62716 Mar 3 '19 at 20:08
• Many thanks Roman, you solved my problem, thanks mjw – user62716 Mar 3 '19 at 20:30
d1[n_] := Table[
Which[i < j, 0,EvenQ[i - j], (i)!/(2^i ((i-j)/2)! j!), True, 0],
{i, 0, n}, {j, 0, n}
]


The output seems to be what you want. Here are a couple of examples:

d1 // MatrixForm: d1 // MatrixForm: • This is not the matrix from the images. In yours, element (3,1) is $3/8$ whereas in the original it is $3/4$. – Roman Mar 3 '19 at 6:19
• Defining a matrix with MatrixForm is not a good idea as it interferes with subsequent calculations and confuses beginners. – Roman Mar 3 '19 at 6:19
• You are right about the 3/4 vs 3/8. I"ll look at it again when I have some time. – mjw Mar 3 '19 at 13:45
• Defining a matrix with MatrixForm... I did not know about the interference, please elaborate. I am also a beginner! – mjw Mar 3 '19 at 13:48
• MatrixForm is only a display wrapper, like InputForm and TeXForm etc. If you define A={{1,1},{1,1}}//MatrixForm then a call like Eigenvalues[A] will fail because it is interpreted as Eigenvalues[MatrixForm[{{1,1},{1,1}}]] instead of Eigenvalues[{{1,1},{1,1}}]. Use MatrixForm only to display a matrix, not to define it. See mathematica.stackexchange.com/questions/3098/… – Roman Mar 3 '19 at 14:32