Recently in linearalgebra Category

Matrix Eigenvalues in Perl Don't Get Much Easier

The Matrix subsystem has really grown up in the last few days since the 0.16 release, including easy-to-use methods to calculate Matrix eigenvalues. All kinds of other methods are now available, like
```my \$det1 = \$A->det;      # determinant
my \$det2 = abs \$A;       # determinant

my \$inv = \$A->inverse;
my @eigs = \$A->eigenvalues;

my (\$eigenvalues, \$eigenvectors) = \$matrix->eigenpair;
my (\$eig1,\$eig2) = @\$eigenvalues;
my (\$u,\$v)       = @\$eigenvectors;
```
Compare this new script for calculating eigenvalues and eigenvectors of a nonsymmetric matrix to the old version. Yes, quite an improvement and quite pleasing to the eye now. Note that \$A->eigenvalues; returns a list of Math::Complex objects because in general the eigenvalues of a real matrix can be complex. Only if your matrix is symmetric are you guaranteed real eigenvalues.

Complex Eigenvalues with Math::GSL

After a question arose recently about finding complex eigenvalues of non-symmetric matrices, I wrote this example to show how it can be done with Math::GSL. The current example is called examples/matrix/nonsymmetric_eigen in the Math::GSL source, which will be in the 0.16 Math::GSL release. It uses the gsl_eigen_nonsymmv() function to do all the hard work, the rest is twiddling complex numbers. The documentation for this can be found with perldoc Math::GSL::Eigen or here.(The HTML rendering of the POD needs some work...) You can read the original C documentation for this function here.

It is a well known theorem of linear algebra that symmetric matrices can have only real eigenvalues. Thus it is much easier to find eigenvalues of symmetric matrices because there is no possibility of complex numbers arising. You can do this with Math::MatrixReal, which is written in pure-Perl.

The output of examples/matrix/nonsymmetric_eigen looks like:

Finding eigenvalue/eigenvectors for
[ 0  -1 ]
[ 1   0 ]

Eigenvectors:

u = (0.707106781186547,-0.707106781186547i)
v = (0.707106781186547,0.707106781186547i)

Eigenvalues:

lambda_0 = i
lambda_1 = -i

By the way, sqrt(2)/2 = 0.707106...

I am not quite happy with how long the program is, mainly because I have not written a Perlish interface around Complex Vectors and Matrices yet, so it is all in that script right now. The stringification could be built into the object and therefore reduce the length of that code dramatically.

Monthly Archives

 Subscribe to math-gsl-dev Email: Visit this group