Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'into_shape()' incompatible memory layout error #1172

Open
HyeokSuLee opened this issue Jun 24, 2022 · 2 comments
Open

'into_shape()' incompatible memory layout error #1172

HyeokSuLee opened this issue Jun 24, 2022 · 2 comments

Comments

@HyeokSuLee
Copy link

       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)

@jturner314
Copy link
Member

jturner314 commented Jun 27, 2022

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.

@HyeokSuLee
Copy link
Author

Thanks for explain. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants