On Do, 2016-01-07 at 22:48 -0500, John Kirkham wrote:
> First, off sorry for the long turnaround on responding to these
> questions. Below I have tried to respond to everyone's questions and
> comments. I have restructured the order of the messages so that my
> responses are a little more structured. If anybody has more thoughts
> or questions, please let me know.
> 

<snip>

> From a performance standpoint, `expand_dims` using `reshape` to add
> these extra dimensions. So, it has the possibility of not returning a
> view, but a copy, which could take some time to build if the array is
> large. By using our method, we guarantee a view will always be
> returned; so, this allocation will never be encountered.

Actually reshape can be used, if stride tricks can do the trick, then
reshape is guaranteed to not do a copy.

Still can't say I feel it is a worthy addition (but others may
disagree), especially since I realized we have `expand_dims` already. I
just am not sure it would actually be used reasonably often.

But how about adding a `dims=1` argument to `expand_dims`, so that your
function becomes at least a bit easier:

newarr = np.expand_dims(arr, -1, after.ndim)
# If manual broadcast is necessary:
newarr = np.broadcast_to(arr, before.shape + arr.shape + after.shape)

Otherwise I guess we could morph `expand_dims` partially to your
function, though it would return a read-only view if broadcasting is
active, so I am a bit unsure I like it.


In other words, my personal currently preferred solution to get some of
this would be to add a simple `dims` argument to `expand_dims` and add
`broadcast_to` to its "See Also" section (also it could be in the
`squeeze` see also section I think).
To further help out other people, we could maybe even mention the
combination in the examples (i.e. broadcast_to example?).

- Sebastian 


> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> https://mail.scipy.org/mailman/listinfo/numpy-discussion
> 

Attachment: signature.asc
Description: This is a digitally signed message part

_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
https://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to