# Finding eigenvalues of a $1500\times1500$ matrix

I need to find the eigenvalues of a $1500\times1500$ real symmetric matrix given by $A_{i,i+1}= A_{i+1,i}=-1$ and also $A_{1,N=1500}=-1$ (this is because of a periodic boundary condition used) and all other off-diagonal elements are zero. Also the diagonal elements are non zero, unequal real numbers. According to what you said my matrix is sparse and numeric.

How do I do this in Mathematica?

-
The code to generate the matrix very much depends on what matrix you want to find the eigenvalues of. Especially important is the question whether your matrix is sparse. Also relevant may be if you need exact eigenvalues (assuming your matrix is exact) or are satisfied with numeric eigenvalues (if the matrix itself is numeric, there's of course no choice about this). –  celtschk Sep 14 '12 at 8:40
The close vote is mine, but now that the question is specific, please disregard it. –  Leonid Shifrin Sep 14 '12 at 10:59

"how to write down my matrix in Mathematica"

Band could be useful to construct a matrix with the structure you describe.

Assuming

 diagonal = RandomReal[10, {1500}];


holds your data for the diagonal entries,

mtrx = SparseArray[{
Band[{1, 1}] -> diagonal,
{1, 1500} -> -1,
Band[{1, 2}] -> -1,
Band[{2, 1}] -> -1}, {1500, 1500}];


gives the matrix you need.

"and find the eigen values"

You can use

Eigenvalues[mtrx]


but you get the warning

Eigenvalues::arh: Because finding 1500 out of the 1500 eigenvalues and/or eigenvectors is likely to be faster with dense matrix methods, the sparse input matrix will be converted. If fewer eigenvalues and/or eigenvectors would be sufficient, consider restricting this number using the second argument to Eigenvalues (emphasis added.)

Despite the warning, Eigenvalues[mtrx] works faster than using the dense list Normal[mtrx] as the argument:

ev= Eigenvalues[Normal[mtrx]]; // AbsoluteTiming
(* {3.2860000, Null} *)


gives the eigenvalues you need.

You can also use Eigensystem which gives both eigenvalues and eigenvectors:

{evalues,evectors}=Eigensystem[Normal[mtrx]];//AbsoluteTiming
(* {11.7250000, Null}*)

-
using Normal makes it slower, not faster. In general the warning just says that you're not buying anything by constructing a sparse matrix, not that you shouldn't use one (it's obviously much easier to construct it like you and I did). –  acl Sep 14 '12 at 12:07
@acl, yes. Interestingly, timings are similar if you construct the dense matrix before using it; that is, with normalmtrx=Normal[mtrx], Eigenvalues[normalmtrx] and Eigenvalues[mtrx] gives similar timings, but Eigenvalues[Normal[mtrx]] is slower. –  kguler Sep 14 '12 at 13:07
yes; I noticed this some time ago. I imagine it is some optimization done internally, but never had the motivation to try to look into it. –  acl Sep 14 '12 at 13:09
It probably should be mentioned that for huge matrices, one is often interested in only the first few or last few eigenvalues, and thus, it is useful to use the second argument of Eigenvalues[] (as already noted in the Eigenvalues::arh warning message.) –  Ｊ. Ｍ. Sep 18 '12 at 3:36
@J.M good point - I made that part of the warning message bold. –  kguler Sep 18 '12 at 4:11

You can construct it like this:

m = With[
{length = 1500},
SparseArray[
{
{i_, i_} :> N@Cos[i],
{i_, j_} /; Abs[i - j] == 1 -> -1,
{1, length} -> -1,
{length, 1} -> -1
},
{length, length}
]
]


(I assumed your periodic boundary condition was mistyped in the question). It looks like this:

MatrixPlot[m]


-

Well:

m = RandomReal[10, {1500, 1500}];
Eigenvalues[m]; // Timing


{1.623, Null}

But I suppose you mean something else, or this question is trivial (and will be deleted). You should make your question more specific.

-
Sir, I am sorry for not being specific. My matrix is a real symmetric matrix. If I name it as A then A(i,i+1)= A(i+1,i)=-1 and also A(1,N=1500)=-1 (this is because of a periodic boundary condition used)and all other off-diagonal elements are zero. Also the diagonal elements are non zero, unequal real numbers. According to what you said my matrix is sparse and numeric. Kindly suggest how to find the eigen values. –  Shrabanti Dhar Sep 14 '12 at 10:22
@ShrabantiDhar please edit this information into your question. Thank you. –  Mr.Wizard Sep 14 '12 at 10:23