Hi Dhruv!

On Sat, Apr 18, 2009 at 1:10 PM, dhruv <lasnabvu...@gmail.com> wrote:
>
> Hello all,
>
> I want to build a class to describe a generating function (as used in
> the context of discrete probability distributions).  It would have
> exactly the functionality of the Poly class, have a normalization
> constraint on the coefficients, have a few more special methods that
> I'd define, and be created with a different set of arguments.  I want
> to be able to write
>
>>>> gf = GeneratingFunction(10,4,4,2) #  0.5 + 0.2 x + 0.2 x^2 + 0.1 x^3
>>>> print sum(gf.coeffs)  # normalized coefficients
> 1.0
>>>> print gf.expected_value() # a method i've written
> 0.9
>>>> two_moment = gf * gf # usual arithmetic for Poly instances
>>>> print sum(two_moment.coeffs)
> 1.0
>>>> print two_moment.expected_value()
> 1.8
>
> My first thought was to subclass the Poly class, force the
> coefficients to be normalized, pass appropriate arguments to
> Poly.__init__, and write methods like GeneratingFunction.expected_value
> () for the added functionality I want.
>
> But there seems to be no Poly.__init__ method for me to override.  If
> I override Poly.__new__ to accept the syntax I want to use (where the
> kth argument is the count of the kth item in the collection -- perfect
> for probability problems) then arithmetic for my generating functions
> breaks, as the __add__, __mul__, &c. methods call a "class method"
> which expect the arguments usually passed to Poly.__new__ and none
> other.

I don't understand the problem here exactly (e.g. why __add__ stops
working), as here is what I would do:

create GeneratingFunction.__new__() with your own arguments and then
call Poly.__new__() from it, with any preprocessing that you need.

See for example Piecewise.__new__() how this could be done.

>
> Can I get the functionality I want by forcing
> GeneratingFunction.__new__ to perform all the same checks and magic on
> its argument that Poly.__new__ is already doing?  Is there an easier
> way?

I think the above should be easy. Let us know if it works.

Ondrej

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to sympy@googlegroups.com
To unsubscribe from this group, send email to sympy+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sympy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to