Skip to content

Commit 831dda2

Browse files
committed
Improve stride copying in generalize a bit
While still not perfect - that should be solved in upstream ndarray, the new version is an improvement since it checks the compatibility before attempting to construct the array. `.into_raw_vec()` is very hard to use correctly (this is inherent, it's an access to the raw memory model), so the new version will actually fail to convert in some cases that were passing as silent errors before (related to internally sliced arrays, rather uncommon).
1 parent a561e5a commit 831dda2

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

ndarray-linalg/src/convert.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,17 @@ where
102102
{
103103
// FIXME
104104
// https://github.com/bluss/rust-ndarray/issues/325
105-
let strides: Vec<isize> = a.strides().to_vec();
106-
let new = if a.is_standard_layout() {
107-
ArrayBase::from_shape_vec(a.dim(), a.into_raw_vec()).unwrap()
108-
} else {
109-
ArrayBase::from_shape_vec(a.dim().f(), a.into_raw_vec()).unwrap()
110-
};
111-
assert_eq!(
112-
new.strides(),
113-
strides.as_slice(),
114-
"Custom stride is not supported"
115-
);
116-
new
105+
//
106+
// copy strides
107+
let mut strides = D::zeros(a.ndim());
108+
for (index, &s) in a.strides().iter().enumerate() {
109+
strides[index] = s as usize;
110+
}
111+
let a_dim = a.raw_dim();
112+
let a_len = a.len();
113+
let data = a.into_raw_vec();
114+
assert_eq!(a_len, data.len(), "generalize: non-contig arrays are not supported");
115+
ArrayBase::from_shape_vec(a_dim.strides(strides), data).unwrap()
117116
}
118117

119118
/// Fills in the remainder of a Hermitian matrix that's represented by only one

0 commit comments

Comments
 (0)