forum.alglib.net http://forum.alglib.net/ |
|
Newbie question, c# http://forum.alglib.net/viewtopic.php?f=2&t=81 |
Page 1 of 1 |
Author: | panzram [ Mon Oct 18, 2010 7:03 am ] |
Post subject: | Newbie question, c# |
Hi all, I'm trying to calculate the eigenvalues of a 3x3 matrix using c#. I've used among other smatrixevd and rmatrixevd. The answers I get are wrong (or at least different), when checking against eg http://www.bluebit.gr/matrix-calculator/ or http://www.arndt-bruenner.de/mathe/scripts/engl_eigenwert.htm. Also, the answers differ between the 2 functions. Here is my code: public static double[] AlgEigenvalues(double[,] a) { int n = a.GetUpperBound(0); int zneeded = 0; bool isupper = true; double[] d; double[,] z; bool s = alglib.smatrixevd(a, n, zneeded, isupper, out d, out z); return d; } public static double[] AlgEigenvalues2(double[,] a) { int n = a.GetUpperBound(0); int vneeded = 0; double[] wr; double[] wi; double[,] vl; double[,] vr; bool s = alglib.rmatrixevd(a, n, vneeded, out wr, out wi, out vl, out vr); return wr; } I feel rather confident that the error is on my part. What the hell am I doing wrong? |
Author: | Sergey.Bochkanov [ Mon Oct 18, 2010 7:56 am ] |
Post subject: | Re: Newbie question, c# |
Can you post code which initializes your test matrices? Your code seems right, maybe something is wrong with matrices you pass to ALGLIB... P.S. there is always some difference between values returned by smatrixevd and rmatrixevd. How large it was in your case? |
Author: | panzram [ Mon Oct 18, 2010 10:10 am ] |
Post subject: | Re: Newbie question, c# |
Thanks for replying. If I initialize the matrices in the code, it looks like this: public static double[] AlgEigenvalues3() { double[,] a = new double[3,3]; a[0,0] = 9; a[0,1] = -2; a[0,2] = 0; a[1,0] = -3; a[1,1] = 4; a[1,2] = 7; a[2,0] = 4; a[2,1] = -4; a[2,2] = 0; int n = a.GetUpperBound(0); int vneeded = 0; double[] wr; double[] wi; double[,] vl; double[,] vr; bool s = alglib.rmatrixevd(a, n, vneeded, out wr, out wi, out vl, out vr); return wr; } public static double[] AlgEigenvalues4() { double[,] a = new double[3,3]; a[0,0] = 9; a[0,1] = -2; a[0,2] = 0; a[1,0] = -3; a[1,1] = 4; a[1,2] = 7; a[2,0] = 4; a[2,1] = -4; a[2,2] = 0; int n = a.GetUpperBound(0); int zneeded = 0; bool isupper = true; double[] d; double[,] z; bool s = alglib.smatrixevd(a, n, zneeded, isupper, out d, out z); return d; } The answer are: 10 and 3 for AlgEigenvalues3 3.298438 and 9.701562 for AlgEigenvalues4 According to the URLs I posted, the reply should be: ( 8.970, 0.000i) ( 2.015, 4.218i) ( 2.015,-4.218i) I can't see what I'm doing wrong... |
Author: | Sergey.Bochkanov [ Mon Oct 18, 2010 10:38 am ] |
Post subject: | Re: Newbie question, c# |
I see two errors: * your matrices are not symmetric, but you use smatrixevd(), which is intended for symmetric matrices only * you forgot to add +1 to int n = a.GetUpperBound(0); With rmatrixevd you should be able to get correct result. |
Author: | panzram [ Mon Oct 18, 2010 11:00 am ] |
Post subject: | Re: Newbie question, c# |
You are correct and I'm an idiot. Thanks, classic MacGruber! |
Author: | Sergey.Bochkanov [ Mon Oct 18, 2010 11:12 am ] |
Post subject: | Re: Newbie question, c# |
OK, everyone makes mistakes. I haven't noticed that n = a.GetUpperBound(0) at your first post, although it was pretty noticeable. |
Page 1 of 1 | All times are UTC |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |