ndarray
ndarray copied to clipboard
'into_shape()' incompatible memory layout error
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());

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)
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.
Thanks for explain. :)