[Numpy-discussion] vectorized function inside a class

mark markbak at gmail.com
Wed Aug 8 16:38:32 EDT 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... at ieee.org> wrote:
> On 8/8/07, mark <mark... at 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... at ieee.org
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discuss... at scipy.orghttp://projects.scipy.org/mailman/listinfo/numpy-discussion




More information about the NumPy-Discussion mailing list