|
1 | 1 | //! Linear operator algebra
|
2 | 2 |
|
| 3 | +use crate::generate::hstack; |
| 4 | +use crate::types::*; |
3 | 5 | use ndarray::*;
|
4 | 6 |
|
5 | 7 | pub trait LinearOperator {
|
6 | 8 | /// Apply operator out-place
|
7 |
| - fn apply<S: Data>(&self, a: &ArrayBase<S, Ix1>) -> Array1<S::Elem> { |
8 |
| - self.apply_tensor(a, 0) |
| 9 | + fn apply<S>(&self, a: &ArrayBase<S, Ix1>) -> Array1<S::Elem> |
| 10 | + where |
| 11 | + S: Data, |
| 12 | + S::Elem: Scalar, |
| 13 | + { |
| 14 | + let mut a = a.to_owned(); |
| 15 | + self.apply_mut(&mut a); |
| 16 | + a |
9 | 17 | }
|
10 | 18 | /// Apply operator in-place
|
11 |
| - fn apply_mut<S: DataMut>(&self, a: &mut ArrayBase<S, Ix1>) { |
12 |
| - self.apply_tensor_mut(a, 0) |
13 |
| - } |
| 19 | + fn apply_mut<S>(&self, a: &mut ArrayBase<S, Ix1>) |
| 20 | + where |
| 21 | + S: DataMut, |
| 22 | + S::Elem: Scalar; |
14 | 23 | /// Apply operator with move
|
15 |
| - fn apply_into<S: DataOwned + DataMut>(&self, a: ArrayBase<S, Ix1>) -> ArrayBase<S, Ix1> { |
16 |
| - self.apply_tensor_into(a, 0) |
| 24 | + fn apply_into<S>(&self, mut a: ArrayBase<S, Ix1>) -> ArrayBase<S, Ix1> |
| 25 | + where |
| 26 | + S: DataOwned + DataMut, |
| 27 | + S::Elem: Scalar, |
| 28 | + { |
| 29 | + self.apply_mut(&mut a); |
| 30 | + a |
17 | 31 | }
|
18 | 32 |
|
19 | 33 | /// Apply operator to matrix out-place
|
20 |
| - fn apply2<S: Data>(&self, a: &ArrayBase<S, Ix2>) -> Array2<S::Elem> { |
21 |
| - self.apply_tensor(a, 0) |
22 |
| - } |
23 |
| - /// Apply operator to matrix in-place |
24 |
| - fn apply2_mut<S: DataMut>(&self, a: &mut ArrayBase<S, Ix2>) { |
25 |
| - self.apply_tensor_mut(a, 0) |
26 |
| - } |
27 |
| - /// Apply operator to matrix with move |
28 |
| - fn apply2_into<S: DataOwned + DataMut>(&self, a: ArrayBase<S, Ix2>) -> ArrayBase<S, Ix2> { |
29 |
| - self.apply_tensor_into(a, 0) |
30 |
| - } |
31 |
| - |
32 |
| - /// Apply operator to the n-th index of the tensor (out-place) |
33 |
| - fn apply_tensor<S, D>(&self, a: &ArrayBase<S, D>, n: usize) -> Array<S::Elem, D> |
| 34 | + fn apply2<S>(&self, a: &ArrayBase<S, Ix2>) -> Array2<S::Elem> |
34 | 35 | where
|
35 | 36 | S: Data,
|
36 |
| - D: Dimension; |
37 |
| - /// Apply operator to the n-th index of the tensor (in-place) |
38 |
| - fn apply_tensor_mut<S, D>(&self, a: &mut ArrayBase<S, D>, n: usize) |
| 37 | + S::Elem: Scalar, |
| 38 | + { |
| 39 | + let cols: Vec<_> = a.axis_iter(Axis(0)).map(|col| self.apply(&col)).collect(); |
| 40 | + hstack(&cols).unwrap() |
| 41 | + } |
| 42 | + /// Apply operator to matrix in-place |
| 43 | + fn apply2_mut<S>(&self, a: &mut ArrayBase<S, Ix2>) |
39 | 44 | where
|
40 | 45 | S: DataMut,
|
41 |
| - D: Dimension; |
42 |
| - /// Apply operator to the n-th index of the tensor (with move) |
43 |
| - fn apply_tensor_into<S, D>(&self, a: ArrayBase<S, D>, n: usize) -> ArrayBase<S, D> |
| 46 | + S::Elem: Scalar, |
| 47 | + { |
| 48 | + for mut col in a.axis_iter_mut(Axis(0)) { |
| 49 | + self.apply_mut(&mut col) |
| 50 | + } |
| 51 | + } |
| 52 | + /// Apply operator to matrix with move |
| 53 | + fn apply2_into<S>(&self, mut a: ArrayBase<S, Ix2>) -> ArrayBase<S, Ix2> |
44 | 54 | where
|
45 | 55 | S: DataOwned + DataMut,
|
46 |
| - D: Dimension; |
| 56 | + S::Elem: Scalar, |
| 57 | + { |
| 58 | + self.apply2_mut(&mut a); |
| 59 | + a |
| 60 | + } |
47 | 61 | }
|
0 commit comments