Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace to failure #118

Merged
merged 1 commit into from
Sep 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ openblas = ["lapack-src/openblas", "blas-src/openblas", "openblas-src"]
serde-1 = ["ndarray/serde-1", "num-complex/serde"]

[dependencies]
derive-new = "0.5"
lapacke = "0.2"
num-traits = "0.2"
procedurals = "0.3"
rand = "0.5"
failure = "0.1"

[dependencies.num-complex]
version = "0.2"
Expand Down
98 changes: 19 additions & 79 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,95 +1,35 @@
//! Define Errors

use ndarray::{Ixs, ShapeError};
use std::error;
use std::fmt;

pub type Result<T> = ::std::result::Result<T, LinalgError>;

/// Master Error type of this crate
#[derive(Debug, EnumError)]
#[derive(Fail, Debug)]
pub enum LinalgError {
NotSquare(NotSquareError),
Lapack(LapackError),
Stride(StrideError),
MemoryCont(MemoryContError),
Shape(ShapeError),
}

/// Error from LAPACK
#[derive(Debug, new)]
pub struct LapackError {
pub return_code: i32,
}

impl fmt::Display for LapackError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "LAPACK: return_code = {}", self.return_code)
}
}

impl error::Error for LapackError {
fn description(&self) -> &str {
"LAPACK subroutine returns non-zero code"
}
}
/// Matrix is not square
#[fail(display = "Not square: rows({}) != cols({})", rows, cols)]
NotSquare { rows: i32, cols: i32 },

impl From<i32> for LapackError {
fn from(code: i32) -> LapackError {
LapackError { return_code: code }
}
}
/// LAPACK subroutine returns non-zero code
#[fail(display = "LAPACK: return_code = {}", return_code)]
LapackFailure { return_code: i32 },

/// Error that matrix is not square
#[derive(Debug, new)]
pub struct NotSquareError {
pub rows: i32,
pub cols: i32,
}
/// Strides of the array is not supported
#[fail(display = "invalid stride: s0={}, s1={}", s0, s1)]
InvalidStride { s0: Ixs, s1: Ixs },

impl fmt::Display for NotSquareError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Not square: rows({}) != cols({})", self.rows, self.cols)
}
}
/// Memory is not aligned continously
#[fail(display = "Memory is not contiguous")]
MemoryNotCont {},

impl error::Error for NotSquareError {
fn description(&self) -> &str {
"Matrix is not square"
}
}

/// Error that strides of the array is not supported
#[derive(Debug, new)]
pub struct StrideError {
pub s0: Ixs,
pub s1: Ixs,
}

impl fmt::Display for StrideError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "invalid stride: s0={}, s1={}", self.s0, self.s1)
}
}

impl error::Error for StrideError {
fn description(&self) -> &str {
"invalid stride"
}
}

/// Error that the memory is not aligned continously
#[derive(Debug, new)]
pub struct MemoryContError {}

impl fmt::Display for MemoryContError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Memory is not contiguous")
}
/// Strides of the array is not supported
#[fail(display = "Shape Error: {}", error)]
ShapeFailure { error: ShapeError },
}

impl error::Error for MemoryContError {
fn description(&self) -> &str {
"Memory is not contiguous"
impl From<ShapeError> for LinalgError {
fn from(error: ShapeError) -> LinalgError {
LinalgError::ShapeFailure { error }
}
}
6 changes: 3 additions & 3 deletions src/lapack_traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ impl LapackScalar for f64 {}
impl LapackScalar for c32 {}
impl LapackScalar for c64 {}

pub fn into_result<T>(info: i32, val: T) -> Result<T> {
if info == 0 {
pub fn into_result<T>(return_code: i32, val: T) -> Result<T> {
if return_code == 0 {
Ok(val)
} else {
Err(LapackError::new(info).into())
Err(LinalgError::LapackFailure { return_code })
}
}

Expand Down
20 changes: 15 additions & 5 deletions src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ where
if shape[1] == strides[0] as usize {
return Ok(MatrixLayout::C((self.rows() as i32, self.cols() as i32)));
}
Err(StrideError::new(strides[0], strides[1]).into())
Err(LinalgError::InvalidStride {
s0: strides[0],
s1: strides[1],
})
}

fn square_layout(&self) -> Result<MatrixLayout> {
Expand All @@ -107,20 +110,25 @@ where
if n == m {
Ok(l)
} else {
Err(NotSquareError::new(n, m).into())
Err(LinalgError::NotSquare { rows: n, cols: m })
}
}

fn ensure_square(&self) -> Result<()> {
if self.is_square() {
Ok(())
} else {
Err(NotSquareError::new(self.rows() as i32, self.cols() as i32).into())
Err(LinalgError::NotSquare {
rows: self.rows() as i32,
cols: self.cols() as i32,
})
}
}

fn as_allocated(&self) -> Result<&[A]> {
Ok(self.as_slice_memory_order().ok_or_else(MemoryContError::new)?)
Ok(self
.as_slice_memory_order()
.ok_or_else(|| LinalgError::MemoryNotCont {})?)
}
}

Expand All @@ -129,6 +137,8 @@ where
S: DataMut<Elem = A>,
{
fn as_allocated_mut(&mut self) -> Result<&mut [A]> {
Ok(self.as_slice_memory_order_mut().ok_or_else(MemoryContError::new)?)
Ok(self
.as_slice_memory_order_mut()
.ok_or_else(|| LinalgError::MemoryNotCont {})?)
}
}
6 changes: 2 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ extern crate lapacke;
extern crate num_complex;
extern crate num_traits;
extern crate rand;
#[macro_use]
extern crate failure;
#[macro_use(s)]
extern crate ndarray;
#[macro_use]
extern crate procedurals;
#[macro_use]
extern crate derive_new;

pub mod assert;
pub mod cholesky;
Expand Down
4 changes: 2 additions & 2 deletions src/solve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ where
self.ensure_square()?;
match self.factorize() {
Ok(fac) => fac.sln_det(),
Err(LinalgError::Lapack(LapackError { return_code })) if return_code > 0 => {
Err(LinalgError::LapackFailure { return_code }) if return_code > 0 => {
// The determinant is zero.
Ok((A::zero(), A::Real::neg_infinity()))
}
Expand All @@ -445,7 +445,7 @@ where
self.ensure_square()?;
match self.factorize_into() {
Ok(fac) => fac.sln_det_into(),
Err(LinalgError::Lapack(LapackError { return_code })) if return_code > 0 => {
Err(LinalgError::LapackFailure { return_code }) if return_code > 0 => {
// The determinant is zero.
Ok((A::zero(), A::Real::neg_infinity()))
}
Expand Down
4 changes: 2 additions & 2 deletions src/solveh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ where
fn sln_deth(&self) -> Result<(A::Real, A::Real)> {
match self.factorizeh() {
Ok(fac) => Ok(fac.sln_deth()),
Err(LinalgError::Lapack(LapackError { return_code })) if return_code > 0 => {
Err(LinalgError::LapackFailure { return_code }) if return_code > 0 => {
// Determinant is zero.
Ok((A::Real::zero(), A::Real::neg_infinity()))
}
Expand All @@ -445,7 +445,7 @@ where
fn sln_deth_into(self) -> Result<(A::Real, A::Real)> {
match self.factorizeh_into() {
Ok(fac) => Ok(fac.sln_deth_into()),
Err(LinalgError::Lapack(LapackError { return_code })) if return_code > 0 => {
Err(LinalgError::LapackFailure { return_code }) if return_code > 0 => {
// Determinant is zero.
Ok((A::Real::zero(), A::Real::neg_infinity()))
}
Expand Down