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 11 for general information about algorithms and iterators.
Computes a value from all items in a range
template <typename InputIter, typename T> 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 second version calls binary_op
instead using of the addition (+
) operator.
The result is computed as follows: for each i in the range [first, last), tmp = binary_op
(tmp, *i), where 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.
Computes differences of adjacent elements in a range
template <typename InIter, typename OutIter> OutIter adjacent_difference(InIter first, InIter last, OutIter result); template <typename InIter, typename OutIter, typename BinOp> 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.
For each i in [first + 1, last) and j in [result, result + (last - first)), assign *j = *i - tmp, where tmp is initially *first, and 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_
o
p
is called exactly last - first - 1 times.
Computes inner product of two ranges
template <typename InIter1, typename InIter2, typename T> T inner_product(InIter1 first1, InIter1 last1, InIter2 first2, T init); template <typename InIter1, typename InIter2, typename T, typename BinaryOp1, typename BinaryOp2> 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
) where last2
= first2
+ (last1
- first1
). The second version calls binary_
o
p1
as the accumulator operator (instead of addition) and binary_op2
as the multiplication operator.
The result is computed as follows: for each i in the range [first1, last1), and for each j in [first2, last2) where last2 = first2 + (last1 - first1), assign tmp = binary_op1
(tmp, binar
y
_op2
(*i, *j)), where 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
Compute sums of subranges in a range
template <typename InIter, typename OutIter> OutIter partial_sum(InIter first, InIter last, OutIter result); template <typename InIter, typename OutIter, typename BinOp> OutIter partial_sum(InIter first, InIter last, OutIter result, BinOp binary_op);
The parti
al
_
s
um
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 *
f
irst
, the second is *
f
irst
+ *(
fi
rst
+
1)
, and so on. The second version calls binary_op
instead of using the addition operator (+
).
For each i in [first, last), assign *(result + k) = sum(first, i), where 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.