qhayaal has asked for the wisdom of the Perl Monks concerning the following question:

Hello,
I need to diagonalize a huge matrix using lapack library. Is there a way by which I could use something, like inlining, I can use lapack libraries in Perl?
Thanks in advance for any feedback,
-Q

Replies are listed 'Best First'.
Re: Lapack, inline and Perl
by monkfan (Curate) on Mar 26, 2005 at 10:07 UTC
    Hi,
    Yes there is. it's called PDL::LAPACK.

    You must check out PDL, it has everything you need. If you are using Linux it should be there.

    Try:
    $ perldl
    It serves not so much as Inline, but as interface to those library.
    Regards,
    Edward
      PDL::LAPACK never wrapped more than a few inversion functions. PDL::LinearAlgebra (by Grégory Vanuxem) on the other hand is a pretty full wrapping of the whole of LAPACK for real and complex data, with nice MATLAB-like wrappings in the linked module.
Re: Lapack, inline and Perl
by ambrus (Abbot) on Mar 26, 2005 at 12:37 UTC

    If you can install octave, then you could try using Inline::Octave (which I've never tried), or just call octave directly. Octave is a free matrix manipulation program similar to matlab, and it has some lapack functions.

    Here's a quick example script that calculates the determinant of a 2x2 matrix with octave.

    #!perl use warnings; use strict; my $OCTAVE = "octave"; use File::Temp "tempfile"; my($datafile, $dataname) = tempfile; my($cmdfile, $cmdname) = tempfile; my @matrix = ( [3.5, 1.2], [-0.9, 4.5], ); my $mrows = @matrix; my $mcols = @{$matrix[0]}; print $datafile pack("d*", map { @$_ } @matrix) or die "error writing data: $!"; close $datafile or die "error closing data: $!"; print $cmdfile qq{ dataname = "$dataname"; % we trust tempfile not to return wier +d characters in filename mcols = $mcols; mrows = $mrows; } . q{ page_screen_output = 0; [datafile, msg] = fopen(dataname, "r"); if (datafile < 0) error ("error opening datafile: %s", msg); e +ndif; d = fread(datafile, mrows * mcols, "double"); % you should che +ck for errors here fclose(datafile); matrix = reshape(d, mcols, mrows).'; result = det(matrix); fwrite(stdout, result, "double"); % check for errors here too }; close $cmdfile or die "error closing data: $!"; open my $octave, "-|", $OCTAVE, "-q", $cmdname or die "cannot launch octave: $!"; my $result = unpack("d*", do { local undef $/; <$octave>; }); close $octave or die "could not close octave: $!"; $? and die "octave died"; print $result, "\n"; __END__
Re: Lapack, inline and Perl
by rg0now (Chaplain) on Mar 26, 2005 at 19:06 UTC
    If your matrix is not sooo large, you could use a pure Perl solution, Math::MatrixReal for diagonalization instead of messing with Lapack.

    Math::MatrixReal has a nice ($l, $V) = $matrix->sym_diagonalize() method that lets you to compute the eigensystem of a quadratic and symmetric matrix. Note that the implementation is really cool (it worked for me even for some astonishingly large matrices I threw at it just for fun): it uses Householder reduction followed by the QL algorithm, so it is in fact O(N^3)!!!

    rg0now