NAME
Math::Symbolic::Custom::Factor - Re-arrange a Math::Symbolic expression into a product of factors
VERSION
Version 0.13
DESCRIPTION
Provides method to_factored() through the Math::Symbolic module extension class. This method attempts to factorize a Math::Symbolic expression.
This is a very early version and can only factor relatively simple expressions. It has a few factoring strategies in it, and hopefully more will come along in later versions if I have time to implement them.
EXAMPLES
use
strict;
# to_factored() returns the full expression as a product of factors
# and an array ref to the factors themselves (so that multiplying them
# together and collecting up should produce the original expression).
my
(
$factored
,
$factors
) = parse_from_string(
"3*x + 12*y"
)->to_factored();
# $factored and the factors in $factors->[] are Math::Symbolic expressions.
"Full expression: $factored\n"
;
# Full expression: (x + (4 * y)) * 3
"Factors: '"
,
join
(
q{', '}
, @{
$factors
}),
"'\n\n"
;
# Factors: '3', 'x + (4 * y)'
(
$factored
,
$factors
) = parse_from_string(
"x^2 - 81"
)->to_factored();
"Full expression: $factored\n"
;
# Full expression: (9 + x) * (x - 9)
"Factors: '"
,
join
(
q{', '}
, @{
$factors
}),
"'\n\n"
;
# Factors: 'x - 9', '9 + x'
(
$factored
,
$factors
) = parse_from_string(
"6*x^2 + 37*x + 6"
)->to_factored();
"Full expression: $factored\n"
;
# Full expression: (6 + x) * (1 + (6 * x))
"Factors: '"
,
join
(
q{', '}
, @{
$factors
}),
"'\n\n"
;
# Factors: '6 + x', '1 + (6 * x)'
(
$factored
,
$factors
) = parse_from_string(
"y^4 - 5*y^3 - 5*y^2 + 23*y + 10"
)->to_factored();
"Full expression: $factored\n"
;
# Full expression: ((y - 5) * (((y ^ 2) - 1) - (2 * y))) * (2 + y)
"Factors: '"
,
join
(
q{', '}
, @{
$factors
}),
"'\n\n"
;
# Factors: '2 + y', 'y - 5', '((y ^ 2) - 1) - (2 * y)'
# This one does not factor (using the strategies in this module).
# The original expression is returned (albeit re-arranged) and the number of entries in
# @{$factors} is 1.
(
$factored
,
$factors
) = parse_from_string(
"x^2 + 2*x + 2"
)->to_factored();
"Full expression: $factored\n"
;
# Full expression: (2 + (2 * x)) + (x ^ 2)
"Factors: '"
,
join
(
q{', '}
, @{
$factors
}),
"'\n"
;
# Factors: '(2 + (2 * x)) + (x ^ 2)'
"Did not factorize\n\n"
if
scalar
(@{
$factors
}) == 1;
# Did not factorize
SEE ALSO
Math::Symbolic::Custom::Collect
Math::Symbolic::Custom::Polynomial
AUTHOR
Matt Johnson, <mjohnson at cpan.org>
ACKNOWLEDGEMENTS
Steffen Mueller, author of Math::Symbolic
LICENSE AND COPYRIGHT
This software is copyright (c) 2025 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.