[Numpy-discussion] converting scalar to array with dimension 1

Bill Baxter wbaxter at gmail.com
Fri Mar 30 17:43:42 EDT 2007


On 3/31/07, Pierre GM <pgmdevlist at gmail.com> wrote:
>
> > I think you'll want to add the copy=False arg if you go that route, or
> > else you'll end up with something that's much slower than atleast_1d
> > for any array that gets passed in.  :-)
>
> Yep indeed. We can also add the subok=True flag.
>
> >   a = array(a, copy=0,ndmin=1)
> >
> > Anyway, sounds like premature optimization to me.
>
> Ah, prematurity depends on the context, doesn't it ? Isn't there some famous
> quote about two-liners ? Here, we have a function that does little more but
> calling array(x,subok=True, copy=False, ndmin=1) in a loop. Is skipping the
> loop for some very specific applications really premature ?

Perhaps not.  But the OP was definitely not talking about some
specific application.
I find asarray_1d et al to be clearer to read, and easier to type than
the array call with all the flags.  So barring some specific
performance critical situation, I go with asarray_1d.  Actually I
didn't realize that it had a loop in it, so thanks for pointing that
out.  I thought it was just and alias for array with some args.

> For example, I
> tend to use more and more the subok=True flag instead of 'asanyarray': is
> there any 'official' recommendation about this ?

Actually, I tend to use subok=False more and more.  Matrix, despite
being a subclass of ndarray, is too incompatible with ndarray to
really mix and match most of the time.  So it seems safest just to
force everything to be a bog-stock ndarray.  I made some convenience
functions that provide the right args to array for my own use.

--bb



More information about the NumPy-Discussion mailing list