-
Notifications
You must be signed in to change notification settings - Fork 0
/
polynomial.c
180 lines (155 loc) · 4.07 KB
/
polynomial.c
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
#include "polynomial.h"
struct polynom_s {
uint32_t Degree;
int32_t *Exponents, *Coefficent; //TODO: Handle negative exp
uint8_t *Variables, *Terms;
};
/* struct polynom_var_s { int32_t v[8]; }; */
polynom_t *InitPolynomial(){
polynom_t *rv;
rv = NULL;
if ((rv = (polynom_t *) malloc(sizeof(polynom_t))) == NULL){
fprintf(stderr, "Err in InitPolynomial: malloc (1)\n");
exit(1);
}
rv->Degree = (uint32_t) 0;
rv->Exponents = NULL;
rv->Coefficent = NULL;
rv->Variables = NULL;
rv->Terms = NULL;
return rv;
}
void DestroyPolynomial(polynom_t *P){
if (P->Degree > (uint32_t) 0){
free(P->Exponents);
free(P->Coefficent);
free(P->Variables);
free(P->Terms);
}
free(P);
}
int AppendTermPolynomial(
int32_t Coefficent,
uint8_t Variables,
int32_t *Exponents,
polynom_t *P){
int i, j, s;
uint8_t a, b;
j = 0; s = 0;
a = Variables;
b = (uint8_t) 1;
P->Degree++;
for(i = 0;i < 8;++i){
if ((a & b) == 1)
s += 1;
a >>= 1;
}
/* Don't expand the array if there are no exponents to add*/
if (s != 0 &&
(P->Exponents = (int32_t *) realloc(P->Exponents, sizeof(int32_t *) * (P->Degree + s))) == NULL){
fprintf(stderr, "Err in PolynomialAppendTerm: realloc (1)\n");
return 1;
}
if ((P->Coefficent = (int32_t *) realloc(P->Coefficent, sizeof(int32_t) * P->Degree)) == NULL){
fprintf(stderr, "Err in PolynomialAppendTerm: realloc (2)\n");
return 1;
}
if ((P->Variables = (uint8_t *) realloc(P->Variables, sizeof(uint8_t) * P->Degree)) == NULL){
fprintf(stderr, "Err in PolynomialAppendTerm: realloc (3)\n");
return 1;
}
if ((P->Terms = (uint8_t *) realloc(P->Terms, sizeof(uint8_t) * P->Degree)) == NULL){
fprintf(stderr, "Err in PolynomialAppendTerm: realloc (4)\n");
return 1;
}
P->Terms[P->Degree - 1] = (uint8_t) s; /* Set Term Step */
if (s != 0){
for(i = 0;i < ((int) P->Degree - 1);++i) /* Read Depth */
j += P->Terms[i];
for(i = 0;i < s;++i)
P->Exponents[j + i] = Exponents[i];
}
P->Coefficent[P->Degree - 1] = Coefficent;
P->Variables[P->Degree - 1] = Variables;
return 0;
}
uint8_t GetVariablesPolynomial(polynom_t *P){
int i, d;
uint8_t rv;
d = (int) P->Degree;
rv = (uint8_t) 0;
for(i = 0;i < d;++i)
rv = rv | P->Variables[i];
return rv;
}
float _PowPolynomial(
float X,
int32_t N){
int i, d;
float rv;
d = (int) N;
rv = X;
if (d > 0) {
for (i = 1;i < d;++i)
rv *= X;
}
else if (d < 0) {
for (i = -1;i > d;--i)
rv *= X;
rv = 1.0F / rv;
}
else {
return 1.0F;
}
return rv;
}
float FunctionOfPolynomial(
polynom_t *P,
polynom_var_t *X){
int i, j, d, var, exp;
float rv, temp;
uint8_t a, b;
d = (int) P->Degree;
exp = 0;
rv = 0.0F;
for (i = 0;i < d; ++i) { /* term loop */
a = P->Variables[i];
var = 0;
b = (uint8_t) 1;
temp = 1.0F;
for (j = 0;j < 8; ++j){ /* variable loop */
if ((a & b) == 1){
temp *= _PowPolynomial(
X->v[var],
P->Exponents[exp]);
exp += 1;
}
var += 1;
a >>= 1;
}
temp *= P->Coefficent[i]; /* multiply by coefficent */
rv += temp; //TODO: Implement a decimal type to ensure assoc.
}
return rv;
}
void PrintPolynomial(polynom_t *P){
int i, j, d, exp;
uint8_t a, b;
d = (int) P->Degree;
exp = 0;
for (i = 0;i < d;++i){
printf("+(%d", (int)P->Coefficent[i]);
a = P->Variables[i];
b = (uint8_t) 1;
for (j = 0;j < 8;++j){
if ((a & b) == 1){
printf("%c^%d", j + 97, P->Exponents[exp]);
exp += 1;
}
a >>= 1;
}
printf(")");
}
printf("\n");
return;
}