1 // This file is part of the ustl library, an STL implementation.
3 // Copyright (C) 2005 by Mike Sharov <msharov@users.sourceforge.net>
4 // This file is free software, distributed under the MIT License.
8 // This file contains numeric algorithm templates.
11 #ifndef UNUMERIC_H_6C99D6F6363832C644A6FFF336E84E18
12 #define UNUMERIC_H_6C99D6F6363832C644A6FFF336E84E18
16 /// Returns the sum of all elements in [first, last) added to \p init.
17 /// \ingroup NumericAlgorithms
19 template <typename InputIterator
, typename T
>
20 inline T
accumulate (InputIterator first
, InputIterator last
, T init
)
27 /// Returns the sum of all elements in [first, last) via \p op, added to \p init.
28 /// \ingroup NumericAlgorithms
30 template <typename InputIterator
, typename T
, typename BinaryFunction
>
31 inline T
accumulate (InputIterator first
, InputIterator last
, T init
, BinaryFunction binary_op
)
34 init
= binary_op (init
, *first
++);
38 /// Assigns range [value, value + (last - first)) to [first, last)
39 /// \ingroup NumericAlgorithms
41 template <typename ForwardIterator
, typename T
>
42 inline void iota (ForwardIterator first
, ForwardIterator last
, T value
)
48 /// Returns the sum of products of respective elements in the given ranges.
49 /// \ingroup NumericAlgorithms
51 template <typename InputIterator1
, typename InputIterator2
, typename T
>
52 inline T
inner_product (InputIterator1 first1
, InputIterator1 last1
, InputIterator2 first2
, T init
)
54 while (first1
< last1
)
55 init
+= *first1
++ * *first2
++;
59 /// Returns the sum of products of respective elements in the given ranges.
60 /// \ingroup NumericAlgorithms
62 template <typename InputIterator1
, typename InputIterator2
, typename T
,
63 typename BinaryOperation1
, typename BinaryOperation2
>
64 inline T inner_product
65 (InputIterator1 first1
, InputIterator1 last1
, InputIterator2 first2
, T init
,
66 BinaryOperation1 sumOp
, BinaryOperation2 productOp
)
68 while (first1
< last1
)
69 init
= sumOp (init
, productOp (*first1
++, *first2
++));
73 /// Writes result such that result[i] = sum (first...first+i)
74 /// \ingroup NumericAlgorithms
76 template <typename InputIterator
, typename OutputIterator
>
77 inline OutputIterator
partial_sum (InputIterator first
, InputIterator last
, OutputIterator result
)
82 *++result
= *first
++ + *result
;
86 /// Writes result such that result[i] = sumOp (first...first+i)
87 /// \ingroup NumericAlgorithms
89 template <typename InputIterator
, typename OutputIterator
, typename BinaryOperation
>
90 inline OutputIterator
partial_sum (InputIterator first
, InputIterator last
, OutputIterator result
, BinaryOperation sumOp
)
95 *++result
= sumOp (*first
++, *result
);
99 /// Writes result such that result[i] = first[i] - first[i - 1]
100 /// \ingroup NumericAlgorithms
102 template <typename InputIterator
, typename OutputIterator
>
103 inline OutputIterator
adjacent_difference (InputIterator first
, InputIterator last
, OutputIterator result
)
106 *result
++ = *first
++;
108 *result
++ = *first
- *(first
- 1);
112 /// Writes result such that result[i] = differenceOp (first[i], first[i - 1])
113 /// \ingroup NumericAlgorithms
115 template <typename InputIterator
, typename OutputIterator
, typename BinaryOperation
>
116 inline OutputIterator
adjacent_difference (InputIterator first
, InputIterator last
, OutputIterator result
, BinaryOperation differenceOp
)
119 *result
++ = *first
++;
121 *result
++ = differenceOp (*first
, *(first
- 1));
125 /// \brief Returns x^n.
126 /// Donald Knuth's Russian Peasant algorithm.
127 /// \ingroup NumericAlgorithms
129 template <typename T
>
130 inline T
power (T x
, unsigned n
)
132 T
result (n
% 2 ? x
: 1);
141 /// \brief Returns x^n, using \p op instead of multiplication.
142 /// Donald Knuth's Russian Peasant algorithm.
143 /// \ingroup NumericAlgorithms
145 template <typename T
, typename BinaryOperation
>
146 inline T
power (T x
, unsigned n
, BinaryOperation op
)
148 T
result (n
% 2 ? x
: 1);
152 result
= op (result
, x
);