Skip to content

Commit 02b320a

Browse files
authored
Merge pull request #41 from termoshtt/norm
Complex support for Norm
2 parents 83a6eb1 + 38e7567 commit 02b320a

File tree

4 files changed

+30
-83
lines changed

4 files changed

+30
-83
lines changed

src/impls/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
//! Implement trait bindings of LAPACK
2-
pub mod outer;
32
pub mod solve;

src/impls/outer.rs

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/vector.rs

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
//! Define trait for vectors
22
33
use std::iter::Sum;
4-
use ndarray::{Array, NdFloat, Ix1, Ix2, LinalgScalar, ArrayBase, Data, Dimension};
5-
use num_traits::float::Float;
6-
use super::impls::outer::ImplOuter;
4+
use ndarray::*;
5+
use num_traits::Float;
76

8-
/// Norms of ndarray
7+
use super::types::*;
8+
9+
/// Define norm as a metric linear space (not as a matrix)
10+
///
11+
/// For operator norms, see opnorm module
912
pub trait Norm {
1013
type Output;
1114
/// rename of `norm_l2`
@@ -31,7 +34,7 @@ impl<A, S, D, T> Norm for ArrayBase<S, D>
3134
self.iter().map(|x| x.abs()).sum()
3235
}
3336
fn norm_l2(&self) -> Self::Output {
34-
self.iter().map(|x| x.sq_abs()).sum::<T>().sqrt()
37+
self.iter().map(|x| x.squared()).sum::<T>().sqrt()
3538
}
3639
fn norm_max(&self) -> Self::Output {
3740
self.iter().fold(T::zero(), |f, &val| {
@@ -44,35 +47,36 @@ impl<A, S, D, T> Norm for ArrayBase<S, D>
4447
/// Field with norm
4548
pub trait Absolute {
4649
type Output: Float;
47-
fn sq_abs(&self) -> Self::Output;
50+
fn squared(&self) -> Self::Output;
4851
fn abs(&self) -> Self::Output {
49-
self.sq_abs().sqrt()
52+
self.squared().sqrt()
5053
}
5154
}
5255

53-
impl<A: Float> Absolute for A {
54-
type Output = A;
55-
fn sq_abs(&self) -> A {
56+
macro_rules! impl_abs {
57+
($f:ty, $c:ty) => {
58+
59+
impl Absolute for $f {
60+
type Output = Self;
61+
fn squared(&self) -> Self::Output {
5662
*self * *self
5763
}
58-
fn abs(&self) -> A {
64+
fn abs(&self) -> Self::Output {
5965
Float::abs(*self)
6066
}
6167
}
6268

63-
/// Outer product
64-
pub fn outer<A, S1, S2>(a: &ArrayBase<S1, Ix1>, b: &ArrayBase<S2, Ix1>) -> Array<A, Ix2>
65-
where A: NdFloat + ImplOuter,
66-
S1: Data<Elem = A>,
67-
S2: Data<Elem = A>
68-
{
69-
let m = a.len();
70-
let n = b.len();
71-
let mut ab = Array::zeros((n, m));
72-
ImplOuter::outer(m,
73-
n,
74-
a.as_slice_memory_order().unwrap(),
75-
b.as_slice_memory_order().unwrap(),
76-
ab.as_slice_memory_order_mut().unwrap());
77-
ab.reversed_axes()
69+
impl Absolute for $c {
70+
type Output = $f;
71+
fn squared(&self) -> Self::Output {
72+
self.norm_sqr()
73+
}
74+
fn abs(&self) -> Self::Output {
75+
self.norm()
76+
}
7877
}
78+
79+
}} // impl_abs!
80+
81+
impl_abs!(f64, c64);
82+
impl_abs!(f32, c32);

tests/outer.rs

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)