-
Notifications
You must be signed in to change notification settings - Fork 0
/
Polynomial.cpp
282 lines (237 loc) · 7.04 KB
/
Polynomial.cpp
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
#include "Polynomial.hpp"
/* Default constructor */
Polynomial::Polynomial() {
}
/* takes a c-string with a polynomial in the given format "exp coeff exp coeff etc..." */
Polynomial::Polynomial( char * poly ) {
/* have to assume that the string is null terminated */
char curr[SLEN];
int index = 0;
curr[0] = '\0';
double coeff = 0.0f;
int exp = 0;
// true => coeff, false => exp
bool mode = true;
for( int i = 0; (i < SLEN) && (poly[i] != '\0'); ++i )
{
if( (poly[i] == ' ') && mode )
{
curr[index] = '\0';
coeff = atof(curr);
mode = false;
index = 0;
}
else if( (poly[i] == ' ') && !mode )
{
curr[index] = '\0';
exp = atoi(curr);
mode = true;
addTerm( coeff, exp );
index = 0;
}
else
{
curr[index] = poly[i];
index++;
}
}
// the last set of terms will have been collected, but not trigger poly[i] == ' '
curr[index] = '\0';
exp = atoi(curr);
addTerm( coeff, exp );
}
/* copy constructor */
Polynomial::Polynomial( const Polynomial& other ) {
copy( other );
}
/* copy method that just calls LinkedList copy */
void Polynomial::copy( const Polynomial& other ) {
_list.copy( other._list );
}
/* returns the degree of the polynomial, useful for the division function */
int Polynomial::degree() const {
return _list.get(0).degree();
}
/* adds a Term to the Polynomial */
void Polynomial::addTerm( const Term& aTerm ) {
addTerm( aTerm.coeff, aTerm.exp );
}
/* adds a term to the Polynomial given coeff and exp */
void Polynomial::addTerm( double coeff, int exp ) {
/* do not add terms with 0 coeff to the list */
if( coeff == 0 )
return;
Term term( coeff, exp );
/* check if the term is already in the list */
for( itr_t it = _list.begin(); it != _list.end(); ++it )
{
int curr_exp = it.getData().exp;
if( curr_exp == exp )
{
*(it.getIter()->_data) += term;
if( it.getData().coeff == 0 ) // if 0 then don't add it
_list.remove( it );
return;
}
if( curr_exp < exp )
break;
}
_list.insert( term );
}
/* turns out I don't actually need the subtraction function, but I already had it */
/* just reverse the coefficient and add it */
void Polynomial::subTerm( double coeff, int exp ) {
addTerm( -1 * coeff, exp );
}
/* destructor doesn't need to do anything */
Polynomial::~Polynomial() {
}
/* add two polynomials together */
const Polynomial Polynomial::add( const Polynomial& rhs ) const {
Polynomial retVal;
for( itr_t it = _list.begin(); it != _list.end(); ++it )
retVal.addTerm( it.getData().coeff, it.getData().exp );
for( itr_t it = rhs._list.begin(); it != rhs._list.end(); ++it )
retVal.addTerm( it.getData().coeff, it.getData().exp );
return retVal;
}
/* again, I don't actually need this function */
const Polynomial Polynomial::sub( const Polynomial& rhs ) const {
Polynomial retVal;
for( itr_t it = _list.begin(); it != _list.end(); ++it )
retVal.addTerm( it.getData().coeff, it.getData().exp );
for( itr_t it = rhs._list.begin(); it != rhs._list.end(); ++it )
retVal.subTerm( it.getData().coeff, it.getData().exp );
return retVal;
}
/* multiply two polynomials together */
const Polynomial Polynomial::mult( const Polynomial& rhs ) const {
Polynomial retVal;
for( itr_t it = _list.begin(); it != _list.end(); ++it )
{
for( itr_t rit = rhs._list.begin(); rit != rhs._list.end(); ++rit )
{
Term aterm = it.getData() * rit.getData();
if( aterm.coeff != 0 ) // don't have to have this check, since addTerm() already does it, but it can save a function jump
{
retVal.addTerm( aterm.coeff, aterm.exp );
}
}
}
return retVal;
}
// doesn't return a remainder
// N.B. this function will break if there is a remainder
const Polynomial Polynomial::div( const Polynomial& rhs ) const {
Polynomial retVal;
if( degree() < rhs.degree() )
{
return retVal; // return 0
}
Polynomial rSide( *this );
int rDeg = rhs.degree();
double rCoeff = rhs._list.begin().getData().coeff;
itr_t it = rSide._list.begin();
while( 1 )
{
if( it == rSide._list.end() ) break;
int deg_diff = it.getData().degree() - rDeg;
if( deg_diff < 0 ) break; // TODO: check this condition, maybe need to put rest into remainder?
double coeff = it.getData().coeff / rCoeff;
Polynomial tmp;
Term multiplier( coeff, deg_diff );
retVal.addTerm( multiplier );
for( itr_t itt = rhs._list.begin(); itt != rhs._list.end(); ++itt )
{
Term res = itt.getData() * multiplier;
tmp.addTerm( res );
}
rSide = rSide.sub( tmp );
it = rSide._list.begin();
}
return retVal;
}
/* evaluates the polynomial at a given point */
double Polynomial::eval( double point ) {
double retVal = 0;
/* just iterate through list and add the result */
for( itr_t it = _list.begin(); it != _list.end(); ++it )
{
retVal += it.getData().coeff * pow( point, it.getData().exp );
}
return retVal;
}
/* differentiate the polynomial and return */
/* since this class only holds polynomials, */
/* differentiation is simple, just multiply */
/* the coefficient by the exponent, and */
/* decrement the coefficient */
const Polynomial Polynomial::differentiate() {
Polynomial retVal;
for( itr_t it = _list.begin(); it != _list.end(); ++it )
{
int exp = it.getData().exp;
double coeff = it.getIter()->_data->coeff * exp;
if( (coeff != 0) && (exp != 0) )
{
exp--;
retVal.addTerm( coeff, exp );
}
}
return retVal;
}
/* integrate the polynomial and return */
/* since this class only holds polynomials */
/* itegration is easy. Just increment the */
/* exponent and divide the coefficient by it */
const Polynomial Polynomial::integrate() {
Polynomial retVal;
for( itr_t it = _list.begin(); it != _list.end(); ++it )
{
int exp = it.getData().exp;
exp++;
double coeff = it.getIter()->_data->coeff / static_cast<double>(exp);
retVal.addTerm( coeff, exp );
// no way for a term to now be zero, so don't need to check
}
return retVal;
}
/* print the polynomial to the given ostream */
/* don't need to check for 0 coefficients */
/* because there is no way for them to have */
/* been addded to the Polynomial in the first place */
void Polynomial::print( std::ostream& os ) const {
itr_t it = _list.begin();
if( it == _list.end() )
{
cout << "0";
return;
}
if( it != _list.end() )
{
if( it.getData().coeff < 0.0f )
os << "-";
if( it != _list.begin() )
os << " ";
os << it.getData() << " ";
}
it++;
for( ; it != _list.end(); ++it )
{
if( it.getData().coeff < 0.0f )
os << "- ";
else
os << "+ ";
os << it.getData() << " ";
}
}
/* overload operator::= to be the copy constructor */
Polynomial& Polynomial::operator=( const Polynomial& rhs ) {
copy( rhs );
return *this;
}
/* overload the insertion operator to print the polynomial */
std::ostream& operator<<( std::ostream& os, const Polynomial& rhs ) {
rhs.print( os );
return os;
}