This repository has been archived by the owner on Mar 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.max_min.h
233 lines (187 loc) · 4.76 KB
/
engine.max_min.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
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
#ifndef MAX_MIN_H
#define MAX_MIN_H
double x_one(double, double); //ax+b
double xp(double, double, double); //-b+sqrt(b^2-4ac)/2a
double xn(double, double, double); //-b-sqrt(b^2-4ac)/2a
array<double> SDforx3(double, double, double, double); //ax^3+bx^2+cx+d
array<double> critical_x(string);
void findRelativeMinMax(array<string> terms, const char &var);
void findRelativeMinMax(array<string> terms, const char &var) {
array<string> diffedTerms = splitTerm(diffExpr(terms, var));
double a = 0, b = 0, c = 0;
for (unsigned short i = 0; i < diffedTerms.length; i++) {
TermComponents tc(diffedTerms[i], var);
// checks tc
if (tc.factors.length > 1) error("only one factor per term in this mode is allowed");
else if (tc.factors.length && parseNum(tc.factors[0].n) > 2) error("power of n in this mode is maximum at 3", 3);
if (!tc.factors.length) c = tc.a;
else if (tc.factors[0].n == "1") b = tc.a;
else if (tc.factors[0].n == "2") a = tc.a;
}
if (b*b-4*a*c < 0) error("imagine number is not allowed in this mode", 2);
double c1 = 0, c2 = 0;
if (a) {
c1 = (-b - sqrt(b*b-4*a*c)) / (2*a);
c2 = (-b + sqrt(b*b-4*a*c)) / (2*a);
double p1 = evalExpr(terms, c1, var);
double p2 = evalExpr(terms, c2, var);
if (p1 > p2) { // swap
double temp_c = c1;
double temp_p = p1;
c1 = c2;
c2 = temp_c;
p1 = p2;
p2 = temp_p;
}
std::cout << "relative min is [" << c1 << ", " << p1 << "]\n";
std::cout << "relative max is [" << c2 << ", " << p2 << "]\n";
}
else if (b) {
c1 = -c/b;
double p1 = evalExpr(terms, c1, var);
if (evalExpr(splitTerm(diffExpr(diffedTerms, var)), c1, var) > 0) {
std::cout << "relative min is [" << c1 << ", " << p1 << "]\n";
}
else {
std::cout << "relative max is [" << c1 << ", " << p1 << "]\n";
}
}
}
array<double> critical_x(string equation)
{
int option;
array<string> term = splitTerm(equation);
array<double> x_value;
for (unsigned i = 0; i < equation.length; i++)
{
if (equation[i] == '^')
option = equation[i + 1];
}
if (option == 1)
{
double a1, b1;
for (unsigned i = 0; i < option + 1; i++)
{
string power = term[i].slice(term[i].length - 1, term[i].length);
if (power == "x")
a1 = parseNum(term[i]);
else
b1 = parseNum(term[i]);
}
x_value.push(x_one(a1, b1));
}
else if (option == 2)
{
double a2, b2, c2;
double x21, x22;
for (unsigned i = 0; i < option + 1; i++)
{
string power = term[i].slice(term[i].length - 2, term[i].length);
if (power == "^2")
a2 = parseNum(term[i]);
else if (power.includes("x"))
b2 = parseNum(term[i]);
else
c2 = parseNum(term[i]);
}
x_value.push(xp(a2, b2, c2));
x_value.push(xn(a2, b2, c2));
}
else if (option == 3)
{
double a3, b3, c3, d3;
for (unsigned i = 0; i < option + 1; i++)
{
string power = term[i].slice(term[i].length - 2, term[i].length);
if (power == "^3")
a3 = parseNum(term[i]);
else if (power == "^2")
b3 = parseNum(term[i]);
else if (power.includes("x"))
c3 = parseNum(term[i]);
else
d3 = parseNum(term[i]);
}
x_value = SDforx3(a3, b3, c3, d3);
}
}
double x_one(double A1, double B1)
{
double ans;
ans = (-1 * B1) / A1;
}
double xp(double A, double B, double C)
{
double ans1;
double t1 = sqrt(pow(B, 2) - (4 * A * C));
ans1 = ((-1 * B) + t1) / (2 * A);
return ans1;
}
double xn(double D, double E, double F)
{
double ans2;
double t1 = sqrt(pow(E, 2) - (4 * D * F));
ans2 = ((-1 * E) - t1) / (2 * D);
return ans2;
}
array<double> SDforx3(double A3, double B3, double C3, double D3)
{
array<double> ans;
double sum3 = 1;
double count1 = 0;
double count2 = 0;
for (double i = 0; i < 1000; i += 1)
{
double t31 = A3;
double t32 = A3 * i;
double t33 = t32 + B3;
double t34 = t33 * i;
double t35 = t34 + C3;
double t36 = t35 * i;
double t37 = t36 + D3;
sum3 = t37;
if (sum3 == 0)
break;
count1 += 1;
}
for (double i = 0; i > -1000; i -= 1)
{
double t31 = A3;
double t32 = A3 * i;
double t33 = t32 + B3;
double t34 = t33 * i;
double t35 = t34 + C3;
double t36 = t35 * i;
double t37 = t36 + D3;
sum3 = t37;
if (sum3 == 0)
break;
count2 -= 1;
}
if (count1 == 999)
{
double num1 = A3;
double num2 = B3 + A3 * count1;
double num3 = num2 * count1 + C3;
double xot1 = xp(num1, num2, num3);
double xot2 = xn(num1, num2, num3);
ans.push(count1*(-1));
ans.push(xot1);
ans.push(xot2);
}
else
{
double num1 = A3;
double num2 = B3 + A3 * count2;
double num3 = num2 * count2 + C3;
double xot1 = xp(num1, num2, num3);
double xot2 = xn(num1, num2, num3);
ans.push(count2*(-1));
ans.push(xot1);
ans.push(xot2);
}
if (count1 == 999 && count2 == -999)
error("CANNOT FIND");
return ans;
}
#endif