ndarray icon indicating copy to clipboard operation
ndarray copied to clipboard

'into_shape()' incompatible memory layout error

Open HyeokSuLee opened this issue 3 years ago • 2 comments

       let d = aview1(&[1., 2., 3., 4.]);

        let d = d.into_shape((2, 2)).unwrap();
        println!("d shape {:?}", &d.shape());

        let a = array![
            [110., 120., 130., 140.,],
            [210., 220., 230., 240.,],
            [310., 320., 330., 340.,],
        ];

        let a = a.slice(s![.., 1]);

        let a = a.into_shape((3, 1)).unwrap();
        println!("a {:?}", &a);
        println!("a shape {:?}", &a.shape());

image

running 1 test
d shape [2, 2]
thread 'env::test::ndarray_into_shape_test' panicked at 'called `Result::unwrap()` on an `Err` value: ShapeError/IncompatibleLayout: incompatible memory layout', src\env.rs:483:38
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
test env::test::ndarray_into_shape_test ... FAILED

Both d and a is same type but panic occurs. Is there any reason why they behave differently? (slice_mut, slice_move, clone() also shows same error)

HyeokSuLee avatar Jun 24 '22 03:06 HyeokSuLee

The current implementation of .into_shape() requires that the input array be c- or f-contiguous. (See the docs.) In the example, d is contiguous, while a is not.

The current implementation of .into_shape() is unnecessarily restrictive, and its behavior can be confusing. There's some discussion about improving it in #390.

If all you want to do is add another axis, .insert_axis() is an alternative without any restrictions.

jturner314 avatar Jun 27 '22 23:06 jturner314

Thanks for explain. :)

HyeokSuLee avatar Jul 02 '22 06:07 HyeokSuLee