Discussion:
Eigenvalues works very slow
(too old to reply)
jure lapajne
2012-10-24 07:30:52 UTC
Permalink
Hello,
I'm trying to calculate eigenvalues of different sized matrices (from 10x10 to 1000x1000) using mathematica's built-in function - Eigenvalues. For smaller matrices it works ok, but for larger matrices it's just too slow. I tried writing the code in another system and it finds eigenvalues very quickly (in a second or two at most) even for big matrices. I'm not sure whether am I doing something wrong or is the speed difference really this big.
My code (you only need to change d to change the size of matrix):
d = 25;
q = Table[Table[1/2*Sqrt[i + j + 1]*KroneckerDelta[Abs[i - j], 1], {j, 0, d-1}],{i, 0, d - 1}];
h0 = Table[Table[(i + 1/2)*KroneckerDelta[i, j], {j, 0, d - 1}], {i, 0,d-1}];
lambda = 1/2;
q4 = lambda*q.q.q.q;
N[Eigenvalues[q4 + h0]]

Thanks for help.
David Bailey
2012-10-25 05:40:34 UTC
Permalink
Post by jure lapajne
Hello,
I'm trying to calculate eigenvalues of different sized matrices (from 10x10 to 1000x1000) using mathematica's built-in function - Eigenvalues. For smaller matrices it works ok, but for larger matrices it's just too slow. I tried writing the code in another system and it finds eigenvalues very quickly (in a second or two at most) even for big matrices. I'm not sure whether am I doing something wrong or is the speed difference really this big.
d = 25;
q = Table[Table[1/2*Sqrt[i + j + 1]*KroneckerDelta[Abs[i - j], 1], {j, 0, d-1}],{i, 0, d - 1}];
h0 = Table[Table[(i + 1/2)*KroneckerDelta[i, j], {j, 0, d - 1}], {i, 0,d-1}];
lambda = 1/2;
q4 = lambda*q.q.q.q;
N[Eigenvalues[q4 + h0]]
Thanks for help.
You are calculating eigenvalues of an integer matrix - so you will get a
result in terms of complicated fractions, that you then reduce to
floating point numbers using N !

Change the last line to

Eigenvalues[N[q4 + h0]]

David Bailey
http://www.dbaileyconsultancy.co.uk
Bill Rowe
2012-10-25 05:40:56 UTC
Permalink
Hello, I'm trying to calculate eigenvalues of different sized
matrices (from 10x10 to 1000x1000) using mathematica's built-in
function - Eigenvalues. For smaller matrices it works ok, but for
larger matrices it's just too slow. I tried writing the code in
another system and it finds eigenvalues very quickly (in a second or
two at most) even for big matrices. I'm not sure whether am I doing
something wrong or is the speed difference really this big. My code
d = 25;
q = Table[Table[1/2*Sqrt[i + j + 1]*KroneckerDelta[Abs[i - j], 1], {j, 0, d-1}],{i, 0, d - 1}];
h0 = Table[Table[(i + 1/2)*KroneckerDelta[i, j], {j, 0, d - 1}], {i, 0,d-1}];
lambda = 1/2;
q4 = lambda*q.q.q.q;
N[Eigenvalues[q4 + h0]]
Your code asks Mathematica to obtain the exact value of each
eigenvalue and later convert these values to machine precision
numbers. I am certain the reason the other system is so much
faster is that it never computes the exact values for any eigenvalue.

Instead of doing:

N[Eigenvalues[q4 + h0]]

try

Eigenvalues[q4 + h0//N]

and you will find it is much faster for large matrices.

The general rule in Mathematica is if you want speed convert
values to machine precision as early as possible in your code.
If you want accuracy and are not concerned with speed, convert
values to numeric values as late as possible in your code.
Harker, Anthony
2012-10-25 05:41:17 UTC
Permalink
As you want real values in the end, stop Mathematica from manipulating enormously complicated exact expressions by letting Eigenvalues[] work on the real form:
Eigenvalues[N[q4 + h0]]


Tony Harker


]-> -----Original Message-----
]-> From: jure lapajne [mailto:***@gmail.com]
]-> Sent: 24 October 2012 08:30
]-> To: ***@smc.vnet.net
]-> Subject: Eigenvalues works very slow
]->
]-> Hello,
]-> I'm trying to calculate eigenvalues of different sized matrices (from 10x10
]-> to 1000x1000) using mathematica's built-in function - Eigenvalues. For
]-> smaller matrices it works ok, but for larger matrices it's just too slow. I tried
]-> writing the code in another system and it finds eigenvalues very quickly (in
]-> a second or two at most) even for big matrices. I'm not sure whether am I
]-> doing something wrong or is the speed difference really this big.
]-> My code (you only need to change d to change the size of matrix):
]-> d = 25;
]-> q = Table[Table[1/2*Sqrt[i + j + 1]*KroneckerDelta[Abs[i - j], 1], {j, 0, d-
]-> 1}],{i, 0, d - 1}];
]-> h0 = Table[Table[(i + 1/2)*KroneckerDelta[i, j], {j, 0, d - 1}], {i, 0,d-1}];
]-> lambda = 1/2;
]-> q4 = lambda*q.q.q.q;
]-> N[Eigenvalues[q4 + h0]]
]->
]-> Thanks for help.
]->
Sseziwa Mukasa
2012-10-25 05:41:37 UTC
Permalink
Does the other system use infinite precision?

You can get an approximation of the eigenvalues by converting to machine precision:

(Debug) In[1]:= d = 25;
q = Table[
Table[1/2*Sqrt[i + j + 1]*KroneckerDelta[Abs[i - j], 1], {j, 0,
d - 1}], {i, 0, d - 1}];
h0 = Table[
Table[(i + 1/2)*KroneckerDelta[i, j], {j, 0, d - 1}], {i, 0, d - 1}];
lambda = 1/2;
q4 = lambda*q.q.q.q;
Timing[N[Eigenvalues[q4 + h0]]][[1]]
Timing[Eigenvalues[N[q4 + h0]]][[1]]
(Debug) Out[6]= 18.6794
(Debug) Out[7]= 0.00085
Post by jure lapajne
Hello,
I'm trying to calculate eigenvalues of different sized matrices (from 10x10 to 1000x1000) using mathematica's built-in function - Eigenvalues. For smaller matrices it works ok, but for larger matrices it's just too slow. I tried writing the code in another system and it finds
eigenvalues very quickly (in a second or two at most) even for big matrices. I'm not sure whether am I doing something wrong or is the speed difference really this big.
Post by jure lapajne
d = 25;
q = Table[Table[1/2*Sqrt[i + j + 1]*KroneckerDelta[Abs[i - j], 1], {j, 0, d-1}],{i, 0, d - 1}];
h0 = Table[Table[(i + 1/2)*KroneckerDelta[i, j], {j, 0, d - 1}], {i, 0,d-1}];
lambda = 1/2;
q4 = lambda*q.q.q.q;
N[Eigenvalues[q4 + h0]]
Thanks for help.
l***@gmail.com
2012-10-26 03:35:19 UTC
Permalink
It works now, thank you.
Michael Weyrauch
2012-10-26 03:35:39 UTC
Permalink
Hello,

there is a very simple solution to this:

Eigenvalues[N[q4 + h0]]

Reason: If you want to use numerical algorithms as most likely
the other system does automatically (because it doesen't know better),
then you need to give numerical input to the function Eigenvalues.

Unlike most other systems, Mathematica is smart enough to
chose different algorithms depending on the input it receives.

Michael
Post by jure lapajne
Hello,
I'm trying to calculate eigenvalues of different sized matrices (from 10x10 to 1000x1000) using mathematica's built-in function - Eigenvalues. For smaller matrices it works ok, but for larger matrices it's just too slow. I tried writing the code in another system and it finds eigenvalues very quickly (in a second or two at most) even for big matrices. I'm not sure whether am I doing something wrong or is the speed difference really this big.
d = 25;
q = Table[Table[1/2*Sqrt[i + j + 1]*KroneckerDelta[Abs[i - j], 1], {j, 0, d-1}],{i, 0, d - 1}];
h0 = Table[Table[(i + 1/2)*KroneckerDelta[i, j], {j, 0, d - 1}], {i, 0,d-1}];
lambda = 1/2;
q4 = lambda*q.q.q.q;
N[Eigenvalues[q4 + h0]]
Thanks for help.
Continue reading on narkive:
Loading...