@@ -5,11 +5,12 @@ use crate::types::*;
5
5
use ndarray:: * ;
6
6
7
7
pub trait LinearOperator {
8
+ type Elem : Scalar ;
9
+
8
10
/// Apply operator out-place
9
11
fn apply < S > ( & self , a : & ArrayBase < S , Ix1 > ) -> Array1 < S :: Elem >
10
12
where
11
- S : Data ,
12
- S :: Elem : Scalar ,
13
+ S : Data < Elem = Self :: Elem > ,
13
14
{
14
15
let mut a = a. to_owned ( ) ;
15
16
self . apply_mut ( & mut a) ;
@@ -18,13 +19,16 @@ pub trait LinearOperator {
18
19
/// Apply operator in-place
19
20
fn apply_mut < S > ( & self , a : & mut ArrayBase < S , Ix1 > )
20
21
where
21
- S : DataMut ,
22
- S :: Elem : Scalar ;
22
+ S : DataMut < Elem = Self :: Elem > ,
23
+ {
24
+ let b = self . apply ( a) ;
25
+ azip ! ( mut a( a) , b in { * a = b } ) ;
26
+ }
27
+
23
28
/// Apply operator with move
24
29
fn apply_into < S > ( & self , mut a : ArrayBase < S , Ix1 > ) -> ArrayBase < S , Ix1 >
25
30
where
26
- S : DataOwned + DataMut ,
27
- S :: Elem : Scalar ,
31
+ S : DataOwned < Elem = Self :: Elem > + DataMut ,
28
32
{
29
33
self . apply_mut ( & mut a) ;
30
34
a
@@ -33,17 +37,15 @@ pub trait LinearOperator {
33
37
/// Apply operator to matrix out-place
34
38
fn apply2 < S > ( & self , a : & ArrayBase < S , Ix2 > ) -> Array2 < S :: Elem >
35
39
where
36
- S : Data ,
37
- S :: Elem : Scalar ,
40
+ S : Data < Elem = Self :: Elem > ,
38
41
{
39
42
let cols: Vec < _ > = a. axis_iter ( Axis ( 0 ) ) . map ( |col| self . apply ( & col) ) . collect ( ) ;
40
43
hstack ( & cols) . unwrap ( )
41
44
}
42
45
/// Apply operator to matrix in-place
43
46
fn apply2_mut < S > ( & self , a : & mut ArrayBase < S , Ix2 > )
44
47
where
45
- S : DataMut ,
46
- S :: Elem : Scalar ,
48
+ S : DataMut < Elem = Self :: Elem > ,
47
49
{
48
50
for mut col in a. axis_iter_mut ( Axis ( 0 ) ) {
49
51
self . apply_mut ( & mut col)
@@ -52,10 +54,24 @@ pub trait LinearOperator {
52
54
/// Apply operator to matrix with move
53
55
fn apply2_into < S > ( & self , mut a : ArrayBase < S , Ix2 > ) -> ArrayBase < S , Ix2 >
54
56
where
55
- S : DataOwned + DataMut ,
56
- S :: Elem : Scalar ,
57
+ S : DataOwned < Elem = Self :: Elem > + DataMut ,
57
58
{
58
59
self . apply2_mut ( & mut a) ;
59
60
a
60
61
}
61
62
}
63
+
64
+ impl < A , Sa > LinearOperator for ArrayBase < Sa , Ix2 >
65
+ where
66
+ A : Scalar ,
67
+ Sa : Data < Elem = A > ,
68
+ {
69
+ type Elem = A ;
70
+
71
+ fn apply < S > ( & self , a : & ArrayBase < S , Ix1 > ) -> Array1 < A >
72
+ where
73
+ S : Data < Elem = A > ,
74
+ {
75
+ self . dot ( a)
76
+ }
77
+ }
0 commit comments