[Numpy-discussion] scalar recordarrays

Matthew Koichi Grimes mkg at cs.nyu.edu
Mon Mar 19 15:59:12 EDT 2007


Francesc Altet wrote:
> with a
> rank-0 'recarr', 'recarr.x' should return a rank-0 array (for
> consistency), but it doesn't:
>
> In [74]:recarr=numpy.rec.array((1.0, 0, 3), dtype)
> In [75]:recarr.x
> Out[75]:1.0
> In [76]:type(recarr.x)
> Out[76]:<type 'numpy.float64'>
>
> While I find this inconsistent, I'm not sure whether it should be be
> fixed or not because the boundaries between rank-0 and scalar objects
> are always a bit fuzzy. Travis can probably bring more light here.
>   
Here's my case for "yes, it's a problem that should be fixed (please?)": 
In my particular project, this inconsistency becomes a pervasive problem 
when writing vectorized functions that are supposed to accept and return 
tensors of various ranks. To accomodate the inconsistency, one has to 
take what would have been a simple statement of the form:

<snip>
output.x[...] = blah
</snip>

and replace it with a bunch of boolean switches:

<snip>
if output.ndim == 0:
    output = output.reshape([1])
    outputWas0dim = True
else:
    outputWas0dim = False

output.x[...] = blah

if outputWas0dim:
    output =  output.reshape([])

return output
</snip>

-- Matt




More information about the NumPy-Discussion mailing list