NAME

Math::Symbolic::Custom::Matrix - Matrix routines for Math::Symbolic

VERSION

Version 0.2

DESCRIPTION

Provides some routines for manipulating matrices of Math::Symbolic expressions. A matrix here is just a 2D array of elements. Passing in matrices with elements which are not already Math::Symbolic objects will cause them to be converted to Math::Symbolic objects.

EXAMPLE

use strict;
use Math::Symbolic qw/:all/;
use Math::Symbolic::MiscAlgebra qw/:all/;
use Math::Symbolic::Custom::Matrix 0.2;
use Math::Symbolic::Custom::Polynomial 0.11;
use Math::Symbolic::Custom::CollectSimplify 0.2;
Math::Symbolic::Custom::CollectSimplify->register();

# Say we want the eigenvalues of some matrix with a parameter.
# 1. A = | 4, 3-k |
#        | 2, 3   |
my @matrix = ([4,'3-k'],[2,3]);
my $A = make_symbolic_matrix(\@matrix);

# 2. get an identity matrix
my $I = identity_matrix(2);

# 3. multiply it with lambda
my $lambda_I = scalar_multiply_matrix("lambda", $I);

# 4. subtract it from matrix A
my $B = sub_matrix($A, $lambda_I);

# 5. form the characteristic polynomial, |A-lambda*I|
my $c_poly = det(@{$B})->simplify();
print "Characteristic polynomial is: $c_poly\n";

# 6. analyze the polynomial to get roots
my ($var, $coeffs, $disc, $roots) = $c_poly->test_polynomial('lambda');
print "Expressions for the roots are:\n\t$roots->[0]\n\t$roots->[1]\n";

# 7. Check for some values of parameter k
foreach my $k (0..3) {
    print "For k = $k: lambda_1 = ", 
        $roots->[0]->value('k' => $k), "; lambda_2 = ", 
        $roots->[1]->value('k' => $k), "\n";
}

EXPORTS

Everything below by default.

make_matrix

Creates a matrix of specified dimensions with every element set to the specified expression.

use strict;
use Math::Symbolic qw/:all/;
use Math::Symbolic::Custom::Matrix;

my $rows = 1;
my $cols = 2;
my $M = make_matrix('x', $rows, $cols);

make_symbolic_matrix

Pass in an array reference to a 2D matrix. This routine will call Math::Symbolic's "parse_from_string()" function to convert any non-Math::Symbolic elements to Math::Symbolic expressions.

Returns an array reference to the resulting matrix.

identity_matrix

Pass in the desired dimension of the (square) identity matrix.

Returns an array reference to the resulting matrix (which will be composed of Math::Symbolic constants 1 and 0 where appropriate).

add_matrix

Pass in two array references to the matrices to be added.

Returns an array reference to the resulting matrix.

sub_matrix

Pass in two array references to the matrices. Subtracts the second matrix from the first.

Returns an array reference to the resulting matrix.

multiply_matrix

Pass in array references to two matrices.

Returns an array reference to the matrix resulting from multiplying first matrix by the second.

scalar_multiply_matrix

This routine will multiply every element of a matrix by a single expression.

Pass in the expression and an array reference to the matrix.

Returns an array reference to the resulting matrix.

scalar_divide_matrix

This routine will produce an output matrix where every element is the input expression divided by every corresponding non-zero element of the input matrix. Elements which are zero are left untouched.

Pass in the expression and an array reference to the matrix.

Returns an array reference to the resulting matrix.

order_of_matrix

Pass in an array reference to a matrix.

This routine will return the number of rows and columns in the matrix. For example:-

use strict;
use Math::Symbolic qw/:all/;
use Math::Symbolic::Custom::Matrix;

my $A = make_symbolic_matrix([[1,2],[3,4],[5,6]]);
my ($r, $c) = order_of_matrix($A);
print "($r, $c)\n"; # (3, 2)

simplify_matrix

This will call "simplify()" on every element of the matrix, in an effort to tidy it up.

Pass in an array reference to the matrix.

Returns an array reference to the resulting matrix.

transpose_matrix

Pass in an array reference to a matrix.

Returns an array reference to the resulting transposed matrix.

evaluate_matrix

This will call Math::Symbolic's "value()" method on each element of the passed matrix.

Pass in an array reference to a matrix, and a hash ref which will be passed in as the parameters to the "value()" method.

Returns an array reference to the resulting matrix.

implement_matrix

This will call Math::Symbolic's "implement()" method on each element of the passed matrix.

Pass in an array reference to a matrix, and a hash ref which will be passed in as the parameters to the "implement()" method.

Returns an array reference to the resulting matrix.

set_matrix

This will call Math::Symbolic's "set_value()" method on each element of the passed matrix.

Pass in an array reference to a matrix, and a hash ref which will be passed in as the parameters to the "set_value()" method.

Returns an array reference to the resulting matrix.

cofactors_matrix

Pass in an array reference to a matrix.

Returns an array reference to the resulting cofactors matrix.

adjugate_matrix

Pass in an array reference to a matrix.

Returns an array reference to the adjugate of the matrix.

invert_matrix

Will attempt to invert the passed in matrix. Requires the determinant to be non-zero; of course if the matrix has variables then that won't necessarily be known until using the inverted matrix later.

Pass in an array reference to a matrix.

Returns an array reference to the inverted matrix.

is_square_matrix

Pass in an array ref to a matrix.

Returns 1 if the matrix is square, 0 otherwise.

is_equals_matrix

Pass in two array references for the matrices to compare.

Returns 1 if the matrices are equal (in terms of string expression), 0 otherwise.

is_symmetric_matrix

Pass in an array reference to a matrix.

Returns 1 if the matrix is symmetric, 0 otherwise.

is_skew_symmetric_matrix

Pass in an array reference to a matrix.

Returns 1 if the matrix is skew-symmetric, 0 otherwise.

SEE ALSO

Math::Symbolic

AUTHOR

Matt Johnson, <mjohnson at cpan.org>

ACKNOWLEDGEMENTS

Steffen Mueller, author of Math::Symbolic

LICENSE AND COPYRIGHT

This software is copyright (c) 2024 by Matt Johnson.

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