-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolynomial.hpp
More file actions
372 lines (300 loc) · 10.5 KB
/
polynomial.hpp
File metadata and controls
372 lines (300 loc) · 10.5 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/**
* @file polynomial.hpp
* @brief Polynomial class for transfer function representation
*
* Supports polynomial arithmetic: +, -, *, evaluation, roots
*/
#ifndef CPPPLOT_CONTROL_POLYNOMIAL_HPP
#define CPPPLOT_CONTROL_POLYNOMIAL_HPP
// Define M_PI for Windows compatibility
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#include <vector>
#include <complex>
#include <cmath>
#include <algorithm>
#include <stdexcept>
#include <sstream>
#include <iomanip>
namespace cppplot {
namespace control {
/**
* @class Polynomial
* @brief Represents a polynomial: a_n*s^n + a_{n-1}*s^{n-1} + ... + a_1*s + a_0
*
* Coefficients are stored in descending order: [a_n, a_{n-1}, ..., a_1, a_0]
* This matches MATLAB/NumPy convention.
*/
class Polynomial {
public:
std::vector<double> coeffs; // Descending order: [a_n, ..., a_0]
// ============ Constructors ============
/// Default: constant 0
Polynomial() : coeffs({0}) {}
/// From coefficient vector (descending order)
explicit Polynomial(const std::vector<double>& c) : coeffs(c) {
if (coeffs.empty()) coeffs = {0};
normalize();
}
/// From initializer list: Polynomial p({1, 2, 1}) = s^2 + 2s + 1
Polynomial(std::initializer_list<double> c) : coeffs(c) {
if (coeffs.empty()) coeffs = {0};
normalize();
}
/// Constant polynomial
explicit Polynomial(double c) : coeffs({c}) {}
// ============ Basic Properties ============
/// Degree of polynomial
int degree() const {
return static_cast<int>(coeffs.size()) - 1;
}
/// Leading coefficient
double leading() const {
return coeffs.empty() ? 0 : coeffs[0];
}
/// Trailing coefficient (constant term)
double constant() const {
return coeffs.empty() ? 0 : coeffs.back();
}
/// Check if zero polynomial
bool isZero() const {
return coeffs.size() == 1 && std::abs(coeffs[0]) < 1e-15;
}
/// Check if constant polynomial
bool isConstant() const {
return coeffs.size() == 1;
}
// ============ Evaluation ============
/// Evaluate at real value using Horner's method
double operator()(double s) const {
double result = 0;
for (double c : coeffs) {
result = result * s + c;
}
return result;
}
/// Evaluate at complex value
std::complex<double> operator()(std::complex<double> s) const {
std::complex<double> result(0, 0);
for (double c : coeffs) {
result = result * s + c;
}
return result;
}
// ============ Arithmetic Operations ============
/// Addition
Polynomial operator+(const Polynomial& other) const {
size_t n1 = coeffs.size();
size_t n2 = other.coeffs.size();
size_t n = std::max(n1, n2);
std::vector<double> result(n, 0);
// Add from right (low order to high)
for (size_t i = 0; i < n1; ++i) {
result[n - n1 + i] += coeffs[i];
}
for (size_t i = 0; i < n2; ++i) {
result[n - n2 + i] += other.coeffs[i];
}
return Polynomial(result);
}
Polynomial operator+(double c) const {
Polynomial result = *this;
result.coeffs.back() += c;
return result;
}
/// Subtraction
Polynomial operator-(const Polynomial& other) const {
return *this + (other * (-1.0));
}
Polynomial operator-() const {
return *this * (-1.0);
}
/// Multiplication
Polynomial operator*(const Polynomial& other) const {
if (isZero() || other.isZero()) return Polynomial({0});
size_t n1 = coeffs.size();
size_t n2 = other.coeffs.size();
std::vector<double> result(n1 + n2 - 1, 0);
for (size_t i = 0; i < n1; ++i) {
for (size_t j = 0; j < n2; ++j) {
result[i + j] += coeffs[i] * other.coeffs[j];
}
}
return Polynomial(result);
}
Polynomial operator*(double c) const {
std::vector<double> result = coeffs;
for (double& coef : result) coef *= c;
return Polynomial(result);
}
/// Division (returns quotient, ignores remainder)
Polynomial operator/(const Polynomial& divisor) const {
if (divisor.isZero()) {
throw std::runtime_error("Polynomial division by zero");
}
if (degree() < divisor.degree()) {
return Polynomial({0});
}
std::vector<double> dividend = coeffs;
std::vector<double> quotient;
while (dividend.size() >= divisor.coeffs.size()) {
double factor = dividend[0] / divisor.coeffs[0];
quotient.push_back(factor);
for (size_t i = 0; i < divisor.coeffs.size(); ++i) {
dividend[i] -= factor * divisor.coeffs[i];
}
dividend.erase(dividend.begin());
}
return Polynomial(quotient);
}
Polynomial operator/(double c) const {
return *this * (1.0 / c);
}
// ============ Roots Finding ============
/**
* @brief Find roots of polynomial using companion matrix method
* @return Vector of complex roots
*/
std::vector<std::complex<double>> roots() const {
std::vector<std::complex<double>> result;
if (degree() <= 0) return result;
// Normalize
std::vector<double> c = coeffs;
double lead = c[0];
for (double& x : c) x /= lead;
if (degree() == 1) {
// Linear: s + a = 0 => s = -a
result.push_back(std::complex<double>(-c[1], 0));
return result;
}
if (degree() == 2) {
// Quadratic formula
double a = c[0], b = c[1], cc = c[2];
double disc = b * b - 4 * a * cc;
if (disc >= 0) {
result.push_back(std::complex<double>((-b + std::sqrt(disc)) / (2 * a), 0));
result.push_back(std::complex<double>((-b - std::sqrt(disc)) / (2 * a), 0));
} else {
double re = -b / (2 * a);
double im = std::sqrt(-disc) / (2 * a);
result.push_back(std::complex<double>(re, im));
result.push_back(std::complex<double>(re, -im));
}
return result;
}
// For higher degrees, use eigenvalue method (companion matrix)
// Simplified: use Durand-Kerner method for now
return durandKerner();
}
// ============ String Representation ============
std::string toString(const std::string& var = "s") const {
if (isZero()) return "0";
std::ostringstream oss;
bool first = true;
int deg = degree();
for (int i = 0; i <= deg; ++i) {
double c = coeffs[i];
int power = deg - i;
if (std::abs(c) < 1e-15) continue;
// Sign
if (!first) {
oss << (c >= 0 ? " + " : " - ");
c = std::abs(c);
} else if (c < 0) {
oss << "-";
c = -c;
}
first = false;
// Coefficient
if (power == 0 || std::abs(c - 1.0) > 1e-10) {
oss << std::setprecision(4) << c;
}
// Variable
if (power > 0) {
oss << var;
if (power > 1) oss << "^" << power;
}
}
return first ? "0" : oss.str();
}
private:
/// Remove leading zeros
void normalize() {
while (coeffs.size() > 1 && std::abs(coeffs[0]) < 1e-15) {
coeffs.erase(coeffs.begin());
}
}
/// Durand-Kerner root finding method
std::vector<std::complex<double>> durandKerner(int maxIter = 1000, double tol = 1e-10) const {
int n = degree();
if (n <= 0) return {};
// Normalize polynomial
std::vector<double> c = coeffs;
double lead = c[0];
for (double& x : c) x /= lead;
// Initial guesses on unit circle
std::vector<std::complex<double>> z(n);
for (int i = 0; i < n; ++i) {
double angle = 2.0 * M_PI * i / n + 0.1;
double r = 1.0 + 0.1 * i;
z[i] = std::complex<double>(r * std::cos(angle), r * std::sin(angle));
}
// Iteration
for (int iter = 0; iter < maxIter; ++iter) {
double maxChange = 0;
for (int i = 0; i < n; ++i) {
// Evaluate polynomial at z[i]
std::complex<double> p = (*this)(z[i]) / lead;
// Product of (z[i] - z[j]) for j != i
std::complex<double> denom(1, 0);
for (int j = 0; j < n; ++j) {
if (i != j) {
denom *= (z[i] - z[j]);
}
}
std::complex<double> delta = p / denom;
z[i] -= delta;
maxChange = std::max(maxChange, std::abs(delta));
}
if (maxChange < tol) break;
}
return z;
}
};
// ============ Non-member operators ============
inline Polynomial operator+(double c, const Polynomial& p) {
return p + c;
}
inline Polynomial operator*(double c, const Polynomial& p) {
return p * c;
}
/**
* @brief Create polynomial from roots: (s - r1)(s - r2)...
*/
inline Polynomial poly(const std::vector<std::complex<double>>& roots) {
Polynomial result({1});
for (const auto& r : roots) {
if (std::abs(r.imag()) < 1e-10) {
// Real root: (s - r)
result = result * Polynomial({1, -r.real()});
} else if (r.imag() > 0) {
// Complex conjugate pair: (s - r)(s - r*) = s^2 - 2*Re(r)*s + |r|^2
double re = r.real();
double mag2 = std::norm(r);
result = result * Polynomial({1, -2*re, mag2});
}
// Skip conjugates (imag < 0) as they're already handled
}
return result;
}
inline Polynomial poly(const std::vector<double>& realRoots) {
Polynomial result({1});
for (double r : realRoots) {
result = result * Polynomial({1, -r});
}
return result;
}
} // namespace control
} // namespace cppplot
#endif // CPPPLOT_CONTROL_POLYNOMIAL_HPP