# Eigenvalues error: “The method ”Banded“ accepts only sparse matrices with elements that are machine-real or machine-complex numbers”

I'm having one issue with the Eigenvalues function in some code priorly discussed here. There the example is tridiagonal, but here, let us consider this simple diagonal matrix:

range[nmax_] := range[nmax] = Range[0, nmax] // N
tab[nmax_Integer, t_] := tab[nmax, t] = t ^# &@range[nmax]
m[nmax_Integer, t_] := SparseArray[Band[{1, 1}] -> tab[nmax, t]]


Now, consider a function of the eigenvalues:

f[nmax_Integer, t_] := Total[Eigenvalues[m[nmax, t], Method -> "Banded"]]


If I try to plot it:

Plot[f[300,r],{r,0,2.5}]


I get the message:

Eigenvalues error: "The method "Banded" accepts only sparse matrices with elements that are machine-real or machine-complex numbers".


This is not specific to this function f. It happens to others as well.

By the discussion in my previous question, it seems that this error appears just in my computer. The people who answered the question didn't get the error with exactly the same code.

So is this something about my Mathematica configurations, or perhaps even about my machine hardware?

What is the reason for this error and how can I solve it?

• @Szabolcs, well, I had previously tried to use N and it did not solve the error. I also had even tried generating the matrix prior to the plot to see what was being generated and couldn't spot anything obviously wrong. For instance, using N all the zeroes are 0. really. Now, I've also noticed that the error just happens with nmax = 72 and bigger. Prior to this value there is no error. Do you think that this is perhaps a bug? Finally, I've tried with this simple matrix you give and I got no error. By the way, I've updated the question with one even simpler matrix. – user1620696 Jan 31 at 12:44
• Sorry, you are correct that there are some non-trivial issues here, and likely a bug too. I had to go away for a while, and could not finish the answer before you edited the post. The answer still uses your original example. I hope it's somewhat helpful. – Szabolcs Jan 31 at 14:00
• If you keep $|t|<1$ does the problem go away? I think that you only get convergence under this condition anyway; the case $|t|\ge1$ may not be interesting. – Roman Jan 31 at 15:39
• @Roman, I actually agree that we should have $|t|<1$. In the original problem we should have actually $t = \tanh r$. But notice that when I plot, I set $t = \tanh r$. Doesn't that immediately rule out $|t| \geq 1$? – user1620696 Jan 31 at 17:09
• You left out the $t=\tanh r$ step from your code. Yes, if you include it, you guarantee $t<1$: Plot[f[300, Tanh[r]], {r, 0, 10}, PlotRange -> All] seems to work well. – Roman Jan 31 at 17:16

There are two problems here:

• As the error states, at one point you are getting a matrix whose elements are not machine-reals or machine-complexes. In other words, you get a matrix whose elements are exact. You can use N to numericize such matrices.

• The specific problem matrix is what you get at t==1. For this value, all matrix values become exact zeros. This is quite counterintuitive because you do provide machine-reals as input to SparseArray, yet the result is exact. Example: SparseArray[{{1, 1}} -> {0.}, {5, 5}]. The result is an exact-zero matrix (not a machine-zero matrix) because the default background element of SparseArray is 0 and not 0.0. To force a numerical one, we could have used SparseArray[{{1, 1}} -> {0.}, {5, 5}, 0.] or N@SparseArray[...].

Now the second problem is that even if you have a machine-real matrix, Method -> "Banded" does not want to handle it. I believe this to be a bug, and I suggest you report it to Wolfram. Example: Eigenvalues[N@SparseArray[{} -> {}, {100, 100}], Method -> "Banded"] does not evaluate.

Now let us implement a workaround to the bug where Method -> "Banded" refuses to work on all-zero matrices.

Using the original example (before you edited the post),

Clear[tab, m]
tab[nmax_Integer, t_] :=
t^(2. (# - 1.)) (t^(2.) + # (1. - t^(2.))) &@Range[0., nmax]
m[nmax_Integer, t_] :=
SparseArray[Band[{1, 1}] -> ((1. - t^2.)/2.)*tab[nmax, t]]

Clear[eigenvalues]
eigenvalues[mat_SparseArray?SquareMatrixQ] :=

If[mat["NonzeroPositions"] === {} && mat["Background"] == 0.,
ConstantArray[0., Length[mat]],
Eigenvalues[mat, Method -> "Banded"]
]

Clear[f]
f[nmax_Integer, t_?NumericQ] := Total@eigenvalues[m[nmax, t]]


Now this works:

LogPlot[f[300, r], {r, 0, 1.3}]


• thanks for your help. I've tried that right now. Unfortunately for me it still gives the same error with exactly this code. Do you have any idea if it could be something related to my Mathematica configuration? – user1620696 Feb 1 at 12:25
• @user1620696 It could be related to the version. Which version are you using? – Szabolcs Feb 1 at 12:59
• It is version 11.2 student edition. Is it perhaps an issue with this particular version? – user1620696 Feb 1 at 13:03
• @user1620696 I tested the code above in v11.2, and it works fine (the same as in 11.3). That it's a student version does not matter. Are you sure that you are running exactly the code I provided, in a fresh kernel (i.e. after restart)? – Szabolcs Feb 1 at 13:08
• Yes, I'm running exactly this code in a fresh kernel. I might have the wrong impression, but this is the point I find weird: for other people like you and those who answered the previous question it works fine. Sorry if this makes no sense at all, but do you think this could be some hardware issue? I have one AMD Phenom II X2 555 3.2Ghz CPU with 6gb RAM. I personally think that for Mathematica this is fine. – user1620696 Feb 1 at 14:00

Define your f-function as

f[nmax_Integer, t_?NumericQ] := Total[Eigenvalues[m[nmax, N[t]], Method->"Banded"]]


to make sure the Eigenvalues function is never called with non-numeric values. Plot sometimes calls a function with non-numeric arguments (in this case, symbolic t) to check if the function can be evaluated analytically and then compiled.