1
1
//! Define trait for vectors
2
2
3
3
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 ;
7
6
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
9
12
pub trait Norm {
10
13
type Output ;
11
14
/// rename of `norm_l2`
@@ -31,7 +34,7 @@ impl<A, S, D, T> Norm for ArrayBase<S, D>
31
34
self . iter ( ) . map ( |x| x. abs ( ) ) . sum ( )
32
35
}
33
36
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 ( )
35
38
}
36
39
fn norm_max ( & self ) -> Self :: Output {
37
40
self . iter ( ) . fold ( T :: zero ( ) , |f, & val| {
@@ -44,35 +47,36 @@ impl<A, S, D, T> Norm for ArrayBase<S, D>
44
47
/// Field with norm
45
48
pub trait Absolute {
46
49
type Output : Float ;
47
- fn sq_abs ( & self ) -> Self :: Output ;
50
+ fn squared ( & self ) -> Self :: Output ;
48
51
fn abs ( & self ) -> Self :: Output {
49
- self . sq_abs ( ) . sqrt ( )
52
+ self . squared ( ) . sqrt ( )
50
53
}
51
54
}
52
55
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 {
56
62
* self * * self
57
63
}
58
- fn abs ( & self ) -> A {
64
+ fn abs( & self ) -> Self :: Output {
59
65
Float :: abs( * self )
60
66
}
61
67
}
62
68
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
+ }
78
77
}
78
+
79
+ } } // impl_abs!
80
+
81
+ impl_abs ! ( f64 , c64) ;
82
+ impl_abs ! ( f32 , c32) ;
0 commit comments