[SciPy-dev] Generic polynomials class (was Re: Volunteer for Scipy Project)
Sebastian Walter
sebastian.walter@gmail....
Tue Oct 13 09:06:59 CDT 2009
On Tue, Oct 13, 2009 at 3:36 PM, Charles R Harris
<charlesr.harris@gmail.com> wrote:
>
>
> On Tue, Oct 13, 2009 at 4:08 AM, Sebastian Walter
> <sebastian.walter@gmail.com> 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
>> ago:
>> 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
class PowerBaseOps(object):
@classmethod
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"""
class PolyClass(object):
def __init__(self, coeffs):
self.coeffs = np.asarray(coeffs, dtype=dtype)
def __mul__(self,rhs):
return PolyClass(BaseOps.mul(self.coeffs, rhs.coeffs))
def __str__(self):
return str(self.coeffs)
return PolyClass
if __name__ == '__main__':
PowerPolynomial = poly_factory(PowerBaseOps, float)
IntPowerPolynomial = poly_factory(PowerBaseOps, int)
x = PowerPolynomial([1.,2.])
y = PowerPolynomial([3.,4.])
z = x * y
print z
print PowerBaseOps.mul([1.,2.],[3.,4.])
i = IntPowerPolynomial([1,2])
j = IntPowerPolynomial([3,4])
k = i * j
print k
--------- output ------------
python poly2.py
[ 3. 10. 8.]
[ 3. 10. 8.]
[ 3 10 8]
>
> Chuck
>
>
> _______________________________________________
> Scipy-dev mailing list
> Scipy-dev@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-dev
>
>
More information about the Scipy-dev
mailing list