-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmethods.java
270 lines (251 loc) · 11.2 KB
/
methods.java
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
/*
* Copyright (c) 2021.
* ABDEL RAHMAN ALHERBAWi.
* Lorem ipsum dolor sit amet, consectetur adipiscing elit.
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
* Vestibulum commodo. Ut rhoncus gravida arcu.
* All Rights reserved.
*/
package NUM;
import java.math.BigDecimal;
import java.util.ArrayList;
public class methods {
static int dic;
static int num_of_it = 0;
static String out;
/** To find the value of the output compensation within the equation */
public static double fx(String str, double x) {
String finalStr = str.replaceAll("x", String.valueOf(x));
return new Object() {
int pos = -1, ch;
void nextChar() {
ch = (++pos < finalStr.length()) ? finalStr.charAt(pos) : -1;
}
boolean eat(int charToEat) {
while (ch == ' ') nextChar();
if (ch == charToEat) {
nextChar();
return true;
}
return false;
}
double parse() {
nextChar();
double x = parseExpression();
if (pos < finalStr.length()) throw new RuntimeException("Unexpected: " + (char) ch);
return truncateDecimal(x);
}
double parseExpression() {
double x = parseTerm();
for (; ; ) {
if (eat('+')) x += parseTerm(); // addition
else if (eat('-')) x -= parseTerm(); // subtraction
else return x;
}
}
double parseTerm() {
double x = parseFactor();
for (; ; ) {
if (eat('*')) x *= parseFactor(); // multiplication
else if (eat('/')) x /= parseFactor(); // division
else return x;
}
}
double parseFactor() {
if (eat('+')) return parseFactor(); // unary plus
if (eat('-')) return -parseFactor(); // unary minus
double x;
int startPos = this.pos;
if (eat('(')) { // parentheses
x = parseExpression();
eat(')');
} else if ((ch >= '0' && ch <= '9') || ch == '.') {
while ((ch >= '0' && ch <= '9') || ch == '.') nextChar();
x = Double.parseDouble(finalStr.substring(startPos, this.pos));
} else if (ch >= 'a' && ch <= 'z') {
while (ch >= 'a' && ch <= 'z') nextChar();
String func = finalStr.substring(startPos, this.pos);
x = parseFactor();
if (func.equals("sqrt")) x = Math.sqrt(x);
else if (func.equals("sin")) x = Math.sin(Math.toRadians(x));
else if (func.equals("cos")) x = Math.cos(Math.toRadians(x));
else if (func.equals("tan")) x = Math.tan(Math.toRadians(x));
else throw new RuntimeException("Unknown function: " + func);
} else {
throw new RuntimeException("Unexpected: " + (char) ch);
}
if (eat('^')) x = Math.pow(x, parseFactor());
return x;
}
}.parse();
}
/**THIS method TO find error between ( current and last ) or (true value and approximation )*/
public static double error(double cur, double prv) {
if(cur!=0) return truncateDecimal(Math.abs(((cur - prv) / cur) * 100));
else return 0; }
/** THIS METHOD TO CHOPPING THE NUMBER BY NUMBER OF DIGITS U ENTER */
public static double truncateDecimal(double x) {
return new BigDecimal(String.valueOf(x)).setScale(dic,BigDecimal.ROUND_FLOOR).doubleValue(); }
/***/
protected static String Bic_met(String equation, double a, double b, int max_num, double EP,
ArrayList<Double> arr_root, ArrayList<Double> ero_arr, ArrayList<Integer> num,
ArrayList<Double> upper, ArrayList<Double> lower) {
if (fx(equation, a) * fx(equation, b) < 0) {
while (num_of_it < max_num) {
if (num_of_it > 1)
if (ero_arr.get(num_of_it-1) <= EP) {
out= "Done";
break;
}
num.add(num_of_it);
///////////////////
upper.add(b);
lower.add(a);
double root = bic_root(a, b);
arr_root.add(root);
if (num_of_it >= 1) {
ero_arr.add(error(arr_root.get(num_of_it), arr_root.get(num_of_it - 1)));
}
if (fx(equation, root) == 0.0) {
out= "Done";
break;
} else if (fx(equation, a) * fx(equation, root) < 0) {
b = root;
++num_of_it;
return Bic_met(equation, a, b, max_num, EP, arr_root, ero_arr, num, upper, lower);
} else if (fx(equation, root) * fx(equation, b) < 0) {
a = root;
++num_of_it;
return Bic_met(equation, a, b, max_num, EP, arr_root, ero_arr, num, upper, lower);
} else out= "Not Allowed";
}
} else out= "Not Allowed";
return out;
}
private static double bic_root(double a, double b) { return truncateDecimal((a + b) / 2); }
/***/
protected static String false_met(String equation, double a, double b,
int max_num, double EP, ArrayList<Double> arr_root,
ArrayList<Double> ero_arr, ArrayList<Integer> num, ArrayList<Double> upper, ArrayList<Double> lower) {
if (fx(equation, a) * fx(equation, b) < 0) {
while (num_of_it < max_num) {
if (num_of_it > 1)
if (ero_arr.get(num_of_it - 1) <= EP) {
out= "Done";
break;
}
num.add(num_of_it);
upper.add(b);
lower.add(a);
double root = false_po_root(equation, a, b);
arr_root.add(root);
if (num_of_it >= 1) {
ero_arr.add(error(arr_root.get(num_of_it), arr_root.get(num_of_it - 1)));
}
if (fx(equation, root) == 0.0) {
out= "Done";
break;
} else if (fx(equation, a) * fx(equation, root) < 0) {
b = root;
++num_of_it;
return false_met(equation, a, b, max_num, EP, arr_root, ero_arr, num, upper, lower);
} else if (fx(equation, root) * fx(equation, b) < 0) {
a = root;
++num_of_it;
return false_met(equation, a, b, max_num, EP, arr_root, ero_arr, num, upper, lower);
} else out= "Not Allowed";
}
} else out= "Not Allowed";
return out;
}
private static double false_po_root(String equation, double a, double b) {
return truncateDecimal(b - ((fx(equation, b) * (a - b)) / (fx(equation, a) - fx(equation, b)))); }
/***/
protected static String Fixed_point_met(String equation, String eq_gx, double x, double EP, int max_num,
ArrayList<Integer> num, ArrayList<Double> arr_root, ArrayList<Double> ero_arr) {
while(num_of_it < max_num) {
if (num_of_it > 1)
if (ero_arr.get(num_of_it - 1) <= EP) {
out= "Done";break;
}
num.add(num_of_it);
arr_root.add(x);
double root = truncateDecimal(fx(eq_gx, x));
if (num_of_it >= 1)
ero_arr.add(error(arr_root.get(num_of_it), arr_root.get(num_of_it - 1)));
if (fx(equation, root) == 0.0) { out= "Done";break;
} else {
++num_of_it;
try{ return Fixed_point_met(equation, eq_gx, root, EP, max_num, num, arr_root, ero_arr); }catch (Exception e){
out = "Divergence";
}
}
}
return out;
}
/***/
protected static String Newton_met(String equation,String eq_gx, double x, double EP,int max_num,
ArrayList<Integer> num,ArrayList<Double> cur_root, ArrayList<Double> arr_root, ArrayList<Double> ero_arr) {
if (fx(eq_gx, x) != 0) {
while (num_of_it < max_num) {
if (num_of_it > 1)
if (ero_arr.get(num_of_it - 1) <= EP) {
out = "Done";
break;
}
num.add(num_of_it);
cur_root.add(x);
double root = newton_root(equation, eq_gx, x);
arr_root.add(root);
if (num_of_it >= 1)
ero_arr.add(error(arr_root.get(num_of_it), arr_root.get(num_of_it - 1)));
if (fx(equation, root) == 0.0) {
out = "Done";
break;
} else {
++num_of_it;
try {
return Newton_met(equation, eq_gx, root, EP, max_num, num, cur_root, arr_root, ero_arr);
} catch (Exception e) {
out = "Divergence";
}
}
}
return out;
}else {return "not allowed !"; }
}
private static double newton_root(String equation,String eq_gx, double x) {
return truncateDecimal( x-(fx(equation,x)/fx(eq_gx, x))); }
/***/
protected static String Secant_met(String equation, double iLast,double iCur, double EP
, int max_num, ArrayList<Integer> num, ArrayList<Double> IC, ArrayList<Double> IL, ArrayList<Double> NRoot, ArrayList<Double> ero_arr) {
while(num_of_it < max_num) {
if (num_of_it > 1)
if (ero_arr.get(num_of_it - 1) <= EP) {
out= "Done";break;
}
num.add(num_of_it);
IC.add(iCur);
IL.add(iLast);
double root = secant_root(equation, iCur, iLast);
NRoot.add(root);
if (num_of_it >= 1)
ero_arr.add(error(NRoot.get(num_of_it), NRoot.get(num_of_it - 1)));
if (fx(equation, root) == 0.0) {
out= "Done";break;
}else {
++num_of_it;
try{ return Secant_met(equation,iCur,root,EP,max_num,num, IC, IL, NRoot,ero_arr); }
catch (Exception e){
out = "Divergence";
}
}
}
return out;
}
private static double secant_root(String equation ,double a,double b) {
return truncateDecimal( a-(fx(equation,a)*( (a-b) / (fx(equation, a) - fx(equation, b)) ) ) );
}
}