Can I create a Python Numpy ufunc from an unbound member method?

Import numpy as np class Foo(object): def f(self,x,y,z): return x + y + z Add a g method to Foo: def g(self,x,y,z): return np. Frompyfunc(self. F,3,1)(x,y,z) Foo.

G = g Calling g from an instance of Foo: I = Foo() print(i. G(np. Array(5,6),np.

Array(6,7),np. Array(7,8))) # 18 21.

Excellent. Exactly what I needed. I've additionally added an 'out=None' parameter to g to match the usual ufunc usage.

Thank you. – Rhys Ulerich Jun 6 at 13:51.

I must admit that I don't quite understand why you'd want f and g to be inside Foo class, but the following works: >>> class Foo(object): ... @staticmethod ... def f(x, y, z): ... return x+y+z ... >>> Foo. G = numpy. Frompyfunc(Foo.

F, 3, 1) >>> I = Foo() >>> i. G(5, 6, 7) 18 EDIT: since you want to use instance data in your method, what you actualy need is a bound method: >>> class Foo(object): ... def f(self, x, y, z): ... return x+y+z ... >>> I = Foo() >>> i. G = numpy.

Frompyfunc(i. F, 3, 1) >>> i. G(5, 6, 7) 18 Of course, in real code you will probably want to assign g in Foo.

__init__ , not outside of the class: >>> class Foo(object): ... def __init__(self): ... self. G = numpy. Frompyfunc(self.

F, 3, 1) ... def f(self, x, y, z): ... return x+y+z ... >>> I = Foo() >>> i. G(5, 6, 7) 18.

Thanks for looking into this. I'd like f and g inside Foo as in reality (i.e. Outside of this cooked example) my Foo contains nontrivial member data used by f(x,y,z).

The @staticmethod approach doesn't quite fit my needs as declaring f(x,y,z) static doesn't allow access to the data members. – Rhys Ulerich May 25 at 17:01 Oh, then you want a normal bound method. See above.

– Igor Nazarenko May 25 at 22:01 Your use of __init__(self) as a way to generate a bound method makes perfect sense (thank you), but I'm afraid my use case doesn't allow me to access to __init__ as Foo is generated by SWIG (unless it's possible to add code to __init__ using the Foo class object). Also, subclassing Foo to get access to the subclass' __init__ won't work either as SWIG will be using Foo and not the subclass. – Rhys Ulerich May 27 at 14:05.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions