Skip to content

Commit 65e13d9

Browse files
jturner314bluss
authored andcommitted
Allow &arr (op) scalar output to be any elem type
This change has two benefits: * The new implementation applies to more combinations of types. For example, it now applies to `&Array2<f32>` and `Complex<f32>`. * The new implementation avoids cloning the elements twice, and it avoids iterating over the elements twice. (The old implementation called `.to_owned()` followed by the arithmetic operation, while the new implementation clones the elements and performs the arithmetic operation in the same iteration.) On my machine, this change improves the performance for both contiguous and discontiguous arrays. (`scalar_add_1/2` go from ~530 ns/iter to ~380 ns/iter, and `scalar_add_strided_1/2` go from ~1540 ns/iter to ~1420 ns/iter.)
1 parent 52ca234 commit 65e13d9

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/impl_ops.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,15 @@ impl<A, S, D, B> $trt<B> for ArrayBase<S, D>
152152
#[doc=$doc]
153153
/// between the reference `self` and the scalar `x`,
154154
/// and return the result as a new `Array`.
155-
impl<'a, A, S, D, B> $trt<B> for &'a ArrayBase<S, D>
156-
where A: Clone + $trt<B, Output=A>,
155+
impl<'a, A, S, D, B, C> $trt<B> for &'a ArrayBase<S, D>
156+
where A: Clone + $trt<B, Output=C>,
157157
S: Data<Elem=A>,
158158
D: Dimension,
159159
B: ScalarOperand,
160160
{
161-
type Output = Array<A, D>;
162-
fn $mth(self, x: B) -> Array<A, D> {
163-
self.to_owned().$mth(x)
161+
type Output = Array<C, D>;
162+
fn $mth(self, x: B) -> Self::Output {
163+
self.map(move |elt| elt.clone() $operator x.clone())
164164
}
165165
}
166166
);

0 commit comments

Comments
 (0)