Skip to content

Commit ece39bb

Browse files
committed
Remove apply_tensor*
1 parent 3abb200 commit ece39bb

File tree

1 file changed

+42
-28
lines changed

1 file changed

+42
-28
lines changed

src/krylov/operator.rs

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,61 @@
11
//! Linear operator algebra
22
3+
use crate::generate::hstack;
4+
use crate::types::*;
35
use ndarray::*;
46

57
pub trait LinearOperator {
68
/// 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
917
}
1018
/// 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;
1423
/// 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
1731
}
1832

1933
/// 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>
3435
where
3536
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>)
3944
where
4045
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>
4454
where
4555
S: DataOwned + DataMut,
46-
D: Dimension;
56+
S::Elem: Scalar,
57+
{
58+
self.apply2_mut(&mut a);
59+
a
60+
}
4761
}

0 commit comments

Comments
 (0)