[Numpy-discussion] Will transpose ever need to copy data?

Sebastian Berg sebastian at sipsolutions.net
Wed Jun 12 08:24:53 EDT 2013


On Tue, 2013-06-11 at 09:24 -0700, Jaime Fernández del Río wrote:
> I noticed today that the documentation for np.transpose states, for
> the return value, that "A view is returned whenever possible."
> 
> 
I guess a subclass could cause a copy (the code looks like subclassing
doing something fancy is not be anticipated, there may be a bug here),
other then that, I don't see a reason for this comment. Transposing is
always possible for strided memory patterns and a copy will never occur.

- Sebastian


> Is there really any situation where swapping axes around could trigger
> the need to copy data, or will a view always be returned no matter
> what?
> 
> 
> I can't think of any such situation, and was planning on relying on
> that for some code: basically, I have an output array, which I would
> like to be contiguous. So I preallocate it with the right shape, then
> take a view of it moving a certain axis to the end to make
> computations easier, run all my computations on the modified view,
> then return the original array, not the view.
> 
> 
> If I started with an array with the axis at the end, and then
> transposed it, I would need to make a copy to make it contiguous,
> which is what I am trying to avoid.
> 
> 
> Is this a bad practice? Is that precaution in the documentation real?
> Should I check that my view's base is the original array and trigger a
> copy, or is it an unnecessary check?
> 
> 
> Thanks in advance,
> 
> 
> Jaime
> 
> 
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion





More information about the NumPy-Discussion mailing list