NAME

Math::Symbolic::Custom::Collect - Collect up Math::Symbolic expressions

VERSION

Version 0.2

SYNOPSIS

use Math::Symbolic qw(:all);
use Math::Symbolic::Custom::Collect;

my $t1 = "0.125";
print "Output: ", parse_from_string($t1)->to_collected()->to_string(), "\n";                
# Output: 1 / 8

my $t2 = "25/100";
print "Output: ", parse_from_string($t2)->to_collected()->to_string(), "\n";                
# Output: 1 / 4

my $t3 = "((1/4)+(1/2))*3*x";
print "Output: ", parse_from_string($t3)->to_collected()->to_string(), "\n";     
# Output: (9 * x) / 4           

my $t4 = "1/(1-(1/x))";
print "Output: ", parse_from_string($t4)->to_collected()->to_string(), "\n";          
# Output: x / (x - 1)

my $t5 = "sin(x^2+y)*sin(y+x^2)";
print "Output: ", parse_from_string($t5)->to_collected()->to_string(), "\n";    
# Output: (sin((x ^ 2) + y)) ^ 2

my $t6 = "x + x^2 + 3*x^3 + 2*x - x^2";
print "Output: ", parse_from_string($t6)->to_collected()->to_string(), "\n";
# Output: (3 * x) + (3 * (x ^ 3))

my $t7 = "((1/(3*a))-(1/(3*b)))/((a/b)-(b/a))";
print "Output: ", parse_from_string($t7)->to_collected()->to_string(), "\n";
# Output: (b - a) / ((3 * (a ^ 2)) - (3 * (b ^ 2)))

my $t8 = "(x+y+z)/2";
my @terms = parse_from_string($t8)->to_terms();
print "Terms: (", join("), (", @terms), ")\n";
# Terms: (x / 2), (y / 2), (z / 2)

$Math::Symbolic::Custom::Collect::COMPLEX_VAR = 'j';   # default is 'i'
my $t9 = "j*(3-7*j)*(2-j)";
print "Output: ", parse_from_string($t9)->to_collected()->to_string(), "\n";
# Output: 17 - j

DESCRIPTION

Provides "to_collected()" and "to_terms()" through the Math::Symbolic module extension class. "to_collected" performs the following operations on the inputted Math::Symbolic tree:-

  • Folds constants

  • Converts decimal numbers to rational numbers

  • Combines fractions

  • Expands brackets

  • Collects like terms

  • Cancels down

The result is often a more concise expression. However, because it does not (yet) factor the expression, the result is not always the simplest representation. Hence it is not offered as a simplify().

"to_terms()" uses "to_collected()" and returns the expression as a list of terms, that is a list of sub-expressions that can be summed to create an expression which is (numerically) equivalent to the original expression.

COMPLEX NUMBERS

From version 0.2, there is some support for complex numbers. The symbol in $Math::Symbolic::Custom::Collect::COMPLEX_VAR (set to 'i' by default) is considered by the module to be the symbol for the imaginary unit and treated as such when collecting up the expression. It is a Math::Symbolic variable to permit easy conversion to Math::Complex numbers using the value() method, for example:

use strict;
use Math::Symbolic qw(:all);
use Math::Symbolic::Custom::Collect;
use Math::Complex;

my $t = "x+sqrt(-100)+y*i";
my $M_S = parse_from_string($t)->to_collected();
print "$M_S\n"; # ((10 * i) + x) + (i * y)

# we want some kind of actual number from this expression
my $M_C = $M_S->value( 
                'x' => 2,
                'y' => 3, 
                'i' => i,  # glue Math::Symbolic and Math::Complex 
                );

# $M_C is a Math::Complex number
print "$M_C\n"; # 2+13i

SEE ALSO

Math::Symbolic

AUTHOR

Matt Johnson, <mjohnson at cpan.org>

BUGS

Please report any bugs or feature requests to bug-math-symbolic-custom-collect at rt.cpan.org, or through the web interface at https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Math-Symbolic-Custom-Collect. 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.