diff --git a/nalgebra-lapack/src/lib.rs b/nalgebra-lapack/src/lib.rs index 9a027772..fccf2717 100644 --- a/nalgebra-lapack/src/lib.rs +++ b/nalgebra-lapack/src/lib.rs @@ -140,6 +140,7 @@ impl ComplexHelper for Complex { } } +// This is UB. unsafe fn uninitialized_vec(n: usize) -> Vec { let mut res = Vec::new(); res.reserve_exact(n); diff --git a/src/base/construction.rs b/src/base/construction.rs index e99b9e02..c45798c2 100644 --- a/src/base/construction.rs +++ b/src/base/construction.rs @@ -25,14 +25,6 @@ use crate::base::{ ArrayStorage, Const, DefaultAllocator, Matrix, OMatrix, OVector, Scalar, Unit, Vector, }; -/// OBJECTIVE: GET RID OF THIS! -#[macro_export] -macro_rules! unimplemented_or_uninitialized_generic { - ($nrows:expr, $ncols:expr) => {{ - crate::base::Matrix::new_uninitialized_generic($nrows, $ncols) - }}; -} - /// # Generic constructors /// This set of matrix and vector construction functions are all generic /// with-regard to the matrix dimensions. They all expect to be given diff --git a/src/sparse/cs_matrix.rs b/src/sparse/cs_matrix.rs index 4bb15759..d59b2438 100644 --- a/src/sparse/cs_matrix.rs +++ b/src/sparse/cs_matrix.rs @@ -279,7 +279,7 @@ where CsMatrix { data: CsVecStorage { shape: (nrows, ncols), - p: OVector::zeros_generic(ncols, U1), + p: OVector::zeros_generic(ncols, Const::<1>), i, vals, }, @@ -429,7 +429,7 @@ impl> CsMatrix { let nvals = self.len(); let mut res = CsMatrix::new_uninitialized_generic(ncols, nrows, nvals); - let mut workspace = Vector::zeros_generic(nrows, U1); + let mut workspace = Vector::zeros_generic(nrows, Const::<1>); // Compute p. for i in 0..nvals { @@ -472,8 +472,7 @@ where { // Size = R let nrows = self.data.shape().0; - let mut workspace = - unsafe { crate::unimplemented_or_uninitialized_generic!(nrows, U1) }; + let mut workspace = Matrix::new_uninitialized_generic(nrows, Const::<1>); self.sort_with_workspace(workspace.as_mut_slice()); } diff --git a/src/sparse/cs_matrix_cholesky.rs b/src/sparse/cs_matrix_cholesky.rs index 3ce66c92..cd8bf975 100644 --- a/src/sparse/cs_matrix_cholesky.rs +++ b/src/sparse/cs_matrix_cholesky.rs @@ -48,10 +48,8 @@ where let (l, u) = Self::nonzero_pattern(m); // Workspaces. - let work_x = - unsafe { crate::unimplemented_or_uninitialized_generic!(m.data.shape().0, U1) }; - let work_c = - unsafe { crate::unimplemented_or_uninitialized_generic!(m.data.shape().1, U1) }; + let work_x = Matrix::new_uninitialized_generic(m.data.shape().0, Const::<1>); + let work_c = Matrix::new_uninitialized_generic(m.data.shape().1, Const::<1>); let mut original_p = m.data.p.as_slice().to_vec(); original_p.push(m.data.i.len()); @@ -294,8 +292,7 @@ where let etree = Self::elimination_tree(m); let (nrows, ncols) = m.data.shape(); let mut rows = Vec::with_capacity(m.len()); - let mut cols = - unsafe { crate::unimplemented_or_uninitialized_generic!(m.data.shape().0, U1) }; + let mut cols = Matrix::new_uninitialized_generic(m.data.shape().0, Const::<1>); let mut marks = Vec::new(); // NOTE: the following will actually compute the non-zero pattern of diff --git a/src/sparse/cs_matrix_ops.rs b/src/sparse/cs_matrix_ops.rs index a9f22fcd..84c63077 100644 --- a/src/sparse/cs_matrix_ops.rs +++ b/src/sparse/cs_matrix_ops.rs @@ -148,7 +148,7 @@ where ); let mut res = CsMatrix::new_uninitialized_generic(nrows1, ncols2, self.len() + rhs.len()); - let mut workspace = OVector::::zeros_generic(nrows1, U1); + let mut workspace = OVector::::zeros_generic(nrows1, Const::<1>); let mut nz = 0; for j in 0..ncols2.value() { @@ -241,9 +241,9 @@ where ); let mut res = CsMatrix::new_uninitialized_generic(nrows1, ncols2, self.len() + rhs.len()); - let mut timestamps = OVector::zeros_generic(nrows1, U1); + let mut timestamps = OVector::zeros_generic(nrows1, Const::<1>); let mut workspace = - unsafe { crate::unimplemented_or_uninitialized_generic!(nrows1, U1) }; + Matrix::new_uninitialized_generic(nrows1, Const::<1>) ; let mut nz = 0; for j in 0..ncols2.value() { diff --git a/src/sparse/cs_matrix_solve.rs b/src/sparse/cs_matrix_solve.rs index ad38fe56..092ad15b 100644 --- a/src/sparse/cs_matrix_solve.rs +++ b/src/sparse/cs_matrix_solve.rs @@ -152,8 +152,7 @@ impl> CsMatrix { self.lower_triangular_reach(b, &mut reach); // We sort the reach so the result matrix has sorted indices. reach.sort_unstable(); - let mut workspace = - unsafe { crate::unimplemented_or_uninitialized_generic!(b.data.shape().0, U1) }; + let mut workspace = Matrix::new_uninitialized_generic(b.data.shape().0, Const::<1>); for i in reach.iter().cloned() { workspace[i] = T::zero(); @@ -191,7 +190,7 @@ impl> CsMatrix { // Copy the result into a sparse vector. let mut result = - CsVector::new_uninitialized_generic(b.data.shape().0, U1, reach.len()); + CsVector::new_uninitialized_generic(b.data.shape().0, Const::<1>, reach.len()); for (i, val) in reach.iter().zip(result.data.vals.iter_mut()) { *val = workspace[*i]; @@ -255,7 +254,7 @@ impl> CsMatrix { S2: CsStorage, DefaultAllocator: Allocator, { - let mut visited = OVector::repeat_generic(self.data.shape().1, U1, false); + let mut visited = OVector::repeat_generic(self.data.shape().1, Const::<1>, false); let mut stack = Vec::new(); for irow in b.data.column_row_indices(0) { diff --git a/src/third_party/alga/alga_matrix.rs b/src/third_party/alga/alga_matrix.rs index e55ba49e..f80b021a 100644 --- a/src/third_party/alga/alga_matrix.rs +++ b/src/third_party/alga/alga_matrix.rs @@ -433,8 +433,8 @@ where "Matrix meet/join error: mismatched dimensions." ); - let mut mres = unsafe { crate::unimplemented_or_uninitialized_generic!(shape.0, shape.1) }; - let mut jres = unsafe { crate::unimplemented_or_uninitialized_generic!(shape.0, shape.1) }; + let mut mres = Matrix::new_uninitialized_generic(shape.0, shape.1); + let mut jres = Matrix::new_uninitialized_generic(shape.0, shape.1); for i in 0..shape.0.value() * shape.1.value() { unsafe {