Subject: | Simplifying sums inserts a 0 constant term |
Before this patch, if there is no constant term when simplifying, or if
the constant term is zero, Math::Symbolic will insert a zero constant term.
# Before
perl -Ilib -MMath::Symbolic -le'print
Math::Symbolic->parse_from_string("x+x^2")->simplify'
(0 + x) + (x ^ 2)
The patch will suppress the constant term if it would be zero.
# After
perl -Ilib -MMath::Symbolic -le'print
Math::Symbolic->parse_from_string("x+x^2")->simplify'
x + (x ^ 2)
# After, unexpectedly.
perl -Ilib -MMath::Symbolic -le'print
Math::Symbolic->parse_from_string("x+x^2+3-3")->simplify'
(3 + (x + (x ^ 2))) - 3
I'm afraid I just discovered the second one, and this patch doesn't
include a test case (for either). I'm a bit busy at present, and wanted
to get this out while I had a moment -- I'll reply with more if I fix
either of those issues.
(PS - double-simplifying the "unexpectedly" case *does* give the wanted
result.)
Subject: | math-symbolic-simplify.diff |
--- lib/Math/Symbolic/Operator.pm.orig 2007-05-26 00:48:17.000000000 +0100
+++ lib/Math/Symbolic/Operator.pm 2007-05-26 01:08:37.000000000 +0100
@@ -814,11 +814,11 @@
: $mul;
}
- my $const = 0;
+ my $const;
$const += $_ foreach @const;
- $const = Math::Symbolic::Constant->new($const) if $const != 0;
+ $const = Math::Symbolic::Constant->new($const) if defined $const and $const != 0;
- $const = shift @vars if not defined $const;
+ $const = shift @vars if not defined $const;
foreach ( @vars ) {
$const = Math::Symbolic::Operator->new('+', $const, $_);
}