Checking equality on array with different layout #1202
-
Hi!
Looking at the difference, the elements and shape are both identical, the 2 arrays only differ on strides and layout. The expected array has a layout of Based on this issue, it seems like this is an implementation detail we shouldn't necessarily care about, but how would you recommend we compare for equality of elements here? Is the only way to do an element by element comparison? Apologies for not having a reproduction of the issue, this is code that's nested pretty deeply in our codebase. Thank you in advance for the help! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
To clarify, my comment on #1198 was specifically about the text "CFcf" in the debug-formatted string being an implementation detail, not the layout itself. The layout itself can be important and is not just an implementation detail. While the memory layout of an array doesn't affect the results of most methods, it does affect performance (iterating over an array is fastest in memory order) and it does affect the results of a few methods, such as To answer your question -- similar to how arithmetic operations work elementwise, comparisons also operate on the corresponding elements of the arrays (i.e. those at the same n-D index), regardless of layout. ( use ndarray::prelude::*;
fn main() {
let shape = [2, 3];
let a = Array2::from_shape_fn(shape, |(i, j)| i * 10 + j);
let b = Array2::from_shape_fn(shape.f(), |(i, j)| i * 10 + j);
assert!(a.is_standard_layout());
assert!(b.t().is_standard_layout());
assert_eq!(a.shape(), b.shape());
assert_eq!(a, b);
assert_eq!(
a,
array![
[00, 01, 02,],
[10, 11, 12,],
]
);
assert_eq!(
b,
array![
[00, 01, 02,],
[10, 11, 12,],
]
);
assert_ne!(
a,
array![
[00, 01, 999999,],
[10, 11, 12,],
]
);
assert_ne!(
a,
array![
[10, 11, 12,],
[00, 01, 02,],
]
);
println!("done");
} Array Do you want to require that the arrays have the same layout, in addition to the same shape and elements? If so, you could do the following: // Check the strides.
assert_eq!(expected_array.strides(), result.some_array.strides());
// Check the shape and elements.
assert_eq!(expected_array, result.some_array); |
Beta Was this translation helpful? Give feedback.
To clarify, my comment on #1198 was specifically about the text "CFcf" in the debug-formatted string being an implementation detail, not the layout itself. The layout itself can be important and is not just an implementation detail. While the memory layout of an array doesn't affect the results of most methods, it does affect performance (iterating over an array is fastest in memory order) and it does affect the results of a few methods, such as
as_slice_memory_order
.To answer your question -- similar to how arithmetic operations work elementwise, comparisons also operate on the corresponding elements of the arrays (i.e. those at the same n-D index), regardless of layout. (
PartialEq
is i…