forked from IS-UMK/lemkis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolynomial.hpp
250 lines (234 loc) · 6.81 KB
/
polynomial.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
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
#pragma once
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <print>
#include <set>
#include <stdexcept>
#include <utility>
#include <vector>
namespace polynomial {
template <typename T>
struct polynomial {
public:
std::vector<T> coefficients{};
std::size_t degree{};
public:
auto operator+=(const polynomial &p) -> polynomial & {
if (p.degree > degree) {
coefficients.resize(p.degree + 1, T{0});
}
for (std::size_t i = 0; i <= p.degree; ++i) {
coefficients[i] += p.coefficients[i];
if (coefficients[i] != 0) {
degree = i;
}
}
return *this;
}
auto operator-() const {
auto copy{*this};
for (auto &c : copy.coefficients) {
c *= -1;
}
return copy;
}
auto operator-=(const polynomial &p) -> polynomial & {
return operator+=(-p);
}
auto operator*=(const polynomial &p) -> polynomial & {
std::vector<T> multiplication_vector{};
multiplication_vector.resize(p.degree + degree + 1, T{0});
coefficients.resize(p.degree + degree + 1, T{0});
for (std::size_t i = degree + 1; i > 0; i--) {
for (std::size_t j = p.degree + 1; j > 0; j--) {
multiplication_vector[i + j - 2] +=
coefficients[i - 1] * p.coefficients[j - 1];
}
}
coefficients = multiplication_vector;
degree += p.degree;
return *this;
}
auto operator/=(const polynomial &p) -> polynomial & {
if (p.degree == 0 && p.coefficients[0] == T{0}) {
throw std::invalid_argument("Division by zero polynomial");
} else if (p.degree > degree) {
throw std::invalid_argument("Division by invalid polynomial");
}
std::vector<T> division_vector{};
division_vector.resize(degree - p.degree + 1, T{0});
for (std::size_t i = degree; i > p.degree - 1; i--) {
division_vector[i - p.degree] =
coefficients[i] / p.coefficients[p.degree];
if (i - 1 > 0) {
for (std::size_t j = p.degree + 1; j > 0; j--) {
coefficients[j + i - p.degree - 1] -=
division_vector[i - p.degree] * p.coefficients[j - 1];
}
}
}
coefficients.resize(degree - p.degree + 1, T{0});
degree -= p.degree;
coefficients = division_vector;
return *this;
}
auto operator%=(const polynomial &p) -> polynomial & {
if (p.degree == 0 && p.coefficients[0] == T{0}) {
throw std::invalid_argument("Division by zero polynomial");
}
polynomial quotient;
polynomial remainder = *this;
quotient = *this / p;
remainder -= quotient * p;
*this = remainder;
return *this;
}
public:
auto operator()(T value) {
T result = 0;
for (int i = this->degree; i >= 0; --i) {
result = result * value + this->coefficients[i];
}
return result;
}
};
template <typename T>
inline auto operator+(polynomial<T> p, polynomial<T> q) {
p += q;
return p;
}
template <typename T>
inline auto operator-(polynomial<T> p, polynomial<T> q) {
p -= q;
return p;
}
template <typename T>
inline auto operator*(polynomial<T> p, polynomial<T> q) {
p *= q;
return p;
}
template <typename T>
inline auto operator/(polynomial<T> p, polynomial<T> q) {
p /= q;
return p;
}
template <typename T>
inline auto operator%(polynomial<T> p, polynomial<T> q) {
p %= q;
return p;
}
template <typename T>
inline auto divide(polynomial<T> p, polynomial<T> q)
-> std::pair<polynomial<T>, polynomial<T>> {
return {p / q, p % q};
}
template <typename T>
inline auto root_rational_candidates(polynomial<T> p) -> std::set<T> {
size_t leading_quotient = p.coefficients[p.degree];
std::vector<T> factor_of_last_term{};
std::vector<T> factor_of_first_term{};
std::set<T> factors{};
for (std::size_t i = 1; i <= leading_quotient; i++) {
if (leading_quotient % i == 0) {
factor_of_last_term.push_back(i);
}
}
for (std::size_t i = 1; i <= static_cast<std::size_t>(p.coefficients[0]);
i++) {
if (static_cast<int>(p.coefficients[0]) % static_cast<int>(i) == 0) {
factor_of_first_term.push_back(i);
}
}
for (std::size_t i = 0; i < factor_of_first_term.size(); i++) {
for (std::size_t j = 0; j < factor_of_last_term.size(); j++) {
factors.emplace(factor_of_first_term[i] / factor_of_last_term[j]);
}
}
return factors;
}
template <typename T>
auto gcd(polynomial<T> p, polynomial<T> q)
-> std::pair<polynomial<T>, std::vector<T>> {
std::vector<T> steps;
while (q.degree < p.degree) {
auto [quotient, remainder] = divide(p, q);
p = q;
q = remainder;
steps.push_back(p.degree);
}
return {p, steps};
}
template <typename T>
std::ostream &operator<<(std::ostream &os, const polynomial<T> &p) {
for (std::size_t i = p.degree; i > 0; --i) {
os << p.coefficients[i] << "x^" << i << " + ";
}
os << p.coefficients[0];
return os;
}
}
template <typename T>
void performoperation(const polynomial::polynomial<T> &p1,
const polynomial::polynomial<T> &p2,
const std::string &operation) {
polynomial::polynomial<T> result;
switch (operation[0]) {
case '+':
result = p1 + p2;
break;
case '-':
result = p1 - p2;
break;
case '*':
result = p1 * p2;
break;
case '/':
result = p1 / p2;
break;
case '%':
result = p1 % p2;
break;
default:
std::cerr << "Unsupported operation: " << operation << std::endl;
return;
}
std::cout << operation << " result: ";
for (auto coeff : result.coefficients) {
std::cout << coeff << " ";
}
std::cout << std::endl;
}
void example() {
polynomial::polynomial<double> p1{{3, 5, 4}}, p2{{1, 1}};
p1.degree = 2;
p2.degree = 1;
std::cout << "This program performs operations on two polynomials" << std::endl;
std::cout << "Polynomial p = " << p1 << ", polynomial q = " << p2
<< std::endl;
performoperation(p1, p2, "+");
performoperation(p1, p2, "-");
performoperation(p1, p2, "*");
performoperation(p1, p2, "/");
performoperation(p1, p2, "%");
auto [quotient, remainder] = polynomial::divide(p1, p2);
auto print_coeffs = [](const auto &coeffs) {
for (auto coeff : coeffs) std::cout << coeff << " ";
std::cout << std::endl;
};
std::cout << "Division result (Quotient): ";
print_coeffs(quotient.coefficients);
std::cout << "Division result (Remainder): ";
print_coeffs(remainder.coefficients);
double value = 2.0;
std::cout << "Evaluation at x = " << value << ": " << p1(value) << std::endl;
std::cout << "Root rational candidates: ";
for (auto root : polynomial::root_rational_candidates(p1))
std::cout << root << " ";
std::cout << std::endl
<< "GCD: " << polynomial::gcd(p1, p2).first << std::endl;
std::cout << "Extended Euclidean Algorithm steps: ";
for (auto &val : polynomial::gcd(p1, p2).second) std::cout << val << " ";
std::cout << std::endl;
}