-
Notifications
You must be signed in to change notification settings - Fork 0
/
Polynomial.hpp
202 lines (169 loc) · 5.3 KB
/
Polynomial.hpp
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
/*
* Polynomial.hpp
*
* Author: "Hans Dulimarta <dulimarh@cis.gvsu.edu>"
*
* Completed by:Harrison, Kevin
* Vanderhoef, Trevor
* Vansteel, Alexander
*/
#ifndef POLYNOM_H_
#define POLYNOM_H_
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include <climits>
#include <math.h>
#include <unordered_map> // we use a HASH MAP
using namespace std;
template<typename T>
class Polynomial {
private:
/* coeffient and exponent of the polynom are stored as a pair<> */
vector<pair<T,int>> poly;
public:
/* each term is a pair of coefficient and exponent, the type of
* coefficient is determined by <T>, but exponent is always an integer */
Polynomial() {
}
Polynomial(const string& input) {
load(input);
}
Polynomial(const Polynomial& other) {
poly = other.poly;
}
~Polynomial()
{
poly.clear();
}
Polynomial& operator= (const Polynomial& other) {
poly = other.poly;
return *this;
}
Polynomial operator* (const Polynomial & other) const {
Polynomial result;
Polynomial temp;
if(this->poly.size() != 0 && other.poly.size() != 0) {
for (auto x: this->poly) {
for (auto i: other.poly) {
temp.poly.push_back(make_pair(x.first * i.first, x.second + i.second));
}
}
sort(temp.poly.begin(), temp.poly.end(), exponent_comparator());
int expo = temp.maxDegree();
float coef = 0.0;
for (auto a: temp.poly) {
if (a.second == expo) {
coef += a.first;
}
else {
result.poly.push_back(make_pair(coef, expo));
expo = a.second;
coef = a.first;
}
}
result.poly.push_back(make_pair(coef, expo));
}
else {
result.poly.push_back(make_pair(0, 0));
}
return result;
}
Polynomial operator% (const Polynomial& other) const {
Polynomial result;
map<int, T> polys;
if(this->poly.size() != 0 && other.poly.size() != 0) {
for (auto x: this->poly) {
for (auto i: other.poly) {
T coe = x.first * i.first;
int exp = x.second + i.second;
polys[exp] += coe;
}
}
for (auto x: polys) {
result.poly.push_back(make_pair(x.second, x.first));
}
sort(result.poly.begin(), result.poly.end(), exponent_comparator());
}
else {
result.poly.push_back(make_pair(0, 0));
}
return result;
}
/* Return the highest degree in the polynomial */
int maxDegree() const {
return poly[0].second;
}
/* return the k-th exponent or zero when the polynom has no terms */
int operator% (int k) {
if(k < poly.size() && poly.size() != 0)
return poly[k].second;
return 0;
}
/* return the k-th coefficient, or zero when the polynom is empty */
T operator[] (int k) const {
if(k < poly.size() && poly.size() != 0)
return poly[k].first;
return 0;
}
T operator() (T arg) const {
double result = 0.0;
for(auto x: poly){
result += pow(arg, x.second) * x.first;
}
return result;
}
/* The following function "object" is needed for sorting
* the polynomial terms in descending order of the exponent */
struct exponent_comparator {
bool operator () (const pair<T,int>& a, const pair<T,int>& b) {
return a.second > b.second;
}
};
private:
/* The load function reads in a string representation of a polynomial
* and creates a vector of "polynomial terms".
* The input string has the following format:
*
* [coeff int] [coeff int] .....
*
* For instance, 3x^5 - 7x^2 + 11 can be represented as one of
* the following string (whitespaces do not matter)
*
* [3 5] [-7 2] [11 0]
* [3 5] [-7 2] [11 0]
*/
void load(const string& polystring) {
/* use a string input stream */
stringstream input (polystring);
const int L = polystring.length();
T coeff;
int expo = INT_MIN, last_expo;
bool sortNeeded = false;
/* skip the input, upto and including the '[' */
input.ignore(L, '[');
last_expo = expo;
/* our string input stream is like a file, so we can check for
* "end-of-file".... */
while (!input.eof()) {
input >> coeff >> expo;
input.ignore(L, ']');
if (fabs(coeff) > 1e-6) /* include only non-zero coeffs */
{
poly.push_back(make_pair(coeff,expo));
if (expo > last_expo)
sortNeeded = true;
last_expo = expo;
}
input.ignore(L, '[');
}
/* sort the terms in increasing order of exponents */
if (sortNeeded)
sort(poly.begin(), poly.end(), exponent_comparator());
}
};
#endif /* POLYNOM_H_ */