Skip to content

Commit d470ad2

Browse files
committed
Remove unnecessary unsafe more
1 parent 6f870a5 commit d470ad2

File tree

6 files changed

+88
-93
lines changed

6 files changed

+88
-93
lines changed

lax/src/opnorm.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ use num_traits::Zero;
77
pub use super::NormType;
88

99
pub trait OperatorNorm_: Scalar {
10-
unsafe fn opnorm(t: NormType, l: MatrixLayout, a: &[Self]) -> Self::Real;
10+
fn opnorm(t: NormType, l: MatrixLayout, a: &[Self]) -> Self::Real;
1111
}
1212

1313
macro_rules! impl_opnorm {
1414
($scalar:ty, $lange:path) => {
1515
impl OperatorNorm_ for $scalar {
16-
unsafe fn opnorm(t: NormType, l: MatrixLayout, a: &[Self]) -> Self::Real {
16+
fn opnorm(t: NormType, l: MatrixLayout, a: &[Self]) -> Self::Real {
1717
let m = l.lda();
1818
let n = l.len();
1919
let t = match l {
@@ -25,7 +25,7 @@ macro_rules! impl_opnorm {
2525
} else {
2626
Vec::new()
2727
};
28-
$lange(t as u8, m, n, a, m, &mut work)
28+
unsafe { $lange(t as u8, m, n, a, m, &mut work) }
2929
}
3030
}
3131
};

lax/src/svd.rs

Lines changed: 41 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,8 @@ pub struct SVDOutput<A: Scalar> {
3636
/// Wraps `*gesvd`
3737
pub trait SVD_: Scalar {
3838
/// Calculate singular value decomposition $ A = U \Sigma V^T $
39-
unsafe fn svd(
40-
l: MatrixLayout,
41-
calc_u: bool,
42-
calc_vt: bool,
43-
a: &mut [Self],
44-
) -> Result<SVDOutput<Self>>;
39+
fn svd(l: MatrixLayout, calc_u: bool, calc_vt: bool, a: &mut [Self])
40+
-> Result<SVDOutput<Self>>;
4541
}
4642

4743
macro_rules! impl_svd {
@@ -53,12 +49,7 @@ macro_rules! impl_svd {
5349
};
5450
(@body, $scalar:ty, $gesvd:path, $($rwork_ident:ident),*) => {
5551
impl SVD_ for $scalar {
56-
unsafe fn svd(
57-
l: MatrixLayout,
58-
calc_u: bool,
59-
calc_vt: bool,
60-
mut a: &mut [Self],
61-
) -> Result<SVDOutput<Self>> {
52+
fn svd(l: MatrixLayout, calc_u: bool, calc_vt: bool, mut a: &mut [Self],) -> Result<SVDOutput<Self>> {
6253
let ju = match l {
6354
MatrixLayout::F { .. } => FlagSVD::from_bool(calc_u),
6455
MatrixLayout::C { .. } => FlagSVD::from_bool(calc_vt),
@@ -90,45 +81,49 @@ macro_rules! impl_svd {
9081
// eval work size
9182
let mut info = 0;
9283
let mut work_size = [Self::zero()];
93-
$gesvd(
94-
ju as u8,
95-
jvt as u8,
96-
m,
97-
n,
98-
&mut a,
99-
m,
100-
&mut s,
101-
u.as_mut().map(|x| x.as_mut_slice()).unwrap_or(&mut []),
102-
m,
103-
vt.as_mut().map(|x| x.as_mut_slice()).unwrap_or(&mut []),
104-
n,
105-
&mut work_size,
106-
-1,
107-
$(&mut $rwork_ident,)*
108-
&mut info,
109-
);
84+
unsafe {
85+
$gesvd(
86+
ju as u8,
87+
jvt as u8,
88+
m,
89+
n,
90+
&mut a,
91+
m,
92+
&mut s,
93+
u.as_mut().map(|x| x.as_mut_slice()).unwrap_or(&mut []),
94+
m,
95+
vt.as_mut().map(|x| x.as_mut_slice()).unwrap_or(&mut []),
96+
n,
97+
&mut work_size,
98+
-1,
99+
$(&mut $rwork_ident,)*
100+
&mut info,
101+
);
102+
}
110103
info.as_lapack_result()?;
111104

112105
// calc
113106
let lwork = work_size[0].to_usize().unwrap();
114107
let mut work = vec![Self::zero(); lwork];
115-
$gesvd(
116-
ju as u8,
117-
jvt as u8,
118-
m,
119-
n,
120-
&mut a,
121-
m,
122-
&mut s,
123-
u.as_mut().map(|x| x.as_mut_slice()).unwrap_or(&mut []),
124-
m,
125-
vt.as_mut().map(|x| x.as_mut_slice()).unwrap_or(&mut []),
126-
n,
127-
&mut work,
128-
lwork as i32,
129-
$(&mut $rwork_ident,)*
130-
&mut info,
131-
);
108+
unsafe {
109+
$gesvd(
110+
ju as u8,
111+
jvt as u8,
112+
m,
113+
n,
114+
&mut a,
115+
m,
116+
&mut s,
117+
u.as_mut().map(|x| x.as_mut_slice()).unwrap_or(&mut []),
118+
m,
119+
vt.as_mut().map(|x| x.as_mut_slice()).unwrap_or(&mut []),
120+
n,
121+
&mut work,
122+
lwork as i32,
123+
$(&mut $rwork_ident,)*
124+
&mut info,
125+
);
126+
}
132127
info.as_lapack_result()?;
133128
match l {
134129
MatrixLayout::F { .. } => Ok(SVDOutput { s, u, vt }),

lax/src/svddc.rs

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub enum UVTFlag {
1818
}
1919

2020
pub trait SVDDC_: Scalar {
21-
unsafe fn svddc(l: MatrixLayout, jobz: UVTFlag, a: &mut [Self]) -> Result<SVDOutput<Self>>;
21+
fn svddc(l: MatrixLayout, jobz: UVTFlag, a: &mut [Self]) -> Result<SVDOutput<Self>>;
2222
}
2323

2424
macro_rules! impl_svddc {
@@ -30,11 +30,7 @@ macro_rules! impl_svddc {
3030
};
3131
(@body, $scalar:ty, $gesdd:path, $($rwork_ident:ident),*) => {
3232
impl SVDDC_ for $scalar {
33-
unsafe fn svddc(
34-
l: MatrixLayout,
35-
jobz: UVTFlag,
36-
mut a: &mut [Self],
37-
) -> Result<SVDOutput<Self>> {
33+
fn svddc(l: MatrixLayout, jobz: UVTFlag, mut a: &mut [Self],) -> Result<SVDOutput<Self>> {
3834
let m = l.lda();
3935
let n = l.len();
4036
let k = m.min(n);
@@ -70,45 +66,49 @@ macro_rules! impl_svddc {
7066
let mut info = 0;
7167
let mut iwork = vec![0; 8 * k as usize];
7268
let mut work_size = [Self::zero()];
73-
$gesdd(
74-
jobz as u8,
75-
m,
76-
n,
77-
&mut a,
78-
m,
79-
&mut s,
80-
u.as_mut().map(|x| x.as_mut_slice()).unwrap_or(&mut []),
81-
m,
82-
vt.as_mut().map(|x| x.as_mut_slice()).unwrap_or(&mut []),
83-
vt_row,
84-
&mut work_size,
85-
-1,
86-
$(&mut $rwork_ident,)*
87-
&mut iwork,
88-
&mut info,
89-
);
69+
unsafe {
70+
$gesdd(
71+
jobz as u8,
72+
m,
73+
n,
74+
&mut a,
75+
m,
76+
&mut s,
77+
u.as_mut().map(|x| x.as_mut_slice()).unwrap_or(&mut []),
78+
m,
79+
vt.as_mut().map(|x| x.as_mut_slice()).unwrap_or(&mut []),
80+
vt_row,
81+
&mut work_size,
82+
-1,
83+
$(&mut $rwork_ident,)*
84+
&mut iwork,
85+
&mut info,
86+
);
87+
}
9088
info.as_lapack_result()?;
9189

9290
// do svd
9391
let lwork = work_size[0].to_usize().unwrap();
9492
let mut work = vec![Self::zero(); lwork];
95-
$gesdd(
96-
jobz as u8,
97-
m,
98-
n,
99-
&mut a,
100-
m,
101-
&mut s,
102-
u.as_mut().map(|x| x.as_mut_slice()).unwrap_or(&mut []),
103-
m,
104-
vt.as_mut().map(|x| x.as_mut_slice()).unwrap_or(&mut []),
105-
vt_row,
106-
&mut work,
107-
lwork as i32,
108-
$(&mut $rwork_ident,)*
109-
&mut iwork,
110-
&mut info,
111-
);
93+
unsafe {
94+
$gesdd(
95+
jobz as u8,
96+
m,
97+
n,
98+
&mut a,
99+
m,
100+
&mut s,
101+
u.as_mut().map(|x| x.as_mut_slice()).unwrap_or(&mut []),
102+
m,
103+
vt.as_mut().map(|x| x.as_mut_slice()).unwrap_or(&mut []),
104+
vt_row,
105+
&mut work,
106+
lwork as i32,
107+
$(&mut $rwork_ident,)*
108+
&mut iwork,
109+
&mut info,
110+
);
111+
}
112112
info.as_lapack_result()?;
113113

114114
match l {

ndarray-linalg/src/opnorm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ where
4545
fn opnorm(&self, t: NormType) -> Result<Self::Output> {
4646
let l = self.layout()?;
4747
let a = self.as_allocated()?;
48-
Ok(unsafe { A::opnorm(t, l, a) })
48+
Ok(A::opnorm(t, l, a))
4949
}
5050
}
5151

@@ -108,6 +108,6 @@ where
108108

109109
let l = arr.layout()?;
110110
let a = arr.as_allocated()?;
111-
Ok(unsafe { A::opnorm(t, l, a) })
111+
Ok(A::opnorm(t, l, a))
112112
}
113113
}

ndarray-linalg/src/svd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ where
9393
calc_vt: bool,
9494
) -> Result<(Option<Self::U>, Self::Sigma, Option<Self::VT>)> {
9595
let l = self.layout()?;
96-
let svd_res = unsafe { A::svd(l, calc_u, calc_vt, self.as_allocated_mut()?)? };
96+
let svd_res = A::svd(l, calc_u, calc_vt, self.as_allocated_mut()?)?;
9797
let (n, m) = l.size();
9898

9999
let u = svd_res.u.map(|u| into_matrix(l.resized(n, n), u).unwrap());

ndarray-linalg/src/svddc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ where
8080
uvt_flag: UVTFlag,
8181
) -> Result<(Option<Self::U>, Self::Sigma, Option<Self::VT>)> {
8282
let l = self.layout()?;
83-
let svd_res = unsafe { A::svddc(l, uvt_flag, self.as_allocated_mut()?)? };
83+
let svd_res = A::svddc(l, uvt_flag, self.as_allocated_mut()?)?;
8484
let (m, n) = l.size();
8585
let k = m.min(n);
8686

0 commit comments

Comments
 (0)