-
Notifications
You must be signed in to change notification settings - Fork 3
/
polynomials.hpp
113 lines (93 loc) · 2.92 KB
/
polynomials.hpp
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
#ifndef POLYNOMIALS_HPP
#define POLYNOMIALS_HPP
#include "utils.hpp"
template<size_t degree>
class Polynomial {
public:
constexpr explicit Polynomial(const std::array<double, degree+1>& coefficients = {}) : coefficients(coefficients) {}
constexpr double get_value(const double x) const {
double ans = 0;
double variable = 1;
for (auto it= coefficients.rbegin(); it != coefficients.rend(); it++) {
ans += (*it) * variable;
variable *= x;
}
return ans;
}
constexpr std::array<double, degree + 1> get_component_value(const double x) const {
std::array<double, degree + 1> ans;
double variable = 1;
for (int i = degree; i >= 0; i--) {
ans[i] = coefficients[i] * variable;
variable *= x;
}
return ans;
}
constexpr Polynomial<degree - 1> get_derivative () const {
std::array<double, degree> new_coefficients;
for (int i = 0; i < degree; i++) {
new_coefficients[i] = coefficients[i] * (degree - i);
}
return Polynomial<degree - 1>(new_coefficients);
}
constexpr std::array<double, degree + 1> get_coefficients () const {
return coefficients;
}
constexpr size_t get_degree() const {
return degree;
}
void print() const {
for (int i = 0; i < degree; i++) {
std::cout << coefficients[i] << "x" << degree - i << " + ";
}
std::cout << coefficients[degree] << '\n';
}
private:
std::array<double, degree + 1> coefficients;
};
template<>
class Polynomial<0> {
public:
constexpr explicit Polynomial(const std::array<double, 1>& coefficients = {0}) : coefficients(coefficients) {}
constexpr double get_value(const double x) const {
return coefficients[0];
}
constexpr std::array<double, 1> get_component_value(const double x) const {
return coefficients;
}
constexpr Polynomial<0> get_derivative () const {
return Polynomial<0>({0});
}
constexpr std::array<double, 1> get_coefficients () const {
return coefficients;
}
constexpr size_t get_degree() const {
return 0;
}
void print() const {
std::cout << coefficients[0] << '\n';
}
private:
std::array<double, 1> coefficients;
};
template<size_t degree>
constexpr std::array<std::array<double, degree+1>, degree+1> get_coefficients_of_poly_and_all_derivatives(const Polynomial<degree>& poly) {
std::array<std::array<double, degree+1>, degree+1> ans;
ans[0] = poly.get_coefficients();
const auto poly_d = poly.get_derivative();
const auto lower_order_coeffs = get_coefficients_of_poly_and_all_derivatives(poly_d);
for (int row = 1; row < degree+1; row++) {
ans[row][0] = 0;
}
for (int row = 1; row < degree+1; row++) {
for (int col = 1; col < degree+1; col++) {
ans[row][col] = lower_order_coeffs[row - 1][col-1];
}
}
return ans;
}
template<>
constexpr std::array<std::array<double, 1>, 1> get_coefficients_of_poly_and_all_derivatives<0>(const Polynomial<0>& poly) {
return {0};
}
#endif // POLYNOMIALS_HPP