-
Notifications
You must be signed in to change notification settings - Fork 0
/
ml_numeric.h
135 lines (93 loc) · 3.17 KB
/
ml_numeric.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* @file ml_numeric.h
* @brief
* @author malin [email protected]
* @date 2015年04月12日 星期日 14时30分54秒
* @version
* @note
*/
#ifndef __ML_INTERNAL_NUMERIC_H
#define __ML_INTERNAL_NUMERIC_H
#include "ml_config.h"
#include "ml_iterator_base.h"
__ML_BEGIN_NAMESPACE
template<typename InputIterator, typename T>
T accumulate(InputIterator first, InputIterator last, T init)
{
for (; first != last; ++first) init = init + *first;
return init;
}
template<typename InputIterator, typename T, typename BinaryOperation>
T accumulate(InputIterator first, InputIterator last, T init, BinaryOperation op)
{
for (; first != last; ++first) init = op(init, *first);
return init;
}
template<typename InputIterator1, typename InputIterator2, typename T>
T inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init)
{
for (; first1 != last1; ++first1, ++first2) init = init + *first1 * *first2;
return init;
}
template<typename InputIterator1, typename InputIterator2, typename T, typename BinaryOperation1, typename BinaryOperation2>
T inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init, BinaryOperation1 op1,
BinaryOperation2 op2)
{
for (; first1 != last1; ++first1, ++first2) init = op1(init, op2(*first1, *first2));
return init;
}
template<typename InputIterator, typename OutputIterator>
OutputIterator partial_sum(InputIterator first, InputIterator last, OutputIterator dest)
{
if (first == last) return dest;
typename iterator_traits<InputIterator>::value_type value = *first;
*dest = *first;
while (++first != last)
{
value = value + *first; *++dest = value;
}
return ++dest;
}
template<typename InputIterator, typename OutputIterator, typename BinaryOperation>
OutputIterator partial_sum(InputIterator first, InputIterator last, OutputIterator dest, BinaryOperation op)
{
if (first == last) return dest;
typename iterator_traits<InputIterator>::value_type value = *first;
*dest = *first;
while (++ first != last)
{
value = op(value, *first); *++dest = value;
}
return ++dest;
}
template<typename InputIterator, typename OutputIterator>
OutputIterator adjacent_difference(InputIterator first, InputIterator last, OutputIterator dest)
{
if (first == last) return dest;
typedef typename iterator_traits<InputIterator>::value_type value_type;
value_type value = *first; *dest = *first;
while (++first != last)
{
value_type tmp = *first; *++dest = tmp - value; value = tmp;
}
return ++dest;
}
template<typename InputIterator, typename OutputIterator, typename BinaryOperation>
OutputIterator adjacent_difference(InputIterator first, InputIterator last, OutputIterator dest, BinaryOperation op)
{
if (first == last) return dest;
typedef typename iterator_traits<InputIterator>::value_type value_type;
value_type value = *first; *dest = *first;
while (++first != last)
{
value_type tmp = *first; *++dest = op(tmp, value); value = tmp;
}
return ++dest;
}
template<typename ForwardIterator, typename T>
void iota(ForwardIterator first, ForwardIterator last, T value)
{
for (; first != last; ++first) *first = value++;
}
__ML_END_NAMESPACE
#endif // __ML_INTERNAL_NUMERIC_H