NAME

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

VERSION

Version 0.1

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::Collect 0.21;
use Math::Symbolic::Custom::Matrix;
use Math::Symbolic::Custom::Polynomial;

# 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})->to_collected();
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_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.

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 "to_collected()" 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.

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>

BUGS

Please report any bugs or feature requests to bug-math-symbolic-custom-matrix at rt.cpan.org, or through the web interface at https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Math-Symbolic-Custom-Matrix. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

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.