Re: [Numpy-discussion] Float view of complex array
On Mon, Jan 26, 2015 at 10:28 PM, Jens Jørgen Mortensen je...@fysik.dtu.dk wrote: On 01/26/2015 11:02 AM, Jaime Fernández del Río wrote: On Mon, Jan 26, 2015 at 1:41 AM, Sebastian Berg sebast...@sipsolutions.net mailto:sebast...@sipsolutions.net wrote: On Mo, 2015-01-26 at 09:24 +0100, Jens Jørgen Mortensen wrote: Hi! I have a view of a 2-d complex array that I would like to view as a 2-d float array. This works OK: np.ones((2, 4), complex).view(float) array([[ 1., 0., 1., 0., 1., 0.,1., 0.], [ 1., 0., 1., 0., 1., 0., 1., 0.]]) but this doesn't: np.ones((2, 4), complex)[:, :2].view(float) Traceback (most recent call last): File stdin, line 1, in module ValueError: new type not compatible with array. np.__version__ '1.9.0' and I don't understand why. When looking at the memory layout, I think it should be possible. Yes, it should be possible, but it is not :). You could hack it by using `np.ndarray` (or stride tricks). Or maybe you are interested making the checks whether it makes sense or not less strict. How would it be possible? He goes from an array with 16 byte strides along the last axis: r0i0, r1i1, r2i2, r3i3 to one with 32 byte strides, which is OK r0i0, , r2i2, but everything breaks down when he wants to have alternating strides of 8 and 24 bytes: r0, i0, , r2, i2, No, that is not what I want. I want this: r0, i0, r1, i1, , with stride 8 on the last axis - which should be fine. My current workaround is to do a copy() before view() - thanks Maniteja. My bad, you are absolutely right, Jens... I have put together a quick PR (https://github.com/numpy/numpy/pull/5508) that fixes your use case, by relaxing the requirements for views of different dtypes. I'd appreciate if you could take a look at the logic in the code (it is profusely commented), and see if you can think of other cases that can be viewed as another dtype that I may have overlooked. Thanks, Jaime Jens Jørgen which cannot be hacked in any sensible way. What I think could be made to work, but also fails, is this: np.ones((2, 4), complex).reshape(2, 4, 1)[:, :2, :].view(float) Here the original strides are (64, 16, xx) and the resulting view should have strides (64, 32, 8), not sure what trips this. Jaime - Sebastian Jens Jørgen ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org mailto:NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org mailto:NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion -- (\__/) ( O.o) ( ) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes de dominación mundial. ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion -- (\__/) ( O.o) ( ) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes de dominación mundial. ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Float view of complex array
On 01/26/2015 11:02 AM, Jaime Fernández del Río wrote: On Mon, Jan 26, 2015 at 1:41 AM, Sebastian Berg sebast...@sipsolutions.net mailto:sebast...@sipsolutions.net wrote: On Mo, 2015-01-26 at 09:24 +0100, Jens Jørgen Mortensen wrote: Hi! I have a view of a 2-d complex array that I would like to view as a 2-d float array. This works OK: np.ones((2, 4), complex).view(float) array([[ 1., 0., 1., 0., 1., 0.,1., 0.], [ 1., 0., 1., 0., 1., 0., 1., 0.]]) but this doesn't: np.ones((2, 4), complex)[:, :2].view(float) Traceback (most recent call last): File stdin, line 1, in module ValueError: new type not compatible with array. np.__version__ '1.9.0' and I don't understand why. When looking at the memory layout, I think it should be possible. Yes, it should be possible, but it is not :). You could hack it by using `np.ndarray` (or stride tricks). Or maybe you are interested making the checks whether it makes sense or not less strict. How would it be possible? He goes from an array with 16 byte strides along the last axis: r0i0, r1i1, r2i2, r3i3 to one with 32 byte strides, which is OK r0i0, , r2i2, but everything breaks down when he wants to have alternating strides of 8 and 24 bytes: r0, i0, , r2, i2, No, that is not what I want. I want this: r0, i0, r1, i1, , with stride 8 on the last axis - which should be fine. My current workaround is to do a copy() before view() - thanks Maniteja. Jens Jørgen which cannot be hacked in any sensible way. What I think could be made to work, but also fails, is this: np.ones((2, 4), complex).reshape(2, 4, 1)[:, :2, :].view(float) Here the original strides are (64, 16, xx) and the resulting view should have strides (64, 32, 8), not sure what trips this. Jaime - Sebastian Jens Jørgen ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org mailto:NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org mailto:NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion -- (\__/) ( O.o) ( ) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes de dominación mundial. ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Float view of complex array
On Mo, 2015-01-26 at 02:02 -0800, Jaime Fernández del Río wrote: On Mon, Jan 26, 2015 at 1:41 AM, Sebastian Berg sebast...@sipsolutions.net wrote: On Mo, 2015-01-26 at 09:24 +0100, Jens Jørgen Mortensen wrote: Hi! I have a view of a 2-d complex array that I would like to view as a 2-d float array. This works OK: np.ones((2, 4), complex).view(float) array([[ 1., 0., 1., 0., 1., 0., 1., 0.], [ 1., 0., 1., 0., 1., 0., 1., 0.]]) but this doesn't: np.ones((2, 4), complex)[:, :2].view(float) Traceback (most recent call last): File stdin, line 1, in module ValueError: new type not compatible with array. np.__version__ '1.9.0' and I don't understand why. When looking at the memory layout, I think it should be possible. Yes, it should be possible, but it is not :). You could hack it by using `np.ndarray` (or stride tricks). Or maybe you are interested making the checks whether it makes sense or not less strict. How would it be possible? He goes from an array with 16 byte strides along the last axis: Oh, sorry, you are right of course. I thought it was going the other way around, from double - complex. That way could work (in this case I think), but does not currently. r0i0, r1i1, r2i2, r3i3 to one with 32 byte strides, which is OK r0i0, , r2i2, but everything breaks down when he wants to have alternating strides of 8 and 24 bytes: r0, i0, , r2, i2, which cannot be hacked in any sensible way. What I think could be made to work, but also fails, is this: np.ones((2, 4), complex).reshape(2, 4, 1)[:, :2, :].view(float) Here the original strides are (64, 16, xx) and the resulting view should have strides (64, 32, 8), not sure what trips this. Jaime - Sebastian Jens Jørgen ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion -- (\__/) ( O.o) ( ) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes de dominación mundial. ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion signature.asc Description: This is a digitally signed message part ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Float view of complex array
On Mo, 2015-01-26 at 09:24 +0100, Jens Jørgen Mortensen wrote: Hi! I have a view of a 2-d complex array that I would like to view as a 2-d float array. This works OK: np.ones((2, 4), complex).view(float) array([[ 1., 0., 1., 0., 1., 0., 1., 0.], [ 1., 0., 1., 0., 1., 0., 1., 0.]]) but this doesn't: np.ones((2, 4), complex)[:, :2].view(float) Traceback (most recent call last): File stdin, line 1, in module ValueError: new type not compatible with array. np.__version__ '1.9.0' and I don't understand why. When looking at the memory layout, I think it should be possible. Yes, it should be possible, but it is not :). You could hack it by using `np.ndarray` (or stride tricks). Or maybe you are interested making the checks whether it makes sense or not less strict. - Sebastian Jens Jørgen ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion signature.asc Description: This is a digitally signed message part ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Float view of complex array
Hi Jens, I don't have enough knowledge about the internal memory layout, but the documentation ndarray.view http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.view.html says that: Views that change the dtype size (bytes per entry) should normally be avoided on arrays defined by slices, transposes, fortran-ordering, etc.: In your case, creating a *copy *of the slice and then calling *view *works. a array([[ 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j], [ 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j]]) a.view(float) array([[ 1., 0., 1., 0., 1., 0., 1., 0.], [ 1., 0., 1., 0., 1., 0., 1., 0.]]) b=a[:,:2].copy() b.view(float) array([[ 1., 0., 1., 0.], [ 1., 0., 1., 0.]]) c=a[:,:2] c.view(float) Traceback (most recent call last): File stdin, line 1, in module ValueError: new type not compatible with array Hope it helps :) Cheers, N.Maniteja. ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion