Hi,

I'd like to kick off some discussion on general issues I've encountered
while developing the quaternion dtype (see other thread, and the code
at: https://github.com/martinling/numpy_quaternion)

The basic issue is that the attributes of ndarray cannot be adapted
to the dtype of a given array. Indeed, their behaviour can't be changed
at all without patching numpy itself.

There are essentially four cases of the problem:

1. Attributes which do the wrong thing even though there is a mechanism
   that should let them do the right thing, e.g:

   >>> a = array([quaternion(1,2,3,4), quaternion(5,6,7,8)])

   >>> conjugate(a) # correct, calls conjugate ufunc I defined
   array([quaternion(1, -2, -3, -4), quaternion(5, -6, -7, -8)], 
dtype=quaternion)

   >>> a.conjugate() # incorrect, why doesn't it do the same?
   array([quaternion(1, 2, 3, 4), quaternion(5, 6, 7, 8)], dtype=quaternion)

   >>> min(a) # works, calls min ufunc I defined
   quaternion(1, 2, 3, 4)

   >>> a.min() # fails, why doesn't it do the same?
   ValueError: No cast function available.

2. Attributes that do the wrong thing with no mechanism to override them:

   >>> array([q.real for q in a])
   array([ 1.,  5.])

   >>> a.real # would like this to return the same, can't make it do so
   array([quaternion(1, 2, 3, 4), quaternion(5, 6, 7, 8)], dtype=quaternion)

3. Attributes that don't exist and could be added to suit the dtype:

   >>> array([q.y for q in a])
   array([ 3.,  7.])

   >>> a.y # would like this to return the same, can't make it do so
   AttributeError: 'numpy.ndarray' object has no attribute 'y'

4. Attributes that already exist and make no sense for some dtypes:

   >>> sa = array(['foo', 'bar', 'baz'])

   >>> sa.imag # why can I even do this?
   array(['', '', ''], dtype='|S3')

We had Ń•ome discussion about this at the SciPy conference sprints and
the consensus seemed to be that allowing dtypes to customize the
attributes of ndarrays would be a good thing. This would also be useful
for struct arrays, datetime arrays, etc.

What do people think?


Martin
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to