# Eigenvalue and SparseArray

This the code of Hofstadter spectrum for square lattice using Mathematica

 matrix[p_, q_] := Module[{sigma}, sigma = p/q;
N@SparseArray[{{m_, m_} ->
2 Cos[2 Pi*m*p/q], {i_, j_} /; Abs[i - j] == 1 -> 1}, {q, q}]];

attachsigma[sigma_, lst_] := {sigma, #} & /@ lst

fracs =
Table[p/q, {q, 2, 25}, {p, 2, q}] // Flatten // DeleteDuplicates;

pq = {Numerator@#, Denominator@#} & /@ fracs;

ens = Eigenvalues[#] & /@ (matrix[#[[1]], #[[2]]] & /@ pq);

pts = Flatten[#, 1] &@MapThread[attachsigma, {fracs, ens}];

plot = Graphics[{PointSize[0.001], Point[pts]}, AspectRatio -> 1,
ImageSize -> 300]



in my case, I have a huge matrix $$M[p,q]$$ I try to do the same thing as this code but it seems that N@SparseArray not useful for me what I'm looking for is: how I should write my input of plotting, my code is quite large just imagine that we have in the end a matrix M that depend on p and q and we need to plot the Hofstadter spectra how we should do this in Mathematica. thanks in advance

matrix[p_, q_] := SparseArray[{Band[{1, 1}] -> 2*Cos[2π*p*Range[q]/q],