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

Sébastien de Menten sdementen at hotmail.com
Wed Apr 6 09:07:07 CDT 2005


>
>>However, I disagree with the “pretty straightforward to implement”. In 
>>fact, if one wants to inherit most of the functionalities of Numeric, it 
>>becomes quite cumbersome. Looking at MA module, I see that it needs to:
>
>It is straightforward AND cumbersome. Lots of work, but nothing difficult. 
>I agree of course that it would be nice to improve the situation.

My fault, I misunderstood your answer (... but it was a little bit 
misleading :-)


>The current behaviour of Numeric is more like
>
>	def ufunc(object):
>		if isarray(object):
>			return array_ufunc(object)
>		elif is_array_like(object):
>			return array_func(array(object))
>		else:
>			return object.ufunc()
>
>A more general version, which should cover your case as well, would be:
>
>	def ufunc(object):
>		if isarray(object):
>			return array_ufunc(object)
>		else:
>			try:
>				return object.applyUfunc(ufunc)
>			except AttributeError:
>				if is_array_like(object):
>					return array_func(array(object))
>				else:
>					raise ValueError
>
>There are two advantages:
>
>1) Classes can handle ufuncs in any way they like, even if they implement
>    array-like objects.
>2) Classes must implement only one method, not one per ufunc.
>
>Compared to the approach that you suggested:
>
>>where I would prefer something like
>>
>>  def ufunc( augmented_array ):
>>    if not isarray(augmented_array):
>>      augmented_array, contructor = 
>>augmented_array.__array_constructor__()
>>    else:
>>      constructor = lambda x:x
>>    return constructor(ufunc.apply(augmented_array))
>
>mine has the advantage of also covering classes that are not array-like at 
>all.
>

Yes !! That's a elegant solution for the ufunc part.

Do you think it is possible to integrate a similar mechanism in array 
functions (like searchsorted, argmax, ...).

If we can register functions taking one array as argument within scipy.base 
and let it dispatch those functions as ufunc, we could use a similar 
strategy.

For instance, let "sort" and "argmax" be registered as gfunc (general 
functions on an array <> ufunc), then any class that would like to overide 
any of them could do it too with the same trick Konrad exposed here above.

If another function uses those gfuncs and ufuncs, it inherits the genericity 
of the latter.

Konrad, do you think it is tricky to have a prototype of your suggestion 
(i.e. the modification does not need a full understanding of Numeric and you 
can locate it approximately in the source code) ?

Seb

>Konrad.
>--






More information about the Numpy-discussion mailing list