-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhmathfunctionsequence.cpp
More file actions
112 lines (85 loc) · 1.76 KB
/
hmathfunctionsequence.cpp
File metadata and controls
112 lines (85 loc) · 1.76 KB
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
#include "hmathfunctionsequence.h"
#include <algorithm>
#include <iostream>
namespace hmath
{
FunctionSequence::FunctionSequence(std::initializer_list<TFunc1> list)
{
functions.reserve(list.size());
for (auto func : list)
{
if (!func)
{
using namespace std;
cerr << "[FunctionSequnece][Error] ctr: Null function found!" << endl;
continue;
}
functions.push_back(func);
}
}
void FunctionSequence::add(TFunc1 func)
{
if (!func)
{
using namespace std;
cerr << "[FunctionSequnece][Error] add: Null function found!" << endl;
return;
}
functions.push_back(func);
}
void FunctionSequence::clear()
{
functions.clear();
}
void FunctionSequence::empty()
{
std::vector<TFunc1>().swap(functions);
}
TFunc1 FunctionSequence::asFunction() const
{
return[*this](HReal value)->HReal
{
return get(value);
};
}
HReal FunctionSequence::get(HReal x) const
{
HReal result = x;
for (auto func : functions)
{
result = func(result);
}
return result;
}
HReal FunctionSequence::getSub(HReal x, int start, int end) const
{
start = truncateIndex(start);
end = truncateIndex(end);
HReal result = x;
for (int i = start; i < end; ++i)
{
result = functions[i](result);
}
return result;
}
int FunctionSequence::truncateIndex(int index) const
{
return std::clamp(index, 0, static_cast<int>(functions.size()));
}
#if DO_TEST
int FunctionSequence::DoTest(int& inOutTestCount,
std::vector<std::string>& outErrorMessages)
{
using namespace std;
int errorCount = 0;
{
cout << "[hmath][FunctionSequence][TC" << ++inOutTestCount << "] Basic tests " << endl;
auto squareFunc = [](HReal x) -> HReal { return x * x; };
FunctionSequence fSeqs;
for (int i = 0; i < 20; ++i)
fSeqs.add(squareFunc);
}
return errorCount;
}
#endif // DO_TEST
} // hmath