[Numpy-discussion] vectorized function inside a class
mark
markbak@gmail....
Wed Aug 8 15:38:32 CDT 2007
Thanks for the ideas to circumvent vectorization.
But the real function I need to vectorize is quite a bit more
complicated.
So I would really like to use vectorize.
Are there any reasons against vectorization? Is it slow?
The way Tim suggests I expect to be slow as there are two functions
calls.
Thanks,
Mark
On Aug 8, 5:54 pm, "Timothy Hochberg" <tim.hochb...@ieee.org> wrote:
> On 8/8/07, mark <mark...@gmail.com> wrote:
>
>
>
>
>
> > I am trying to figure out a way to define a vectorized function inside
> > a class.
> > This is what I tried:
>
> > class test:
> > def __init__(self):
> > self.x = 3.0
> > def func(self,y):
> > rv = self.x
> > if y > self.x: rv = y
> > return rv
> > f = vectorize(func)
>
> > >>> m = test()
> > >>> m.f( m, [-20,4,6] )
> > array([ 3., 4., 6.])
>
> > But as you can see, I can only call the m.f function when I also pass
> > it the instance m again.
> > I really want to call it as
> > m.f( [-20,4,6] )
> > But then I get an error
> > ValueError: mismatch between python function inputs and received
> > arguments
>
> > Any ideas how to do this better?
>
> Don't use vectorize? Something like:
>
> def f(self,y):
> return np.where(y > self.x, y, self.x)
>
> You could also use vectorize by wrapping the result in a real method like
> this:
>
> _f = vectorize(func)
> def f(self, y):
> return self._f(self, y)
>
> That seems kind of silly in this instance though.
>
> -tim
>
> --
> . __
> . |-\
> .
> . tim.hochb...@ieee.org
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discuss...@scipy.orghttp://projects.scipy.org/mailman/listinfo/numpy-discussion
More information about the Numpy-discussion
mailing list