Matrix Ops with Rational Output

Discussion in 'Perl Misc' started by David, Aug 10, 2010.

  1. David

    David Guest

    All,

    I have no experience with Perl modules that do linear algebra. I've
    searched CPAN and see several.

    Can someone advise me on what to use? I'd like to have the ability to
    start with a matrix with integer entries, put it in reduced row
    echelon form, with all of the entries as rational numbers, such as:


    A =

    4 0 0 2
    -1 -3 0 1
    0 2 0 -1


    ans =

    1 0 0 1/2
    0 1 0 -1/2
    0 0 0 0

    Suggestions? This has to be in perl.

    D.
     
    David, Aug 10, 2010
    #1
    1. Advertisements

  2. David

    Guest

    On Mon, 9 Aug 2010 20:25:53 -0700 (PDT), David <> wrote:

    >All,
    >
    >I have no experience with Perl modules that do linear algebra. I've
    >searched CPAN and see several.
    >
    >Can someone advise me on what to use? I'd like to have the ability to
    >start with a matrix with integer entries, put it in reduced row
    >echelon form, with all of the entries as rational numbers, such as:
    >
    >
    >A =
    >
    > 4 0 0 2
    > -1 -3 0 1
    > 0 2 0 -1
    >
    >
    >ans =
    >
    > 1 0 0 1/2
    > 0 1 0 -1/2
    > 0 0 0 0
    >
    >Suggestions? This has to be in perl.
    >
    >D.


    Try this, only tested on your matrix.
    And the swapped rows (redo) didn't come into
    play on your matrix. I just guess its a redo
    but don't know.
    Code is based on Pseudo code from wikipedia.

    -sln

    ---------------------------
    use strict;
    use warnings;

    #
    my @Matrix = (
    [ 4, 0, 0, 2 ],
    [ -1, -3, 0, 1 ],
    [ 0, 2, 0, -1 ],
    );

    my @Mreduced = GetReducedRowEchelonForm( @Matrix);

    print "\n";
    print "@{$_}\n" for (@Matrix);
    print "------ \n";
    print "@{$_}\n" for (@Mreduced);

    exit;

    #
    sub GetReducedRowEchelonForm
    {
    return () unless
    @_ && ref $_[0] eq "ARRAY";
    my @M;
    for my $k (0 .. $#_) {
    $M[$k] = [@{$_[$k]}];
    }
    my $lead = 0;
    my $rowCount = @M - 1;
    my $columnCount = @{$M[0]} - 1;

    FUNC:
    for my $r (0 .. $rowCount)
    {
    last FUNC if $columnCount <= $lead;
    my $i = $r;

    while( $M[$i][$lead] == 0) {
    $i++;
    if ( $rowCount == $i) {
    $i = $r;
    $lead++;
    last FUNC if $columnCount == $lead;
    }
    last FUNC if $i > $rowCount;
    }

    if ($i != $r) {
    my $irow = $M[$i];
    $M[$i] = $M[$r];
    $M[$r] = $irow;
    redo FUNC; # swapped rows, should we redo? Don't know.
    }

    my $divisor = $M[$r][$lead];
    foreach my $rowval ( @{$M[$r]} ) {
    $rowval /= $divisor;
    }

    for $i (0 .. $rowCount) {
    if ($i != $r) {
    my $multiplier = $M[$i][$lead];
    for my $ndx (0 .. $columnCount) {
    $M[$i][$ndx] -= $multiplier * $M[$r][$ndx];
    }
    }
    }
    $lead++;
    }
    return @M;
    }

    __END__

    http://en.wikipedia.org/wiki/Row_echelon_form

    The following pseudocode converts a matrix to reduced row-echelon form:

    function ToReducedRowEchelonForm(Matrix M) is
    lead := 0
    rowCount := the number of rows in M
    columnCount := the number of columns in M
    for 0 <= r < rowCount do
    if columnCount <= lead then
    stop function
    end if
    i = r
    while M[i, lead] = 0 do
    i = i + 1
    if rowCount = i then
    i = r
    lead = lead + 1
    if columnCount = lead then
    stop function
    end if
    end if
    end while
    if i != r then Swap rows i and r
    Divide row r by M[r, lead]
    for 0 <= i < rowCount do
    if i != r do
    Subtract M[i, lead] multiplied by row r from row i
    end if
    end for
    lead = lead + 1
    end for
    end function
     
    , Aug 10, 2010
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wendy S
    Replies:
    3
    Views:
    3,310
    Blah Blah
    Jul 13, 2003
  2. Glen Able
    Replies:
    4
    Views:
    552
    Attila Feher
    Jan 28, 2004
  3. Sergey
    Replies:
    6
    Views:
    3,147
    Victor Bazarov
    Apr 1, 2005
  4. lvcargnini

    Matrix composed by two matrix

    lvcargnini, Jul 4, 2006, in forum: VHDL
    Replies:
    3
    Views:
    2,923
    Jonathan Bromley
    Jul 5, 2006
  5. jalkadir

    overloaded ops << and >>

    jalkadir, Jun 30, 2005, in forum: C++
    Replies:
    10
    Views:
    712
    Old Wolf
    Jul 4, 2005
  6. Replies:
    15
    Views:
    947
  7. vsgdp

    logic ops,

    vsgdp, Sep 7, 2006, in forum: C++
    Replies:
    1
    Views:
    443
    Marcus Kwok
    Sep 7, 2006
  8. Holgerson

    Matrix*Vector and Vector*Matrix

    Holgerson, Oct 25, 2007, in forum: C++
    Replies:
    3
    Views:
    578
    Holgerson
    Oct 26, 2007
Loading...