Skip Menu |

This queue is for tickets about the Math-Symbolic CPAN distribution.

Report information
The Basics
Id: 64269
Status: resolved
Priority: 0/
Queue: Math-Symbolic

People
Owner: Nobody in particular
Requestors: user42 [...] zip.com.au
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: simplify() wrong on sum of negative multiple
Date: Tue, 28 Dec 2010 08:16:08 +1100
To: bug-Math-Symbolic [...] rt.cpan.org
From: Kevin Ryde <user42 [...] zip.com.au>
In Math::Symbolic 0.604 on recent i386 debian perl 5.10.1 the program foo.pl below "simplifies" an expression x + (-5)*y to x + (-(-5 * y)) which I think has wrongly doubled the negation. I expected maybe x + (-(5 * y)) It seems when a sum "x + (-5)*y" is simplified the "-5" multiple of y becomes a U_MINUS but also leaves the negative on the multiplier "-5". Is it meant to have an abs() if it does that, per below? I struck this when trying "2*x^2 + (-1)*x" which should be the hexagonal numbers but gave wrong results unless entered as "2*x^2 - x". I might have been happy for the simplifier to leave a -5 constant instead of turning it into an extra U_MINUS operation, at least for numerical use where a negative -5 is the same price as a +5. Unless that's an option for that I haven't spotted. I suppose for human readability a subtract instead of add+uminus would be possible too, like "x - (5*y)", unless that's meant to be left to the print stage.
use strict; use warnings; use Math::Symbolic; my $tree = Math::Symbolic->parse_from_string('x + (-5)*y'); print "parsed: ",$tree->to_string,"\n"; print " ",$tree->to_string('prefix'),"\n"; my $simp = $tree->simplify; print "simplified: ",$simp->to_string,"\n"; print " ",$simp->to_string('prefix'),"\n";
--- Operator.pm.orig 2010-12-27 15:24:47.000000000 +1100 +++ Operator.pm 2010-12-27 15:24:47.000000000 +1100 @@ -835,7 +835,7 @@ } my $mul = Math::Symbolic::Operator->new( '*', - Math::Symbolic::Constant->new($num), + Math::Symbolic::Constant->new(abs($num)), Math::Symbolic::Variable->new($_) ); push @ops, $num < 0
Hi, thanks for the bug report! On Mon Dec 27 16:16:18 2010, user42@zip.com.au wrote: Show quoted text
> In Math::Symbolic 0.604 on recent i386 debian perl 5.10.1 the program > foo.pl below "simplifies" an expression
Apart from the fact that the term simplification doesn't always simplify, yes. This is a real bug. [snip] Indeed, I had missed an abs() operation. Fix committed and will be released as 0.605 within the hour. Thanks again for the bug report, diagnosis and fix all at once! Best regards, Steffen