[Numpy-discussion] Behavior of array scalars
ndarray at mac.com
Fri Feb 17 16:31:03 CST 2006
Sorry for a truncated post. Here is what I intended.
On 2/17/06, Travis Oliphant <oliphant at ee.byu.edu> wrote:
> NumPy (starting with Numeric) has always had this love-hate relationship
> with zero-dimensional arrays. We use them internally to simplify the
> code, but try not to expose them to the user. Ultimately, we couldn't
> figure out how to do that cleanly and so we have the current compromise
> situation where 0-d arrays are available but treated as second-class
> citizens. Thus, we still get funny behavior in certain circumstances.
It would be nice to collect the motivations behind the current state
of affairs with rank-0 arrays in one place. Due to the "hard-hat"
nature of the issue, I would suggest to do it at
Travis' Numeric3 design document actually leaves the issue open
What does single element indexing return? Scalars or rank-0 arrays?
Right now, a scalar is returned if there is a direct map to a Python
type, otherwise a rank-0 array (Numeric scalar) is returned. But, in
problems which reduce to an array of arbitrary size, this can lead to
a lot of code that basically just checks to see if the object is a
scalar. There are two ways I can see to solve this: 1) always return
rank-0 arrays (never convert to Python scalars) and 2) always use
special functions (like alen) that handle Python scalars correctly.
I'm open to both ideas, but probably prefer #1 (never convert to
Python scalars) unless requested.
I can think of two compelling reasons in favor of scalar array types:
1. Rank-0 arrays cannot be used as indices to tuples.
2. Rank-0 arrays cannot be used as keys in dicts.
Neither of these resons is future proof. It looks like python 2.5
will introduce __index__ slot that will fix #1 and #2 is probably
better solved by introduction of "frozen" ndarray.
In any case I will collect all these thoughts on the ZeroRankArray
page unless I hear that this belongs to the main wiki.
More information about the Numpy-discussion