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

d1 = 
     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?

enter image description here

  • $\begingroup$ 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? $\endgroup$ – mjw Mar 1 '19 at 20:45
  • $\begingroup$ Dear mjw, I added both cases even and odd...thanks $\endgroup$ – user62716 Mar 1 '19 at 20:54
  • $\begingroup$ 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? $\endgroup$ – mjw Mar 1 '19 at 21:01
  • 1
    $\begingroup$ How should we interpret the image you show? Is it the output are now getting or the output you want to get? $\endgroup$ – m_goldberg Mar 2 '19 at 0:34
  • 1
    $\begingroup$ I suggest using SparseArray and Band. $\endgroup$ – Αλέξανδρος Ζεγγ 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?

| improve this answer | |
  • $\begingroup$ Or use // MatrixForm to view the matrix. $\endgroup$ – mjw Mar 3 '19 at 3:54
  • $\begingroup$ Dear all, many thanks for all comments, I will check and back...best regards $\endgroup$ – user62716 Mar 3 '19 at 20:08
  • $\begingroup$ Many thanks Roman, you solved my problem, thanks mjw $\endgroup$ – 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[3] // MatrixForm:

enter image description here

d1[4] // MatrixForm:

enter image description here

| improve this answer | |
  • $\begingroup$ This is not the matrix from the images. In yours, element (3,1) is $3/8$ whereas in the original it is $3/4$. $\endgroup$ – Roman Mar 3 '19 at 6:19
  • $\begingroup$ Defining a matrix with MatrixForm is not a good idea as it interferes with subsequent calculations and confuses beginners. $\endgroup$ – Roman Mar 3 '19 at 6:19
  • $\begingroup$ You are right about the 3/4 vs 3/8. I"ll look at it again when I have some time. $\endgroup$ – mjw Mar 3 '19 at 13:45
  • $\begingroup$ Defining a matrix with MatrixForm... I did not know about the interference, please elaborate. I am also a beginner! $\endgroup$ – mjw Mar 3 '19 at 13:48
  • 1
    $\begingroup$ 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/… $\endgroup$ – Roman Mar 3 '19 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.