[Numpy-discussion] strange multiplication behavior with numpy.float64 and ndarray subclass

Darren Dale dsdale24 at gmail.com
Wed Jan 21 11:34:07 EST 2009


I have a simple test script here that multiplies an ndarray subclass with
another number. Can anyone help me understand why each of these combinations
returns a new instance of MyArray:

mine = MyArray()
print type(np.float32(1)*mine)
print type(mine*np.float32(1))
print type(mine*np.float64(1))
print type(1*mine)
print type(mine*1)

but this one returns a np.float64 instance?

print type(np.float64(1)*mine)


Here is the full script:

import numpy as np

class MyArray(np.ndarray):

    __array_priority__ = 20

    def __new__(cls):
        return np.asarray(1).view(cls).copy()

    def __repr__(self):
        return 'my_array'

    __str__ = __repr__

    def __mul__(self, other):
        return super(MyArray, self).__mul__(other)

    def __rmul__(self, other):
        return super(MyArray, self).__rmul__(other)

mine = MyArray()
print type(np.float32(1)*mine)
print type(mine*np.float32(1))
print type(mine*np.float64(1))
print type(1*mine)
print type(mine*1)

print type(np.float64(1)*mine)


Thanks,
Darren
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20090121/52c6f7fb/attachment.html>


More information about the NumPy-Discussion mailing list