Skip to content

Commit

Permalink
Lift DimName requirement on Sum implementation.
Browse files Browse the repository at this point in the history
If the sequence is empty, a zero matrix is produced in which any dynamic dimensions default to size 0.

Fixes #514
  • Loading branch information
jswrenn committed Dec 29, 2018
1 parent 1dd762d commit d60f138
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/base/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,23 +374,29 @@ componentwise_binop_impl!(Sub, sub, ClosedSub;
SubAssign, sub_assign, sub_assign_statically_unchecked, sub_assign_statically_unchecked_mut;
sub_to, sub_to_statically_unchecked);

impl<N, R: DimName, C: DimName> iter::Sum for MatrixMN<N, R, C>
impl<N, R: Dim, C: Dim> iter::Sum for MatrixMN<N, R, C>
where
N: Scalar + ClosedAdd + Zero,
DefaultAllocator: Allocator<N, R, C>,
{
fn sum<I: Iterator<Item = MatrixMN<N, R, C>>>(iter: I) -> MatrixMN<N, R, C> {
iter.fold(Matrix::zero(), |acc, x| acc + x)
/// If the sequence is empty, a zero matrix is produced in which any dynamic dimensions default to size 0.
fn sum<I: Iterator<Item = MatrixMN<N, R, C>>>(mut iter: I) -> MatrixMN<N, R, C> {
iter.next()
.map(|first| iter.fold(first, Add::add))
.unwrap_or(MatrixMN::zero())
}
}

impl<'a, N, R: DimName, C: DimName> iter::Sum<&'a MatrixMN<N, R, C>> for MatrixMN<N, R, C>
impl<'a, N, R: Dim, C: Dim> iter::Sum<&'a MatrixMN<N, R, C>> for MatrixMN<N, R, C>
where
N: Scalar + ClosedAdd + Zero,
DefaultAllocator: Allocator<N, R, C>,
{
fn sum<I: Iterator<Item = &'a MatrixMN<N, R, C>>>(iter: I) -> MatrixMN<N, R, C> {
iter.fold(Matrix::zero(), |acc, x| acc + x)
/// If the sequence is empty, a zero matrix is produced in which any dynamic dimensions default to size 0.
fn sum<I: Iterator<Item = &'a MatrixMN<N, R, C>>>(mut iter: I) -> MatrixMN<N, R, C> {
iter.next()
.map(|first| iter.fold(first.to_owned(), Add::add))
.unwrap_or(MatrixMN::zero())
}
}

Expand Down

0 comments on commit d60f138

Please sign in to comment.