1 |
786 |
skrzyp |
// This file is part of the uSTL library, an STL implementation.
|
2 |
|
|
//
|
3 |
|
|
// Copyright (c) 2005-2009 by Mike Sharov <msharov@users.sourceforge.net>
|
4 |
|
|
// This file is free software, distributed under the MIT License.
|
5 |
|
|
|
6 |
|
|
#ifndef UNUMERIC_H_6C99D6F6363832C644A6FFF336E84E18
|
7 |
|
|
#define UNUMERIC_H_6C99D6F6363832C644A6FFF336E84E18
|
8 |
|
|
|
9 |
|
|
namespace ustl {
|
10 |
|
|
|
11 |
|
|
/// Returns the sum of all elements in [first, last) added to \p init.
|
12 |
|
|
/// \ingroup NumericAlgorithms
|
13 |
|
|
///
|
14 |
|
|
template <typename InputIterator, typename T>
|
15 |
|
|
inline T accumulate (InputIterator first, InputIterator last, T init)
|
16 |
|
|
{
|
17 |
|
|
while (first < last)
|
18 |
|
|
init += *first++;
|
19 |
|
|
return (init);
|
20 |
|
|
}
|
21 |
|
|
|
22 |
|
|
/// Returns the sum of all elements in [first, last) via \p op, added to \p init.
|
23 |
|
|
/// \ingroup NumericAlgorithms
|
24 |
|
|
///
|
25 |
|
|
template <typename InputIterator, typename T, typename BinaryFunction>
|
26 |
|
|
inline T accumulate (InputIterator first, InputIterator last, T init, BinaryFunction binary_op)
|
27 |
|
|
{
|
28 |
|
|
while (first < last)
|
29 |
|
|
init = binary_op (init, *first++);
|
30 |
|
|
return (init);
|
31 |
|
|
}
|
32 |
|
|
|
33 |
|
|
/// Assigns range [value, value + (last - first)) to [first, last)
|
34 |
|
|
/// \ingroup NumericAlgorithms
|
35 |
|
|
///
|
36 |
|
|
template <typename ForwardIterator, typename T>
|
37 |
|
|
inline void iota (ForwardIterator first, ForwardIterator last, T value)
|
38 |
|
|
{
|
39 |
|
|
while (first < last)
|
40 |
|
|
*first++ = value++;
|
41 |
|
|
}
|
42 |
|
|
|
43 |
|
|
/// Returns the sum of products of respective elements in the given ranges.
|
44 |
|
|
/// \ingroup NumericAlgorithms
|
45 |
|
|
///
|
46 |
|
|
template <typename InputIterator1, typename InputIterator2, typename T>
|
47 |
|
|
inline T inner_product (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init)
|
48 |
|
|
{
|
49 |
|
|
while (first1 < last1)
|
50 |
|
|
init += *first1++ * *first2++;
|
51 |
|
|
return (init);
|
52 |
|
|
}
|
53 |
|
|
|
54 |
|
|
/// Returns the sum of products of respective elements in the given ranges.
|
55 |
|
|
/// \ingroup NumericAlgorithms
|
56 |
|
|
///
|
57 |
|
|
template <typename InputIterator1, typename InputIterator2, typename T,
|
58 |
|
|
typename BinaryOperation1, typename BinaryOperation2>
|
59 |
|
|
inline T inner_product
|
60 |
|
|
(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init,
|
61 |
|
|
BinaryOperation1 sumOp, BinaryOperation2 productOp)
|
62 |
|
|
{
|
63 |
|
|
while (first1 < last1)
|
64 |
|
|
init = sumOp (init, productOp (*first1++, *first2++));
|
65 |
|
|
return (init);
|
66 |
|
|
}
|
67 |
|
|
|
68 |
|
|
/// Writes result such that result[i] = sum (first...first+i)
|
69 |
|
|
/// \ingroup NumericAlgorithms
|
70 |
|
|
///
|
71 |
|
|
template <typename InputIterator, typename OutputIterator>
|
72 |
|
|
inline OutputIterator partial_sum (InputIterator first, InputIterator last, OutputIterator result)
|
73 |
|
|
{
|
74 |
|
|
if (first < last)
|
75 |
|
|
*result = *first++;
|
76 |
|
|
while (first < last)
|
77 |
|
|
*++result = *first++ + *result;
|
78 |
|
|
return (result);
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
/// Writes result such that result[i] = sumOp (first...first+i)
|
82 |
|
|
/// \ingroup NumericAlgorithms
|
83 |
|
|
///
|
84 |
|
|
template <typename InputIterator, typename OutputIterator, typename BinaryOperation>
|
85 |
|
|
inline OutputIterator partial_sum (InputIterator first, InputIterator last, OutputIterator result, BinaryOperation sumOp)
|
86 |
|
|
{
|
87 |
|
|
if (first < last)
|
88 |
|
|
*result = *first++;
|
89 |
|
|
while (first < last)
|
90 |
|
|
*++result = sumOp (*first++, *result);
|
91 |
|
|
return (result);
|
92 |
|
|
}
|
93 |
|
|
|
94 |
|
|
/// Writes result such that result[i] = first[i] - first[i - 1]
|
95 |
|
|
/// \ingroup NumericAlgorithms
|
96 |
|
|
///
|
97 |
|
|
template <typename InputIterator, typename OutputIterator>
|
98 |
|
|
inline OutputIterator adjacent_difference (InputIterator first, InputIterator last, OutputIterator result)
|
99 |
|
|
{
|
100 |
|
|
if (first < last)
|
101 |
|
|
*result++ = *first++;
|
102 |
|
|
while (first < last)
|
103 |
|
|
*result++ = *first - *(first - 1);
|
104 |
|
|
return (result);
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
/// Writes result such that result[i] = differenceOp (first[i], first[i - 1])
|
108 |
|
|
/// \ingroup NumericAlgorithms
|
109 |
|
|
///
|
110 |
|
|
template <typename InputIterator, typename OutputIterator, typename BinaryOperation>
|
111 |
|
|
inline OutputIterator adjacent_difference (InputIterator first, InputIterator last, OutputIterator result, BinaryOperation differenceOp)
|
112 |
|
|
{
|
113 |
|
|
if (first < last)
|
114 |
|
|
*result++ = *first++;
|
115 |
|
|
while (first < last)
|
116 |
|
|
*result++ = differenceOp (*first, *(first - 1));
|
117 |
|
|
return (result);
|
118 |
|
|
}
|
119 |
|
|
|
120 |
|
|
/// \brief Returns x^n.
|
121 |
|
|
/// Donald Knuth's Russian Peasant algorithm.
|
122 |
|
|
/// \ingroup NumericAlgorithms
|
123 |
|
|
///
|
124 |
|
|
template <typename T>
|
125 |
|
|
inline T power (T x, unsigned n)
|
126 |
|
|
{
|
127 |
|
|
T result (n % 2 ? x : 1);
|
128 |
|
|
while (n /= 2) {
|
129 |
|
|
x *= x;
|
130 |
|
|
if (n % 2)
|
131 |
|
|
result *= x;
|
132 |
|
|
}
|
133 |
|
|
return (result);
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
/// \brief Returns x^n, using \p op instead of multiplication.
|
137 |
|
|
/// Donald Knuth's Russian Peasant algorithm.
|
138 |
|
|
/// \ingroup NumericAlgorithms
|
139 |
|
|
///
|
140 |
|
|
template <typename T, typename BinaryOperation>
|
141 |
|
|
inline T power (T x, unsigned n, BinaryOperation op)
|
142 |
|
|
{
|
143 |
|
|
T result (n % 2 ? x : 1);
|
144 |
|
|
while (n /= 2) {
|
145 |
|
|
x = op (x, x);
|
146 |
|
|
if (n % 2)
|
147 |
|
|
result = op (result, x);
|
148 |
|
|
}
|
149 |
|
|
return (result);
|
150 |
|
|
}
|
151 |
|
|
|
152 |
|
|
} // namespace ustl
|
153 |
|
|
|
154 |
|
|
#endif
|