-
Notifications
You must be signed in to change notification settings - Fork 0
/
variable.cpp
118 lines (99 loc) · 1.83 KB
/
variable.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
#include "variable.h"
variable::variable()
{
coefficient = 0.0;
powerX = 0.0;
powerY = 0.0;
}
variable::variable(double c,double myPowerX,double myPowerY)
{
coefficient = c;
powerX = myPowerX;
powerY = myPowerY;
}
variable::~variable()
{
}
variable variable::PartialDifferentiation(symbol s)
{
variable v = variable(coefficient, powerX, powerY);
if (s == symbol::x) {
v.coefficient = coefficient * powerX;
v.powerX = powerX - 1.0;
v.zerolization();
}
else if (s == symbol::y) {
v.coefficient = coefficient * powerY;
v.powerY = powerY - 1.0;
v.zerolization();
}
return v;
}
void variable::zerolization() {
if (std::abs(coefficient) <= 1.0e-31) {
coefficient = 0.0;
powerX = 0.0;
powerY = 0.0;
}
}
bool variable::isConstant() {
return(powerX == 0.0 && powerY == 0.0);
}
std::string variable::toString() {
std::ostringstream strs;
zerolization();
if (coefficient != 0.0) {
strs << coefficient;
}
if (powerX != 0.0) {
if (powerX == 1.0) {
strs << "x";
}
else {
strs << "x^" << powerX;
}
}
if (powerY != 0.0) {
if (powerY == 1.0) {
strs << "y";
}
else {
strs << "y^" << powerY;
}
}
return strs.str();
}
double variable::getValue(double x, double y) {
double result = 1;
if (std::abs(coefficient) < 1.0e-18) {
return 0;
}
else {
result *= coefficient;
}
if (std::abs(x) > 1.0e-18) {
if (std::abs(powerX) > 1.0e-18) {
result *= std::pow(x, powerX);
}
}
else
{
if (std::abs(powerX) > 1.0e-18) {
result *= 0;
}
}
if (std::abs(y) > 1.0e-18) {
if (std::abs(powerY) > 1.0e-18) {
result *= std::pow(y, powerY);
}
}
else
{
if (std::abs(powerY) > 1.0e-18) {
result *= 0;
}
}
//double result = coefficient * std::pow(x, powerX) * std::pow(y, powerY);
//std::cout<< toString()<<"(" << x << "," << y << ")=" << result << std::endl;
return result;
}