[SciPy-dev] Generic polynomials class (was Re: Volunteer for Scipy Project)
Tue Oct 13 09:06:59 CDT 2009
On Tue, Oct 13, 2009 at 3:36 PM, Charles R Harris
> On Tue, Oct 13, 2009 at 4:08 AM, Sebastian Walter
> <firstname.lastname@example.org> wrote:
>> IMHO one should adhere to the KISS principle and separate the
>> algorithms from the syntactic sugar.
>> It also seems to me that hiding the base of the polynomial from the
>> user is actually a bad thing.
> It is kiss. You have the separate functions (which is what Robert wanted),
> and an interface that is reused. What it buys is complete code reuse in the
> interface as well as the ability to limit the operators to classes of the
> same sort. Inheritance doesn't really work for this because we don't want to
> operate on a common base class (is a relationship). But I think we do want
> every class to operate with itself in a similar manner.
>> As far as I understood, Robert Kern suggested the following a few weeks
>> 1) define the algorithms on the coefficients as class methods to avoid
>> name cluttering
> That was my initial thought: static methods. But I don't think that is what
> Robert had in mind, I think he just wanted to be sure the basic functions
> were available as standalone functions and not just methods of some class.
>> 2) have a convenience implementation that uses operator overloading
> The classes do that.
Hmm, ok, I think you are right. I changed my code it now uses a class factory.
import numpy as np
def mul(cls,lhs_coeffs, rhs_coeffs):
return np.convolve(lhs_coeffs, rhs_coeffs, mode='full')
def poly_factory(BaseOps, dtype):
""" produces polynomials with dtype coefficients"""
def __init__(self, coeffs):
self.coeffs = np.asarray(coeffs, dtype=dtype)
return PolyClass(BaseOps.mul(self.coeffs, rhs.coeffs))
if __name__ == '__main__':
PowerPolynomial = poly_factory(PowerBaseOps, float)
IntPowerPolynomial = poly_factory(PowerBaseOps, int)
x = PowerPolynomial([1.,2.])
y = PowerPolynomial([3.,4.])
z = x * y
i = IntPowerPolynomial([1,2])
j = IntPowerPolynomial([3,4])
k = i * j
--------- output ------------
[ 3. 10. 8.]
[ 3. 10. 8.]
[ 3 10 8]
> Scipy-dev mailing list
More information about the Scipy-dev