12 Dec 16:09 2009

## Suppressing of numpy ____mul____, ____div____ etc

Dmitrey <tmp50 <at> ukr.net>

2009-12-12 15:09:19 GMT

2009-12-12 15:09:19 GMT

hello all,

I have class oofun (in a package FuncDesigner, that is already used by lots of people) where some operations on numpy arrays (along with Python lists and numbers) are overloaded, such as __mul__, __add__, __pow__, __div__ etc.

There is a problem with numpy arrays: if I use a*f where a is array and f is oofun, it returns array with elements a[i]*f. Would it omit calling array.__mul__ and call only oofun.__rmul__, like it is done in numpy.matrix and Python lists/numbers, all would work ok as expected; but now it makes FuncDesigner code to work much slower or unexpectedly or doesn't work at all instead.

So, does anyone mind if I'll commit some changes to numpy __mul__, __div__ etc?

I intend to implement the following walkaround:

Now the code looks like this for array:

def __mul__(self, i):

return asarray(multiply(self, i))

and like this for numpy/matrixlib/defmatrix.py:

def __mul__(self, other):

if isinstance(other,(N.ndarray, list, tuple)) :

return N.dot(self, asmatrix(other))

if isscalar(other) or not hasattr(other, '__rmul__') :

return N.dot(self, other)

return NotImplemented

and I want to add an empty class named "CNumpyLeftOperatorOverloaded" to numpy, and if someone defines his class as a child of the one, __mul__ and others will not invoke __div__ etc, calling otherClass.__rdiv__ etc:

def __mul__(self, i):

return asarray(multiply(self, i)) if not isinstance(i,CNumpyLeftOperatorOverloaded) else i.__rmul__(self)

and declare my class as a child of the one.

As far as I understood, the changes should be added to numpy/core/defcherarray.py

So, does anyone mind me to implement it?

D.

I have class oofun (in a package FuncDesigner, that is already used by lots of people) where some operations on numpy arrays (along with Python lists and numbers) are overloaded, such as __mul__, __add__, __pow__, __div__ etc.

There is a problem with numpy arrays: if I use a*f where a is array and f is oofun, it returns array with elements a[i]*f. Would it omit calling array.__mul__ and call only oofun.__rmul__, like it is done in numpy.matrix and Python lists/numbers, all would work ok as expected; but now it makes FuncDesigner code to work much slower or unexpectedly or doesn't work at all instead.

So, does anyone mind if I'll commit some changes to numpy __mul__, __div__ etc?

I intend to implement the following walkaround:

Now the code looks like this for array:

def __mul__(self, i):

return asarray(multiply(self, i))

and like this for numpy/matrixlib/defmatrix.py:

def __mul__(self, other):

if isinstance(other,(N.ndarray, list, tuple)) :

return N.dot(self, asmatrix(other))

if isscalar(other) or not hasattr(other, '__rmul__') :

return N.dot(self, other)

return NotImplemented

and I want to add an empty class named "CNumpyLeftOperatorOverloaded" to numpy, and if someone defines his class as a child of the one, __mul__ and others will not invoke __div__ etc, calling otherClass.__rdiv__ etc:

def __mul__(self, i):

return asarray(multiply(self, i)) if not isinstance(i,CNumpyLeftOperatorOverloaded) else i.__rmul__(self)

and declare my class as a child of the one.

As far as I understood, the changes should be added to numpy/core/defcherarray.py

So, does anyone mind me to implement it?

D.

_______________________________________________ SciPy-Dev mailing list SciPy-Dev <at> scipy.org http://mail.scipy.org/mailman/listinfo/scipy-dev