Skip to content

Commit 34c7f5a

Browse files
committed
Drop unsafe of solveh and others in #216
1 parent 62c950a commit 34c7f5a

File tree

2 files changed

+47
-58
lines changed

2 files changed

+47
-58
lines changed

lax/src/solveh.rs

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,17 @@ use num_traits::{ToPrimitive, Zero};
99

1010
pub trait Solveh_: Sized {
1111
/// Bunch-Kaufman: wrapper of `*sytrf` and `*hetrf`
12-
unsafe fn bk(l: MatrixLayout, uplo: UPLO, a: &mut [Self]) -> Result<Pivot>;
12+
fn bk(l: MatrixLayout, uplo: UPLO, a: &mut [Self]) -> Result<Pivot>;
1313
/// Wrapper of `*sytri` and `*hetri`
14-
unsafe fn invh(l: MatrixLayout, uplo: UPLO, a: &mut [Self], ipiv: &Pivot) -> Result<()>;
14+
fn invh(l: MatrixLayout, uplo: UPLO, a: &mut [Self], ipiv: &Pivot) -> Result<()>;
1515
/// Wrapper of `*sytrs` and `*hetrs`
16-
unsafe fn solveh(
17-
l: MatrixLayout,
18-
uplo: UPLO,
19-
a: &[Self],
20-
ipiv: &Pivot,
21-
b: &mut [Self],
22-
) -> Result<()>;
16+
fn solveh(l: MatrixLayout, uplo: UPLO, a: &[Self], ipiv: &Pivot, b: &mut [Self]) -> Result<()>;
2317
}
2418

2519
macro_rules! impl_solveh {
2620
($scalar:ty, $trf:path, $tri:path, $trs:path) => {
2721
impl Solveh_ for $scalar {
28-
unsafe fn bk(l: MatrixLayout, uplo: UPLO, a: &mut [Self]) -> Result<Pivot> {
22+
fn bk(l: MatrixLayout, uplo: UPLO, a: &mut [Self]) -> Result<Pivot> {
2923
let (n, _) = l.size();
3024
let mut ipiv = vec![0; n as usize];
3125
if n == 0 {
@@ -35,50 +29,49 @@ macro_rules! impl_solveh {
3529
// calc work size
3630
let mut info = 0;
3731
let mut work_size = [Self::zero()];
38-
$trf(
39-
uplo as u8,
40-
n,
41-
a,
42-
l.lda(),
43-
&mut ipiv,
44-
&mut work_size,
45-
-1,
46-
&mut info,
47-
);
32+
unsafe {
33+
$trf(
34+
uplo as u8,
35+
n,
36+
a,
37+
l.lda(),
38+
&mut ipiv,
39+
&mut work_size,
40+
-1,
41+
&mut info,
42+
)
43+
};
4844
info.as_lapack_result()?;
4945

5046
// actual
5147
let lwork = work_size[0].to_usize().unwrap();
5248
let mut work = vec![Self::zero(); lwork];
53-
$trf(
54-
uplo as u8,
55-
n,
56-
a,
57-
l.lda(),
58-
&mut ipiv,
59-
&mut work,
60-
lwork as i32,
61-
&mut info,
62-
);
49+
unsafe {
50+
$trf(
51+
uplo as u8,
52+
n,
53+
a,
54+
l.lda(),
55+
&mut ipiv,
56+
&mut work,
57+
lwork as i32,
58+
&mut info,
59+
)
60+
};
6361
info.as_lapack_result()?;
6462
Ok(ipiv)
6563
}
6664

67-
unsafe fn invh(
68-
l: MatrixLayout,
69-
uplo: UPLO,
70-
a: &mut [Self],
71-
ipiv: &Pivot,
72-
) -> Result<()> {
65+
fn invh(l: MatrixLayout, uplo: UPLO, a: &mut [Self], ipiv: &Pivot) -> Result<()> {
7366
let (n, _) = l.size();
7467
let mut info = 0;
7568
let mut work = vec![Self::zero(); n as usize];
76-
$tri(uplo as u8, n, a, l.lda(), ipiv, &mut work, &mut info);
69+
unsafe { $tri(uplo as u8, n, a, l.lda(), ipiv, &mut work, &mut info) };
7770
info.as_lapack_result()?;
7871
Ok(())
7972
}
8073

81-
unsafe fn solveh(
74+
fn solveh(
8275
l: MatrixLayout,
8376
uplo: UPLO,
8477
a: &[Self],
@@ -87,7 +80,7 @@ macro_rules! impl_solveh {
8780
) -> Result<()> {
8881
let (n, _) = l.size();
8982
let mut info = 0;
90-
$trs(uplo as u8, n, 1, a, l.lda(), ipiv, b, n, &mut info);
83+
unsafe { $trs(uplo as u8, n, 1, a, l.lda(), ipiv, b, n, &mut info) };
9184
info.as_lapack_result()?;
9285
Ok(())
9386
}

ndarray-linalg/src/solveh.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,13 @@ where
113113
where
114114
Sb: DataMut<Elem = A>,
115115
{
116-
unsafe {
117-
A::solveh(
118-
self.a.square_layout()?,
119-
UPLO::Upper,
120-
self.a.as_allocated()?,
121-
&self.ipiv,
122-
rhs.as_slice_mut().unwrap(),
123-
)?
124-
};
116+
A::solveh(
117+
self.a.square_layout()?,
118+
UPLO::Upper,
119+
self.a.as_allocated()?,
120+
&self.ipiv,
121+
rhs.as_slice_mut().unwrap(),
122+
)?;
125123
Ok(rhs)
126124
}
127125
}
@@ -165,7 +163,7 @@ where
165163
S: DataMut<Elem = A>,
166164
{
167165
fn factorizeh_into(mut self) -> Result<BKFactorized<S>> {
168-
let ipiv = unsafe { A::bk(self.square_layout()?, UPLO::Upper, self.as_allocated_mut()?)? };
166+
let ipiv = A::bk(self.square_layout()?, UPLO::Upper, self.as_allocated_mut()?)?;
169167
Ok(BKFactorized { a: self, ipiv })
170168
}
171169
}
@@ -177,7 +175,7 @@ where
177175
{
178176
fn factorizeh(&self) -> Result<BKFactorized<OwnedRepr<A>>> {
179177
let mut a: Array2<A> = replicate(self);
180-
let ipiv = unsafe { A::bk(a.square_layout()?, UPLO::Upper, a.as_allocated_mut()?)? };
178+
let ipiv = A::bk(a.square_layout()?, UPLO::Upper, a.as_allocated_mut()?)?;
181179
Ok(BKFactorized { a, ipiv })
182180
}
183181
}
@@ -204,14 +202,12 @@ where
204202
type Output = ArrayBase<S, Ix2>;
205203

206204
fn invh_into(mut self) -> Result<ArrayBase<S, Ix2>> {
207-
unsafe {
208-
A::invh(
209-
self.a.square_layout()?,
210-
UPLO::Upper,
211-
self.a.as_allocated_mut()?,
212-
&self.ipiv,
213-
)?
214-
};
205+
A::invh(
206+
self.a.square_layout()?,
207+
UPLO::Upper,
208+
self.a.as_allocated_mut()?,
209+
&self.ipiv,
210+
)?;
215211
triangular_fill_hermitian(&mut self.a, UPLO::Upper);
216212
Ok(self.a)
217213
}

0 commit comments

Comments
 (0)