-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.cpp
222 lines (176 loc) · 5.77 KB
/
backend.cpp
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "backend.h"
#include "sofunction.h"
#include <boost/multiprecision/cpp_dec_float.hpp>
#include "solvers/regulafalsi.h"
#include "solvers/secant.h"
#include "solvers/bisection.h"
#include <iomanip>
#include <fenv.h>
Backend::Backend() {
}
Backend::~Backend() {
delete function;
}
std::string Backend::intervalToString(interval x, int decimals) {
std::stringstream str;
str << std::scientific;
str << std::setprecision(decimals);
if (singleton(x)) {
str << "[" << median(x) << "]";
} else {
int old_rounding = fegetround();
fesetround(FE_DOWNWARD);
str << "[" << x.lower() << ", ";
fesetround(FE_UPWARD);
str << x.upper() << "]";
fesetround(old_rounding);
}
return str.str();
}
long double Backend::stringToFloat(const std::string &value) {
if (value.find("bla") != std::string::npos) {
throw "Nie tym razem, panie profesorze ;)";
}
try {
return std::stold(value);
} catch (std::invalid_argument &error) {
throw "Nie udało się zinterpretować wpisanych danych jako liczbę!";
}
}
interval Backend::stringToInterval(const std::string &value, char separator) {
size_t split_pos = value.find(separator);
std::string left_str, right_str;
boost::multiprecision::cpp_dec_float_50 left_mp, right_mp;
long double left, right;
if (split_pos == std::string::npos) {
left_str = right_str = value;
} else {
// przedział
left_str = value.substr(0, split_pos);
right_str = value.substr(split_pos + 1);
}
try {
left_mp.assign(left_str);
right_mp.assign(right_str);
} catch (std::runtime_error &error) {
throw "Nie udało się zinterpretować wpisanych danych jako liczbę!";
}
int old_rounding = fegetround();
fesetround(FE_DOWNWARD);
left = left_mp.convert_to<long double>();
fesetround(FE_UPWARD);
right = right_mp.convert_to<long double>();
fesetround(old_rounding);
return interval(left, right);
}
struct SingleFloatSummary Backend::floatSummary(long double solution, std::string more) {
struct SingleFloatSummary out;
std::stringstream str;
str << std::scientific;
str << std::setprecision(decimals);
str << solution;
out.x = str.str();
str.str(std::string());
long double y = function->evaluate(solution);
str << y;
out.y = str.str();
str.str(std::string());
out.more = more;
return out;
}
struct SingleIntervalSummary Backend::intervalSummary(interval solution, std::string more) {
struct SingleIntervalSummary out;
std::stringstream str;
str << std::scientific;
str << std::setprecision(decimals);
out.x = intervalToString(solution, decimals);
str << median(solution);
out.median = str.str();
str.str(std::string());
int old_rounding = fegetround();
fesetround(FE_UPWARD);
str << upper(solution) - lower(solution);
out.width = str.str();
str.str(std::string());
fesetround(old_rounding);
interval y = function->evaluate(solution);
out.y = intervalToString(y, decimals);
out.more = more;
return out;
}
void Backend::loadFunction(char filename[]) {
Function *new_function;
new_function = new SOFunction(filename);
if (function != nullptr) {
delete function;
}
function = new_function;
}
struct FloatSummary Backend::solveFloatingPoint(const std::string &a_str, const std::string &b_str) {
long double a, b, x;
a = stringToFloat(a_str);
b = stringToFloat(b_str);
struct FloatSummary out;
bool secant_only = false;
try {
check_interval(a, b, function, true);
} catch (int err) {
if (err == NO_REAL_ROOTS) {
secant_only = true;
}
}
try {
x = Secant(a, b, function);
out.secant = floatSummary(x);
if (!secant_only) {
x = RegulaFalsi(a, b, function);
out.regulafalsi = floatSummary(x);
bool reached;
x = Bisection(a, b, function, bisectionTolerance, bisectionIterations, reached);
out.bisection = floatSummary(x, std::string("reached = ")+(reached?"true":"false"));
}
} catch(int err) {
if (err == WRONG_INTERVAL) {
throw "Lewy koniec przedziału musi być mniejszy od prawego końca!";
} else if (err == NO_REAL_ROOTS) {
throw "Brak rozwiązań rzeczywistych w tym przedziale. Upewnij się, że f(a) * f(b) < 0";
}
}
return out;
}
struct IntervalSummary Backend::solveInterval(const std::string &a_str, const std::string &b_str) {
interval a, b, x;
try {
a = stringToInterval(a_str);
b = stringToInterval(b_str);
} catch (std::runtime_error err) {
throw "Nie można zbudować takiego przedziału!";
}
struct IntervalSummary out;
bool secant_only = false;
try {
check_interval(a, b, function, true);
} catch (int err) {
if (err == NO_REAL_ROOTS) {
secant_only = true;
}
}
try {
x = Secant(a, b, function);
out.secant = intervalSummary(x);
if (!secant_only) {
bool reached;
x = Bisection(a, b, function, bisectionTolerance, bisectionIterations, reached);
out.bisection = intervalSummary(x, std::string("reached = ")+(reached?"true":"false"));
x = RegulaFalsi(a, b, function);
out.regulafalsi = intervalSummary(x);
}
} catch(int err) {
if (err == WRONG_INTERVAL) {
throw "Lewy koniec przedziału musi być mniejszy od prawego końca!";
} else if (err == NO_REAL_ROOTS) {
throw "Możliwy brak rozwiązań rzeczywistych w tym przedziale. Upewnij się, że f(a) * f(b) < 0";
}
}
return out;
}