[Numpy-discussion] A case for rank-0 arrays

Sasha ndarray at mac.com
Fri Feb 24 06:25:03 CST 2006


On 2/24/06, Travis Oliphant <oliphant.travis at ieee.org> wrote:
> Sasha wrote:
> ...
> >>>>type(array(2)*2)
> >>>>
> >>>>
> ><type 'int32scalar'>
> >
> >I believe it should result in a rank-0 array instead.
> >
> >
> Can you be more precise about when rank-0 array should be returned and
> when scalars should be?

A simple rule could be that unary functions that don't change the rank
when operating on higer dimensional arrays should not change the type
(scalar vs. array) of the dimensionless objects. For binary operations
such as in the example above, the situation is less clear, but in this
case an analogy with functions helps. Multiplication between a
function f(...) and a scalar 2 is naturally defined as a function
(2*f)(...) = 2*f(...), where ... stands for any number of arguments
including zero.  The scalars should be returned when an operation
involves extracting an element, or evaluation of a function. This
includes indexing with a complete set of indices (and no ellipsis) and
reduce operations over all elements (more on that later.)




More information about the Numpy-discussion mailing list