-
Notifications
You must be signed in to change notification settings - Fork 0
/
Polynomial.cpp
314 lines (293 loc) · 7.36 KB
/
Polynomial.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
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
#include <iostream>
#include <cmath>
#include "Polynomial.h"
/*
Creates 0 polynomial with specific ordering.
Valid options are "lex", "grlex", and "grevlex".
*/
template<unsigned int N>
Polynomial<N>::Polynomial(const char* monomialOrdering) : ordering(monomialOrdering)
{
terms.emplace_back(Term<N>());
}
/*
Creates Polynomial object from a string. Example:
Polynomial("lex", "-x^3y^2z + 3z^3 - xy + z");
*/
template<unsigned int N>
Polynomial<N>::Polynomial(const char* polynomial, const char* monomialOrdering) : ordering(monomialOrdering)
{
for (int i = 0; polynomial[i] != '\0';)
{
bool detectedTerm = false;
bool minus = false;
int coefficient = 1;
std::array<unsigned int, N> alpha{};
// Increments forward until start of new term is detected
while (polynomial[i] != '-' && (polynomial[i] < 'z' - N + 1 || polynomial[i] > 'z') && (polynomial[i] < '1' || polynomial[i] > '9') && polynomial[i] != '\0') ++i;
// Stores minus sign on coefficient
if (polynomial[i] == '-')
{
minus = true;
++i;
}
// Stores coefficient and powers on x,y,z
while (polynomial[i] != '+' && polynomial[i] != '-' && polynomial[i] != '\0')
{
if (polynomial[i] >= '1' && polynomial[i] <= '9')
{
coefficient = 0;
detectedTerm = true;
std::vector<int> digits = { polynomial[i] - '0' };
++i;
while (polynomial[i] >= '0' && polynomial[i] <= '9')
{
digits.emplace_back(polynomial[i] - '0');
++i;
}
unsigned int K = digits.size();
for (unsigned int i = 0; i < K; ++i)
coefficient += digits[K - i - 1] * pow(10, i);
}
else if (polynomial[i] >= 'z' - N + 1 && polynomial[i] <= 'z')
{
detectedTerm = true;
char variable = polynomial[i];
++i;
if (polynomial[i] != '^') alpha[variable - ('z' - N + 1)] = 1;
else
{
++i;
if (polynomial[i] >= '1' && polynomial[i] <= '9')
{
int exponent = 0;
std::vector<int> digits = { polynomial[i] - '0' };
++i;
while (polynomial[i] >= '0' && polynomial[i] <= '9')
{
digits.emplace_back(polynomial[i] - '0');
++i;
}
unsigned int K = digits.size();
for (unsigned int i = 0; i < K; ++i)
exponent += digits[K - i - 1] * pow(10, i);
alpha[variable - ('z' - N + 1)] = exponent;
}
}
}
else ++i;
}
// Adds term to polynomial
if (detectedTerm)
{
if (minus) coefficient *= -1;
*this += Term<N>(coefficient, alpha);
}
}
}
template<unsigned int N>
const char* Polynomial<N>::getOrdering() const
{
return ordering;
}
template<unsigned int N>
Term<N> Polynomial<N>::leadingTerm() const
{
return terms[0];
}
template<unsigned int N>
void Polynomial<N>::operator+=(const Term<N>& term)
{
terms.emplace_back(term);
sortTerms();
reduceTerms();
}
template<unsigned int N>
void Polynomial<N>::operator-=(Term<N> term)
{
term.c *= -1;
terms.emplace_back(term);
sortTerms();
reduceTerms();
}
template<unsigned int N>
void Polynomial<N>::operator-=(const Polynomial<N>& polynomial)
{
for (unsigned int i = 0; i < polynomial.terms.size(); ++i)
*this -= polynomial.terms[i];
}
template<unsigned int N>
Polynomial<N> Polynomial<N>::operator-(const Polynomial<N>& polynomial) const
{
Polynomial p = *this;
for (unsigned int i = 0; i < polynomial.terms.size(); ++i)
p -= polynomial.terms[i];
return p;
}
template<unsigned int N>
Polynomial<N> Polynomial<N>::operator*(const Term<N>& monomial) const
{
Polynomial p = Polynomial(ordering);
for (unsigned int i = 0; i < terms.size(); ++i)
{
p += terms[i] * monomial;
}
return p;
}
/*
Prints out full polynomial.
*/
template<unsigned int N>
void Polynomial<N>::printP() const
{
if (terms[0].totalOrder() == 0 || abs(terms[0].c) != 1)
std::cout << terms[0].c;
else if (terms[0].c == -1)
std::cout << "-";
if (terms[0].c < -1.0e-14 || terms[0].c > 1.0e-14)
printM(terms[0]);
for (unsigned int i = 1; i < terms.size(); ++i)
{
if (terms[i].c > 0)
std::cout << " + ";
else
std::cout << " - ";
if (terms[i].totalOrder() == 0 || abs(terms[i].c) != 1)
std::cout << abs(terms[i].c);
printM(terms[i]);
}
std::cout << std::endl;
}
template<unsigned int N>
bool Polynomial<N>::isZero() const
{
if (leadingTerm().c > -1.0e-14 && leadingTerm().c < 1.0e-14)
{
return true;
}
return false;
}
/*
Sorts terms using bubble sort. How it's sorted depends
on the chosen polynomial ordering.
*/
template<unsigned int N>
void Polynomial<N>::sortTerms()
{
bool sorted = false;
while (!sorted)
{
int swaps = 0;
for (unsigned int i = 0; i < terms.size() - 1; ++i)
{
// Swap elements if they are not in the correct order
bool needSwap = false;
std::array<int, N> diff{};
for (int j = 0; j < N; ++j)
diff[j] = terms[i][j] - terms[i + 1][j];
if (ordering == "lex")
{
for (int i = 0; i < N; ++i)
{
if (diff[i] < 0)
{
needSwap = true;
break;
}
else if (diff[i] > 0)
break;
}
}
else if (ordering == "grlex")
{
if (terms[i + 1].totalOrder() > terms[i].totalOrder())
needSwap = true;
else if (terms[i + 1].totalOrder() == terms[i].totalOrder())
{
for (int i = 0; i < N; ++i)
{
if (diff[i] < 0)
{
needSwap = true;
break;
}
else if (diff[i] > 0)
break;
}
}
}
else if (ordering == "grevlex")
{
if (terms[i + 1].totalOrder() > terms[i].totalOrder())
needSwap = true;
else if (terms[i + 1].totalOrder() == terms[i].totalOrder())
{
for (int i = N - 1; i >= 0; --i)
{
if (diff[i] > 0)
{
needSwap = true;
break;
}
else if (diff[i] < 0)
break;
}
}
}
if (needSwap)
{
Term<N> tmp = terms[i];
terms[i] = terms[i + 1];
terms[i + 1] = tmp;
swaps++;
}
}
if (swaps == 0)
sorted = true;
}
}
/*
Adds like terms together and removes terms that are 0
if the polynomial has n > 1 terms.
*/
template<unsigned int N>
void Polynomial<N>::reduceTerms()
{
// Adds like terms
for (unsigned int i = 0; i < terms.size() - 1; ++i)
{
if (terms[i + 1].degree() == terms[i].degree())
{
terms[i + 1].c += terms[i].c;
terms.erase(terms.begin() + i);
--i;
}
}
// Removes terms if they are 0
for (unsigned int i = 0; i < terms.size(); ++i)
{
// if (terms[i].c > -1.0e-14 && terms[i].c < 1.0e-14 && terms.size() > 1)
if (terms[i].c == 0 && terms.size() > 1)
{
terms.erase(terms.begin() + i);
--i;
}
}
}
/*
Helper function that prints out a single monomial.
*/
template<unsigned int N>
void Polynomial<N>::printM(const Term<N>& t) const
{
for (int i = 0; i < N; ++i)
{
char var = 'z' - N + i + 1;
if (t(i) > 0)
std::cout << var;
if (t(i) > 1)
std::cout << "^" << t(i);
}
}
template class Polynomial<3>;
template class Polynomial<26>;