I don't know if this helps but you could use where to do the dispatch between the two different formulas.
I don't know the answer to your original question however. On 2/14/07, Wojciech Śmigaj <[EMAIL PROTECTED]> wrote: > Timothy Hochberg wrote: > > On 2/14/07, *Wojciech Śmigaj* <[EMAIL PROTECTED] > > <mailto:[EMAIL PROTECTED]>> wrote: > >> I have a question about the vectorize function. I'd like to use it to > >> create a vectorized version of a class method. I've tried the following > >> code: > >> > >> from numpy import * > >> > >> class X: > >> def func(self, n): > >> return 2 * n # example > >> func = vectorize(func) > >> > >> [...] > > > I think you want staticmethod. Something like: > > > > class X: > > def f(x): > > return 2*x > > f = staticmethod(vectorize(x)) > > > > However, I don't have a Python distribution available here to check > > that. If that doesn't work, as search on staticmethod should get you to > > the correct syntax. > > > > I'll just note in passing that if your function is composed of > > completely of things that will operate on arrays as is (as your example > > is), then vectorizing the function is counter productive. However, your > > real code may well need vectorize to work. > > Thank you for your answer. I see now that my example was oversimplified. > In reality, the method func() accesses internal data of the object, so > it cannot be made a staticmethod. In addition, it does not operate on > the array as a whole: basically, it does calculations according to some > formula if n != 0, and to another one otherwise. Perhaps both parts > could be merged in some intelligent way, but right now I want to make > the program work, and optimization will be done later. And vectorize is > a very nice and quick way of making functions accept arrays, even if it > is not super-efficient. > > Best regards, > Wojciech Smigaj > > _______________________________________________ > Numpy-discussion mailing list > [email protected] > http://projects.scipy.org/mailman/listinfo/numpy-discussion > _______________________________________________ Numpy-discussion mailing list [email protected] http://projects.scipy.org/mailman/listinfo/numpy-discussion
