[Numpy-discussion] array, asarray as contiguous and friends

Tim Hochberg tim.hochberg at cox.net
Thu Mar 23 13:47:08 EST 2006


Travis Oliphant wrote:

> Tim Hochberg wrote:
>
>>
>>
>> I was just looking at the interface for array and asarray to see what 
>> other stuff should go in the interface of the hypothetical 
>> ascontiguous.  There's 'dtype', which I knew about, and 'fortran', 
>> which I didn't, but which makes sense. However, there's also 'ndmin'. 
>> First off, it's not described in docstring for asarray, but I was 
>> able to find it in the docstring for array without a problem. Second, 
>> is it really necessary? It seems to be useful in an awfully narrow 
>> set of circumstances, particularly since when you are padding axes 
>> not everyone wants to pad to the left.
>
>
>
> Padding to the left is "default" behavior for broadcasting and so it 
> seems appropriate.  This is how all lower-dimensional arrays are 
> interpreted as "higher" dimensional arrays throughout the code.

That makes some sense.

>
> The ndmin is very handy as atested to by the uses of atleast_1d or 
> atleast_2d in numpy library code.   It was added later as an 
> optimization step because of the number of library routines that were 
> using it.  I've since used it several times to simplify code. 

OK, I'll take your word for it.

> I think an ascontinguous on the Python level is appropriate since such 
> a beast exists on the C-level.   On the other hand, while Tim prefers 
> narrow interfaces, the array_from_object interface is traditionally 
> pretty broad. Thus, in my mind, the array call should get another flag 
> keyword that forces a contiguous result.  

This doesn't bother me since I long ago gave up any hope that the array 
constructor would have a narrow interface.

> This translates easily to the C-domain, in much the same way as the 
> fortran keyword does.


I'll buy that.

While I accept array() needs a wide interface, I still prefer to keep as 
many other interfaces as possible narrow. In particular, is ndmin widely 
used in asarray? Or do the library routines generally use array instead. 
Given the choice I'd sweep as much of the dust, AKA wideness, into 
array90 as possible since that's irredeemably wide anyway and keep the 
other interfaces as narrowly focused as possible.

Put another way, asarray and ascontiguous are about clarity of intent. 
With too much extra baggage, the intent becomes obscured. The coupling 
seems tight enough for dtype and fortran, but once you get to ndmin, it 
seems that you might as well go with the big guns and break out 
"array(x, copy=False, ndmin=n)". That's my $0.02 on this subject and 
I'll shut up about it now.

-tim





More information about the NumPy-Discussion mailing list