Skip to content

Commit cb4f764

Browse files
authored
Merge pull request #207 from rust-ndarray/lax
ndarray_linalg::lapack module as "lax" crate
2 parents 5cce4ac + 44c2819 commit cb4f764

32 files changed

+604
-481
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Generated by Cargo
22
# will have compiled files and executables
3-
/target/
3+
target/
44
*.rustfmt
55
rusty-tags.*
66

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
[workspace]
2-
members = ["ndarray-linalg"]
2+
members = [
3+
"ndarray-linalg",
4+
"lax",
5+
]
File renamed without changes.

lax/Cargo.toml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[package]
2+
name = "lax"
3+
version = "0.1.0"
4+
authors = ["Toshiki Teramura <[email protected]>"]
5+
edition = "2018"
6+
7+
[features]
8+
default = []
9+
intel-mkl = ["lapack-src/intel-mkl", "blas-src/intel-mkl"]
10+
netlib = ["lapack-src/netlib", "blas-src/netlib"]
11+
openblas = ["lapack-src/openblas", "blas-src/openblas"]
12+
13+
[dependencies]
14+
thiserror = "1"
15+
cauchy = "0.2"
16+
lapacke = "0.2.0"
17+
num-traits = "0.2"
18+
19+
[dependencies.blas-src]
20+
version = "0.6.1"
21+
default-features = false
22+
23+
[dependencies.lapack-src]
24+
version = "0.6.0"
25+
default-features = false
26+
27+
[dependencies.openblas-src]
28+
version = "0.9.0"
29+
default-features = false
30+
features = ["static"]
31+
optional = true

ndarray-linalg/src/lapack/cholesky.rs renamed to lax/src/cholesky.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
//! Cholesky decomposition
22
33
use super::*;
4-
use crate::{error::*, layout::MatrixLayout, types::*};
4+
use crate::{error::*, layout::MatrixLayout};
5+
use cauchy::*;
56

67
pub trait Cholesky_: Sized {
78
/// Cholesky: wrapper of `*potrf`

ndarray-linalg/src/lapack/eig.rs renamed to lax/src/eig.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Eigenvalue decomposition for general matrices
22
3-
use super::*;
4-
use crate::{error::*, layout::MatrixLayout, types::*};
3+
use crate::{error::*, layout::MatrixLayout};
4+
use cauchy::*;
55
use num_traits::Zero;
66

77
/// Wraps `*geev` for real/complex

ndarray-linalg/src/lapack/eigh.rs renamed to lax/src/eigh.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
//! Eigenvalue decomposition for Hermite matrices
22
33
use super::*;
4-
use crate::{error::*, layout::MatrixLayout, types::*};
4+
use crate::{error::*, layout::MatrixLayout};
5+
use cauchy::*;
56
use num_traits::Zero;
67

78
/// Wraps `*syev` for real and `*heev` for complex

lax/src/error.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use thiserror::Error;
2+
3+
pub type Result<T> = ::std::result::Result<T, Error>;
4+
5+
#[derive(Error, Debug)]
6+
pub enum Error {
7+
#[error(
8+
"Invalid value for LAPACK subroutine {}-th argument",
9+
-return_code
10+
)]
11+
LapackInvalidValue { return_code: i32 },
12+
13+
#[error(
14+
"Comutational failure in LAPACK subroutine: return_code = {}",
15+
return_code
16+
)]
17+
LapackComputationalFailure { return_code: i32 },
18+
19+
/// Strides of the array is not supported
20+
#[error("Invalid shape")]
21+
InvalidShape,
22+
}
23+
24+
pub trait AsLapackResult {
25+
fn as_lapack_result(self) -> Result<()>;
26+
}
27+
28+
impl AsLapackResult for i32 {
29+
fn as_lapack_result(self) -> Result<()> {
30+
if self > 0 {
31+
return Err(Error::LapackComputationalFailure { return_code: self });
32+
}
33+
if self < 0 {
34+
return Err(Error::LapackInvalidValue { return_code: self });
35+
}
36+
Ok(())
37+
}
38+
}

lax/src/layout.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//! Memory layout of matrices
2+
3+
pub type LDA = i32;
4+
pub type LEN = i32;
5+
pub type Col = i32;
6+
pub type Row = i32;
7+
8+
#[derive(Debug, Clone, Copy, PartialEq)]
9+
pub enum MatrixLayout {
10+
C((Row, LDA)),
11+
F((Col, LDA)),
12+
}
13+
14+
impl MatrixLayout {
15+
pub fn size(&self) -> (Row, Col) {
16+
match *self {
17+
MatrixLayout::C((row, lda)) => (row, lda),
18+
MatrixLayout::F((col, lda)) => (lda, col),
19+
}
20+
}
21+
22+
pub fn resized(&self, row: Row, col: Col) -> MatrixLayout {
23+
match *self {
24+
MatrixLayout::C(_) => MatrixLayout::C((row, col)),
25+
MatrixLayout::F(_) => MatrixLayout::F((col, row)),
26+
}
27+
}
28+
29+
pub fn lda(&self) -> LDA {
30+
std::cmp::max(
31+
1,
32+
match *self {
33+
MatrixLayout::C((_, lda)) | MatrixLayout::F((_, lda)) => lda,
34+
},
35+
)
36+
}
37+
38+
pub fn len(&self) -> LEN {
39+
match *self {
40+
MatrixLayout::C((row, _)) => row,
41+
MatrixLayout::F((col, _)) => col,
42+
}
43+
}
44+
45+
pub fn is_empty(&self) -> bool {
46+
self.len() == 0
47+
}
48+
49+
pub fn lapacke_layout(&self) -> lapacke::Layout {
50+
match *self {
51+
MatrixLayout::C(_) => lapacke::Layout::RowMajor,
52+
MatrixLayout::F(_) => lapacke::Layout::ColumnMajor,
53+
}
54+
}
55+
56+
pub fn same_order(&self, other: &MatrixLayout) -> bool {
57+
self.lapacke_layout() == other.lapacke_layout()
58+
}
59+
60+
pub fn toggle_order(&self) -> Self {
61+
match *self {
62+
MatrixLayout::C((row, col)) => MatrixLayout::F((col, row)),
63+
MatrixLayout::F((col, row)) => MatrixLayout::C((row, col)),
64+
}
65+
}
66+
}

ndarray-linalg/src/lapack/least_squares.rs renamed to lax/src/least_squares.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
//! Least squares
22
3-
use super::*;
4-
use crate::{error::*, layout::MatrixLayout, types::*};
5-
use ndarray::{ErrorKind, ShapeError};
3+
use crate::{error::*, layout::MatrixLayout};
4+
use cauchy::*;
65
use num_traits::Zero;
76

87
/// Result of LeastSquares
@@ -39,9 +38,7 @@ macro_rules! impl_least_squares {
3938
) -> Result<LeastSquaresOutput<Self>> {
4039
let (m, n) = a_layout.size();
4140
if (m as usize) > b.len() || (n as usize) > b.len() {
42-
return Err(LinalgError::Shape(ShapeError::from_kind(
43-
ErrorKind::IncompatibleShape,
44-
)));
41+
return Err(Error::InvalidShape);
4542
}
4643
let k = ::std::cmp::min(m, n);
4744
let nrhs = 1;
@@ -83,9 +80,7 @@ macro_rules! impl_least_squares {
8380
|| (n as usize) > b.len()
8481
|| a_layout.lapacke_layout() != b_layout.lapacke_layout()
8582
{
86-
return Err(LinalgError::Shape(ShapeError::from_kind(
87-
ErrorKind::IncompatibleShape,
88-
)));
83+
return Err(Error::InvalidShape);
8984
}
9085
let k = ::std::cmp::min(m, n);
9186
let nrhs = b_layout.size().1;

0 commit comments

Comments
 (0)