[Numpy-discussion] Re: asarray behaviour

Chris Barker Chris.Barker at noaa.gov
Wed Oct 12 21:47:11 EDT 2005

Travis Oliphant wrote:
> Chris Barker wrote: 
>> Travis Oliphant wrote:
>>> 2) new behavior
>>> array (and asarray)  always return an ndarray base-class object (or a 
>>> big-nd array if the object is already one).
>> +1 ...I think. What is a big-nd array?
> A big-nd arrary is the parent-class of the ndarray object.  It doesn't 
> have the sequence protocols or the buffer protocols and should not 
> suffer from the 32-bit limitations of those protocols.   An ndarray 
> inherits from the bigarray. Eventually, when Python cleans up its 
> 32-bit limitations, the bigarray will disappear.
>>> asanyarray ---  returns an nd-array or a sub-class if the object is 
>>> already a sub-class.
>> What about having each subclass define it's own function-- asmatrix, 
>> etc? Maybe that's not general enough, but I know I use asarray because 
>> I know I want a NumPy Array, and nothing else.
> I guess the problem is that we are not used to coding to interfaces.   

I'm not sure I get this. If we were coding to an interface, then I'd 
write my functions to jsut expect the type I want, and be done iowth it, 
I wouldn't need asarray().

> I'm going to make the change suggested by the second point, just because 
> I think it's more explicit and will make porting scipy a lot easier.  

um, I'm not sure I follow...what change are you making?

> The fact that multiplication could be redefined by the matrix which 
> still passes as an array, means that lots of code can choke on matrices.

Exactly. What I like about asarray is that I can write a function that 
really does require a NumPy array, and let the user code pass in 
anything that can be turned into one, without any performance penalty if 
a NumPy array is passed in. However, it's very important that I know 
EXACTLY what type asarray will return. The same thing applies to 
matrixes, etc. If I write a function that manipulates a matrix, Iwant to 
be darn sure that's what I've got...I'd need an asmatrix() function to 
give it to me.

> Of course, this will have negative consequences.  It will make matrices 
> much less pervasive through function calls "automatically", but it will 
> be safer.  People who believe their code is safe for matrices, can use 
> asanyarray.

I can see that there may well be sone code that could take any subclass 
of ndarray..perhaps if it's only accessing the data, for instance, so 
it's nice to have that option.


Christopher Barker, Ph.D.

NOAA/OR&R/HAZMAT         (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

More information about the NumPy-Discussion mailing list