Skip to content

Commit b9d1f01

Browse files
committed
Fix bug for Layout detection
1 parent e5ea6e2 commit b9d1f01

File tree

5 files changed

+36
-35
lines changed

5 files changed

+36
-35
lines changed

src/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ impl From<i32> for LapackError {
4040

4141
#[derive(Debug, new)]
4242
pub struct NotSquareError {
43-
pub rows: usize,
44-
pub cols: usize,
43+
pub rows: i32,
44+
pub cols: i32,
4545
}
4646

4747
impl fmt::Display for NotSquareError {

src/impl2/opnorm.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Implement Operator norms for matrices
22
33
use lapack::c;
4+
use lapack::c::Layout::ColumnMajor as cm;
45

56
use types::*;
67
use layout::*;
@@ -12,6 +13,16 @@ pub enum NormType {
1213
Frobenius = b'f',
1314
}
1415

16+
impl NormType {
17+
fn transpose(self) -> Self {
18+
match self {
19+
NormType::One => NormType::Infinity,
20+
NormType::Infinity => NormType::One,
21+
NormType::Frobenius => NormType::Frobenius,
22+
}
23+
}
24+
}
25+
1526
pub trait OperatorNorm_: AssociatedReal {
1627
fn opnorm(NormType, Layout, &[Self]) -> Self::Real;
1728
}
@@ -20,8 +31,10 @@ macro_rules! impl_opnorm {
2031
($scalar:ty, $lange:path) => {
2132
impl OperatorNorm_ for $scalar {
2233
fn opnorm(t: NormType, l: Layout, a: &[Self]) -> Self::Real {
23-
let (m, n) = l.ffi_size();
24-
$lange(l.ffi_layout(), t as u8, m, n, a, m)
34+
match l {
35+
Layout::F((col, lda)) => $lange(cm, t as u8, lda, col, a, lda),
36+
Layout::C((row, lda)) => $lange(cm, t.transpose() as u8, lda, row, a, lda),
37+
}
2538
}
2639
}
2740
}} // impl_opnorm!

src/layout.rs

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,21 @@
22
use ndarray::*;
33

44
use super::error::*;
5-
use lapack::c::Layout as Layout_;
65

7-
pub type Row = usize;
8-
pub type Col = usize;
9-
10-
pub type Row_ = i32;
11-
pub type Col_ = i32;
6+
pub type LDA = i32;
7+
pub type Col = i32;
8+
pub type Row = i32;
129

1310
pub enum Layout {
14-
C((Row, Col)),
15-
F((Row, Col)),
11+
C((Row, LDA)),
12+
F((Col, LDA)),
1613
}
1714

1815
impl Layout {
1916
pub fn size(&self) -> (Row, Col) {
20-
match self {
21-
&Layout::C(s) => s,
22-
&Layout::F(s) => s,
23-
}
24-
}
25-
26-
pub fn ffi_size(&self) -> (Row_, Col_) {
27-
let (n, m) = self.size();
28-
(n as Row_, m as Col_)
29-
}
30-
31-
pub fn ffi_layout(&self) -> Layout_ {
32-
match self {
33-
&Layout::C(_) => Layout_::RowMajor,
34-
&Layout::F(_) => Layout_::ColumnMajor,
17+
match *self {
18+
Layout::C((row, lda)) => (row, lda),
19+
Layout::F((col, lda)) => (lda, col),
3520
}
3621
}
3722
}
@@ -53,10 +38,10 @@ impl<A, S> AllocatedArray for ArrayBase<S, Ix2>
5338
if ::std::cmp::min(strides[0], strides[1]) != 1 {
5439
return Err(StrideError::new(strides[0], strides[1]).into());
5540
}
56-
if strides[0] < strides[1] {
57-
Ok(Layout::C((self.rows(), self.cols())))
41+
if strides[0] > strides[1] {
42+
Ok(Layout::C((self.rows() as i32, self.cols() as i32)))
5843
} else {
59-
Ok(Layout::F((self.rows(), self.cols())))
44+
Ok(Layout::F((self.cols() as i32, self.rows() as i32)))
6045
}
6146
}
6247

src/square.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ pub trait SquareMatrix: Matrix {
2525
Ok(())
2626
} else {
2727
Err(NotSquareError {
28-
rows: rows,
29-
cols: cols,
28+
rows: rows as i32,
29+
cols: cols as i32,
3030
})
3131
}
3232
}

tests/opnorm.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ macro_rules! impl_test {
55
#[test]
66
fn $funcname() {
77
let a = $a;
8-
assert_rclose!(a.opnorm_one().unwrap(), $op1, 1e-7);
9-
assert_rclose!(a.opnorm_inf().unwrap(), $opi, 1e-7);
10-
assert_rclose!(a.opnorm_fro().unwrap(), $opf, 1e-7);
8+
println!("ONE = {:?}", a.opnorm_one());
9+
println!("INF = {:?}", a.opnorm_inf());
10+
println!("FRO = {:?}", a.opnorm_fro());
11+
assert_rclose!(a.opnorm_fro().unwrap(), $opf, 1e-7; "Frobenius norm");
12+
assert_rclose!(a.opnorm_one().unwrap(), $op1, 1e-7; "One norm");
13+
assert_rclose!(a.opnorm_inf().unwrap(), $opi, 1e-7; "Infinity norm");
1114
}
1215
}} // impl_test
1316

0 commit comments

Comments
 (0)