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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* Copyright (C) 2026 Richard Qin */
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include <stddef.h>
#include <stdbool.h>
#include <limits.h>
#include <math.h>
#include <stdlib.h>
#include "vec.h"
#include "list.h"
/*
* Polynomial term:
*
* coef * x^exp
*
* exp >= 0
*/
struct poly_term {
double coef;
unsigned int exp;
};
/*
* ============================================================
* Sequential representation
* ============================================================
*
* Terms are stored in descending order of exponent:
*
* 5x^4 + 3x^2 - 7
*
* [ {5,4}, {3,2}, {-7,0} ]
*
* Invariants:
*
* terms.elem_size == sizeof(struct poly_term)
* terms are ordered by exp descending
* duplicate exponents do not exist
* zero-coefficient terms do not exist
*/
struct poly_vec {
struct vec terms;
};
/*
* ============================================================
* Linked representation
* ============================================================
*/
struct poly_node {
struct poly_term term;
struct list_head link;
};
struct poly_list {
struct list_head head;
size_t size;
};
/*
* ============================================================
* Initialization / destruction
* ============================================================
*/
static inline void poly_vec_init(struct poly_vec *poly)
{
vec_init(&poly->terms, struct poly_term);
}
static inline void poly_vec_clear(struct poly_vec *poly)
{
vec_clear(&poly->terms);
}
static inline void poly_vec_destroy(struct poly_vec *poly)
{
vec_destroy(&poly->terms);
}
static inline void poly_list_init(struct poly_list *poly)
{
INIT_LIST_HEAD(&poly->head);
poly->size = 0;
}
static inline void poly_list_clear(struct poly_list *poly)
{
struct list_head *pos = poly->head.next;
while (pos != &poly->head) {
struct list_head *next = pos->next;
struct poly_node *node = list_entry(pos, struct poly_node, link);
list_del(pos);
free(node);
pos = next;
}
INIT_LIST_HEAD(&poly->head);
poly->size = 0;
}
static inline void poly_list_destroy(struct poly_list *poly)
{
poly_list_clear(poly);
}
static inline void __poly_vec_replace(struct poly_vec *dst,
struct poly_vec *src)
{
struct vec old = dst->terms;
dst->terms = src->terms;
src->terms = old;
vec_destroy(&src->terms);
}
static inline void __poly_list_replace(struct poly_list *dst,
struct poly_list *src)
{
poly_list_clear(dst);
if (src->size == 0) {
return;
}
dst->head.next = src->head.next;
dst->head.prev = src->head.prev;
dst->head.next->prev = &dst->head;
dst->head.prev->next = &dst->head;
dst->size = src->size;
INIT_LIST_HEAD(&src->head);
src->size = 0;
}
/*
* ============================================================
* Basic properties
* ============================================================
*/
static inline bool poly_vec_empty(const struct poly_vec *poly)
{
return vec_empty(&poly->terms);
}
static inline size_t poly_vec_size(const struct poly_vec *poly)
{
return vec_size(&poly->terms);
}
static inline bool poly_list_empty(const struct poly_list *poly)
{
return list_empty(&poly->head);
}
static inline size_t poly_list_size(const struct poly_list *poly)
{
return poly->size;
}
/*
* ============================================================
* Term access
* ============================================================
*
* Sequential representation supports indexed access.
*/
static inline struct poly_term *
poly_vec_at(struct poly_vec *poly, size_t index)
{
return vec_at(&poly->terms, index);
}
static inline const struct poly_term *
poly_vec_at_const(const struct poly_vec *poly, size_t index)
{
return vec_at_const(&poly->terms, index);
}
/*
* ============================================================
* Term modification
* ============================================================
*
* poly_*_set_term():
*
* Sets the coefficient for x^exp.
*
* Existing exponent:
* replace coefficient
*
* Missing exponent:
* insert at the proper position
*
* coef == 0:
* remove that exponent
*
*
* poly_*_add_term():
*
* Adds coef*x^exp to the polynomial.
*
* Existing exponent:
* old_coef += coef
*
* Result coefficient == 0:
* remove that term
*
* These two operations preserve all polynomial invariants.
*/
static inline size_t __poly_vec_find(const struct poly_vec *poly,
unsigned int exp,
bool *found)
{
size_t i;
for (i = 0; i < vec_size(&poly->terms); ++i) {
const struct poly_term *term = vec_at_const(&poly->terms, i);
if (term->exp == exp) {
*found = true;
return i;
}
if (term->exp < exp) {
break;
}
}
*found = false;
return i;
}
static inline bool poly_vec_remove_term(struct poly_vec *poly,
unsigned int exp);
static inline bool poly_vec_set_term(struct poly_vec *poly,
double coef,
unsigned int exp)
{
if (coef == 0.0) {
poly_vec_remove_term(poly, exp);
return true;
}
bool found;
size_t index = __poly_vec_find(poly, exp, &found);
if (found) {
poly_vec_at(poly, index)->coef = coef;
return true;
}
struct poly_term term = { .coef = coef, .exp = exp };
return vec_insert(&poly->terms, index, &term);
}
static inline bool poly_vec_add_term(struct poly_vec *poly,
double coef,
unsigned int exp)
{
if (coef == 0.0) {
return true;
}
bool found;
size_t index = __poly_vec_find(poly, exp, &found);
if (found) {
struct poly_term *term = poly_vec_at(poly, index);
double result = term->coef + coef;
if (result == 0.0) {
vec_erase(&poly->terms, index);
} else {
term->coef = result;
}
return true;
}
struct poly_term term = { .coef = coef, .exp = exp };
return vec_insert(&poly->terms, index, &term);
}
static inline bool poly_vec_remove_term(struct poly_vec *poly,
unsigned int exp)
{
bool found;
size_t index = __poly_vec_find(poly, exp, &found);
if (!found) {
return false;
}
vec_erase(&poly->terms, index);
return true;
}
static inline bool poly_vec_get_term(const struct poly_vec *poly,
unsigned int exp,
double *coef)
{
bool found;
size_t index = __poly_vec_find(poly, exp, &found);
if (!found) {
return false;
}
*coef = poly_vec_at_const(poly, index)->coef;
return true;
}
static inline bool poly_list_remove_term(struct poly_list *poly,
unsigned int exp);
static inline bool poly_list_set_term(struct poly_list *poly,
double coef,
unsigned int exp)
{
if (coef == 0.0) {
poly_list_remove_term(poly, exp);
return true;
}
struct list_head *pos = poly->head.next;
while (pos != &poly->head) {
struct poly_node *node = list_entry(pos, struct poly_node, link);
if (node->term.exp == exp) {
node->term.coef = coef;
return true;
}
if (node->term.exp < exp) {
break;
}
pos = pos->next;
}
struct poly_node *node = malloc(sizeof(*node));
if (!node) {
return false;
}
node->term.coef = coef;
node->term.exp = exp;
__list_add(&node->link, pos->prev, pos);
++poly->size;
return true;
}
static inline bool poly_list_add_term(struct poly_list *poly,
double coef,
unsigned int exp)
{
if (coef == 0.0) {
return true;
}
struct list_head *pos = poly->head.next;
while (pos != &poly->head) {
struct poly_node *node = list_entry(pos, struct poly_node, link);
if (node->term.exp == exp) {
double result = node->term.coef + coef;
if (result == 0.0) {
list_del(pos);
free(node);
--poly->size;
} else {
node->term.coef = result;
}
return true;
}
if (node->term.exp < exp) {
break;
}
pos = pos->next;
}
struct poly_node *node = malloc(sizeof(*node));
if (!node) {
return false;
}
node->term.coef = coef;
node->term.exp = exp;
__list_add(&node->link, pos->prev, pos);
++poly->size;
return true;
}
static inline bool poly_list_remove_term(struct poly_list *poly,
unsigned int exp)
{
struct list_head *pos = poly->head.next;
while (pos != &poly->head) {
struct poly_node *node = list_entry(pos, struct poly_node, link);
if (node->term.exp == exp) {
list_del(pos);
free(node);
--poly->size;
return true;
}
if (node->term.exp < exp) {
return false;
}
pos = pos->next;
}
return false;
}
static inline bool poly_list_get_term(const struct poly_list *poly,
unsigned int exp,
double *coef)
{
const struct list_head *pos = poly->head.next;
while (pos != &poly->head) {
const struct poly_node *node =
list_entry(pos, const struct poly_node, link);
if (node->term.exp == exp) {
*coef = node->term.coef;
return true;
}
if (node->term.exp < exp) {
return false;
}
pos = pos->next;
}
return false;
}
/*
* ============================================================
* Copy / conversion
* ============================================================
*/
static inline bool poly_vec_copy(struct poly_vec *dst,
const struct poly_vec *src)
{
struct poly_vec tmp;
poly_vec_init(&tmp);
if (!vec_append(&tmp.terms, src->terms.data, src->terms.size)) {
poly_vec_destroy(&tmp);
return false;
}
__poly_vec_replace(dst, &tmp);
return true;
}
static inline bool poly_list_copy(struct poly_list *dst,
const struct poly_list *src)
{
struct poly_list tmp;
poly_list_init(&tmp);
const struct list_head *pos = src->head.next;
while (pos != &src->head) {
const struct poly_node *node =
list_entry(pos, const struct poly_node, link);
if (!poly_list_set_term(&tmp, node->term.coef, node->term.exp)) {
poly_list_destroy(&tmp);
return false;
}
pos = pos->next;
}
__poly_list_replace(dst, &tmp);
return true;
}
/*
* Conversion between the two representations.
*/
static inline bool poly_vec_from_list(struct poly_vec *dst,
const struct poly_list *src)
{
struct poly_vec tmp;
poly_vec_init(&tmp);
const struct list_head *pos = src->head.next;
while (pos != &src->head) {
const struct poly_node *node =
list_entry(pos, const struct poly_node, link);
if (!poly_vec_add_term(&tmp, node->term.coef, node->term.exp)) {
poly_vec_destroy(&tmp);
return false;
}
pos = pos->next;
}
__poly_vec_replace(dst, &tmp);
return true;
}
static inline bool poly_list_from_vec(struct poly_list *dst,
const struct poly_vec *src)
{
struct poly_list tmp;
poly_list_init(&tmp);
for (size_t i = 0; i < src->terms.size; ++i) {
const struct poly_term *term =
vec_at_const(&src->terms, i);
if (!poly_list_add_term(&tmp, term->coef, term->exp)) {
poly_list_destroy(&tmp);
return false;
}
}
__poly_list_replace(dst, &tmp);
return true;
}
/*
* ============================================================
* Arithmetic
* ============================================================
*
* dst may alias lhs or rhs unless otherwise documented.
*/
static inline bool __poly_vec_add_scaled(struct poly_vec *dst,
const struct poly_vec *src,
double scale)
{
for (size_t i = 0; i < src->terms.size; ++i) {
const struct poly_term *term =
vec_at_const(&src->terms, i);
if (!poly_vec_add_term(dst, term->coef * scale, term->exp)) {
return false;
}
}
return true;
}
static inline bool __poly_list_add_scaled(struct poly_list *dst,
const struct poly_list *src,
double scale)
{
const struct list_head *pos = src->head.next;
while (pos != &src->head) {
const struct poly_node *node =
list_entry(pos, const struct poly_node, link);
if (!poly_list_add_term(dst, node->term.coef * scale,
node->term.exp)) {
return false;
}
pos = pos->next;
}
return true;
}
static inline bool poly_vec_add(struct poly_vec *dst,
const struct poly_vec *lhs,
const struct poly_vec *rhs)
{
struct poly_vec tmp;
poly_vec_init(&tmp);
if (!__poly_vec_add_scaled(&tmp, lhs, 1.0) ||
!__poly_vec_add_scaled(&tmp, rhs, 1.0)) {
poly_vec_destroy(&tmp);
return false;
}
__poly_vec_replace(dst, &tmp);
return true;
}
static inline bool poly_vec_sub(struct poly_vec *dst,
const struct poly_vec *lhs,
const struct poly_vec *rhs)
{
struct poly_vec tmp;
poly_vec_init(&tmp);
if (!__poly_vec_add_scaled(&tmp, lhs, 1.0) ||
!__poly_vec_add_scaled(&tmp, rhs, -1.0)) {
poly_vec_destroy(&tmp);
return false;
}
__poly_vec_replace(dst, &tmp);
return true;
}
static inline bool poly_list_add(struct poly_list *dst,
const struct poly_list *lhs,
const struct poly_list *rhs)
{
struct poly_list tmp;
poly_list_init(&tmp);
if (!__poly_list_add_scaled(&tmp, lhs, 1.0) ||
!__poly_list_add_scaled(&tmp, rhs, 1.0)) {
poly_list_destroy(&tmp);
return false;
}
__poly_list_replace(dst, &tmp);
return true;
}
static inline bool poly_list_sub(struct poly_list *dst,
const struct poly_list *lhs,
const struct poly_list *rhs)
{
struct poly_list tmp;
poly_list_init(&tmp);
if (!__poly_list_add_scaled(&tmp, lhs, 1.0) ||
!__poly_list_add_scaled(&tmp, rhs, -1.0)) {
poly_list_destroy(&tmp);
return false;
}
__poly_list_replace(dst, &tmp);
return true;
}
/*
* ============================================================
* Extended operations
* ============================================================
*/
static inline bool poly_vec_mul(struct poly_vec *dst,
const struct poly_vec *lhs,
const struct poly_vec *rhs)
{
struct poly_vec tmp;
poly_vec_init(&tmp);
for (size_t i = 0; i < lhs->terms.size; ++i) {
const struct poly_term *left =
vec_at_const(&lhs->terms, i);
for (size_t j = 0; j < rhs->terms.size; ++j) {
const struct poly_term *right =
vec_at_const(&rhs->terms, j);
if (left->exp > UINT_MAX - right->exp ||
!poly_vec_add_term(&tmp,
left->coef * right->coef,
left->exp + right->exp)) {
poly_vec_destroy(&tmp);
return false;
}
}
}
__poly_vec_replace(dst, &tmp);
return true;
}
static inline bool poly_list_mul(struct poly_list *dst,
const struct poly_list *lhs,
const struct poly_list *rhs)
{
struct poly_list tmp;
poly_list_init(&tmp);
const struct list_head *left_pos = lhs->head.next;
while (left_pos != &lhs->head) {
const struct poly_node *left =
list_entry(left_pos, const struct poly_node, link);
const struct list_head *right_pos = rhs->head.next;
while (right_pos != &rhs->head) {
const struct poly_node *right =
list_entry(right_pos, const struct poly_node, link);
if (left->term.exp > UINT_MAX - right->term.exp ||
!poly_list_add_term(&tmp,
left->term.coef * right->term.coef,
left->term.exp + right->term.exp)) {
poly_list_destroy(&tmp);
return false;
}
right_pos = right_pos->next;
}
left_pos = left_pos->next;
}
__poly_list_replace(dst, &tmp);
return true;
}
/*
* Differentiate polynomial 'order' times.
*
* order == 0:
* dst = src
*/
static inline bool poly_vec_derivative(struct poly_vec *dst,
const struct poly_vec *src,
unsigned int order)
{
struct poly_vec tmp;
poly_vec_init(&tmp);
for (size_t i = 0; i < src->terms.size; ++i) {
const struct poly_term *term =
vec_at_const(&src->terms, i);
if (term->exp < order) {
continue;
}
double coef = term->coef;
unsigned int exp = term->exp;
for (unsigned int j = 0; j < order; ++j) {
coef *= exp;
--exp;
}
if (!poly_vec_add_term(&tmp, coef, exp)) {
poly_vec_destroy(&tmp);
return false;
}
}
__poly_vec_replace(dst, &tmp);
return true;
}
static inline bool poly_list_derivative(struct poly_list *dst,
const struct poly_list *src,
unsigned int order)
{
struct poly_list tmp;
poly_list_init(&tmp);
const struct list_head *pos = src->head.next;
while (pos != &src->head) {
const struct poly_node *node =
list_entry(pos, const struct poly_node, link);
if (node->term.exp >= order) {
double coef = node->term.coef;
unsigned int exp = node->term.exp;
for (unsigned int i = 0; i < order; ++i) {
coef *= exp;
--exp;
}
if (!poly_list_add_term(&tmp, coef, exp)) {
poly_list_destroy(&tmp);
return false;
}
}
pos = pos->next;
}
__poly_list_replace(dst, &tmp);
return true;
}
/*
* ============================================================
* Evaluation
* ============================================================
*/
static inline double poly_vec_eval(const struct poly_vec *poly, double x)
{
double result = 0.0;
for (size_t i = 0; i < poly->terms.size; ++i) {
const struct poly_term *term =
vec_at_const(&poly->terms, i);
result += term->coef * pow(x, term->exp);
}
return result;
}
static inline double poly_list_eval(const struct poly_list *poly, double x)
{
double result = 0.0;
const struct list_head *pos = poly->head.next;
while (pos != &poly->head) {
const struct poly_node *node =
list_entry(pos, const struct poly_node, link);
result += node->term.coef * pow(x, node->term.exp);
pos = pos->next;
}
return result;
}
/*
* ============================================================
* Comparison
* ============================================================
*
* eps controls floating-point coefficient comparison.
*/
static inline bool poly_vec_equal(const struct poly_vec *lhs,
const struct poly_vec *rhs,
double eps)
{
if (lhs->terms.size != rhs->terms.size) {
return false;
}
for (size_t i = 0; i < lhs->terms.size; ++i) {
const struct poly_term *left =
vec_at_const(&lhs->terms, i);
const struct poly_term *right =
vec_at_const(&rhs->terms, i);
if (left->exp != right->exp ||
!(fabs(left->coef - right->coef) <= eps)) {
return false;
}
}
return true;
}
static inline bool poly_list_equal(const struct poly_list *lhs,
const struct poly_list *rhs,
double eps)
{
if (lhs->size != rhs->size) {
return false;
}
const struct list_head *left_pos = lhs->head.next;
const struct list_head *right_pos = rhs->head.next;
while (left_pos != &lhs->head) {
const struct poly_node *left =
list_entry(left_pos, const struct poly_node, link);
const struct poly_node *right =
list_entry(right_pos, const struct poly_node, link);
if (left->term.exp != right->term.exp ||
!(fabs(left->term.coef - right->term.coef) <= eps)) {
return false;
}
left_pos = left_pos->next;
right_pos = right_pos->next;
}
return true;
}
/*
* ============================================================
* Iteration: sequential representation
* ============================================================
*/
#define poly_vec_for_each(term, poly) \
for (size_t __poly_i = 0; \
__poly_i < poly_vec_size(poly) && \
(((term) = poly_vec_at((poly), __poly_i)), true); \
++__poly_i)
#define poly_vec_for_each_const(term, poly) \
for (size_t __poly_i = 0; \
__poly_i < poly_vec_size(poly) && \
(((term) = poly_vec_at_const((poly), __poly_i)), true); \
++__poly_i)
/*
* ============================================================
* Iteration: linked representation
* ============================================================
*/
#define poly_list_for_each(node, poly) \
list_for_each_entry((node), &(poly)->head, link)
#define poly_list_for_each_reverse(node, poly) \
list_for_each_entry_reverse((node), &(poly)->head, link)
#define poly_list_for_each_safe(node, next, poly) \
list_for_each_entry_safe((node), (next), &(poly)->head, link)
#endif /* POLYNOMIAL_H */
|