Skip to content

Commit ec6f98b

Browse files
committed
Replace LinalgError::Lapack by lax::error::Error
Split Lapack error into InvalidValue and ComputationalFailure
1 parent 4472a78 commit ec6f98b

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

lax/src/error.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,23 @@ pub type Result<T> = ::std::result::Result<T, Error>;
44

55
#[derive(Error, Debug)]
66
pub enum Error {
7-
#[error("LAPACK call failed with {return_code}")]
8-
Lapack { return_code: i32 },
7+
#[error("LAPACK call with invalid value {return_code}")]
8+
LapackInvalidValue { return_code: i32 },
9+
10+
#[error("Computational failure in LAPACK routine: {return_code}")]
11+
LapackComputationalFailure { return_code: i32 },
912

1013
/// Strides of the array is not supported
1114
#[error("Invalid shape")]
1215
InvalidShape,
1316
}
1417

1518
pub fn into_result<T>(return_code: i32, val: T) -> Result<T> {
16-
if return_code == 0 {
17-
Ok(val)
18-
} else {
19-
Err(Error::Lapack { return_code })
19+
if return_code > 0 {
20+
return Err(Error::LapackComputationalFailure { return_code });
21+
}
22+
if return_code < 0 {
23+
return Err(Error::LapackInvalidValue { return_code });
2024
}
25+
Ok(val)
2126
}

ndarray-linalg/src/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ pub enum LinalgError {
1313
NotSquare { rows: i32, cols: i32 },
1414

1515
/// LAPACK subroutine returns non-zero code
16-
#[error("LAPACK: return_code = {}", return_code)]
17-
Lapack { return_code: i32 },
16+
#[error(transparent)]
17+
Lapack(#[from] lapack::error::Error),
1818

1919
/// Strides of the array is not supported
2020
#[error("invalid stride: s0={}, s1={}", s0, s1)]

ndarray-linalg/src/solve.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,8 @@ where
476476
self.ensure_square()?;
477477
match self.factorize() {
478478
Ok(fac) => fac.sln_det(),
479-
Err(LinalgError::Lapack { return_code }) if return_code > 0 => {
479+
Err(LinalgError::Lapack(e)) if matches!(e, lapack::error::Error::LapackComputationalFailure {..}) =>
480+
{
480481
// The determinant is zero.
481482
Ok((A::zero(), A::Real::neg_infinity()))
482483
}
@@ -494,7 +495,8 @@ where
494495
self.ensure_square()?;
495496
match self.factorize_into() {
496497
Ok(fac) => fac.sln_det_into(),
497-
Err(LinalgError::Lapack { return_code }) if return_code > 0 => {
498+
Err(LinalgError::Lapack(e)) if matches!(e, lapack::error::Error::LapackComputationalFailure { .. }) =>
499+
{
498500
// The determinant is zero.
499501
Ok((A::zero(), A::Real::neg_infinity()))
500502
}
@@ -538,11 +540,11 @@ where
538540
{
539541
fn rcond(&self) -> Result<A::Real> {
540542
unsafe {
541-
A::rcond(
543+
Ok(A::rcond(
542544
self.a.layout()?,
543545
self.a.as_allocated()?,
544546
self.a.opnorm_one()?,
545-
)
547+
)?)
546548
}
547549
}
548550
}

0 commit comments

Comments
 (0)