From dd15149d797b4655b868a64f703a83598514f57a Mon Sep 17 00:00:00 2001 From: Xu Shaohua Date: Tue, 14 Nov 2023 16:41:35 +0800 Subject: [PATCH] weiss: Add array impl of polynomial --- weiss/c03/CMakeLists.txt | 7 ++++- weiss/c03/linked_list.c | 2 +- weiss/c03/polynomial_array.c | 54 ++++++++++++++++++++++++++++++++++++ weiss/c03/polynomial_array.h | 40 ++++++++++++++++++++++++++ 4 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 weiss/c03/polynomial_array.c create mode 100644 weiss/c03/polynomial_array.h diff --git a/weiss/c03/CMakeLists.txt b/weiss/c03/CMakeLists.txt index b6402bed..d07e2880 100644 --- a/weiss/c03/CMakeLists.txt +++ b/weiss/c03/CMakeLists.txt @@ -1,5 +1,10 @@ add_library(linked_list STATIC - linked_list.h linked_list.c + linked_list.h +) + +add_library(polynomial_array STATIC + polynomial_array.c + polynomial_array.h ) diff --git a/weiss/c03/linked_list.c b/weiss/c03/linked_list.c index d4a910b2..2840b18d 100644 --- a/weiss/c03/linked_list.c +++ b/weiss/c03/linked_list.c @@ -85,7 +85,7 @@ void list_clear(node_t** list) { assert(list != NULL); while (*list != NULL) { node_t* tmp = *list; - *list = *list->next; + *list = tmp->next; free(tmp); } } diff --git a/weiss/c03/polynomial_array.c b/weiss/c03/polynomial_array.c new file mode 100644 index 00000000..5736e88a --- /dev/null +++ b/weiss/c03/polynomial_array.c @@ -0,0 +1,54 @@ +// Copyright (c) 2023 Xu Shaohua . All rights reserved. +// Use of this source is governed by General Public License that can be +// found in the LICENSE file. + +#include "polynomial_array.h" + +#include +#include + +#ifndef max +#define max(x, y) ((x) > (y)) ? (x) : (y) +#endif + +void polynomial_zero(polynomial_t* poly) { + assert(poly != NULL); + + for (size_t i = 0; i < MAX_DEGREE; ++i) { + poly->coeff_array[i] = 0; + } + poly->high_power = 0; +} + +void polynomial_add(polynomial_t* poly1, polynomial_t* poly2, + polynomial_t* poly_sum) { + assert(poly1 != NULL); + assert(poly2 != NULL); + assert(poly_sum != NULL); + + polynomial_zero(poly_sum); + poly_sum->high_power = max(poly1->high_power, poly2->high_power); + for (uint32_t i = poly_sum->high_power; i >= 0; --i) { + poly_sum->coeff_array[i] = poly1->coeff_array[i] + poly2->coeff_array[i]; + } +} + +void polynomial_mul(polynomial_t* poly1, polynomial_t* poly2, + polynomial_t* poly_prod) { + assert(poly1); + assert(poly2); + assert(poly_prod); + + polynomial_zero(poly_prod); + poly_prod->high_power = poly1->high_power + poly2->high_power; + if (poly_prod->high_power > MAX_DEGREE) { + fprintf(stderr, "Exceeded array size\n"); + return; + } + + for (uint32_t i = 0; i < poly1->high_power; ++i) { + for (uint32_t j = 0; j < poly2->high_power; ++j) { + poly_prod->coeff_array[i + j] = poly1->coeff_array[i] + poly2->coeff_array[j]; + } + } +} diff --git a/weiss/c03/polynomial_array.h b/weiss/c03/polynomial_array.h new file mode 100644 index 00000000..37297e62 --- /dev/null +++ b/weiss/c03/polynomial_array.h @@ -0,0 +1,40 @@ +// Copyright (c) 2023 Xu Shaohua . All rights reserved. +// Use of this source is governed by General Public License that can be +// found in the LICENSE file. + +#ifndef WEISS_C03_POLYNOMIAL_ARRAY_H_ +#define WEISS_C03_POLYNOMIAL_ARRAY_H_ + +#include +#include + +#define MAX_DEGREE 32 + +struct polynomial_s { + int32_t coeff_array[MAX_DEGREE + 1]; + uint32_t high_power; +}; + +typedef struct polynomial_s polynomial_t; + +/** + * Reset all fields in polynomial to zero. + * + * @param poly + */ +extern void polynomial_zero(polynomial_t* poly); + +/** + * Add two polynomials. + * + * @param poly1 + * @param poly2 + * @param poly_sum + */ +extern void polynomial_add(polynomial_t* poly1, polynomial_t* poly2, + polynomial_t* poly_sum); + +extern void polynomial_mul(polynomial_t* poly1, polynomial_t* poly2, + polynomial_t* poly_prod); + +#endif // WEISS_C03_POLYNOMIAL_ARRAY_H_