Skip to content

'into_shape()' incompatible memory layout error #1172

Open
@HyeokSuLee

Description

@HyeokSuLee
       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)

Activity

jturner314

jturner314 commented on Jun 27, 2022

@jturner314
Member

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

HyeokSuLee commented on Jul 2, 2022

@HyeokSuLee
Author

Thanks for explain. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @jturner314@HyeokSuLee

        Issue actions

          'into_shape()' incompatible memory layout error · Issue #1172 · rust-ndarray/ndarray