[Numpy-discussion] scalar recordarrays
Francesc Altet
faltet at carabos.com
Mon Mar 19 16:08:51 EDT 2007
El dl 19 de 03 del 2007 a les 15:59 -0400, en/na Matthew Koichi Grimes
va escriure:
> 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>
Yeah, I agree. In fact, one of the main reasons to keep rank-0 arrays
around is to provide generality enough to address to these kind of
problems in an elegant way.
Cheers,
--
Francesc Altet | Be careful about using the following code --
Carabos Coop. V. | I've only proven that it works,
www.carabos.com | I haven't tested it. -- Donald Knuth
More information about the NumPy-Discussion
mailing list