-
Notifications
You must be signed in to change notification settings - Fork 0
/
polynomial_linked_list.cpp
524 lines (478 loc) · 15.6 KB
/
polynomial_linked_list.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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#ifndef __POLYNOMIAL_LINKED_LIST_
#define __POLYNOMIAL_LINKED_LIST_
/*****************************************************************************\
* This file is part of DynGB. *
* *
* DynGB is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 2 of the License, or *
* (at your option) any later version. *
* *
* DynGB is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with DynGB. If not, see <http://www.gnu.org/licenses/>. *
\*****************************************************************************/
#include "polynomial_linked_list.hpp"
/**
@brief memory manager for Monomial_Node
@ingroup memorygroup
@details Automatically initialized, but clients need to call the destructor
when finished.
*/
Grading_Order_Data_Allocator<Monomial_Node> * monododa = nullptr;
void * Monomial_Node::operator new(size_t size) {
if (monododa == nullptr) monododa = new Grading_Order_Data_Allocator<Monomial_Node>(size);
Monomial_Node * result = monododa->get_new_block();
return result;
}
void Monomial_Node::operator delete(void *t) {
monododa->return_used_block(static_cast<Monomial_Node *>(t));
}
Monomial_Node::Monomial_Node(const Prime_Field_Element & a, const Monomial & u)
: t(u), c(a)
{ }
Monomial_Node::Monomial_Node(Prime_Field & F, const Monomial & u)
: t(u), c(F.unity())
{}
Monomial & Monomial_Node::monomial() { return t; }
Prime_Field_Element & Monomial_Node::coefficient() { return c; }
bool LLPolynomial_Iterator::canMoveRight() const {
return iter_curr != nullptr and iter_curr->next != nullptr;
}
bool LLPolynomial_Iterator::canMoveLeft() const {
return iter_curr != nullptr and iter_curr->prev != nullptr;
}
bool LLPolynomial_Iterator::fellOff() const { return iter_curr == nullptr; }
const Monomial & LLPolynomial_Iterator::currMonomial() const {
return iter_curr->t;
}
const Prime_Field_Element & LLPolynomial_Iterator::currCoeff() const {
return iter_curr->c;
}
void LLPolynomial_Iterator::set_currCoeff(const Prime_Field_Element & a) {
if (!fellOff()) iter_curr->c = a;
}
void LLPolynomial_Iterator::set_currMonomial(const Monomial & t) {
if (!fellOff()) iter_curr->t = t;
}
LLPolynomial_Iterator::LLPolynomial_Iterator(
Polynomial_Linked_List * poly, bool at_end
) {
p_base = p = poly;
iter_curr = p->head;
if (at_end and iter_curr != nullptr)
while (iter_curr->next != nullptr)
iter_curr = iter_curr->next;
}
LLPolynomial_Iterator::LLPolynomial_Iterator(
const Polynomial_Linked_List * poly, bool at_end)
{
p_base = p = const_cast<Polynomial_Linked_List *>(poly);
iter_curr = p->head;
if (at_end and iter_curr != nullptr)
while (iter_curr->next != nullptr)
iter_curr = iter_curr->next;
}
void LLPolynomial_Iterator::restart_iteration() {
iter_curr = p->head;
}
Polynomial_Linked_List::Polynomial_Linked_List(
Polynomial_Ring & R,
const Monomial_Ordering * order
) : Mutable_Polynomial(R, order)
{
head = nullptr;
}
Polynomial_Linked_List::Polynomial_Linked_List(
Polynomial_Ring & R,
const Monomial & t,
const Monomial_Ordering * order
) : Mutable_Polynomial(R, order)
{
if (order == nullptr) {
if (t.monomial_ordering() == nullptr)
order = generic_grevlex_ptr;
else
order = t.monomial_ordering();
}
head = new Monomial_Node(R.ground_field(), t);
if (order != t.monomial_ordering())
head->t.set_monomial_ordering(order);
head->next = nullptr;
}
Polynomial_Linked_List::Polynomial_Linked_List(
Polynomial_Ring & R,
const Prime_Field_Element & c, const Monomial & t,
const Monomial_Ordering * order
) : Mutable_Polynomial(R, order), head(new Monomial_Node(c, t))
{
if (order == nullptr) {
if (t.monomial_ordering() == nullptr)
order = generic_grevlex_ptr;
else
order = t.monomial_ordering();
}
if (order != t.monomial_ordering())
head->t.set_monomial_ordering(order);
head->next = nullptr;
}
Polynomial_Linked_List::Polynomial_Linked_List(
Polynomial_Ring & R,
Monomial_Node * node,
const Monomial_Ordering * order
) : Mutable_Polynomial(R, order)
{
if (order == nullptr) {
if (node == nullptr or node->t.monomial_ordering() == nullptr)
order = generic_grevlex_ptr;
else
order = node->t.monomial_ordering();
}
head = node;
if (node != nullptr) {
node->next = node->prev = nullptr; // to be safe
head->t.set_monomial_ordering(order);
}
}
Polynomial_Linked_List::Polynomial_Linked_List(
const Polynomial_Linked_List & other
) : Mutable_Polynomial(other.base_ring(), other.monomial_ordering())
{
head = nullptr;
Monomial_Node *curr = nullptr;
Monomial_Node *other_curr = other.head;
while (other_curr != nullptr) {
Monomial_Node * next = new Monomial_Node(*other_curr);
if (head == nullptr) head = curr = next;
else
{
curr->next = next;
curr->next->prev = curr;
curr = next;
}
other_curr = other_curr->next;
}
curr->next = nullptr;
}
Polynomial_Linked_List::Polynomial_Linked_List(const Abstract_Polynomial & p)
: Mutable_Polynomial(p.base_ring(), p.monomial_ordering())
{
Polynomial_Iterator *pi = p.new_iterator();
Monomial_Node *curr = nullptr;
while (!pi->fellOff()) {
if (curr == nullptr) {
head = curr = new Monomial_Node(pi->currCoeff(), pi->currMonomial());
curr->prev = nullptr;
}
else {
curr->next = new Monomial_Node(pi->currCoeff(), pi->currMonomial());
curr->next->prev = curr;
curr = curr->next;
}
pi->moveRight();
}
delete pi;
curr->next = nullptr;
}
Polynomial_Linked_List::~Polynomial_Linked_List() {
while (head != nullptr) {
Monomial_Node * tmp = head->next;
delete head;
head = tmp;
}
}
bool Polynomial_Linked_List::is_zero() const {
return head == nullptr or head->c.is_zero();
}
Monomial & Polynomial_Linked_List::leading_monomial() const { return head->t; }
Prime_Field_Element Polynomial_Linked_List::leading_coefficient() const {
return head->c;
}
unsigned Polynomial_Linked_List::length() const {
unsigned i = 0;
for (Monomial_Node *curr = head; curr != nullptr; ++i) { curr = curr->next; }
return i;
}
void Polynomial_Linked_List::set_monomial_ordering(
const Monomial_Ordering * ord, bool sort_anew
) {
for (Monomial_Node *curr = head; curr != nullptr; curr = curr->next)
curr->t.set_monomial_ordering(ord);
if (sort_anew)
sort_by_order();
}
Polynomial_Linked_List * Polynomial_Linked_List::zero_polynomial() const {
return new Polynomial_Linked_List(R);
}
Polynomial_Linked_List * Polynomial_Linked_List::monomial_multiple(
const Monomial &u
) const {
Polynomial_Linked_List * p = new Polynomial_Linked_List(*this);
for (Monomial_Node *curr = p->head; curr != nullptr and !(curr->c.is_zero());
curr = curr->next)
curr->t *= u;
return p;
}
Polynomial_Linked_List * Polynomial_Linked_List::scalar_multiple(
const Prime_Field_Element &c
) const {
Polynomial_Linked_List * p = new Polynomial_Linked_List(*this);
for (Monomial_Node *curr = p->head; curr != nullptr and !(curr->c.is_zero());
curr = curr->next)
curr->c *= c;
return p;
}
Polynomial_Linked_List & Polynomial_Linked_List::operator +=(
const Abstract_Polynomial &other
) {
// need an iterator
Polynomial_Iterator * oi = other.new_iterator();
// add until one of the polynomials is exhausted
for (Monomial_Node *p = head;
(!oi->fellOff() and !(oi->currCoeff().is_zero()))
and (p != nullptr and !(p->c.is_zero()));
)
{
DEG_TYPE a = p->t.ordering_degree();
while (not oi->fellOff() and a < oi->currMonomial().ordering_degree()){
Monomial_Node *r = new Monomial_Node(oi->currCoeff(), oi->currMonomial());
r->prev = p->prev;
if (p->prev != nullptr) p->prev->next = r;
if (head == p) head = r;
p->prev = r;
r->next = p;
oi->moveRight();
}
if (not oi->fellOff()) {
if (p->t.is_like(oi->currMonomial())) {
p->c += oi->currCoeff();
oi->moveRight();
if (not p->c.is_zero())
p = p->next;
else {
Monomial_Node *r = p->next;
if (p->prev != nullptr) p->prev->next = r;
if (r != nullptr) r->prev = p->prev;
if (head == p) head = r;
delete p;
p = r;
}
} else if (p->t > oi->currMonomial())
p = p->next;
else {
Monomial_Node *r = new Monomial_Node(oi->currCoeff(), oi->currMonomial());
r->prev = p->prev;
if (p->prev != nullptr) p->prev->next = r;
if (head == p) head = r;
p->prev = r;
r->next = p;
oi->moveRight();
}
}
}
// if this polynomial is exhausted, we need to add what remains of other
if (!oi->fellOff() and !(oi->currCoeff().is_zero())) {
if (head == nullptr) {
head = new Monomial_Node(oi->currCoeff(), oi->currMonomial());
head->next = head->prev = nullptr;
oi->moveRight();
}
Monomial_Node *p = head;
while (p->next != nullptr) p = p->next;
while (!oi->fellOff() and !(oi->currCoeff().is_zero())) {
p->next = new Monomial_Node(oi->currCoeff(), oi->currMonomial());
p->next->prev = p;
p = p->next;
oi->moveRight();
}
p->next = nullptr;
}
delete oi;
return *this;
}
Polynomial_Linked_List & Polynomial_Linked_List::operator -=(
const Abstract_Polynomial &other
) {
// need an iterator
Polynomial_Iterator * oi = other.new_iterator();
// add until one of the polynomials is exhausted
for (Monomial_Node *p = head;
(!oi->fellOff() and !(oi->currCoeff().is_zero()))
and (p != nullptr and !(p->c.is_zero()));
)
{
if (p->t.is_like(oi->currMonomial())) {
p->c += oi->currCoeff();
if (p->c.is_zero()) {
Monomial_Node *r = p->next;
if (p->prev != nullptr) p->prev->next = r;
if (r != nullptr) r->prev = p->prev;
if (head == p) head = r;
delete p;
p = r;
oi->moveRight();
}
} else if (p->t > oi->currMonomial())
p = p->next;
else {
Monomial_Node *r = new Monomial_Node(oi->currCoeff(), oi->currMonomial());
r->prev = p->prev;
if (p->prev != nullptr) p->prev->next = r;
if (head == p) head = r;
p->prev = r;
r->next = p;
oi->moveRight();
}
}
// if this polynomial is exhausted, we need to add what remains of other
if (!oi->fellOff() and !(oi->currCoeff().is_zero())) {
if (head == nullptr) {
head = new Monomial_Node(-(oi->currCoeff()), oi->currMonomial());
head->next = head->prev = nullptr;
oi->moveRight();
}
Monomial_Node *p = head;
while (p->next != nullptr) p = p->next;
while (!oi->fellOff() and !(oi->currCoeff().is_zero())) {
p->next = new Monomial_Node(oi->currCoeff(), oi->currMonomial());
p->next->prev = p;
p = p->next;
oi->moveRight();
}
p->next = nullptr;
}
delete oi;
return *this;
}
void Polynomial_Linked_List::add_polynomial_multiple(
const Prime_Field_Element &a,
const Monomial &t,
const Abstract_Polynomial &q,
bool subtract
) {
//cout << "\tadding " << a << " * " << t << " * (" << q << ')' << endl;
// need an iterator
Polynomial_Iterator * oi = q.new_iterator();
// first add monomials until this or q is exhausted
for (Monomial_Node *curr = head;
curr != nullptr and !(curr->c.is_zero()) and
!oi->fellOff() and !(oi->currCoeff().is_zero());
)
{
if (curr->t.like_multiple(oi->currMonomial(), t)) {
if (subtract) curr->c -= a * oi->currCoeff();
else curr->c += a * oi->currCoeff();
if ((curr->c).is_zero()) {
if (curr->prev != nullptr) curr->prev->next = curr->next;
if (curr->next != nullptr) curr->next->prev = curr->prev;
Monomial_Node *tmp = curr->next;
if (head == curr) head = tmp;
delete curr;
curr = tmp;
}
else
curr = curr->next;
oi->moveRight();
} else if (curr->t.larger_than_multiple(oi->currMonomial(), t)) {
curr = curr->next;
} else { // smaller
Prime_Field_Element c(oi->currCoeff());
if (subtract) c = -c;
Monomial_Node *new_node = new Monomial_Node(c, oi->currMonomial());
new_node->t *= t;
new_node->c *= a;
if (new_node->c.is_zero())
delete new_node;
else {
new_node->prev = curr->prev;
if (curr->prev != nullptr) curr->prev->next = new_node;
if (head == curr) head = new_node;
curr->prev = new_node;
new_node->next = curr;
oi->moveRight();
}
}
}
// if this is exhausted but q is not, add the remaining monomials
if (!oi->fellOff() and !(oi->currCoeff().is_zero())) {
if (head == nullptr) {
head = new Monomial_Node(-(oi->currCoeff()), oi->currMonomial());
head->t *= t;
head->c *= a;
head->next = head->prev = nullptr;
oi->moveRight();
}
Monomial_Node * curr = head;
while (curr->next != nullptr) curr = curr->next;
while (!oi->fellOff() and !(oi->currCoeff().is_zero())) {
if (subtract)
curr->next = new Monomial_Node(-(oi->currCoeff()), oi->currMonomial());
else
curr->next = new Monomial_Node(oi->currCoeff(), oi->currMonomial());
curr->next->prev = curr;
curr = curr->next;
curr->t *= t;
curr->c *= a;
oi->moveRight();
}
curr->next = nullptr;
}
delete oi;
// cout << "\tresulted in " << *this << endl;
}
void Polynomial_Linked_List::sort_by_order()
{
// insertion sort, as we don't expect worst case
for (Monomial_Node *curr = head->next; curr != nullptr; /* */) {
while (curr->t > curr->prev->t) {
Monomial_Node *tmp = curr->next;
curr->prev->next = tmp;
if (tmp != nullptr) tmp->prev = curr->prev;
curr->next = curr->prev;
curr->prev = curr->prev->prev;
curr->prev->prev = curr;
curr = tmp;
}
}
}
LLPolynomial_Iterator * Polynomial_Linked_List::new_iterator() const {
return new LLPolynomial_Iterator(this);
}
Polynomial_Iterator * Polynomial_Linked_List::begin() const {
return new LLPolynomial_Iterator(this);
}
Polynomial_Iterator * Polynomial_Linked_List::end() const {
return new LLPolynomial_Iterator(this, true);
}
LLPolynomial_Iterator * Polynomial_Linked_List::new_mutable_iterator() {
return new LLPolynomial_Iterator(this);
}
Polynomial_Linked_List * Polynomial_Linked_List::detach_head() {
Monomial_Node *tmp = head;
if (head != nullptr) {
head = head->next;
if (head != nullptr) head->prev = nullptr;
}
Polynomial_Linked_List * result = new Polynomial_Linked_List(R, tmp);
return result;
}
void Polynomial_Linked_List::add_last(
const Prime_Field_Element & c,
const Monomial & t
) {
if (head == nullptr) {
head = new Monomial_Node(c, t);
head->prev = head->next = nullptr;
} else {
Monomial_Node * tail = head;
while (tail->next != nullptr) { tail = tail->next; }
tail->next = new Monomial_Node(c, t);
tail->next->prev = tail;
tail->next->next = nullptr;
}
}
#endif