@@ -5,58 +5,62 @@ use ndarray::{Array, NdFloat, Ix1, Ix2, LinalgScalar, ArrayBase, Data, Dimension
5
5
use num_traits:: float:: Float ;
6
6
use super :: impls:: outer:: ImplOuter ;
7
7
8
- /// Methods for vectors
9
- pub trait Vector {
10
- type Scalar ;
11
- /// rename of norm_l2
12
- fn norm ( & self ) -> Self :: Scalar {
8
+ /// Norms of ndarray
9
+ pub trait Norm {
10
+ type Output ;
11
+ /// rename of ` norm_l2`
12
+ fn norm ( & self ) -> Self :: Output {
13
13
self . norm_l2 ( )
14
14
}
15
15
/// L-1 norm
16
- fn norm_l1 ( & self ) -> Self :: Scalar ;
16
+ fn norm_l1 ( & self ) -> Self :: Output ;
17
17
/// L-2 norm
18
- fn norm_l2 ( & self ) -> Self :: Scalar ;
18
+ fn norm_l2 ( & self ) -> Self :: Output ;
19
19
/// maximum norm
20
- fn norm_max ( & self ) -> Self :: Scalar ;
20
+ fn norm_max ( & self ) -> Self :: Output ;
21
21
}
22
22
23
- impl < A , S , D , T > Vector for ArrayBase < S , D >
24
- where A : LinalgScalar + Squared < Output = T > ,
23
+ impl < A , S , D , T > Norm for ArrayBase < S , D >
24
+ where A : LinalgScalar + NormedField < Output = T > ,
25
25
T : Float + Sum ,
26
26
S : Data < Elem = A > ,
27
27
D : Dimension
28
28
{
29
- type Scalar = T ;
30
- fn norm_l1 ( & self ) -> Self :: Scalar {
31
- self . iter ( ) . map ( |x| x. sq_abs ( ) ) . sum ( )
29
+ type Output = T ;
30
+ fn norm_l1 ( & self ) -> Self :: Output {
31
+ self . iter ( ) . map ( |x| x. norm ( ) ) . sum ( )
32
32
}
33
- fn norm_l2 ( & self ) -> Self :: Scalar {
33
+ fn norm_l2 ( & self ) -> Self :: Output {
34
34
self . iter ( ) . map ( |x| x. squared ( ) ) . sum :: < T > ( ) . sqrt ( )
35
35
}
36
- fn norm_max ( & self ) -> Self :: Scalar {
36
+ fn norm_max ( & self ) -> Self :: Output {
37
37
self . iter ( ) . fold ( T :: zero ( ) , |f, & val| {
38
- let v = val. sq_abs ( ) ;
38
+ let v = val. norm ( ) ;
39
39
if f > v { f } else { v }
40
40
} )
41
41
}
42
42
}
43
43
44
- pub trait Squared {
45
- type Output ;
44
+ /// Field with norm
45
+ pub trait NormedField {
46
+ type Output : Float ;
46
47
fn squared ( & self ) -> Self :: Output ;
47
- fn sq_abs ( & self ) -> Self :: Output ;
48
+ fn norm ( & self ) -> Self :: Output {
49
+ self . squared ( ) . sqrt ( )
50
+ }
48
51
}
49
52
50
- impl < A : Float > Squared for A {
53
+ impl < A : Float > NormedField for A {
51
54
type Output = A ;
52
55
fn squared ( & self ) -> A {
53
56
* self * * self
54
57
}
55
- fn sq_abs ( & self ) -> A {
58
+ fn norm ( & self ) -> A {
56
59
self . abs ( )
57
60
}
58
61
}
59
62
63
+ /// Outer product
60
64
pub fn outer < A , S1 , S2 > ( a : & ArrayBase < S1 , Ix1 > , b : & ArrayBase < S2 , Ix1 > ) -> Array < A , Ix2 >
61
65
where A : NdFloat + ImplOuter ,
62
66
S1 : Data < Elem = A > ,
0 commit comments