# 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.

 accumulate function template Computes a value from all items in a range

 ```template T accumulate(InputIter first, InputIter last, T init); template < typename InputIter, typename T, typename BinaryOp> T accumulate(InputIter first, InputIter last, T init, BinaryOp binary_op);```

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 Notes

The 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.

 ```template OutIter adjacent_difference(InIter first, InIter last, OutIter result); template OutIter adjacent_difference(InIter first, InIter last, OutIter result, BinOp binary_op);```

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 Notes

For 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.

 inner_product function template Computes inner product of two ranges

 ```template T inner_product(InIter1 first1, InIter1 last1, InIter2 first2, T init); template T inner_product(InIter1 first1, InIter1 last1, InIter2 first2, T init, BinaryOp1 binary_op1, BinaryOp2 binary_op2);```

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 Notes

The 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.

accumulate function template

 partial_sum function template Compute sums of subranges in a range

 ```template OutIter partial_sum(InIter first, InIter last, OutIter result); template OutIter partial_sum(InIter first, InIter last, OutIter result, BinOp binary_op);```

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 Notes

For 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.

 Chapter 1. Language Basics
 Chapter 2. Declarations
 Chapter 3. Expressions
 Chapter 4. Statements
 Chapter 5. Functions
 Chapter 6. Classes
 Chapter 7. Templates
 Chapter 8. Standard Library
 Chapter 9. Input and Output
 Chapter 10. Containers, Iterators, and Algorithms
 Chapter 11. Preprocessor Reference
 Chapter 12. Language Reference
 Appendix A. Compiler Extensions
 Appendix B. Projects
 Glossary