forked from Udunen/frc-ring-shooter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RootFinder.java
253 lines (235 loc) · 10.4 KB
/
RootFinder.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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class RootFinder {
public static List<Double> rootFinder(List<Double> equation, double error) {
List<List<Double>> derivatives = new ArrayList<>();
derivatives.add(equation);
// Take derivatives of the equation down to the quadratic
for (int i = 0; i < equation.size() - 3; i++) {
derivatives.add(takeDerivative(derivatives.get(i)));
}
// Find solutions to the quadratic
List<Double> roots = quadraticFormula(derivatives.get(derivatives.size() - 1));
Collections.sort(roots);
if (roots.size() != 2) {
System.out.println("Could not find roots :(");
return null;
}
derivatives.remove(derivatives.size() - 1); // Remove the quadratic now that we have its roots
for (int i = derivatives.size() - 1; i >= 0; i--) {
List<Double> newRoots = new ArrayList<>();
newRoots.add(newtonsMethod(derivatives.get(i), roots.get(0) - 20, error));
for (int j = 0; j < roots.size() - 1; j++) {
Double root;
if (functionOutput(derivatives.get(i), roots.get(j)) > functionOutput(derivatives.get(i), roots.get(j + 1))) {
root = binarySearchDescending(derivatives.get(i), roots.get(j), roots.get(j + 1), error);
} else {
root = binarySearchAscending(derivatives.get(i), roots.get(j), roots.get(j + 1), error);
}
if (root != null) {
newRoots.add(root);
}
}
newRoots.add(newtonsMethod(derivatives.get(i), roots.get(roots.size() - 1) + 20, error));
roots = newRoots;
}
return roots;
}
private static List<Double> takeDerivative(List<Double> equation) {
List<Double> derivative = new ArrayList<>();
int largestPower = equation.size() - 1;
for (int i = 0; i < largestPower; i++) {
int exponent = largestPower - i;
derivative.add(exponent * equation.get(i));
}
return derivative;
}
private static List<Double> quadraticFormula(List<Double> equation) {
double a = equation.get(0);
double b = equation.get(1);
double c = equation.get(2);
double discriminant = b * b - 4 * a * c;
if (discriminant < 0) {
return Collections.emptyList();
} else if (discriminant == 0) {
return Collections.singletonList(-b / (2 * a));
} else {
double squareRoot = Math.sqrt(discriminant);
return Arrays.asList((-b + squareRoot) / (2 * a), (-b - squareRoot) / (2 * a));
}
}
private static Double binarySearchDescending(List<Double> equation, double left, double right, double error) {
while (left <= right) {
double mid = (left + right) / 2;
double output = functionOutput(equation, mid);
if (withinRange(output, 0, error)) {
return mid;
} else if (output > 0) {
left = mid + error / 100;
} else {
right = mid - error / 100;
}
}
return null;
}
private static Double binarySearchAscending(List<Double> equation, double left, double right, double error) {
while (left <= right) {
double mid = (left + right) / 2;
double output = functionOutput(equation, mid);
if (withinRange(output, 0, error)) {
return mid;
} else if (output < 0) {
left = mid + error / 100;
} else {
right = mid - error / 100;
}
}
return null;
}
private static double functionOutput(List<Double> equation, double input) {
double result = 0;
int length = equation.size();
for (int i = 0; i < length; i++) {
result += equation.get(length - i - 1) * Math.pow(input, i);
}
return result;
}
private static boolean withinRange(double num, double target, double range) {
return num <= target + range && num >= target - range;
}
private static Double newtonsMethod(List<Double> equation, double guess, double error) {
List<Double> derivative = takeDerivative(equation);
double delta = Math.abs(functionOutput(equation, guess));
while (delta > error) {
guess = guess - functionOutput(equation, guess) / functionOutput(derivative, guess);
delta = Math.abs(functionOutput(equation, guess));
}
return guess;
}
}
// import java.util.ArrayList;
// import java.util.Arrays;
// import java.util.List;
// public class RootFinder {
// public static List<Double> rootFinder(List<Double> equation, double error) {
// // System.out.println("Equation: " + equation);
// List<List<Double>> derivatives = new ArrayList<>();
// derivatives.add(equation);
// for (int i = 0; i < equation.size() - 3; i++) {
// derivatives.add(takeDerivative(derivatives.get(i)));
// }
// // System.out.println("Derivatives: " + derivatives);
// List<Double> roots = quadraticFormula(derivatives.get(derivatives.size() - 1));
// // System.out.println("Roots: " + roots);
// if (roots.size() != 2) {
// System.out.println("Could not find roots :(");
// return null;
// }
// derivatives.remove(derivatives.size()-1);
// for (int i = derivatives.size() - 1; i >= 0; i--) {
// List<Double> newRoots = new ArrayList<>();
// newRoots.add(newtonsMethod(derivatives.get(i), roots.get(0) - 20, error));
// System.out.println(roots.size());
// for (int j = 0; j < roots.size() - 1; j++) {
// double root;
// System.out.println(functionOutput(derivatives.get(i), roots.get(j)) > functionOutput(derivatives.get(i), roots.get(j + 1)));
// // System.out.println(functionOutput(derivatives.get(i), roots.get(j)));
// System.out.println(derivatives.get(i));
// System.out.println(roots.get(j));
// System.out.println(roots.get(j+1));
// // System.out.println(functionOutput(derivatives.get(i), roots.get(j + 1)));
// if (functionOutput(derivatives.get(i), roots.get(j)) > functionOutput(derivatives.get(i), roots.get(j + 1))) {
// root = binarySearchDescending(derivatives.get(i), roots.get(j), roots.get(j + 1), error);
// // System.out.println(root);
// } else {
// root = binarySearchAscending(derivatives.get(i), roots.get(j), roots.get(j + 1), error);
// // System.out.println(root);
// }
// if (!Double.isNaN(root)) {
// newRoots.add(root);
// }
// }
// newRoots.add(newtonsMethod(derivatives.get(i), roots.get(roots.size()-1) + 20, error));
// roots = newRoots;
// // System.out.println(newRoots);
// }
// // System.out.println("Roots: " + roots);
// return roots;
// }
// public static List<Double> takeDerivative(List<Double> equation) {
// List<Double> derivative = new ArrayList<>();
// int largestPower = equation.size() - 1;
// for (int i = 0; i < largestPower; i++) {
// int exponent = largestPower - i;
// derivative.add(exponent * equation.get(i));
// }
// return derivative;
// }
// public static List<Double> quadraticFormula(List<Double> equation) {
// double a = equation.get(0);
// double b = equation.get(1);
// double c = equation.get(2);
// double discriminant = b * b - 4 * a * c;
// if (discriminant < 0) {
// return new ArrayList<>();
// } else if (discriminant == 0) {
// return Arrays.asList(-b / (2 * a));
// }
// double squareRoot = Math.sqrt(discriminant);
// return Arrays.asList((-b + squareRoot) / (2 * a), (-b - squareRoot) / (2 * a));
// }
// public static double binarySearchDescending(List<Double> equation, double left, double right, double error) {
// System.out.println(equation);
// System.out.println(left);
// System.out.println(right);
// System.out.println(error);
// while (left <= right) {
// double mid = (left + right) / 2;
// double output = functionOutput(equation, mid);
// if (withinRange(output, 0, error)) {
// return mid;
// } else if (output > 0) {
// left = mid + error / 100;
// } else {
// right = mid - error / 100;
// }
// }
// return Double.NaN;
// }
// public static double binarySearchAscending(List<Double> equation, double left, double right, double error) {
// while (left <= right) {
// double mid = (left + right) / 2;
// double output = functionOutput(equation, mid);
// if (withinRange(output, 0, error)) {
// return mid;
// } else if (output < 0) {
// left = mid + error / 100;
// } else {
// right = mid - error / 100;
// }
// }
// return Double.NaN;
// }
// public static double functionOutput(List<Double> equation, double input) {
// int length = equation.size();
// double result = 0.0;
// for (int i = length - 1; i >= 0; i--) {
// result += equation.get(length - i - 1) * Math.pow(input, i);
// }
// return result;
// }
// public static boolean withinRange(double num, double target, double range) {
// return num <= target + range && num >= target - range;
// }
// public static double newtonsMethod(List<Double> equation, double guess, double error) {
// List<Double> derivative = takeDerivative(equation);
// double delta = Math.abs(0 - functionOutput(equation, guess));
// while (delta > error) {
// guess = guess - functionOutput(equation, guess) / functionOutput(derivative, guess);
// delta = Math.abs(0 - functionOutput(equation, guess));
// }
// return guess;
// }
// }