[Numpy-discussion] Re: [Numpy-user] Optional shape in array description

Travis Oliphant oliphant.travis at ieee.org
Thu Jan 12 13:23:05 CST 2006


Francesc Altet wrote:

>Hi,
>
>I went over a weirdness on interpretation of the optional shape in the
>descriptor of types when declaring records.
>
>In [176]: numpy.zeros((2,), dtype=[('c1', 'f4')])
>Out[176]: array([(0.0,), (0.0,)], dtype=(void,4))
>
>So far, so good. However:
>
>In [177]: numpy.zeros((2,), dtype=[('c1', 'f4', 1)])
>Out[177]: array([(array([ 0.], dtype=float32),), (array([ 0.], 
>dtype=float32),)], dtype=(void,4))
>
>And:
>
>In [178]: numpy.zeros((2,), dtype=[('c1', 'f4', (1,))])
>Out[178]: array([(array([ 0.], dtype=float32),), (array([ 0.], 
>dtype=float32),)], dtype=(void,4))
>
>i.e. the very same value is obtained specifying shape=1 and shape=(1,).
>
>OTOH, in the point 3 of the description of the __array_descr__ of the
>array protocol (http://numeric.scipy.org/array_interface.html), one
>can read:
>
>"""
>An optional shape tuple providing how many times this part of the
>record should be repeated.  Assumed (1,) if not given.
>"""
>
>But this is clearly not what it is implemented.
>
>IMO, the correct thing (and what the numarray does) would be:
>
>In [176]: numpy.zeros((2,), dtype=[('c1', 'f4')])
>Out[176]: array([(0.0,), (0.0,)], dtype=(void,4))
>In [192]: numpy.zeros((2,), dtype=[('c1', 'f4')])[0][0]
>Out[192]: 0.0
>
>In [177]: numpy.zeros((2,), dtype=[('c1', 'f4', 1)])
>Out[177]: array([(0.0,), (0.0,)], dtype=(void,4))
>In [193]: numpy.zeros((2,), dtype=[('c1', 'f4', 1)])[0][0]
>Out[193]: 0.0
>
>In [178]: numpy.zeros((2,), dtype=[('c1', 'f4', (1,))])
>Out[178]: array([(array([ 0.], dtype=float32),), (array([ 0.], 
>dtype=float32),)], dtype=(void,4))
>In [194]: numpy.zeros((2,), dtype=[('c1', 'f4', (1,))])[0][0]
>Out[194]: array([ 0.], dtype=float32)
>
>i.e. that shape=1 would be equivalent to don't specify it (scalar
>case), and shape=(1,) would be equivalent to declare the field as
>unidimensional but with one element.
>  
>
This is what is now done in SVN...

Thanks for catching the corner case.  We should also change the verbage 
on the array_protocol page.

-Travis





More information about the Numpy-discussion mailing list