Jonathan Leto > Math-MatrixReal-1.9 > Math::MatrixReal

Download:
Math-MatrixReal-1.9.tar.gz

Dependencies

Annotate this POD

Related Modules

Math::Matrix
Parse::RecDescent
PDL::Slatec
Math::BigFloat
Data::Dumper
CGI::Carp
Math::Complex
Math::Trig
Math::Pari
Inline::C
more...
By perlmonks.org

CPAN RT

New  5
Open  0
View/Report Bugs
Module Version: 1.9   Source   Latest Release: Math-MatrixReal-2.02
        for(my $i = 0;$i < $rows; $i++ ){
                $big = abs($matrix->[0][$i][0]);
                for(my $j=0;$j < $cols; $j++ ){
                        $big = $big < abs($matrix->[0][$i][$j]) 
                                ? abs($matrix->[0][$i][$j]) : $big;
                }
                next unless $big;
                # now $big is biggest element in row
                for(my $j = 0;$j < $cols; $j++ ){
                        $matrix->[0][$i][$j]  /= $big;
                }
                # now all elements are between [-1,1] and 
                # dependence can be easily checked
        }

        for(my $i = 0;$i < $rows; $i++ ){
                for(my $k = 0; $k < $rows; $k++ ){
                        next if ($i == $k);
                        print "i,k = $i,$k\n";
                        $tmprow = $matrix->row($i+1) + $matrix->row($k+1);
                        print "tmprow= $tmprow\n";
                        print "tmprow has " . $tmprow->_count_zeroes_row . " zeroes.\n";
                        print "i+1 has " . $matrix->row($i+1)->_count_zeroes_row . " zeroes\n";
                        print "k+1 has " . $matrix->row($k+1)->_count_zeroes_row . " zeroes\n";

                        $tmprow = $tmprow->_normalize_row;      
                        if( $tmprow->norm_sum == 0 ){
                                for(my $j=0;$j<$cols;$j++){
                                        $matrix->[0][$k][$j] = 0;
                                }
                        } elsif ( $tmprow->_count_zeroes_row > $matrix->row($i+1)->_count_zeroes_row ){
                                print "tmprow has more zeroes, replacing row " . ($i+1) . "\n";
                                ## assign row   
                                $matrix->assign_row($i+1,$tmprow);
                        } elsif ( $tmprow->_count_zeroes_row > $matrix->row($k+1)->_count_zeroes_row ){
                                print "tmprow has more zeroes, replacing row " . ($k+1) . "\n";
                                $matrix->assign_row($k+1,$tmprow);
                        }       
                                        
                }
        }
        return $matrix;
}

NAME ^

Math::MatrixReal - Matrix of Reals

Implements the data type "matrix of reals" (and consequently also "vector of reals").

DESCRIPTION ^

Implements the data type "matrix of reals", which can be used almost like any other basic Perl type thanks to OPERATOR OVERLOADING, i.e.,

  $product = $matrix1 * $matrix2;

does what you would like it to do (a matrix multiplication).

Also features many important operations and methods: matrix norm, matrix transposition, matrix inverse, determinant of a matrix, order and numerical condition of a matrix, scalar product of vectors, vector product of vectors, vector length, projection of row and column vectors, a comfortable way for reading in a matrix from a file, the keyboard or your code, and many more.

Allows to solve linear equation systems using an efficient algorithm known as "L-R-decomposition" and several approximative (iterative) methods.

Features an implementation of Kleene's algorithm to compute the minimal costs for all paths in a graph with weighted edges (the "weights" being the costs associated with each edge).

SYNOPSIS ^

Constructor Methods And Such

Matrix Row, Column and Element operations

Matrix Operations

Arithmetic Operations

Boolean Matrix Operations

Eigensystems

Miscellaneous

OVERLOADED OPERATORS ^

SYNOPSIS

DESCRIPTION

'-'

Unary minus

Returns the negative of the given matrix, i.e., the matrix with all elements multiplied with the factor "-1".

Example:

    $matrix = -$matrix;
'~'

Transposition

Returns the transposed of the given matrix.

Examples:

    $temp = ~$vector * $vector;
    $length = sqrt( $temp->element(1,1) );

    if (~$matrix == $matrix) { # matrix is symmetric ... }
abs

Norm

Returns the "one"-Norm of the given matrix.

Example:

    $error = abs( $A * $x - $b );
test

Boolean test

Tests wether there is at least one non-zero element in the matrix.

Example:

    if ($xn_vector) { # result of iteration is not zero ... }
'!'

Negated boolean test

Tests wether the matrix contains only zero's.

Examples:

    if (! $b_vector) { # heterogenous equation system ... }
    else             { # homogenous equation system ... }

    unless ($x_vector) { # not the null-vector! }
'""""'

"Stringify" operator

Converts the given matrix into a string.

Uses scientific representation to keep precision loss to a minimum in case you want to read this string back in again later with "new_from_string()".

Uses a 13-digit mantissa and a 20-character field for each element so that lines will wrap nicely on an 80-column screen.

Examples:

    $matrix = Math::MatrixReal->new_from_string(<<"MATRIX");
    [ 1  0 ]
    [ 0 -1 ]
    MATRIX
    print "$matrix";

    [  1.000000000000E+00  0.000000000000E+00 ]
    [  0.000000000000E+00 -1.000000000000E+00 ]

    $string = "$matrix";
    $test = Math::MatrixReal->new_from_string($string);
    if ($test == $matrix) { print ":-)\n"; } else { print ":-(\n"; }
'+'

Addition

Returns the sum of the two given matrices.

Examples:

    $matrix_S = $matrix_A + $matrix_B;

    $matrix_A += $matrix_B;
'-'

Subtraction

Returns the difference of the two given matrices.

Examples:

    $matrix_D = $matrix_A - $matrix_B;

    $matrix_A -= $matrix_B;

Note that this is the same as:

    $matrix_S = $matrix_A + -$matrix_B;

    $matrix_A += -$matrix_B;

(The latter are less efficient, though)

'*'

Multiplication

Returns the matrix product of the two given matrices or the product of the given matrix and scalar factor.

Examples:

    $matrix_P = $matrix_A * $matrix_B;

    $matrix_A *= $matrix_B;

    $vector_b = $matrix_A * $vector_x;

    $matrix_B = -1 * $matrix_A;

    $matrix_B = $matrix_A * -1;

    $matrix_A *= -1;
'**'

Exponentiation

Returns the matrix raised to an integer power. If 0 is passed, the identity matrix is returned. If a negative integer is passed, it computes the inverse (if it exists) and then raised the inverse to the absolute value of the integer. The matrix must be quadratic.

Examples:

    $matrix2 = $matrix ** 2;

    $matrix **= 2;

    $inv2 = $matrix ** -2;

    $ident = $matrix ** 0;
'=='

Equality

Tests two matrices for equality.

Example:

    if ( $A * $x == $b ) { print "EUREKA!\n"; }

Note that in most cases, due to numerical errors (due to the finite precision of computer arithmetics), it is a bad idea to compare two matrices or vectors this way.

Better use the norm of the difference of the two matrices you want to compare and compare that norm with a small number, like this:

    if ( abs( $A * $x - $b ) < 1E-12 ) { print "BINGO!\n"; }
'!='

Inequality

Tests two matrices for inequality.

Example:

    while ($x0_vector != $xn_vector) { # proceed with iteration ... }

(Stops when the iteration becomes stationary)

Note that (just like with the '==' operator), it is usually a bad idea to compare matrices or vectors this way. Compare the norm of the difference of the two matrices with a small number instead.

'<'

Less than

Examples:

    if ( $matrix1 < $matrix2 ) { # ... }

    if ( $vector < $epsilon ) { # ... }

    if ( 1E-12 < $vector ) { # ... }

    if ( $A * $x - $b < 1E-12 ) { # ... }

These are just shortcuts for saying:

    if ( abs($matrix1) < abs($matrix2) ) { # ... }

    if ( abs($vector) < abs($epsilon) ) { # ... }

    if ( abs(1E-12) < abs($vector) ) { # ... }

    if ( abs( $A * $x - $b ) < abs(1E-12) ) { # ... }

Uses the "one"-norm for matrices and Perl's built-in "abs()" for scalars.

'<='

Less than or equal

As with the '<' operator, this is just a shortcut for the same expression with "abs()" around all arguments.

Example:

    if ( $A * $x - $b <= 1E-12 ) { # ... }

which in fact is the same as:

    if ( abs( $A * $x - $b ) <= abs(1E-12) ) { # ... }

Uses the "one"-norm for matrices and Perl's built-in "abs()" for scalars.

'>'

Greater than

As with the '<' and '<=' operator, this

    if ( $xn - $x0 > 1E-12 ) { # ... }

is just a shortcut for:

    if ( abs( $xn - $x0 ) > abs(1E-12) ) { # ... }

Uses the "one"-norm for matrices and Perl's built-in "abs()" for scalars.

'>='

Greater than or equal

As with the '<', '<=' and '>' operator, the following

    if ( $LR >= $A ) { # ... }

is simply a shortcut for:

    if ( abs($LR) >= abs($A) ) { # ... }

Uses the "one"-norm for matrices and Perl's built-in "abs()" for scalars.

SEE ALSO ^

Math::VectorReal, Math::PARI, Math::MatrixBool, DFA::Kleene, Math::Kleene, Set::IntegerRange, Set::IntegerFast .

VERSION ^

This man page documents Math::MatrixReal version 1.9.

The latest version can always be found at http://leto.net/code/Math-MatrixReal/

AUTHORS ^

Steffen Beyer <sb@engelschall.com>, Rodolphe Ortalo <ortalo@laas.fr>, Jonathan Leto <jonathan@leto.net>.

Currently maintained by Jonathan Leto, send all bugs/patches to me.

CREDITS ^

Many thanks to Prof. Pahlings for stoking the fire of my enthusiasm for Algebra and Linear Algebra at the university (RWTH Aachen, Germany), and to Prof. Esser and his assistant, Mr. Jarausch, for their fascinating lectures in Numerical Analysis!

COPYRIGHT ^

Copyright (c) 1996-2002 by Steffen Beyer, Rodolphe Ortalo, Jonathan Leto. All rights reserved.

LICENSE AGREEMENT ^

This package is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

syntax highlighting: