Charles R Harris
Fri Oct 22 11:26:55 CDT 2010
On Fri, Oct 22, 2010 at 9:51 AM, <firstname.lastname@example.org> wrote:
> I'm subclassing numpy.polynomial.Polynomial. So far it works well.
> One question on inplace changes
> Is it safe to change coef directly without creating a new instance?
> I'm not trying to change anything else in the polynomial, just for
> example pad, truncate or invert the coef inplace, e.g
> def pad(self, maxlag):
> self.coef = np.r_[self.coef, np.zeros(maxlag - len(self.coef))]
> Currently, I have rewritten this to return a new instance.
You can (currently) modify the coef and it should work, but I think it best
to regard the Polynomial class as immutable. I'm even contemplating making
the coef attribute read only just to avoid such things. Another tip is to
use // instead of / for division, polynomials are rather like integers that
way and don't have a true divide so plain old / will fail for python 3.x
Note that most operations will trim trailing zeros off the result.
In : P((1,1,1,0,0,0))
Out: Polynomial([ 1., 1., 1., 0., 0., 0.], [-1., 1.])
In : P((1,1,1,0,0,0)) + 1
Out: Polynomial([ 2., 1., 1.], [-1., 1.])
The reason the constructor doesn't was because trailing zeros can be of
interest in least squares fits. Is there a particular use case for which
trailing zeros are important for you? The polynomial modules aren't finished
products yet, I can still add some functionality if you think it useful.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the NumPy-Discussion