-
Notifications
You must be signed in to change notification settings - Fork 0
/
Polynomial.h
executable file
·145 lines (114 loc) · 3.6 KB
/
Polynomial.h
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
/*
Standard Polynomial functionality
Supports addition, substraction, division, multiplication on degree 0 polynomials
Supports addition, multiplication on degree 1 polynomials
Supports solving for the roots of a degree 1 polynomial
*/
#include <vector>
#include <cmath>
using namespace std;
typedef double value_type;
#define POLYNOMIAL_EPS 1e-6
class Polynomial {
private:
vector<value_type> coeff;
public:
~Polynomial () {;}
Polynomial() {
coeff.resize(1);
coeff[0] = 0;
}
Polynomial (const value_type & value) {
coeff.resize(1);
coeff[0] = value;
}
Polynomial (const vector<value_type> & values) {
coeff = values;
}
Polynomial (string x) {
coeff.resize(2);
coeff[0] = 0;
coeff[1] = 1;
}
Polynomial operator- (void) const {
Polynomial result = *this;
for (int i = 0; i < degree(); ++i)
result.coeff[i] *= -1;
return result;
}
Polynomial operator+ (const Polynomial & right) const {
int max_degree = max (degree(), right.degree());
vector<value_type> new_coeff (max_degree);
for (int i = 0; i < max_degree; ++i) {
if (i < degree())
new_coeff[i] += coeff[i];
if (i < right.degree())
new_coeff[i] += right.coeff[i];
}
return Polynomial (new_coeff);
}
Polynomial operator- (const Polynomial & right) const {
// degree 0 only
if (degree() > 1 && right.degree() > 1) {
throw string ("Substraction not supported for polynomials of degree >= 1");
}
Polynomial result = *this;
result.coeff[0] -= right.coeff[0];
return result;
}
Polynomial operator* (const Polynomial & right) const {
if (degree() >= 2 && right.degree() >= 2) {
throw string ("Multiplication of polynomials of degree >= 2 not allowed");
}
int max_degree = max (degree(), right.degree());
vector<value_type> new_coeff (max_degree);
if (degree() == max_degree) {
new_coeff = coeff;
for (int i = 0; i < degree(); ++i)
new_coeff[i] *= right.coeff[0];
} else {
new_coeff = right.coeff;
for (int i = 0; i < right.degree(); ++i)
new_coeff[i] *= coeff[0];
}
return Polynomial (new_coeff);
}
Polynomial operator/ (const Polynomial & right) const {
// degree 0 only
if (right.degree() > 1) {
throw string ("Division not supported by polynomials of degree >= 1");
}
if (abs(right.coeff[0]) < POLYNOMIAL_EPS) {
throw string ("Can't divide polynomial by 0");
}
Polynomial result = *this;
for (int i = 0; i < degree(); ++i)
result.coeff[i] /= right.coeff[0];
return result;
}
int degree() const {
return coeff.size();
}
bool is_constant() const {
return coeff.size() == 1;
}
value_type get_0() const {
return coeff[0];
}
vector<value_type> get_coeff() const {
return coeff;
}
static Polynomial Zero() {
return Polynomial (0);
}
value_type solve_degree_1() const {
if (degree() == 0 || abs(coeff[1]) < POLYNOMIAL_EPS) {
if (abs(coeff[0]) < POLYNOMIAL_EPS) {
throw string ("Expression evaluates to 0, infinite number of solutions");
} else {
throw string ("Constant can't equal 0, no solutions");
}
}
return -coeff[0] / coeff[1];
}
};