13.38 numeric

   

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

adjacent_difference function template 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.

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 <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 ), 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.

See Also

accumulate function template

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

   


C++ in a Nutshell
C++ in a Nutshell
ISBN: 059600298X
EAN: 2147483647
Year: 2005
Pages: 270
Authors: Ray Lischner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net