Skip to content

Commit 962497e

Browse files
committed
Drop unsafe
1 parent 00b044b commit 962497e

File tree

2 files changed

+54
-54
lines changed

2 files changed

+54
-54
lines changed

lax/src/least_squares.rs

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ pub struct LeastSquaresOutput<A: Scalar> {
1414

1515
/// Wraps `*gelsd`
1616
pub trait LeastSquaresSvdDivideConquer_: Scalar {
17-
unsafe fn least_squares(
17+
fn least_squares(
1818
a_layout: MatrixLayout,
1919
a: &mut [Self],
2020
b: &mut [Self],
2121
) -> Result<LeastSquaresOutput<Self>>;
2222

23-
unsafe fn least_squares_nrhs(
23+
fn least_squares_nrhs(
2424
a_layout: MatrixLayout,
2525
a: &mut [Self],
2626
b_layout: MatrixLayout,
@@ -38,7 +38,7 @@ macro_rules! impl_least_squares {
3838

3939
(@body, $scalar:ty, $gelsd:path, $($rwork:ident),*) => {
4040
impl LeastSquaresSvdDivideConquer_ for $scalar {
41-
unsafe fn least_squares(
41+
fn least_squares(
4242
l: MatrixLayout,
4343
a: &mut [Self],
4444
b: &mut [Self],
@@ -47,7 +47,7 @@ macro_rules! impl_least_squares {
4747
Self::least_squares_nrhs(l, a, b_layout, b)
4848
}
4949

50-
unsafe fn least_squares_nrhs(
50+
fn least_squares_nrhs(
5151
a_layout: MatrixLayout,
5252
a: &mut [Self],
5353
b_layout: MatrixLayout,
@@ -95,23 +95,25 @@ macro_rules! impl_least_squares {
9595
$(
9696
let mut $rwork = [Self::Real::zero()];
9797
)*
98-
$gelsd(
99-
m,
100-
n,
101-
nrhs,
102-
a_t.as_mut().map(|v| v.as_mut_slice()).unwrap_or(a),
103-
a_layout.lda(),
104-
b_t.as_mut().map(|v| v.as_mut_slice()).unwrap_or(b),
105-
b_layout.lda(),
106-
&mut singular_values,
107-
rcond,
108-
&mut rank,
109-
&mut work_size,
110-
-1,
111-
$(&mut $rwork,)*
112-
&mut iwork_size,
113-
&mut info,
114-
);
98+
unsafe {
99+
$gelsd(
100+
m,
101+
n,
102+
nrhs,
103+
a_t.as_mut().map(|v| v.as_mut_slice()).unwrap_or(a),
104+
a_layout.lda(),
105+
b_t.as_mut().map(|v| v.as_mut_slice()).unwrap_or(b),
106+
b_layout.lda(),
107+
&mut singular_values,
108+
rcond,
109+
&mut rank,
110+
&mut work_size,
111+
-1,
112+
$(&mut $rwork,)*
113+
&mut iwork_size,
114+
&mut info,
115+
)
116+
};
115117
info.as_lapack_result()?;
116118

117119
// calc
@@ -123,23 +125,25 @@ macro_rules! impl_least_squares {
123125
let lrwork = $rwork[0].to_usize().unwrap();
124126
let mut $rwork = vec![Self::Real::zero(); lrwork];
125127
)*
126-
$gelsd(
127-
m,
128-
n,
129-
nrhs,
130-
a_t.as_mut().map(|v| v.as_mut_slice()).unwrap_or(a),
131-
a_layout.lda(),
132-
b_t.as_mut().map(|v| v.as_mut_slice()).unwrap_or(b),
133-
b_layout.lda(),
134-
&mut singular_values,
135-
rcond,
136-
&mut rank,
137-
&mut work,
138-
lwork as i32,
139-
$(&mut $rwork,)*
140-
&mut iwork,
141-
&mut info,
142-
);
128+
unsafe {
129+
$gelsd(
130+
m,
131+
n,
132+
nrhs,
133+
a_t.as_mut().map(|v| v.as_mut_slice()).unwrap_or(a),
134+
a_layout.lda(),
135+
b_t.as_mut().map(|v| v.as_mut_slice()).unwrap_or(b),
136+
b_layout.lda(),
137+
&mut singular_values,
138+
rcond,
139+
&mut rank,
140+
&mut work,
141+
lwork as i32,
142+
$(&mut $rwork,)*
143+
&mut iwork,
144+
&mut info,
145+
);
146+
}
143147
info.as_lapack_result()?;
144148

145149
// Skip a_t -> a transpose because A has been destroyed

ndarray-linalg/src/least_squares.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -295,14 +295,12 @@ where
295295
let LeastSquaresOutput::<E> {
296296
singular_values,
297297
rank,
298-
} = unsafe {
299-
E::least_squares(
300-
a.layout()?,
301-
a.as_allocated_mut()?,
302-
rhs.as_slice_memory_order_mut()
303-
.ok_or_else(|| LinalgError::MemoryNotCont)?,
304-
)?
305-
};
298+
} = E::least_squares(
299+
a.layout()?,
300+
a.as_allocated_mut()?,
301+
rhs.as_slice_memory_order_mut()
302+
.ok_or_else(|| LinalgError::MemoryNotCont)?,
303+
)?;
306304

307305
let (m, n) = (a.shape()[0], a.shape()[1]);
308306
let solution = rhs.slice(s![0..n]).to_owned();
@@ -385,14 +383,12 @@ where
385383
let LeastSquaresOutput::<E> {
386384
singular_values,
387385
rank,
388-
} = unsafe {
389-
E::least_squares_nrhs(
390-
a_layout,
391-
a.as_allocated_mut()?,
392-
rhs_layout,
393-
rhs.as_allocated_mut()?,
394-
)?
395-
};
386+
} = E::least_squares_nrhs(
387+
a_layout,
388+
a.as_allocated_mut()?,
389+
rhs_layout,
390+
rhs.as_allocated_mut()?,
391+
)?;
396392

397393
let solution: Array2<E> = rhs.slice(s![..a.shape()[1], ..]).to_owned();
398394
let singular_values = Array::from_shape_vec((singular_values.len(),), singular_values)?;

0 commit comments

Comments
 (0)