-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolynomial.cpp
More file actions
383 lines (333 loc) · 10.6 KB
/
polynomial.cpp
File metadata and controls
383 lines (333 loc) · 10.6 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include <iostream>
#include <string>
#include <cstring>
#include "monomial.cpp"
class Polynomial {
public:
// constructors
Polynomial();
Polynomial(std::string args);
// operator overloading
Polynomial operator+(Polynomial p);
Polynomial operator-(Polynomial p);
Polynomial operator*(Polynomial p);
Polynomial operator/(Polynomial p);
void operator=(Polynomial p);
template <class T> float evaluate(T x);
void add(Polynomial* p);
void subtract(Polynomial* p);
void multiply(Polynomial* p);
void multiply(int x);
void divide(Polynomial* p);
Polynomial derivative();
Polynomial indefinite_integral();
template <class T> float definite_integral(T x1, T x2);
void string2polynomial(std::string args);
std::string polynomial2string();
void print();
void set_monomial(Monomial m);
Polynomial deep_copy();
void add_monomial(Monomial m);
void subtract_monomial(Monomial m);
void multiply_monomial(Monomial m);
private:
Monomial m;
Polynomial* next = NULL;
};
Polynomial::Polynomial() {
this->next = NULL;
}
Polynomial::Polynomial(std::string args) {
this->string2polynomial(args);
}
Polynomial Polynomial::operator+(Polynomial p) {
Polynomial* temp = new Polynomial();
*temp = *this;
Polynomial* current = &p;
while(current) {
temp->add_monomial(current->m);
current = current->next;
}
return *temp;
}
Polynomial Polynomial::operator-(Polynomial p) {
Polynomial* temp = new Polynomial();
*temp = *this;
Polynomial* current = &p;
while(current) {
//std::cout << temp->polynomial2string() << " - " << current->m.monomial2string() << " = ";
temp->subtract_monomial(current->m);
//std::cout << temp->polynomial2string() << std::endl;
current = current->next;
}
return *temp;
}
Polynomial Polynomial::operator*(Polynomial p) {
Polynomial* current = &p;
Polynomial* sum = new Polynomial();
*sum = *this;
Polynomial* t = new Polynomial();
while(current) {
*t = *this;
t->multiply_monomial(current->m);
*sum = *sum + *t;
delete(t);
current = current->next;
}
*sum = *sum - *this;
return *sum;
}
void Polynomial::operator=(Polynomial p) {
Polynomial temp = p.deep_copy();
this->m = temp.m;
this->next = temp.next;
}
Polynomial Polynomial::deep_copy() {
Polynomial* current = this;
Polynomial* new_head = NULL;
Polynomial* new_tail = NULL;
while(current) {
Polynomial* t = new Polynomial();
t->set_monomial(current->m);
if(new_head == NULL) {
new_head = t;
new_tail = new_head;
} else {
t->next = new_tail->next;
new_tail->next = t;
new_tail = new_tail->next;
}
current = current->next;
}
return *new_head;
}
template <class T> float Polynomial::evaluate(T x) {
float sum = 0.0;
Polynomial* current = this;
while(current) {
sum += current->m.evaluate(x);
current = current->next;
}
return sum;
}
/*
Polynomial Polynomial::derivative() {
Polynomial* temp = new Polynomial();
*temp = *this;
Polynomial* current = temp;
while(current) {
current->m = current->m.derivative();
current = current->next;
}
}
Polynomial Polynomial::indefinite_integral() {
Polynomial* current = this;
while(current) {
current->m.indefinite_integral();
current = current->next;
}
}
*/
template <class T> float Polynomial::definite_integral(T x1, T x2) {
float sum = 0;
Polynomial* current = this;
while(current) {
sum += current->m.definite_integral(x1, x2);
current = current->next;
}
return sum;
}
void Polynomial::string2polynomial(std::string args) {
//parse the argument to extract monomials
std::string monomial_string;
std::string::iterator it, start;
start = args.begin();
Polynomial* current = this;
int f = 0;
for(it = args.begin(); it < args.end(); it++) {
if((*it == '+' || *it == '-') && (*(it - 1) != '^' && *(it - 1) != '/')){ // x^-2/3 or 2/-3 type should not be included
monomial_string = std::string(start, it);
// make sure that monomial_string is not empty
if(monomial_string == "") {
start = it;
continue;
}
// if the coefficient is 0 then continue
Monomial t(monomial_string);
if(t.get_coefficient().value() == 0) {
start = it;
continue;
}
//std::cout << monomial_string << std::endl;
if(f == 0) {
//current->m = new Monomial(monomial_string);
current->m.string2monomial(monomial_string);
current->next = NULL;
f++;
} else {
Polynomial* temp = new Polynomial();
temp->m.string2monomial(monomial_string);
temp->next = NULL;
current->next = temp;
current = current->next;
}
start = it;
}
}
monomial_string = std::string(start, it);
//std::cout << monomial_string << std::endl;
if(f == 0) {
current->m.string2monomial(monomial_string);
current->next = NULL;
f++;
} else {
Polynomial* temp = new Polynomial();
temp->m.string2monomial(monomial_string);
temp->next = NULL;
current->next = temp;
current = current->next;
}
}
std::string Polynomial::polynomial2string() {
std::string temp = "";
Polynomial* current = this;
int f = 0;
while(current) {
float sign = current->m.get_coefficient().value();
if(sign > 0 && f != 0) {
temp += "+";
}
if(sign == 0) {
current = current->next;
continue;
}
temp += current->m.monomial2string();
current = current->next;
f++;
}
return temp;
}
void Polynomial::print() {
std::cout << this->polynomial2string() << std::endl;
}
void Polynomial::set_monomial(Monomial m) {
this->m = m;
}
void Polynomial::add_monomial(Monomial m) {
Polynomial* previous = NULL;
Polynomial* current = this;
while(current) {
if(current->m.get_power() == m.get_power()) {
if((current->m.get_coefficient() + m.get_coefficient()).value() == 0) { // remove current->m from the polynomial(this)
if(previous == NULL) {
this->m = (current->next) ? current->next->m : current->m + m;
this->next = (current->next) ? current->next->next : NULL;
} else {
previous->next = current->next;
delete(current);
}
} else {
current->m = current->m + m;
}
return;
}
if(current->m.get_power() < m.get_power()) {
Polynomial* t = new Polynomial();
if(previous == NULL) { // inserting in the beginning
t->m = this->m;
t->next = this->next;
this->m = m;
this->next = t;
} else { // inserting in the middle
t->m = m;
t->next = current;
previous->next = t;
}
return;
}
previous = current;
current = current->next;
}
Polynomial* t = new Polynomial();
if(previous == NULL) { // inserting in the beginning
t->m = this->m;
t->next = this->next;
this->m = m;
this->next = t;
} else { // inserting in the middle
t->m = m;
t->next = current;
previous->next = t;
}
return;
}
void Polynomial::subtract_monomial(Monomial m) {
Polynomial* previous = NULL;
Polynomial* current = this;
Monomial minus_one("-1");
while(current) {
if(current->m.get_power() == m.get_power()) {
if((current->m.get_coefficient() - m.get_coefficient()).value() == 0) { // remove current->m from the polynomial(this)
if(previous == NULL) {
this->m = (current->next) ? current->next->m : current->m - m;
this->next = (current->next) ? current->next->next : NULL;
} else {
previous->next = current->next;
delete(current);
}
} else {
current->m = current->m - m;
}
return;
}
if(current->m.get_power() < m.get_power()) {
Polynomial* t = new Polynomial();
if(previous == NULL) { // inserting in the beginning
t->m = this->m;
t->next = this->next;
this->m = m * minus_one;
this->next = t;
} else { // inserting in the middle
t->m = m * minus_one;
t->next = current;
previous->next = t;
}
return;
}
previous = current;
current = current->next;
}
Polynomial* t = new Polynomial();
if(previous == NULL) { // inserting in the beginning
t->m = this->m;
t->next = this->next;
this->m = m * minus_one;
this->next = t;
} else { // inserting in the middle
t->m = m * minus_one;
t->next = current;
previous->next = t;
}
return;
}
void Polynomial::multiply_monomial(Monomial m) {
Polynomial* current = this;
while(current) {
current->m = current->m * m;
current = current->next;
}
}
// 4 not freed per polynomial
int main(int argc, char** argv) {
Polynomial p1(argv[1]), p2(argv[2]);
Polynomial sum = p1 + p2;
Polynomial diff = p1 - p2;
Polynomial prod = p1 * p2;
std::cout << p1.polynomial2string() << " + " << p2.polynomial2string() << " = " << sum.polynomial2string() << std::endl;
std::cout << p1.polynomial2string() << " - " << p2.polynomial2string() << " = " << diff.polynomial2string() << std::endl;
std::cout << p1.polynomial2string() << " * " << p2.polynomial2string() << " = " << prod.polynomial2string() << std::endl;
//Monomial m1(argv[3]);
//p1.multiply_monomial(m1);
//std::cout << p1.polynomial2string() << std::endl;
return 0;
}