forum.alglib.net

ALGLIB forum
It is currently Sun Dec 22, 2024 4:23 am

All times are UTC


Forum rules


1. This forum can be used for discussion of both ALGLIB-related and general numerical analysis questions
2. This forum is English-only - postings in other languages will be removed.



Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Newbie question, c#
PostPosted: Mon Oct 18, 2010 7:03 am 
Offline

Joined: Mon Oct 18, 2010 6:55 am
Posts: 8
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?


Top
 Profile  
 
 Post subject: Re: Newbie question, c#
PostPosted: Mon Oct 18, 2010 7:56 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
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?


Top
 Profile  
 
 Post subject: Re: Newbie question, c#
PostPosted: Mon Oct 18, 2010 10:10 am 
Offline

Joined: Mon Oct 18, 2010 6:55 am
Posts: 8
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...


Top
 Profile  
 
 Post subject: Re: Newbie question, c#
PostPosted: Mon Oct 18, 2010 10:38 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
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.


Top
 Profile  
 
 Post subject: Re: Newbie question, c#
PostPosted: Mon Oct 18, 2010 11:00 am 
Offline

Joined: Mon Oct 18, 2010 6:55 am
Posts: 8
You are correct and I'm an idiot. Thanks, classic MacGruber!


Top
 Profile  
 
 Post subject: Re: Newbie question, c#
PostPosted: Mon Oct 18, 2010 11:12 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
OK, everyone makes mistakes. I haven't noticed that n = a.GetUpperBound(0) at your first post, although it was pretty noticeable.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 20 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group