NAME
Math::Symbolic::Custom::CollectSimplify - Simplify Math::Symbolic expressions using Math::Symbolic::Custom::Collect
VERSION
Version 0.2
SYNOPSIS
use strict;
use Math::Symbolic qw(:all);
use Math::Symbolic::Custom::CollectSimplify;
# 1. We have some expressions
my $f1 = parse_from_string('2*(x+3)');
my $f2 = parse_from_string('(6*x+2)*(4+x)');
my $f3 = parse_from_string('3*x+(2*(x+1))');
# 2. Manipulate them in some way to create a big expression
my $f4 = $f1 + $f2 + $f3;
# 3. We want to simplify this
print "Expression: $f4\n";
# Expression: ((2 * (x + 3)) + (((6 * x) + 2) * (4 + x))) + ((3 * x) + (2 * (x + 1)))
# 4. Try with the simplify() that comes with Math::Symbolic
my $f4_s1 = $f4->simplify();
print "Original: $f4_s1\n";
# Original: (((2 * (3 + x)) + ((2 + (6 * x)) * (4 + x))) + (2 * (1 + x))) + (3 * x)
if ( $f4->test_num_equiv($f4_s1) ) {
print "\t- Is numerically equivalent with original expression\n";
}
# 5. Try with the simplify() in this module instead
# redefine "simplify()" using the register() method
Math::Symbolic::Custom::CollectSimplify->register();
my $f4_s2 = $f4->simplify();
print "New: $f4_s2\n";
# New: (16 + (33 * x)) + (6 * (x ^ 2))
if ( $f4->test_num_equiv($f4_s2) ) {
print "\t- Is numerically equivalent with original expression\n";
}
DESCRIPTION
Redefines Math::Symbolic's "simplify()" method using the Math::Symbolic module extension class Math::Symbolic::Custom::Simplification. This new simplify() method uses "to_collected()" in Math::Symbolic::Custom::Collect.
Be aware that "to_collected()" doesn't always produce a simpler expression from the inputted expression, because it does not factorize expressions. Setting the package variable $Math::Symbolic::Custom::CollectSimplify::TEST_COMPLEXITY
to 1 will make the simplify() routine check to see if the resultant expression is any simpler (using a measure of expression complexity based on the number of constants, variables and operators) and if not it will return the expression passed to it. Use this if you want to make sure you are getting the simplest possible expression. This behaviour is off by default.
SEE ALSO
Math::Symbolic::Custom::Simplification
Math::Symbolic::Custom::Collect
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.