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()) } }