-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathMathUtils.java
362 lines (263 loc) · 9.14 KB
/
MathUtils.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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package cadcore;
import java.awt.geom.Point2D;
import javax.vecmath.Point3d;
public class MathUtils {
public static double DEG_TO_RAD = Math.PI/180.0;
public interface Function
{
double f(double x);
}
public interface FunctionXY
{
Point2D.Double f(double x);
}
public interface FunctionXYZ
{
Point3d f(double x);
}
public interface DerivableFunction extends Function
{
double f(double x);
double fd(double x);
}
public interface TwoParamFunction
{
double f(double x, double y);
}
public interface TwoParamFunctionXY
{
Point2D.Double f(double x, double y);
}
public interface TwoParamFunctionXYZ
{
Point3d f(double x, double y);
}
public interface TwoParamDerivableFunction extends Function
{
double f(double x, double y);
double fd(double x, double y);
}
public static double clamp(double value, double minLimit, double maxLimit)
{
if(value < minLimit)
value = minLimit;
if(value > maxLimit)
value = maxLimit;
return value;
}
public static class RootFinder
{
static public double ROOTFINER_VALUE_TOLERANCE = 0.001;
static public double getRoot(Function function, double targetValue)
{
return getRoot(function, targetValue, 0.0, 1.0);
}
static public double getRoot(Function function, double targetValue, double minLimit, double maxLimit)
{
double x = SecantRootFinder.getRoot(function, targetValue, minLimit, maxLimit);
//Sanity check
if(Math.abs(function.f(x)-targetValue) > ROOTFINER_VALUE_TOLERANCE)
{
// System.out.printf("getRoot(): SecantRootFinder failed, x:%f, value:%f, targetValue:%f, error: %f\n", x, function.f(x), targetValue, function.f(x)-targetValue);
x = BisectRootFinder.getRoot(function, targetValue, minLimit, maxLimit);
if(Math.abs(function.f(x)-targetValue) > ROOTFINER_VALUE_TOLERANCE)
{
// System.out.printf("getRoot(): BisectRootFinder failed, x:%f, value:%f, targetValue:%f, error: %f\n", x, function.f(x), targetValue, function.f(x)-targetValue);
}
}
return x;
}
static public double getRoot(DerivableFunction function, double targetValue)
{
return getRoot(function, targetValue, 0.0, 1.0);
}
static public double getRoot(DerivableFunction function, double targetValue, double minLimit, double maxLimit)
{
double x = NewtonRaphsonRootFinder.getRoot(function, targetValue, minLimit, maxLimit);
//Sanity check
if(Math.abs(function.f(x)-targetValue) > ROOTFINER_VALUE_TOLERANCE)
{
// System.out.printf("getRoot(): SecantRootFinder failed, error: %f\n", function.f(x)-targetValue);
x = BisectRootFinder.getRoot(function, targetValue, minLimit, maxLimit);
}
return x;
}
public static class NewtonRaphsonRootFinder
{
static private int NEWTONRAPHSON_MAX_ITERATIONS = 50;
static public double getRoot(DerivableFunction function, double target_value, double minLimit, double maxLimit)
{
//Guess initial value
double valueAtMin = function.f(minLimit);
double valueAtMax = function.f(maxLimit);
double x = (target_value-valueAtMin)/(valueAtMax-valueAtMin);
x = clamp(x,minLimit,maxLimit);
double value = function.f(x);
double error = target_value-value;
// double lasterror = error;
int n = 0;
while(Math.abs(error) > ROOTFINER_VALUE_TOLERANCE && n++ < NEWTONRAPHSON_MAX_ITERATIONS)
{
double currentSlope = 1/function.fd(x);
x = x + (error*currentSlope);
value = function.f(x);
error = target_value-value;
}
return x;
}
}
public static class SecantRootFinder
{
static private int SECANT_MAX_ITERATIONS = 50;
static public double getRoot(Function function, double target_value, double minLimit, double maxLimit)
{
double valueAtMin = function.f(minLimit);
double valueAtMax = function.f(maxLimit);
double x = (target_value-valueAtMin)/(valueAtMax-valueAtMin);
x = clamp(x,minLimit,maxLimit);
double last_x = x + (maxLimit-minLimit)/10.0;
if(last_x > maxLimit)
last_x = x - (maxLimit-minLimit)/10.0;
//Search for the angle using secant method
double current_value = function.f(x);
double last_value = function.f(last_x);
double current_error = target_value-current_value;
// System.out.printf("SecantRootFinder.getRoot(): START current_error:%f, x:%f last_value:%f current_value:%f target_value:%f\n", current_error, x, last_value, current_value, target_value);
int n = 0;
while(Math.abs(current_error) > ROOTFINER_VALUE_TOLERANCE && n++ < SECANT_MAX_ITERATIONS && current_value != last_value)
{
double d = ((x-last_x)/(current_value-last_value))*current_error;
last_x = x;
x = x + d;
x = clamp(x,minLimit,maxLimit);
last_value = current_value;
current_value = function.f(x);
current_error = target_value-current_value;
// System.out.printf("SecantRootFinder.getRoot(): d:%f, current_error:%f, x:%f last_value:%f current_value:%f target_value:%f\n", d, current_error, x, last_value, current_value, target_value);
}
// System.out.printf("SecantRootFinder.getRoot(): target_value:%f, current_value:%f current_error:%f\n", target_value, current_error);
return x;
}
}
public static class BisectRootFinder
{
static private int BISECT_MAX_ITERATIONS = 50;
static public double getRoot(Function function, double target_value, double minLimit, double maxLimit)
{
int n = 0;
double lt = minLimit;
double ht = maxLimit;
double l_error = function.f(lt)-target_value;
double h_error = function.f(ht)-target_value;
// if(l_error*h_error > 0)
// return 0.0; //NO ROOT WITHIN LIMITS? FUNCTION NOT SUITABLE FOR BISECT METHOD
double current_error = 100000000;
double x = 0;
while(Math.abs(current_error) > ROOTFINER_VALUE_TOLERANCE && n++ < BISECT_MAX_ITERATIONS && ht-lt > 0.0001)
{
x = (ht+lt)/2.0;
double current_value = function.f(x);
current_error = current_value-target_value;
if(current_error*l_error < 0.0) //if these two positions have the opposite sign, then the root must be within the this span
{
// System.out.printf("low increased\n");
ht = x;
h_error = current_error;
}
else
{
// System.out.printf("high decreased\n");
lt = x;
l_error = current_error;
}
// System.out.printf("BisectRootFinder.getRoot(): current_error:%f, x:%f lt:%f ht:%f current_value:%f target_value:%f \n", current_error, x, lt, ht, current_value, target_value);
}
// System.out.printf("BisectRootFinder.getRoot(): n:%d\n", n);
return x;
}
}
}
public static class Integral
{
static public double getIntegral(Function function, double min, double max, int splits)
{
return SimpsonsRuleIntegral.getIntegral(function, min, max, splits);
}
static public double getIntegral(FunctionXY function, double min, double max, int splits)
{
return TrapezoidRuleIntegral.getIntegral(function, min, max, splits);
}
public static class SimpsonsRuleIntegral
{
public static double getIntegral(Function func, double min, double max, double splits)
{
double result = 0;
double m = (max-min)/splits;
double an = min;
double x0 = func.f(an);
for(int n = 0; n < splits; n++)
{
double am = an+m;
double x1 = func.f((an+am)/2);
double x2 = func.f(am);
if(Double.isNaN(x0))
{
x0 = 0;
}
if(Double.isNaN(x1))
{
x1 = 0;
}
if(Double.isNaN(x2))
{
x2 = 0;
}
double sum = ((am-an)/6)*(x0 + (4*x1) + x2);
result += sum;
an += m;
x0 = x2;
}
return result;
}
}
public static class TrapezoidRuleIntegral
{
static double getIntegral(FunctionXY func, double min, double max, int splits)
{
double result = 0;
double m = (max-min)/splits;
double an = min;
Point2D.Double x0 = func.f(an);
for(int n = 0; n < splits; n++)
{
an = an+m;
Point2D.Double x1 = func.f(an);
result += ((x0.y + x1.y)/2.0) * Math.abs(x1.x-x0.x);
x0 = x1;
}
return result;
}
}
}
public static class CurveLength
{
static public double getCurveLength(FunctionXY function, double min, double max, int splits)
{
float length = 0;
Point2D.Double last = function.f(min);
double step = (max-min)/splits;
for(int i = 1; i < splits+1; i++)
{
double x = min+(i*step);
Point2D.Double current = function.f(x);
length += VecMath.getVecLength(last.x, last.y, current.x, current.y);
last = current;
}
return length;
}
static public double getCurveLength(FunctionXY function, double min, double max)
{
return getCurveLength(function, min, max, 10);
}
}
}