numeric_loops/sum/
mod.rs

1//! Summation loops
2
3mod unroll;
4#[cfg(feature = "use_nightly")]
5mod specialization;
6
7use num_traits::Zero;
8
9use std::ops::{
10    Add,
11    Mul,
12};
13
14/// Compute the sum of the values in `xs`
15///
16/// With `"use_nightly"`, this is special cased for `f32, f64`.
17pub fn sum<A>(xs: &[A]) -> A
18    where A: Clone + Add<Output=A> + Zero,
19{
20    Sum::sum(xs)
21}
22
23/// Compute the dot product.
24///
25/// `xs` and `ys` should be the same length and there *may* be a panic if they
26/// are not.
27///
28/// With `"use_nightly"`, this is special cased for `f32, f64`.
29pub fn dot<A>(xs: &[A], ys: &[A]) -> A
30    where A: Add<Output=A> + Mul<Output=A> + Zero + Copy,
31{
32    debug_assert_eq!(xs.len(), ys.len());
33    Dot::dot(xs, ys)
34}
35
36trait Sum : Sized {
37    fn sum(lhs: &[Self]) -> Self;
38}
39
40trait Dot : Sized {
41    fn dot(lhs: &[Self], rhs: &[Self]) -> Self;
42}
43
44impl<A> Sum for A
45    where A: Clone + Add<Output=A> + Zero,
46{
47    #[cfg(feature = "use_nightly")]
48    default fn sum(lhs: &[A]) -> A {
49        unroll::sum(lhs)
50    }
51    #[cfg(not(feature = "use_nightly"))]
52    fn sum(lhs: &[A]) -> A {
53        unroll::sum(lhs)
54    }
55}
56
57impl<A> Dot for A
58    where A: Add<Output=A> + Mul<Output=A> + Zero + Copy,
59{
60    #[cfg(feature = "use_nightly")]
61    default fn dot(lhs: &[A], rhs: &[A]) -> A {
62        unroll::dot(lhs, rhs)
63    }
64    #[cfg(not(feature = "use_nightly"))]
65    fn dot(lhs: &[A], rhs: &[A]) -> A {
66        unroll::dot(lhs, rhs)
67    }
68}