Skip to content

Commit

Permalink
weiss: Add array impl of polynomial
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Nov 14, 2023
1 parent 15e0a66 commit dd15149
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 2 deletions.
7 changes: 6 additions & 1 deletion weiss/c03/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
)
2 changes: 1 addition & 1 deletion weiss/c03/linked_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
54 changes: 54 additions & 0 deletions weiss/c03/polynomial_array.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) 2023 Xu Shaohua <[email protected]>. 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 <assert.h>
#include <stdio.h>

#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];
}
}
}
40 changes: 40 additions & 0 deletions weiss/c03/polynomial_array.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) 2023 Xu Shaohua <[email protected]>. 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 <stddef.h>
#include <stdint.h>

#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_

0 comments on commit dd15149

Please sign in to comment.