NAME

Math::Assistant - functions for various exact algebraic calculations

SYNOPSIS

use Math::Assistant qw(:algebra);

my $M = [ [4,1,4,3], [3,-4,7,5], [4,-9,8,5], [-3,2,-5,3], [2,2,-1,0] ];

# Rank of rectangular matrix
my $rank = Rank( $M );
print "Rank = $rank\n";

shift @$M; # Now a quadratic matrix

# Determinant of quadratic (integer) matrix
my $determinant = Det( $M, {'int' => 1} );
print "Determinant = $determinant\n";

# Solve an equation system
my $B = [ 1, 2, 3, 4 ];
my $solve = Solve_Det($M, $B, {'int' => 1} ); # 'eqs' => 'row' (default)
print "Equations is rows of matrix:\n", map{ "$_\n" } @$solve;

use Math::BigRat;
print(Math::BigRat->new("$_")->numify(),"\n") for @$solve;

print "Equations is columns of matrix:\n";
print "$_\n" for @{ Solve_Det( $M, $B, {'eqs' => 'column', 'int' => 1} ) ;

will print

    Rank = 4
    Determinant = -558
    Equations is rows of matrix:
	433/279
	-32/279
	-314/279
	70/93

    1.55197132616487
    -0.114695340501792
    -1.12544802867384
    0.752688172043011

    Equations is columns of matrix:
	283/186
	-77/93
	11/62
	13/93

DESCRIPTION

The module contains important algebraic operations: matrix rank, determinant and solve an equation system. The integer values are accepted. Calculations with the raised accuracy.

SUBROUTINES

Math::Assistant provides these subroutines:

Rank( \@matrix )
Det( \@matrix [, { int => 1 }] )
Solve_Det( \@A_matrix, \@b_vector [, { eqs => 'row|column', int => 1 }] )
Gaussian_elimination( \@matrix )
test_matrix( \@matrix )

All of them are context-sensitive.

Rank( \@matrix )

Calculates rank of rectangular (quadratic or non-quadratic) @matrix. Rank is a measure of the number of linear independent row and column (or number of linear independent equations in the case of a matrix representing an equation system).

Det( \@matrix [, { int => 1 }] )

This subroutine returns the determinant of the @matrix. Only quadratic matrices have determinant. Subroutine test_matrix uses for testing of non-quadratic @matrix.

If all elements of @matrix are integers then are using the facultative parameter 'int'. This causes subroutine to be a bit faster.

test_matrix( \@matrix )

Use this subroutine for testing of @matrix. This subroutine returns: 0 (Ok), 1..4 (Error). E.g.:

    my $t = Math::Assistant::test_matrix( $M );
    if( $t > 3 ){
	print "Use of uninitialized value in matrix\n";

    }elsif( $t ){
	croak("Matrix is not quadratic");
    }

Solve_Det(\@A_matrix, \@b_vector [, {eqs => 'row|column', int => 1}] )

Use this subroutine to actually solve an equation system.

Matrix "@A_matrix" must be quadratic matrix of your equation system A * x = b. By default the equations are in rows.

The input vector "@b_vector" is the vector "b" in your equation system A * x = b, which must be a row vector and have the same number of elements as the input matrix "@A_matrix" have rows (columns).

The subroutine returns the solution vector "$x" (which is the vector "x" of your equation system A * x = b) or undef is no solution.

    # Equation system:
    # x1 + x2 + x3 + x4 + x5 + x6 + x7 = 4
    # 64*x1 + 32*x2 + 16*x3 + 8*x4 + 4*x5 + 2*x6 + x7 = 85
    # ...................................
    # 7**6*x1 + 7**5*x2 + 7**4*x3 + 7**3*x4 + 7**2*x5 + 7*x6 + x7 = 120100

    my $M = [
	[1,1,1,1,1,1,1],
	[64,32,16,8,4,2,1],
	[729,243,81,27,9,3,1],
	[4**6, 4**5, 256,64,16,4,1],
	[5**6, 5**5, 5**4, 5**3, 5**2, 5, 1],
	[6**6, 6**5, 6**4, 6**3, 6**2, 6, 1],
	[7**6, 7**5, 7**4, 7**3, 7**2, 7, 1],
	 ];

    my $B = [ 4, 85, 820, 4369, 16276, 47989, 120100 ];

    print "$_\t" for @{ Solve_Det( $M, $B, {int => 1} ) };

will print:

1 0 1 0 1 0 1

Other example:

    # Equation system:
    # 1.3*x1 + 2.1*x2 + 34*x3 + 78*x4 = 1.1
    # 2.5*x1 + 4.7*x2 + 8.2*x3 + 16*x4 = 2.2
    # 3.1*x1 + 6.2*x2 + 12*x3 + 24*x4 = 3.3
    # 4.2*x1 + 8.7*x2 + 16*x3 + 33*x4 = 4.4

    $M = [  [1.3, 2.5, 3.1, 4.2],
	    [2.1, 4.7, 6.2, 8.7],
	    [34,  8.2, 12,  16],
	    [78,  16,  24,  33] ];
    print "$_\t" for @{ Solve_Det($M, [ 1.1, 2.2, 3.3, 4.4 ], {eqs => 'column'} ) };

will print:

-38049/17377  22902/17377  35101/34754  -36938/86885

Gaussian_elimination( \@matrix )

This subroutine returns matrix Gaussian elimination of the @matrix. The initial @matrix does not vary.

EXPORT

Math::Assistant exports nothing by default. Each of the subroutines can be exported on demand, as in

use Math::Assistant qw( Rank );

the tag algebra exports the subroutines Rank, Det, Solve_Det:

use Math::Assistant qw(:algebra);

and the tag all exports them all:

use Math::Assistant qw(:all);

DEPENDENCIES

Math::Assistant is known to run under perl 5.12.4 on Linux. The distribution uses Math::BigInt, Math::BigFloat, Test::More and Carp.

SEE ALSO

Math::MatrixReal is a Perl module that offers similar features.

AUTHOR

Alessandro Gorohovski, <an.gorohovski@gmail.com>

COPYRIGHT AND LICENSE

Copyright (C) 2010-2013 by A. N. Gorohovski

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.