[Scipy-tickets] [SciPy] #609: Transposed array cannot be reshaped without a copy
SciPy
scipy-tickets@scipy....
Thu Feb 21 20:08:22 CST 2008
#609: Transposed array cannot be reshaped without a copy
--------------------+-------------------------------------------------------
Reporter: sputta | Owner: somebody
Type: defect | Status: closed
Priority: normal | Milestone: 0.7
Component: Other | Version:
Severity: normal | Resolution: wontfix
Keywords: |
--------------------+-------------------------------------------------------
Changes (by rkern):
* status: new => closed
* resolution: => wontfix
Old description:
> Changing the shape of an array created from the transpose of another
> array is throwing an exception. I am guessing this touches upon function
> other than transpose.
>
> The following illustrates the problem.
>
> >>> print numpy.__version__
> 1.0.1
> >>> x = numpy.zeros((4,5), numpy.float)
> >>> y = numpy.transpose(x)
> >>> x.shape = 20
> >>> y.shape
> (5, 4)
> >>> y.shape = 20
> Traceback (most recent call last):
> File "<string>", line 1, in <string>
> AttributeError: incompatible shape for a non-contiguous array
> >>> y = y.copy()
> >>> y.shape = 20
> >>>
New description:
Changing the shape of an array created from the transpose of another array
is throwing an exception. I am guessing this touches upon function other
than transpose.
The following illustrates the problem.
{{{
>>> print numpy.__version__
1.0.1
>>> x = numpy.zeros((4,5), numpy.float)
>>> y = numpy.transpose(x)
>>> x.shape = 20
>>> y.shape
(5, 4)
>>> y.shape = 20
Traceback (most recent call last):
File "<string>", line 1, in <string>
AttributeError: incompatible shape for a non-contiguous array
>>> y = y.copy()
>>> y.shape = 20
>>>
}}}
Comment:
In the future, please post numpy issues to the numpy Trac:
http://scipy.org/scipy/numpy
However, the behavior you report is by design and won't change. Every
array keeps a set of "strides" to describe its structure. For each
dimension of the array, the stride is an integer that tells numpy how many
bytes to advance in order to go from {{{i}}} to {{{i+1}}} in that
dimension. There is only one stride for each dimensions. This limits the
kinds of memory layout that numpy can deal with, but that also means that
fast code can be written for the limited set of layouts. When you
transpose an array, you get a view of the original memory with a different
set of strides. When you assign to the attribute {{{.shape}}}, you are
also changing the strides.
Because {{{y=transpose(x)}}} is just a view on {{{x}}}, setting {{{y.shape
= 20}}} tries to change the strides to make a flat array of one dimension
to point to the appropriate memory locations in {{{x}}}. However, this
cannot be represented with a single stride for the single dimension.
Consequently, we raise the exception since the operation cannot be
performed.
--
Ticket URL: <http://scipy.org/scipy/scipy/ticket/609#comment:1>
SciPy <http://www.scipy.org/>
SciPy is open-source software for mathematics, science, and engineering.
More information about the Scipy-tickets
mailing list