Skip to content

Commit

Permalink
Allow &arr (op) scalar output to be any elem type
Browse files Browse the repository at this point in the history
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.)
  • Loading branch information
jturner314 committed Feb 15, 2020
1 parent 73ae59b commit 073bc0e
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/impl_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,15 @@ impl<A, S, D, B> $trt<B> for ArrayBase<S, D>
#[doc=$doc]
/// between the reference `self` and the scalar `x`,
/// and return the result as a new `Array`.
impl<'a, A, S, D, B> $trt<B> for &'a ArrayBase<S, D>
where A: Clone + $trt<B, Output=A>,
impl<'a, A, S, D, B, C> $trt<B> for &'a ArrayBase<S, D>
where A: Clone + $trt<B, Output=C>,
S: Data<Elem=A>,
D: Dimension,
B: ScalarOperand,
{
type Output = Array<A, D>;
fn $mth(self, x: B) -> Array<A, D> {
self.to_owned().$mth(x)
type Output = Array<C, D>;
fn $mth(self, x: B) -> Self::Output {
self.map(move |elt| elt.clone() $operator x.clone())
}
}
);
Expand Down

0 comments on commit 073bc0e

Please sign in to comment.