From 831ecbe6a185fe8718c32dd5a009df21580024e6 Mon Sep 17 00:00:00 2001 From: bluss Date: Mon, 17 May 2021 21:20:25 +0200 Subject: [PATCH] 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). --- ndarray-linalg/src/convert.rs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/ndarray-linalg/src/convert.rs b/ndarray-linalg/src/convert.rs index e1446e96..075d3687 100644 --- a/ndarray-linalg/src/convert.rs +++ b/ndarray-linalg/src/convert.rs @@ -102,18 +102,21 @@ where { // FIXME // https://github.com/bluss/rust-ndarray/issues/325 - let strides: Vec = a.strides().to_vec(); - let new = if a.is_standard_layout() { - ArrayBase::from_shape_vec(a.dim(), a.into_raw_vec()).unwrap() - } else { - ArrayBase::from_shape_vec(a.dim().f(), a.into_raw_vec()).unwrap() - }; + // + // copy strides + let mut strides = D::zeros(a.ndim()); + for (index, &s) in a.strides().iter().enumerate() { + strides[index] = s as usize; + } + let a_dim = a.raw_dim(); + let a_len = a.len(); + let data = a.into_raw_vec(); assert_eq!( - new.strides(), - strides.as_slice(), - "Custom stride is not supported" + a_len, + data.len(), + "generalize: non-contig arrays are not supported" ); - new + ArrayBase::from_shape_vec(a_dim.strides(strides), data).unwrap() } /// Fills in the remainder of a Hermitian matrix that's represented by only one