2
$\begingroup$

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?

$\endgroup$
2
  • $\begingroup$ 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). $\endgroup$
    – celtschk
    Commented Sep 14, 2012 at 8:40
  • $\begingroup$ The close vote is mine, but now that the question is specific, please disregard it. $\endgroup$ Commented Sep 14, 2012 at 10:59

3 Answers 3

6
$\begingroup$

"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}*)
$\endgroup$
5
  • 1
    $\begingroup$ 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). $\endgroup$
    – acl
    Commented Sep 14, 2012 at 12:07
  • 1
    $\begingroup$ @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. $\endgroup$
    – kglr
    Commented Sep 14, 2012 at 13:07
  • $\begingroup$ 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. $\endgroup$
    – acl
    Commented Sep 14, 2012 at 13:09
  • $\begingroup$ 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.) $\endgroup$ Commented Sep 18, 2012 at 3:36
  • $\begingroup$ @J.M good point - I made that part of the warning message bold. $\endgroup$
    – kglr
    Commented Sep 18, 2012 at 4:11
4
$\begingroup$

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]

Mathematica graphics

$\endgroup$
2
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$ Commented Sep 14, 2012 at 10:22
  • $\begingroup$ @ShrabantiDhar please edit this information into your question. Thank you. $\endgroup$
    – Mr.Wizard
    Commented Sep 14, 2012 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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