Skip to content

Commit e352347

Browse files
committed
Drop unsafe of cholesky
1 parent 4a96e4d commit e352347

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

lax/src/cholesky.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,37 @@ pub trait Cholesky_: Sized {
88
/// Cholesky: wrapper of `*potrf`
99
///
1010
/// **Warning: Only the portion of `a` corresponding to `UPLO` is written.**
11-
unsafe fn cholesky(l: MatrixLayout, uplo: UPLO, a: &mut [Self]) -> Result<()>;
11+
fn cholesky(l: MatrixLayout, uplo: UPLO, a: &mut [Self]) -> Result<()>;
12+
1213
/// Wrapper of `*potri`
1314
///
1415
/// **Warning: Only the portion of `a` corresponding to `UPLO` is written.**
15-
unsafe fn inv_cholesky(l: MatrixLayout, uplo: UPLO, a: &mut [Self]) -> Result<()>;
16+
fn inv_cholesky(l: MatrixLayout, uplo: UPLO, a: &mut [Self]) -> Result<()>;
17+
1618
/// Wrapper of `*potrs`
17-
unsafe fn solve_cholesky(l: MatrixLayout, uplo: UPLO, a: &[Self], b: &mut [Self])
18-
-> Result<()>;
19+
fn solve_cholesky(l: MatrixLayout, uplo: UPLO, a: &[Self], b: &mut [Self]) -> Result<()>;
1920
}
2021

2122
macro_rules! impl_cholesky {
2223
($scalar:ty, $trf:path, $tri:path, $trs:path) => {
2324
impl Cholesky_ for $scalar {
24-
unsafe fn cholesky(l: MatrixLayout, uplo: UPLO, a: &mut [Self]) -> Result<()> {
25+
fn cholesky(l: MatrixLayout, uplo: UPLO, a: &mut [Self]) -> Result<()> {
2526
let (n, _) = l.size();
26-
$trf(l.lapacke_layout(), uplo as u8, n, a, n).as_lapack_result()?;
27+
unsafe {
28+
$trf(l.lapacke_layout(), uplo as u8, n, a, n).as_lapack_result()?;
29+
}
2730
Ok(())
2831
}
2932

30-
unsafe fn inv_cholesky(l: MatrixLayout, uplo: UPLO, a: &mut [Self]) -> Result<()> {
33+
fn inv_cholesky(l: MatrixLayout, uplo: UPLO, a: &mut [Self]) -> Result<()> {
3134
let (n, _) = l.size();
32-
$tri(l.lapacke_layout(), uplo as u8, n, a, l.lda()).as_lapack_result()?;
35+
unsafe {
36+
$tri(l.lapacke_layout(), uplo as u8, n, a, l.lda()).as_lapack_result()?;
37+
}
3338
Ok(())
3439
}
3540

36-
unsafe fn solve_cholesky(
41+
fn solve_cholesky(
3742
l: MatrixLayout,
3843
uplo: UPLO,
3944
a: &[Self],
@@ -42,8 +47,10 @@ macro_rules! impl_cholesky {
4247
let (n, _) = l.size();
4348
let nrhs = 1;
4449
let ldb = 1;
45-
$trs(l.lapacke_layout(), uplo as u8, n, nrhs, a, l.lda(), b, ldb)
46-
.as_lapack_result()?;
50+
unsafe {
51+
$trs(l.lapacke_layout(), uplo as u8, n, nrhs, a, l.lda(), b, ldb)
52+
.as_lapack_result()?;
53+
}
4754
Ok(())
4855
}
4956
}

ndarray-linalg/src/cholesky.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ where
155155

156156
fn invc_into(self) -> Result<Self::Output> {
157157
let mut a = self.factor;
158-
unsafe { A::inv_cholesky(a.square_layout()?, self.uplo, a.as_allocated_mut()?)? };
158+
A::inv_cholesky(a.square_layout()?, self.uplo, a.as_allocated_mut()?)?;
159159
triangular_fill_hermitian(&mut a, self.uplo);
160160
Ok(a)
161161
}
@@ -173,14 +173,12 @@ where
173173
where
174174
Sb: DataMut<Elem = A>,
175175
{
176-
unsafe {
177-
A::solve_cholesky(
178-
self.factor.square_layout()?,
179-
self.uplo,
180-
self.factor.as_allocated()?,
181-
b.as_slice_mut().unwrap(),
182-
)?
183-
};
176+
A::solve_cholesky(
177+
self.factor.square_layout()?,
178+
self.uplo,
179+
self.factor.as_allocated()?,
180+
b.as_slice_mut().unwrap(),
181+
)?;
184182
Ok(b)
185183
}
186184
}
@@ -259,7 +257,7 @@ where
259257
S: DataMut<Elem = A>,
260258
{
261259
fn cholesky_inplace(&mut self, uplo: UPLO) -> Result<&mut Self> {
262-
unsafe { A::cholesky(self.square_layout()?, uplo, self.as_allocated_mut()?)? };
260+
A::cholesky(self.square_layout()?, uplo, self.as_allocated_mut()?)?;
263261
Ok(self.into_triangular(uplo))
264262
}
265263
}

0 commit comments

Comments
 (0)