From d60f138f626bc8faa8bc2eb37536435e7fdc6bc9 Mon Sep 17 00:00:00 2001 From: Jack Wrenn Date: Sat, 29 Dec 2018 12:50:41 -0500 Subject: [PATCH] Lift `DimName` requirement on `Sum` implementation. If the sequence is empty, a zero matrix is produced in which any dynamic dimensions default to size 0. Fixes #514 --- src/base/ops.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/base/ops.rs b/src/base/ops.rs index da81789f..a431536e 100644 --- a/src/base/ops.rs +++ b/src/base/ops.rs @@ -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 iter::Sum for MatrixMN +impl iter::Sum for MatrixMN where N: Scalar + ClosedAdd + Zero, DefaultAllocator: Allocator, { - fn sum>>(iter: I) -> MatrixMN { - 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>>(mut iter: I) -> MatrixMN { + iter.next() + .map(|first| iter.fold(first, Add::add)) + .unwrap_or(MatrixMN::zero()) } } -impl<'a, N, R: DimName, C: DimName> iter::Sum<&'a MatrixMN> for MatrixMN +impl<'a, N, R: Dim, C: Dim> iter::Sum<&'a MatrixMN> for MatrixMN where N: Scalar + ClosedAdd + Zero, DefaultAllocator: Allocator, { - fn sum>>(iter: I) -> MatrixMN { - 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>>(mut iter: I) -> MatrixMN { + iter.next() + .map(|first| iter.fold(first.to_owned(), Add::add)) + .unwrap_or(MatrixMN::zero()) } }