Skip to content

Commit 69aef5e

Browse files
committed
impl OperatorNorm
1 parent 3e01ce7 commit 69aef5e

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

src/impl2/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
pub mod opnorm;

src/impl2/opnorm.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//! Implement Operator norms for matrices
2+
3+
use lapack::c;
4+
5+
use types::*;
6+
use layout::*;
7+
8+
#[repr(u8)]
9+
pub enum NormType {
10+
One = b'o',
11+
Infinity = b'i',
12+
Frobenius = b'f',
13+
}
14+
15+
pub trait OperatorNorm: Sized {
16+
type Output;
17+
18+
fn opnorm(NormType, Layout, &[Self]) -> Self::Output;
19+
20+
fn opnorm_one(l: Layout, a: &[Self]) -> Self::Output {
21+
Self::opnorm(NormType::One, l, a)
22+
}
23+
24+
fn opnorm_inf(l: Layout, a: &[Self]) -> Self::Output {
25+
Self::opnorm(NormType::Infinity, l, a)
26+
}
27+
28+
fn opnorm_fro(l: Layout, a: &[Self]) -> Self::Output {
29+
Self::opnorm(NormType::Frobenius, l, a)
30+
}
31+
}
32+
33+
macro_rules! impl_opnorm {
34+
($scalar:ty, $output:ty, $lange:path) => {
35+
impl OperatorNorm for $scalar {
36+
type Output = $output;
37+
fn opnorm(t: NormType, l: Layout, a: &[Self]) -> Self::Output {
38+
let (m, n) = l.ffi_size();
39+
$lange(l.ffi_layout(), t as u8, m, n, a, m)
40+
}
41+
}
42+
}} // impl_opnorm!
43+
44+
impl_opnorm!(f64, f64, c::dlange);
45+
impl_opnorm!(f32, f32, c::slange);
46+
impl_opnorm!(c64, f64, c::zlange);
47+
impl_opnorm!(c32, f32, c::clange);

src/layout.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
use ndarray::*;
33

44
use super::error::*;
5+
use lapack::c::Layout as FFI_Layout;
56

67
pub enum Layout {
78
C((usize, usize)),
@@ -15,6 +16,18 @@ impl Layout {
1516
&Layout::F(s) => s,
1617
}
1718
}
19+
20+
pub fn ffi_size(&self) -> (i32, i32) {
21+
let (n, m) = self.size();
22+
(m as i32, n as i32)
23+
}
24+
25+
pub fn ffi_layout(&self) -> FFI_Layout {
26+
match self {
27+
&Layout::C(_) => FFI_Layout::RowMajor,
28+
&Layout::F(_) => FFI_Layout::ColumnMajor,
29+
}
30+
}
1831
}
1932

2033
pub trait AllocatedArray2D {

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ extern crate enum_error_derive;
4343
extern crate derive_new;
4444

4545
pub mod types;
46-
pub mod impls;
4746
pub mod error;
4847
pub mod layout;
48+
pub mod impls;
49+
pub mod impl2;
4950

5051
pub mod vector;
5152
pub mod matrix;

0 commit comments

Comments
 (0)