[Numpy-discussion] metadata and metabehavior for arrays (for scipy.base or Numeric3)

konrad.hinsen at laposte.net konrad.hinsen at laposte.net
Thu Apr 7 01:34:37 EDT 2005

On Apr 7, 2005, at 10:06, David M. Cooke wrote:

> Hmm, I had misread your previous code. Here it is again, made more
> specific, and I'll assume this function lives in the ndarray package
> (as there is more than one package that defines ufuncs)

At the moment, there is one in Numeric and one in numarray. The Python 
API of both is nearly or fully identical.

> The thing is obj.__ufunc__ must understand about the *particular*
> object cos: the ndarray one. I was thinking more along the lines of

No, it must only know the interface. In most cases, it would do 
something like

	class MyArray:
		def __ufunc__(self, ufunc):
			return MyArray(apply(ufunc, self.data))

> obj.__ufunc__('cos'), where the name is passed instead.

That's also an interesting option. It would require the implementing 
class to choose an appropriate function from an appropriate module. 
Alternatively, it would work if ufuncs were also accessible as methods 
on array objects.

> For binary ufuncs, you could use (with arguments obj1 and obj2),
> obj1.__ufunc__('add', obj2)

Except that it would perhaps be better to have a different method, as 
otherwise nearly every implementation would have to start with a 
condition test to distinguish unary from binary ufuncs.

> Output argument (obj3): obj1.__ufunc__('add', obj2, obj3)
> Special methods:
>     obj1.__ufunc__('add.reduce')
>     obj1.__ufunc__('add.accumulate')
>     obj1.__ufunc__('add.outer', obj2)
> Basically, special methods are just another ufunc. This suggests that
> add.outer should optionally take an output argument...

But they are not just another ufunc, because a standard unary ufunc 
always returns an array of the same shape as its argument.

I'd probably prefer a few explicit methods:

	object.__binary__(add, other)


Konrad Hinsen
Laboratoire Léon Brillouin, CEA Saclay,
91191 Gif-sur-Yvette Cedex, France
Tel.: +33-1 69 08 79 25
Fax: +33-1 69 08 82 61
E-Mail: khinsen at cea.fr

More information about the NumPy-Discussion mailing list