summaryrefslogtreecommitdiff
path: root/lab04/polynomial.h
diff options
context:
space:
mode:
authorRichard Qin <richard.qin.x@gmail.com>2026-10-22 19:18:52 +0800
committerRichard Qin <richard.qin.x@gmail.com>2026-10-22 19:18:52 +0800
commitee73946a385a804fc4fc40f3045df7b684386625 (patch)
treec54ef65587152eee973400a7c43dea4057259d91 /lab04/polynomial.h
parent9253f8f578faa8de23110c1e119bc9109006b43e (diff)
downloadRUCDSA-ee73946a385a804fc4fc40f3045df7b684386625.tar.gz
RUCDSA-ee73946a385a804fc4fc40f3045df7b684386625.tar.xz
Add polynomial calculas
Signed-off-by: Richard Qin <richard.qin.x@gmail.com>
Diffstat (limited to 'lab04/polynomial.h')
-rw-r--r--lab04/polynomial.h46
1 files changed, 46 insertions, 0 deletions
diff --git a/lab04/polynomial.h b/lab04/polynomial.h
index a270211..6641e36 100644
--- a/lab04/polynomial.h
+++ b/lab04/polynomial.h
@@ -749,6 +749,52 @@ static inline bool poly_list_derivative(struct poly_list *dst,
return true;
}
+/* Indefinite integral with the constant term set to zero. */
+static inline bool poly_vec_integral(struct poly_vec *dst,
+ const struct poly_vec *src)
+{
+ 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 == UINT_MAX ||
+ !poly_vec_add_term(&tmp, term->coef / (term->exp + 1.0),
+ term->exp + 1)) {
+ poly_vec_destroy(&tmp);
+ return false;
+ }
+ }
+
+ __poly_vec_replace(dst, &tmp);
+ return true;
+}
+
+static inline bool poly_list_integral(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, struct poly_node, link);
+ if (node->term.exp == UINT_MAX ||
+ !poly_list_add_term(&tmp,
+ node->term.coef / (node->term.exp + 1.0),
+ node->term.exp + 1)) {
+ poly_list_destroy(&tmp);
+ return false;
+ }
+ pos = pos->next;
+ }
+
+ __poly_list_replace(dst, &tmp);
+ return true;
+}
+
/*
* ============================================================