Skip to content

Commit 831ecbe

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 831ecbe

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

ndarray-linalg/src/convert.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,21 @@ 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-
};
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();
111114
assert_eq!(
112-
new.strides(),
113-
strides.as_slice(),
114-
"Custom stride is not supported"
115+
a_len,
116+
data.len(),
117+
"generalize: non-contig arrays are not supported"
115118
);
116-
new
119+
ArrayBase::from_shape_vec(a_dim.strides(strides), data).unwrap()
117120
}
118121

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

0 commit comments

Comments
 (0)