ufuncs on user-defined types in numarray

I am working on making ScientificPython (http://dirac.cnrs-orleans.fr/ScientificPython/) compatible with numarray. One problem I stumbled across is that one feature of Numeric seems to be absent from numarray: the possibility to call ufuncs with arguments of non-numeric types, leading to a corresponding method call. As an illustration, the following example works fine with Numeric, but crashes with numarray: from Numeric import sqrt #from numarray import sqrt class DummyValue: def __init__(self, string): self.string = string def __str__(self): return self.string def sqrt(self): return DummyValue('sqrt(%s)' % self.string) x = DummyValue('x') print sqrt(x) Is this a bug or a feature? Is there another way to make ufuncs work with user-defined classes and types? Konrad. -- --------------------------------------------------------------------- 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: hinsen@llb.saclay.cea.fr ---------------------------------------------------------------------

On Fri, 2004-12-17 at 08:31, konrad.hinsen@laposte.net wrote:
I am working on making ScientificPython (http://dirac.cnrs-orleans.fr/ScientificPython/) compatible with numarray. One problem I stumbled across is that one feature of Numeric seems to be absent from numarray: the possibility to call ufuncs with arguments of non-numeric types, leading to a corresponding method call.
As an illustration, the following example works fine with Numeric, but crashes with numarray:
from Numeric import sqrt #from numarray import sqrt
class DummyValue:
def __init__(self, string): self.string = string
def __str__(self): return self.string
def sqrt(self): return DummyValue('sqrt(%s)' % self.string)
x = DummyValue('x') print sqrt(x)
Is this a bug or a feature?
It's a missing feature.
Is there another way to make ufuncs work with user-defined classes and types?
Not that I know of. How critical a feature is it? Do you have a work around? numarray ufunc handling is in flux as I'm adding numarray support to scipy. At some point, there's going to be a major ufunc consolidation, and perhaps I can add this additional feature then. Regards, Todd

On Dec 17, 2004, at 16:09, Todd Miller wrote:
Is there another way to make ufuncs work with user-defined classes and types?
Not that I know of. How critical a feature is it? Do you have a work around?
It is critical for some applications, though not for many. The most important application in ScientificPython is automatic derivatives. These are implemented as a number-like class whose instances store the values of a variable and of its derivatives. Such objects can be fed to any routine that does mathematical calculations, which is what makes it so powerful. Without this mechanism, all function applications would have to be written as method calls, which is less habitual, but more importantly creates code that doesn't work with plain numbers.
numarray ufunc handling is in flux as I'm adding numarray support to scipy. At some point, there's going to be a major ufunc consolidation, and perhaps I can add this additional feature then.
Sounds good. I'll postpone the ScientificPython update until then. Konrad. -- --------------------------------------------------------------------- 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: hinsen@llb.saclay.cea.fr ---------------------------------------------------------------------
participants (2)
-
konrad.hinsen@laposte.net
-
Todd Miller