-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpolynomial.cpp
More file actions
209 lines (181 loc) · 7.26 KB
/
polynomial.cpp
File metadata and controls
209 lines (181 loc) · 7.26 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
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
#include "polynomial.h"
#include <QDebug>
#include <QPolygonF>
#include <array>
#include <cmath>
#include <utility>
#include <vector>
template <index_t Size, class T = ld>
using array = std::array<T, Size>;
template <index_t N = 0>
struct It {
};
struct calcDegrees_i {
virtual std::vector<ld> calcDegrees(const Data&) = 0;
};
// #define __CT__ // Скорее это бенчмарк крмпилятора,а не алгоритма.
#ifdef __CT__
template <index_t D>
struct calcDegreesCt final : calcDegrees_i {
std::vector<ld> calcDegrees(const Data& data) override {
constexpr index_t degree = D + 1;
Timer{__FUNCTION__};
array<degree, array<degree>> matrix{};
array<degree> y{};
array<degree> c{}; // результат
constexpr auto I = [](index_t I) { return D - I; };
constexpr auto J = [](index_t J) { return D - J; };
constexpr auto K = [](index_t K) { return D - K; };
{ // построение исходной матрицы
// Timer t { 1 };
auto forMatrix = [&]<index_t J, index_t... Is>(ld x, It<J>, Seq<Is...>) {
((matrix[Is][J] += pow(x, J + Is)), ...);
};
auto forY = [&]<index_t J>(It<J>) {
for(auto&& var: data) {
y[J] += pow(var.x(), J) * var.y();
forMatrix(var.x(), It<J>{}, MakeSeq<degree>{});
}
};
[&]<index_t... Js>(Seq<Js...>) { (forY(It<Js>{}), ...); }(MakeSeq<degree>{});
}
{ // преобразование матрицы системы уравнений в диагональную а-ля половинчатую матрицу
// Timer t { 2 };
auto for3 = [&]<index_t I, index_t K, index_t... Js>(It<I>, It<K>, ld&& Koef, Seq<Js...>) {
((matrix[Js][K] = (Js == I) ? ld{} : matrix[Js][K] * Koef - matrix[Js][I]), ...);
y[K] = y[K] * Koef - y[I];
};
auto for2 = [&]<index_t I, index_t... Ks>(It<I>, Seq<Ks...>) {
((matrix[I][K(Ks)] != 0 ? for3(It<I>{}, It<K(Ks)>{}, matrix[I][I] / matrix[I][K(Ks)], MakeSeq<degree>{}) : void(Ks)), ...);
};
[&]<index_t... I>(Seq<I...>) { (for2(It<I>{}, MakeSeq<D - I>{}), ...); }(MakeSeq<degree>{});
}
{ // поиск коэффициэнтов C
// Timer t { 3 };
auto for2 = [&]<index_t I, index_t... Js>(It<I>, Seq<Js...>) {
((c[I] -= matrix[J(Js)][I] * c[J(Js)] / matrix[I][I]), ...);
};
[&]<index_t... Is>(Seq<Is...>) {
((c[I(Is)] = y[I(Is)] / matrix[I(Is)][I(Is)], for2(It<I(Is)>{}, MakeSeq<D - I(Is)>{})), ...);
}(MakeSeq<degree>{});
std::vector<ld> coeff(degree);
for(int i = 0; i < degree; ++i)
coeff[i] = c[i];
return coeff;
}
}
};
template <index_t... Is>
auto arrayOfCalcCt(Seq<Is...>) {
static constexpr auto size = 8; // sizeof(calcDegreesCt<0>);
static char placeHolder[size * sizeof...(Is)] = {};
static calcDegrees_i* funcs[] = {new(placeHolder + Is * size) calcDegreesCt<Is>...};
return ((funcs));
};
#else
template <index_t D>
struct calcDegreesRt final : calcDegrees_i {
std::vector<ld> calcDegrees(const Data& data) override {
constexpr index_t degree = D + 1;
Timer{__FUNCTION__};
array<degree, array<degree>> matrix{};
array<degree> y{};
array<degree> c{}; // результат
{ // построение исходной матрицы
// Timer t { 5 };
for(index_t j = 0; j < degree; j++) {
for(auto&& var: data) {
y[j] += pow(var.x(), j) * var.y();
for(index_t i = 0; i < degree; i++) {
matrix[i][j] += pow(var.x(), j + i);
}
}
}
}
{ // преобразование матрицы системы уравнений в диагональную а-ля половинчатую матрицу
// Timer t { 6 };
for(index_t i = 0; i < degree; i++) {
for(index_t k = i + 1; k < degree; k++) {
if(matrix[i][k] != 0) {
ld Koef = matrix[i][i] / matrix[i][k];
for(index_t j = 0; j < degree; j++) {
matrix[j][k] = (j == i) ? ld{} : matrix[j][k] * Koef - matrix[j][i];
}
y[k] = y[k] * Koef - y[i];
}
}
}
}
{ // поиск коэффициэнтов C
// Timer t { 7 };
for(index_t i = degree - 1; i != std::numeric_limits<index_t>::max(); --i) {
c[i] = y[i] / matrix[i][i];
for(index_t j = i + 1; j < degree; ++j)
c[i] -= matrix[j][i] * c[j] / matrix[i][i];
}
std::vector<ld> coeff(degree);
for(int i = 0; i < degree; ++i)
coeff[i] = c[i];
return coeff;
}
}
};
template <index_t... Is>
auto arrayOfCalcRt(Seq<Is...>) {
static constexpr auto size = 8; // sizeof(calcDegreesRt<0>);
static char placeHolder[size * sizeof...(Is)] = {};
static calcDegrees_i* funcs[] = {new(placeHolder + Is * size) calcDegreesRt<Is>...};
return ((funcs));
};
#endif
ld Polynomial::calcPoly(ld x, index_t size) {
if(!degrees_.size())
return {};
return (size < degrees_.size() - 2) ? degrees_[size] + x * calcPoly(x, size + 1)
: degrees_[size] + x * degrees_[size + 1];
}
Polynomial::Polynomial() { }
void Polynomial::addData(double x, double y) { data_.push_back({x, y}); }
void Polynomial::addData(const DataPoint& xy) { data_.push_back(xy); }
void Polynomial::setData(const Data& xy) { data_ = xy; }
void Polynomial::clear() {
degrees_.clear();
data_.clear();
}
void Polynomial::calcDegrees(index_t D) {
if(D >= MaxDegree + 1 || D < 1)
return;
try {
#ifdef __CT__
degrees_ = arrayOfCalcCt(MakeSeq<MaxDegree + 1>{})[D]->calcDegrees(data_);
#else
degrees_ = arrayOfCalcRt(MakeSeq<MaxDegree + 1>{})[D]->calcDegrees(data_);
#endif
} catch(...) {
return;
}
emit degreesChanged(degrees());
emit dataChanged(calcData(data_));
emit deltaChanged(delta_);
}
Data Polynomial::calcData(Data in) {
delta_.clear();
delta_.reserve(in.size());
for(auto&& point: in) {
delta_.push_back({point.x(), static_cast<double>(calcPoly(point.x()))});
delta_.back().ry() -= point.y();
point.ry() = calcPoly(point.x());
}
return in;
}
Degrees Polynomial::degrees() const {
Degrees copy(degrees_.size(), 0.0);
for(size_t i = 0; i < degrees_.size(); ++i)
copy[i] = degrees_[i];
return copy;
}
void Polynomial::setDegrees(const Degrees& degrees) {
degrees_.resize(degrees.size());
for(size_t i = 0; i < degrees_.size(); ++i)
degrees_[i] = degrees[i];
}