13.38 <numeric>The <numeric> header declares several function templates for numerical algorithms. See <algorithm> earlier in this chapter for most of the standard algorithms. See <cmath> for math functions that operate on scalar quantities. The <functional> section contains the standard functors, which might be useful when calling the numeric algorithms. Refer to Chapter 10 for general information about algorithms and iterators. See Appendix B for information about other libraries that provide additional numeric functions.
The accumulate function template sums all the values in the range [first, last) added with init and returns the result. The result and intermediate sum have the same type as init. The second version calls binary_op instead of using the addition (+) operator. Technical NotesThe result is computed as follows: for each i in the range [first, last), tmp = binary_op(tmp, *i), in which tmp is initialized to init. The final value of tmp is returned. The binary_op function or functor must not have any side effects. Complexity is linear: binary_op is called exactly last - first times.
The adjacent_difference function computes the differences of adjacent elements in the range [first, last) and assigns those differences to the output range starting at result. The second version calls binary_op instead of using the subtraction (-) operator. Technical NotesFor each i in [first + 1, last) and j in [result, result + (last - first)), assign *j = *i - tmp, in which tmp is initially *first; it becomes *i after each assignment to *j. The return value is the result iterator pointing to one past the last element written. The binary_op function or functor must not have any side effects. The result iterator can be the same as first. Complexity is linear: binary_op is called exactly last - first - 1 times.
The inner_product function template computes an inner product of two ranges. It accumulates the products of corresponding items in [first1, last1) and [first2, last2), in which last2 = first2 + (last1 - first1). The second version calls binary_op1 as the accumulator operator (instead of addition) and binary_op2 as the multiplication operator. Technical NotesThe result is computed as follows: for each i in the range [first1, last1), and for each j in [first2, last2), in which last2 = first2 + (last1 - first1), assign tmp = binary_op1(tmp, binary_op2(*i, *j)), in which tmp is initialized to init. The final value of tmp is returned. The binary_op1 and binary_op2 functions or functors must not have side effects. Complexity is linear: binary_op1 and binary_op2 are called exactly last - first times. See Alsoaccumulate function template
The partial_sum function template assigns partial sums to the range that starts at result. The partial sums are computed by accumulating successively larger subranges of [first, last). Thus, the first result item is *first, the second is *first + *(first + 1), and so on. The second version calls binary_op instead of using the addition operator (+). Technical NotesFor each i in [first, last), assign *(result + k) = sum(first, i), in which k = i - first, and sum(a, b) computes the sum in the manner of accumulate(a + 1, b, *a, binary_op). The return value is the result iterator, pointing to one past the last item written. The binary_op function or functor must not have any side effects. The result iterator can be the same as first. Complexity is linear: binary_op is called exactly (last - first) - 1 times. |