-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonomial.cpp
More file actions
334 lines (274 loc) · 9.56 KB
/
monomial.cpp
File metadata and controls
334 lines (274 loc) · 9.56 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
#include <iostream>
#include <string>
#include <cmath>
#include "fraction.cpp"
class Monomial
{
public:
// constructors
Monomial();
Monomial(std::string args);
// operator overloading
Monomial operator+(Monomial m);
Monomial operator-(Monomial m);
Monomial operator*(Monomial m);
Monomial operator/(Monomial m);
void operator=(Monomial m);
template <class T> float evaluate(T x);
Monomial derivative();
Monomial indefinite_integral();
template <class T> float definite_integral(T x1, T x2);
void string2monomial(std::string args);
std::string monomial2string();
void print();
Fraction get_coefficient();
std::string get_base();
Fraction get_power();
void set_coefficient(Fraction f);
void set_base(std::string base);
void set_power(Fraction f);
void is_like_monomial(Monomial m1, Monomial m2);
private:
Fraction coefficient;
std::string base;
Fraction power;
};
Monomial::Monomial() {
this->base = "x";
}
Monomial::Monomial(std::string args) {
this->string2monomial(args);
}
Monomial Monomial::operator+(Monomial m) {
is_like_monomial(*this, m);
Monomial temp;
temp.set_coefficient(this->coefficient + m.get_coefficient());
temp.set_base(this->base);
Fraction zero;
if(temp.get_coefficient() == zero) {
temp.set_power(zero);
} else {
temp.set_power(this->power);
}
return temp;
}
Monomial Monomial::operator-(Monomial m) {
is_like_monomial(*this, m);
Monomial temp;
temp.set_coefficient(this->coefficient - m.get_coefficient());
temp.set_base(this->base);
Fraction zero;
if(temp.get_coefficient() == zero) {
temp.set_power(zero);
} else {
temp.set_power(this->power);
}
return temp;
}
Monomial Monomial::operator*(Monomial m) {
if(this->base != m.get_base()) {
std::cout << "Monomials with different bases not supported for multiplication." << std::endl;
exit(0);
}
Monomial temp;
temp.set_coefficient(this->coefficient * m.get_coefficient());
temp.set_base(this->base);
Fraction zero;
if(temp.get_coefficient() == zero) {
temp.set_power(zero);
} else {
temp.set_power(this->power + m.get_power());
}
return temp;
}
Monomial Monomial::operator/(Monomial m) {
if(this->base != m.get_base()) {
std::cout << "Monomials with different bases not supported for division." << std::endl;
exit(0);
}
Fraction zero;
if(m.get_coefficient() == zero) {
std::cout << "Division by zero not allowed." << std::endl;
exit(0);
}
Monomial temp;
temp.set_coefficient(this->coefficient / m.get_coefficient());
temp.set_base(this->base);
temp.set_power(this->power - m.get_power());
return temp;
}
void Monomial::operator=(Monomial m) {
this->set_coefficient(m.get_coefficient());
this->set_base(m.get_base());
this->set_power(m.get_power());
}
template <class T> float Monomial::evaluate(T x) {
float c = this->coefficient.value();
float p = this->power.value();
float v = (float)pow((double)x, (double) p);
return c*v;
}
Monomial Monomial::derivative() {
Monomial temp;
// if m is not a constant
if((int) this->power.value() != 0) {
temp.set_coefficient(this->coefficient * this->power);
temp.set_base(this->base);
temp.set_power(this->power.subtract(1));
}
return temp;
}
Monomial Monomial::indefinite_integral() {
Monomial temp;
// if m is x^-1 type
if(this->power.value() == -1) {
std::cout << "Integration is out of scope of the polynomial range." << std::endl;
exit(0);
} else {
temp.set_coefficient(this->coefficient / this->power.add(1));
temp.set_base(this->base);
if(temp.get_coefficient().value() == 0) {
Fraction zero;
temp.set_power(zero);
} else {
temp.set_power(this->power.add(1));
}
}
return temp;
}
template <class T> float Monomial::definite_integral(T x1, T x2) {
Monomial t = this->indefinite_integral();
float X1 = t.evaluate(x1);
float X2 = t.evaluate(x2);
return X2-X1;
}
void Monomial::string2monomial(std::string args) {
size_t start = args.find("*");
size_t end = args.find("^");
std::string coefficient_string;
std::string power_string;
if(start != std::string::npos && end != std::string::npos) { // 2*x^3 type or 0*x^3 type
//printf("1\n");
coefficient_string = args.substr(0, start);
base = args.substr(start+1, end - start - 1);
power_string = (coefficient_string == "0") ? "0" : args.substr(end+1);
} else if (start == std::string::npos && end != std::string::npos) { // x^3 type or -x^3 type
//printf("2\n");
if(args[0] == '-') {
coefficient_string = "-1";
base = args.substr(1, end-1);
} else {
coefficient_string = "1";
base = args.substr(0, end);
}
power_string = args.substr(end+1);
} else if (start != std::string::npos && end == std::string::npos) { // 2*x type or 0*x
//printf("3\n");
coefficient_string = args.substr(0, start);
base = args.substr(start+1);
power_string = (coefficient_string == "0") ? "0" : "1";
} else { // x type or or +x type or -x type or 1 type
//printf("4\n");
if((args[0] >= 'A' && args[0] <= 'Z') || (args[0] >= 'a' && args[0] <= 'z')){
coefficient_string = "1";
base = args;
power_string = "1";
} else if (args[0] == '-' && ((args[1] >= 'A' && args[1] <= 'Z') || (args[1] >= 'a' && args[1] <= 'z'))) {
coefficient_string = "-1";
base = args.substr(1);
power_string = "1";
} else if (args[0] == '+' && ((args[1] >= 'A' && args[1] <= 'Z') || (args[1] >= 'a' && args[1] <= 'z'))) {
coefficient_string = "1";
base = args.substr(1);
power_string = "1";
}else {
coefficient_string = args;
base = "x";
power_string = "0";
}
}
//std::cout << "coefficient:" << coefficient_string << "\tbase:" << base << "\tpower:" << power_string << std::endl;
Fraction t1(coefficient_string), t2(power_string);
this->coefficient = t1;
this->power = t2;
}
std::string Monomial::monomial2string() {
std::string temp = "";
// rule for constants only
if((int)this->power.value() == 0) {
temp += this->coefficient.fraction2string();
return temp;
}
// rule for coefficient to print
if(abs((int)this->coefficient.value()) != 1) {
temp += this->coefficient.fraction2string();
}
if((int)this->coefficient.value() == -1) {
temp += "-";
}
// rule for * to print
if(abs((int)this->coefficient.value()) != 1) {
temp += "*";
}
// rule for variable to print
if((int)this->power.value() != 0) {
temp += this->base;
}
// rule for power to print
if((int)this->power.value() != 0 && (int)this->power.value() != 1) {
temp += "^";
temp += this->power.fraction2string();
}
return temp;
}
void Monomial::print() {
std::cout << this->monomial2string();
}
Fraction Monomial::get_coefficient() {
return this->coefficient;
}
std::string Monomial::get_base() {
return this->base;
}
Fraction Monomial::get_power() {
return this->power;
}
void Monomial::set_coefficient(Fraction f) {
this->coefficient = f;
}
void Monomial::set_base(std::string base) {
this->base = base;
}
void Monomial::set_power(Fraction f) {
this->power = f;
}
void Monomial::is_like_monomial(Monomial m1, Monomial m2) {
// ensure the variables are the same
if(m1.get_base() != m2.get_base()) {
std::cout << "Monomials with different bases are unlike monomials. Not fit for addition/subtraction." << std::endl;
exit(0);
}
// ensure that monomials have same power
if(m1.get_power() != m2.get_power()) {
std::cout << "Monomials with different powers are unlike monomials. Not fit for addition/subtraction." << std::endl;
exit(0);
}
}
void check_monomial(std::string argv1, std::string argv2) {
Monomial m1(argv1), m2(argv2);
Monomial sum = m1+m2;
Monomial diff = m1-m2;
Monomial prod = m1 * m2;
Monomial div = m1 / m2;
Monomial dc = m1.derivative();
Monomial ii = m1.indefinite_integral();
std::cout << m1.monomial2string() << " + " << m2.monomial2string() << " = " << sum.monomial2string() << std::endl;
std::cout << m1.monomial2string() << " - " << m2.monomial2string() << " = " << diff.monomial2string() << std::endl;
std::cout << m1.monomial2string() << " * " << m2.monomial2string() << " = " << prod.monomial2string() << std::endl;
std::cout << m1.monomial2string() << " / " << m2.monomial2string() << " = " << div.monomial2string() << std::endl;
std::cout << m1.monomial2string() << " (5) = " << m1.evaluate(5) << std::endl;
std::cout << m1.monomial2string() << " (5.0) = " << m1.evaluate(5.0) << std::endl;
std::cout << "Derivative of " << m1.monomial2string() << " is " << dc.monomial2string() << std::endl;
std::cout << "Integral of " << m1.monomial2string() << " is " << ii.monomial2string() << std::endl;
std::cout << "Integration of = " << m1.monomial2string() << " from 0 to 1 is " << m1.definite_integral(0,1) << std::endl;
}