Skip to content

Commit c0b58ed

Browse files
committed
opnorm() for ArrayBase
1 parent c997e8d commit c0b58ed

File tree

3 files changed

+46
-23
lines changed

3 files changed

+46
-23
lines changed

src/impl2/opnorm.rs

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,21 @@ pub enum NormType {
1212
Frobenius = b'f',
1313
}
1414

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-
}
15+
pub trait OperatorNorm_: AssociatedReal {
16+
fn opnorm(NormType, Layout, &[Self]) -> Self::Real;
3117
}
3218

3319
macro_rules! impl_opnorm {
34-
($scalar:ty, $output:ty, $lange:path) => {
20+
($scalar:ty, $lange:path) => {
3521
impl OperatorNorm_ for $scalar {
36-
type Output = $output;
37-
fn opnorm(t: NormType, l: Layout, a: &[Self]) -> Self::Output {
22+
fn opnorm(t: NormType, l: Layout, a: &[Self]) -> Self::Real {
3823
let (m, n) = l.ffi_size();
3924
$lange(l.ffi_layout(), t as u8, m, n, a, m)
4025
}
4126
}
4227
}} // impl_opnorm!
4328

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);
29+
impl_opnorm!(f64, c::dlange);
30+
impl_opnorm!(f32, c::slange);
31+
impl_opnorm!(c64, c::zlange);
32+
impl_opnorm!(c32, c::clange);

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ pub mod layout;
4848
pub mod impls;
4949
pub mod impl2;
5050

51+
pub mod traits;
52+
5153
pub mod vector;
5254
pub mod matrix;
5355
pub mod square;

src/traits.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
pub use impl2::LapackScalar;
3+
pub use impl2::NormType;
4+
5+
use ndarray::*;
6+
7+
use super::types::*;
8+
use super::error::*;
9+
use super::layout::*;
10+
11+
pub trait OperationNorm {
12+
type Output;
13+
fn opnorm(&self, t: NormType) -> Self::Output;
14+
fn opnorm_one(&self) -> Self::Output {
15+
self.opnorm(NormType::One)
16+
}
17+
fn opnorm_inf(&self) -> Self::Output {
18+
self.opnorm(NormType::Infinity)
19+
}
20+
fn opnorm_fro(&self) -> Self::Output {
21+
self.opnorm(NormType::Frobenius)
22+
}
23+
}
24+
25+
impl<A, S> OperationNorm for ArrayBase<S, Ix2>
26+
where A: LapackScalar + AssociatedReal,
27+
S: Data<Elem = A>
28+
{
29+
type Output = Result<A::Real>;
30+
31+
fn opnorm(&self, t: NormType) -> Self::Output {
32+
let l = self.layout()?;
33+
let a = self.as_allocated()?;
34+
Ok(A::opnorm(t, l, a))
35+
}
36+
}

0 commit comments

Comments
 (0)