Skip Menu |

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

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

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

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



Subject: suggest mul1c and div1c as "root" operations
Date: Thu, 28 May 2009 09:19:22 +1000
To: bug-Math-Polynomial [...] rt.cpan.org
From: Kevin Ryde <user42 [...] zip.com.au>
Are the mul1c() and div1c() functions still available? They were slightly odd, but might be kept as "root" operations, per below. There might even be a place for a new_from_roots($c1,...$cN) forming (x-$c1)*(x-$c2)*...*(x-$cN), especially if it could be done more efficiently than O(N^2) which simple successive multiplications would take. =item I<$q = $p-E<gt>mul_root($c)> =item I<$q = $p-E<gt>div_root($c)> Return C<$p> multiplied by or divided by the polynomial C<x - $c> with given coefficient C<$c>. C<mul_root> has the effect of incorporating C<$c> as a root, ie. a zero, in the return. C<div_root> conversely removes a root C<$c>, or if C<$c> is not a root then the remainder from the division is discarded, the same as for C<div>. =cut sub mul_root { my ($this, $c) = @_; # FIXME: this can be done faster by a mul and add of the coeffs return $this->mul ($this->new(-$c,1)); } sub div_root { my ($this, $c) = @_; # FIXME: this can be done faster by a mul and sub of the coeffs return $this->div ($this->new(-$c,1)); }
On Wed May 27 19:19:48 2009, user42@zip.com.au wrote: Show quoted text
> Are the mul1c() and div1c() functions still available? They were > slightly odd, but might be kept as "root" operations, per below.
They are gone. I considered them odd, too. Show quoted text
> There might even be a place for a new_from_roots($c1,...$cN) forming > (x-$c1)*(x-$c2)*...*(x-$cN), especially if it could be done more > efficiently than O(N^2) which simple successive multiplications would > take.
This is an interesting suggestion. I like it for its expressive power though, rather than for the small performance to gain out of optimizing away a couple of multiplications by one. Show quoted text
> =item I<$q = $p-E<gt>mul_root($c)> > > =item I<$q = $p-E<gt>div_root($c)> > > Return C<$p> multiplied by or divided by the polynomial C<x - $c> with > given coefficient C<$c>. > > C<mul_root> has the effect of incorporating C<$c> as a root, ie. a zero, > in the return. C<div_root> conversely removes a root C<$c>, or if C<$c> > is not a root then the remainder from the division is discarded, the > same as for C<div>.
I am concerned whether those two methods would be as easy to understand as the equivalent explicit multiplication and division. As for efficiency, this: $p->mul($p->new(-$c, 1)) # 2n+2 multiplications, n additions, 1 negation ...could be made faster like so: $p->shift_up(1)->sub($p->mul_const($c)) # n+1 multiplications, n+1 subtractions ...which is not much worse than optimal (n+1 multiplications, n subtractions, 1 negation). But some users will prefer to waste a few CPU cycles in order to keep their application simple. I do see potential for interface extensions supporting other maybe more important optimizations. One such extension already in the making is Math::Polynomial::Sparse, which will make polynomials with lots of zero coefficients cheaper to handle. I have categorized this request as "wishlist". Thanks for your feedback, -Martin
Subject: Re: [rt.cpan.org #46427] suggest mul1c and div1c as "root" operations
Date: Sun, 31 May 2009 11:12:25 +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
> > multiplications by one.
If the coefficients are bignums I wouldn't count that class recognising ones in the creation or multiply. Show quoted text
> $p->mul($p->new(-$c, 1))
Oh, well, that creates a short-lived object, and you have to remember to get the args swapped around (ie. "x-$c" is -$c,1). If $c is an expression then the "-" is a bit unattractive too $mypoly->mul(Math::Polynomial->new(-($a+$b*$c), 1)) It was converting a few bits to this latter look that got me thinking "why can't someone else do it".
On Sat May 30 21:12:55 2009, user42@zip.com.au wrote: Show quoted text
> > $p->mul($p->new(-$c, 1))
> > Oh, well, that creates a short-lived object, and you have to remember to > get the args swapped around (ie. "x-$c" is -$c,1). If $c is an > expression then the "-" is a bit unattractive too > > $mypoly->mul(Math::Polynomial->new(-($a+$b*$c), 1)) > > It was converting a few bits to this latter look that got me thinking > "why can't someone else do it".
I'd do it with one medium-lifetime object like this: my $X = $mpoly->monomial(1); Then use expressions like $mpoly * ($X - $const) later on to multiply the polynomial by a linear "explicit root" factor. Using Math::Polynomial::Generic (which is not yet stable, so be careful), this would read: $mpoly * (X - C $const). So much for application code simplicity. This wastes a couple of operations in the coefficient space, however. I don't consider this very important but I'll think about it some more. -Martin
Subject: Re: [rt.cpan.org #46427] suggest mul1c and div1c as "root" operations
Date: Wed, 03 Jun 2009 11:46:13 +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
> > $mpoly * ($X - $const)
That looks fair, if you don't mind the extra $X kept around. My point though is what I said first, which is that it'd be about expressiveness, and compatibility (with the previous poly module), in a couple of quite nice operations. You did note I presume that you've got mul_root in interpolate() right now? And might well use div_root instead of building up from the deltas each time? (I had them in my own interpolate too, one based on the successive difference method shown in Knuth for when the input X points are successive.)
On Tue Jun 02 21:46:47 2009, user42@zip.com.au wrote: Show quoted text
> My point though is what I said first, which is that it'd be about > expressiveness, and compatibility (with the previous poly module), in a > couple of quite nice operations.
Point taken, both operations as well as a constructor building polynomials from "roots" will be in 1.002. Only one detail is yet to be defined: How should div_root treat non-root values? I am playing with the idea of a second result component for the remainder in analogy to divmod, but that looks somewhat unwieldy. Always discarding the remainder feels like a bit of a waste though. -Martin
Subject: Re: [rt.cpan.org #46427] suggest mul1c and div1c as "root" operations
Date: Sat, 13 Jun 2009 06:08:16 +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
> > 1.002
Good stuff. Show quoted text
> I am playing with > the idea of a second result component for the remainder in analogy to > divmod, but that looks somewhat unwieldy.
I suppose rounding in floats leaves a small meaningless remainder even when it's a root, only integers or bigrats would ever be exact. I'd be inclined to follow ->div() and discard a remainder for now, and think about a divmod_root later if there seems to be any use for it.
Version 1.002 is released now with the new functionality. -Martin