-
Notifications
You must be signed in to change notification settings - Fork 2
/
mathutil.h
79 lines (63 loc) · 1.1 KB
/
mathutil.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
#ifndef MATHUTIL_H
#define MATHUTIL_H
#include <limits>
#include <vector>
template<class T, class U>
class Harmonic_Rec
{
public:
typedef T data_t;
T operator()()
{
return T(0);
}
T operator()(T last, U curr)
{
return last + T(1)/T(curr);
}
};
template<class T, class U>
class Harmonic_2_Rec
{
public:
typedef T data_t;
T operator()()
{
return T(0);
}
T operator()(T last, U curr)
{
return last + T(1)/T(curr*curr);
}
};
template<class OP>
class RecursiveSeries
{
public:
typedef typename OP::data_t data_t;
protected:
std::vector<data_t> _data;
OP _op;
public:
RecursiveSeries()
: _data(1)
{
_data[0] = _op();
}
template<class I>
data_t operator()(I n)
{
for (size_t i=_data.size(); i<=n; i++)
_data.push_back(_op(_data.back(), i));
return _data[n];
}
};
typedef RecursiveSeries<Harmonic_Rec<double, int> > Harmonic;
typedef RecursiveSeries<Harmonic_2_Rec<double, int> > Harmonic_2;
inline double undefined()
{
return std::numeric_limits<double>::quiet_NaN();
}
template<class I>
I num_pairs(I n) { return n*(n-1)/2; }
#endif // MATHUTIL_H