[Numpy-discussion] Scalar output from sub-classed Numpy array
Thomas Robitaille
thomas.robitaille at gmail.com
Fri May 10 06:34:13 EDT 2013
Hi everyone,
I am currently trying to write a sub-class of Numpy ndarray, but am
running into issues for functions that return scalar results rather
than array results. For example, in the following case:
import numpy as np
class TestClass(np.ndarray):
def __new__(cls, input_array, unit=None):
return np.asarray(input_array).view(cls)
def __array_finalize__(self, obj):
if obj is None:
return
def __array_wrap__(self, out_arr, context=None):
return np.ndarray.__array_wrap__(self, out_arr, context)
I get:
In [4]: a = TestClass([1,2,3])
In [5]: print type(np.dot(a,a))
<type 'numpy.int64'>
In [6]: a = TestClass([[1,2],[1,2]])
In [7]: print type(np.dot(a,a))
<class '__main__.TestClass'>
that is, in the case where the output is a scalar, it doesn't get
wrapped, while in the case where the output is an array, it does.
Could anyone explain this behavior to me, and most importantly, is
there a way around this and have the above example return a wrapped
0-d TestClass array instead of a Numpy int64?
Thanks,
Tom
More information about the NumPy-Discussion
mailing list