1

I'm trying to create a matrix which will be of size 2N by 2N and have entries which will depend on the column and row number (u and v here). I'm sure this is a very trivial question but i'm really struggling to do this for some reason. Here's what I've attempted so far:

armchair_hamiltonian[N_, k_] := Table[
  if[Mod[u, 2] != 0 && v == (u + 1), -e^(-i*k/2), 0];
  if[Mod[u, 2] == 0 && v = (u - 1), -e^(i*k/2), 0];
  if[Mod[u + v - 1, 4] == 0 && Abs[u - v] < 4, -1, 0];
  {u, 2*N}, {v, 2*N}]

armchair_hamiltonian[5, 1]; MatrixForm[%];

Again sorry if this is trivial, I'm genuinely stumped as I'm very new to mathematica.

1 Answers1

3

You're not too far off; you just need to use nested If functions rather than three successive commands:

armchairhamiltonian[n_, k_] := Table[
  If[Mod[u, 2] != 0 && v == (u + 1), -E^(-I*k/2), 
   If[Mod[u, 2] == 0 && v == (u - 1), -E^(I*k/2), 
    If[Mod[u + v - 1, 4] == 0 && Abs[u - v] < 4, -1, 0]]],
  {u, 2*n}, {v, 2*n}]

armchairhamiltonian[5, 1] // MatrixForm

enter image description here

Also note the following syntax corrections that I made to your code above:

  • Mathematica uses different commands for setting variables and for testing equality/inequality (= and == respectively.) If you want something that returns True or False for an If statement, you must use == rather than =. Your original code used = in one spot (probably just a typo.)

  • The underscore character _ is reserved for pattern matching and cannot be used in variable names. If you want to denote word boundaries in your variable names, I recommend camel case (while always starting the first word in lowercase to avoid potential conflict with built-in functions.)

  • The imaginary unit $i$ is I in Mathematica, not i; and the base of natural logarithms $e$ is E, not e. Given the mathematical expressions you're using, I suspect you mean I and E.

  • N is a protected symbol; it is the function that returns the numerical value of an expression. (For example, Mathematica treats Pi as an exact number by default; if you want its decimal expansion, use N[Pi] and you'll get 3.14159.) Use n or NN or something like that instead.

Finally, for future reference: it is often easier to construct such matrices using SparseArray. This also can save memory and calculation time if the matrix is large. In particular, the Band structure would be particularly helpful for constructing this matrix.

Michael Seifert
  • 15,208
  • 31
  • 68