Skip Menu |

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

Report information
The Basics
Id: 44829
Status: resolved
Priority: 0/
Queue: Math-Polynomial

People
Owner: hasch-cpan [...] cozap.com
Requestors: user42 [...] zip.com.au
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: 0.04
Fixed in: 1.001



Subject: verbose stringize with leading -1 coeff
Date: Tue, 07 Apr 2009 06:45:58 +1000
To: bug-Math-Polynomial [...] rt.cpan.org
From: Kevin Ryde <user42 [...] zip.com.au>
With Math::Polynomial 0.04 and the debian perl 5.10.0, a program use Math::Polynomial; Math::Polynomial->verbose(1); $poly = Math::Polynomial->new(-1,0); print $poly,"\n"; prints -*$X where I hoped it would be just -$X without the "*" multiply (or alternately "-1*$X", but I suppose from the code the intention is to omit +1 or -1).
Thanks for your report. Stringification issues like this one are addressed in the upcoming release of Math::Polynomial. The old code for this has been replaced completely. You'll get nicer results and more control, too. Stay tuned. -Martin
Subject: Re: [rt.cpan.org #44829] verbose stringize with leading -1 coeff
Date: Fri, 10 Apr 2009 11:24:48 +1000
To: bug-Math-Polynomial [...] rt.cpan.org
From: Kevin Ryde <user42 [...] zip.com.au>
"Martin Becker via RT" <bug-Math-Polynomial@rt.cpan.org> writes: Show quoted text
> > Stringification
One form I wanted was something to cut and paste as code with nesting instead of powering, like ((($x + 1)*$x + 2)*$x) + 3 for x^3+x^2+2x+3. I got to the slightly nasty few lines below. I know there's advanced strategies for reducing operations in such an expression to eval though. Something geared towards perl could be cute. Maybe it would mean += and *= if they save a couple of cycles by being "in-place". do { my $result=$x; $result+=1; $result*=$x; ...; $result } sub to_string_for_eval { my ($poly) = @_; my $i = $poly->degree; if ($i < 0) { return '0'; } # build in @ret to avoid O(N^2) string copying from a ".=" append my @ret = (undef); my $need_parens = 0; my $open_parens = 0; my $times = ''; my $xpow = 0; { my $coeff = $poly->coeff($i); if ($coeff == 1 && $i > 0) { # just "$X" to start } elsif ($coeff == -1 && $i > 0) { my $minus = $CONFIG{MINUS}; $minus =~ tr/ //d; # no spaces on leading '-$X' push @ret, $minus; } else { push @ret, "$coeff"; # stringize $times = $CONFIG{TIMES}; } } for (;;) { $i--; my $coeff; if ($i >= 0) { $xpow++; $coeff = $poly->coeff($i); if ($coeff == 0) { next; } } if ($xpow != 0) { if ($need_parens) { $open_parens++; push @ret, ')'; } push @ret, "$times$CONFIG{VARIABLE}"; $times = $CONFIG{TIMES}; if ($xpow > 1) { push @ret, "$CONFIG{POWER}$xpow"; } $xpow = 0; } if ($i < 0) { last; } push @ret, ($coeff > 0 ? $CONFIG{PLUS}.$coeff : $CONFIG{MINUS}.-$coeff); $need_parens = 1; } $ret[0] = '(' x $open_parens; return join('', @ret); }
Subject: Stringification (was: verbose stringize with leading -1 coeff)
On Thu Apr 09 21:25:20 2009, user42@zip.com.au wrote: Show quoted text
> "Martin Becker via RT" <bug-Math-Polynomial@rt.cpan.org> writes:
> > > > Stringification
> > One form I wanted was something to cut and paste as code with nesting > instead of powering, like > > ((($x + 1)*$x + 2)*$x) + 3 > > for x^3+x^2+2x+3. > > I got to the slightly nasty few lines below. I know there's advanced > strategies for reducing operations in such an expression to eval though. > Something geared towards perl could be cute. Maybe it would mean += and > *= if they save a couple of cycles by being "in-place". > [code sample omitted]
If I got you right you want polynomials formatted like executable perl code (that's easy with Math::Polynomial 1.001) and an option to arrange output like a Horner schema rather than a sum of powers. The latter can be done but I am not sure whether such a feature is important enough to include it in the main module, as I/O is usually more concerned about readability than hypothetical execution performance. My idea was that Math::Polynomial would be used to evaluate polynomials directly without a code generation step in-between. Anyway, I put a Horner expression generator in the examples collection distributed with Math::Polynomial 1.001 for your pleasure. Thanks for the suggestion. And here is an example of how to coerce a polynomial into perl code that will evaluate to creating the same polynomial anew: use Math::Polynomial 1.001; my $p = Math::Polynomial->new(0, -2, 1); my $config = { with_variable => 0, ascending => 1, plus => q{, }, prefix => q{Math::Polynomial->new(}, suffix => q{)}, }; print $p->as_string($config), "\n"; Yours, -Martin
Subject: Re: [rt.cpan.org #44829] Stringification
Date: Thu, 28 May 2009 09:57:33 +1000
To: bug-Math-Polynomial [...] rt.cpan.org
From: Kevin Ryde <user42 [...] zip.com.au>
"Martin Becker via RT" <bug-Math-Polynomial@rt.cpan.org> writes: Show quoted text
> > Horner schema rather than a sum of powers.
Yes, and I'd be open to other eval strategies too. Show quoted text
> feature ... in the main module
Maybe a sub-module could do some things with eval strategies, either to run within the program, or to print the approach chosen as an expression or program code. I suppose for a start it might find the roots and see if there's some squared or higher terms to take out. Though that might be a bit like hard work, or might only work properly at all if coefficients are exact (integers or BigRats or whatnot). Show quoted text
> Math::Polynomial would be used to evaluate polynomials directly
I had a bit of code with a poly in a single function, and thought not to depend on a math module at runtime when I could cut and paste reasonably easily. For interest the worst one is a kind of nested job, a poly with terms which are polys themselves. sub laguerre_omitted { my ($f, $k) = @_; return $f ** ($k-2) * ((1/36*$k**2 + -1/36)*$k + ($f # f^($k-1) * ((1/4*$k + 1/4)*$k + ($f # f^$k * ((((-1/12*$k + -1/6)*$k + 3/4)*$k + 5/6) ... I calculate the terms like "(1/36*$k**2 + -1/36)*$k" in a separate slow program and it prints the slightly diabolical result to paste. The outer eval in $f is an explicit print, I was only looking at the terms in $k for Math::Polynomial to do.
On Wed May 27 19:58:22 2009, user42@zip.com.au wrote: Show quoted text
> "Martin Becker via RT" <bug-Math-Polynomial@rt.cpan.org> writes:
> > > > Horner schema rather than a sum of powers.
> > Yes, and I'd be open to other eval strategies too. >
> > feature ... in the main module
> > Maybe a sub-module could do some things with eval strategies, either to > run within the program, or to print the approach chosen as an expression > or program code. I suppose for a start it might find the roots and see > if there's some squared or higher terms to take out. Though that might > be a bit like hard work, or might only work properly at all if > coefficients are exact (integers or BigRats or whatnot).
I see, but I think stuff like this should best be covered by a symbolic math package or in any case something more generic than Math::Polynomial. You'd use very similar algorithms for rational functions, for example. Show quoted text
> > Math::Polynomial would be used to evaluate polynomials directly
> > [code generation usage example]
Thanks for the background. Now I can see where a different kind of formatting would have come in handy for you. But I stand by my decision not to make this core functionality. It will be in Math::Polynomial::PrettyPrint when I get around to it. I close this ticket again, as it was mainly about a bug which has been fixed. Cheers, -Martin
Subject: Re: [rt.cpan.org #44829] verbose stringize with leading -1 coeff
Date: Sun, 31 May 2009 11:24:03 +1000
To: bug-Math-Polynomial [...] rt.cpan.org
From: Kevin Ryde <user42 [...] zip.com.au>
"Martin Becker via RT" <bug-Math-Polynomial@rt.cpan.org> writes: Show quoted text
> > a different kind of formatting would have come in handy
Some horner form printing could go in a submodule like Math::Polynomial::Horner. A separate module would keep it out of normal operations, but be more accessible than an example program. It could obey existing stringize options, plus perhaps some extras like emitting perl code with "*=" etc if that looks like being a speedup, or clearer. I might work up something like that, if you don't get to it first.
On Sat May 30 21:24:29 2009, user42@zip.com.au wrote: Show quoted text
> Some horner form printing could go in a submodule like > Math::Polynomial::Horner. A separate module would keep it out of normal > operations, but be more accessible than an example program.
This issue has now an own ticket: https://rt.cpan.org/Ticket/Display.html?id=46575 I'll close 44829 again as the -1 bug addressed in the original report is resolved. -Martin