thouis / numpy-trac-migration

numpy Trac to github issues migration
2 stars 3 forks source link

exclude ndarray operations __mul__, __pow__ etc (migrated from Trac #1333) #2886

Open thouis opened 11 years ago

thouis commented 11 years ago

Original ticket http://projects.scipy.org/numpy/ticket/1333 Reported 2009-12-15 by trac user dmitrey, assigned to unknown.

numpy ndarray operations !mul!, !div!, !pow!, !lt!, !gt!, !eq! etc (all non-unary ones) should be replaced by the lines like return asarray(multiply(self, i)) if not getattr(i,'!exclude_ndarray_operations!', False) else i.!rmul!(self)

This bug prevents correct stacking numpy ndarrays with matrices, polynomials, SAGE arrays, FuncDesigner types, etc.

See here for details http://comments.gmane.org/gmane.comp.python.scientific.devel/12436

thouis commented 11 years ago

Comment in Trac by atmention:pv, 2010-09-05

thouis commented 11 years ago

Comment in Trac by atmention:charris, 2010-09-05

For objects that don't inherit from ndarray or define the array method it is sufficient to simply define the array_priority attribute, the value doesn't matter. That is what I ended up doing for the polynomials and I think that was the solution reached in the linked comment thread.

Maybe we should bring this up on the list again just so we can see if the problems continue. Dag might have something to say relevant to Cython, he made some comments in the thread.

thouis commented 11 years ago

Comment in Trac by atmention:charris, 2010-09-05

The detection of array_priority is in the ufuncs though, not the ndarray class itself. I suppose one could argue that that makes the behavior depend on the implementation details of ndarray rather than being intrinsic. Even so, having some way to make the ufuncs reject certain objects is a plus.

In [4]: from numpy.polynomial import Polynomial as P

In [5]: multiply(ones(2), P([1,2]))
Out[5]: NotImplemented

In [6]: ones(2) * P([1,2])
Out[6]: Polynomial([ 1.,  3.,  2.], [-1.,  1.])