-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
101 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |