25package umontreal.ssj.functions;
27import umontreal.ssj.util.Misc;
35public class MathFunctionUtil {
40 public static double H = 1e-6;
42 private MathFunctionUtil() {
46 private static final double[] Cg = { 0, 0.17267316464601142812, 0.5, 0.82732683535398857188, 1 };
47 private static final double[] Wg = { 0.05, 0.27222222222222222222, 0.35555555555555555555, 0.27222222222222222222,
50 private static double[] fixBounds(MathFunction func,
double a,
double b,
int numIntervals) {
54 final double h = (b - a) / numIntervals;
56 while ((0 == func.evaluate(x)) && (x > a))
62 while ((0 == func.evaluate(x)) && (x < b))
66 double[] D = { a, b };
151 throw new IllegalArgumentException(
"n must not be negative");
154 final double err = Math.pow(h, 1.0 / n);
155 final double[] values =
new double[n + 1];
156 for (
int i = 0; i < values.length; i++)
157 values[i] = func.
evaluate(x + i * err);
158 for (
int j = 0; j < n; j++) {
159 for (
int i = 0; i < n - j; i++)
160 values[i] = values[i + 1] - values[i];
162 return values[0] / h;
175 final double fplus = func.
evaluate(x + h);
176 final double fminus = func.
evaluate(x - h);
177 return (fplus - fminus) / (2 * h);
195 throw new IllegalArgumentException(
"n must not be negative");
199 throw new IllegalArgumentException(
"n must be even");
200 final double err = Math.pow(h, 1.0 / n);
215 public static double[][]
removeNaNs(
double[] x,
double[] y) {
216 if (x.length != y.length)
217 throw new IllegalArgumentException();
219 for (
int i = 0; i < x.length; i++)
220 if (Double.isNaN(x[i]) || Double.isNaN(y[i]))
223 return new double[][] { x, y };
224 final double[] nx =
new double[x.length - numNaNs];
225 final double[] ny =
new double[y.length - numNaNs];
227 for (
int i = 0; i < x.length; i++)
228 if (!Double.isNaN(x[i]) && !Double.isNaN(y[i])) {
232 return new double[][] { nx, ny };
272 if (numIntervals % 2 != 0)
273 throw new IllegalArgumentException(
"numIntervals must be an even number");
274 if (Double.isInfinite(a) || Double.isInfinite(b) || Double.isNaN(a) || Double.isNaN(b))
275 throw new IllegalArgumentException(
"a and b must not be infinite or NaN");
277 throw new IllegalArgumentException(
"b < a");
280 double[] D = fixBounds(func, a, b, numIntervals);
283 final double h = (b - a) / numIntervals;
284 final double h2 = 2 * h;
285 final int m = numIntervals / 2;
287 for (
int i = 0; i < m - 1; i++) {
288 final double x = a + h + h2 * i;
313 throw new IllegalArgumentException(
"b < a");
314 if (Double.isInfinite(a) || Double.isInfinite(b) || Double.isNaN(a) || Double.isNaN(b))
315 throw new IllegalArgumentException(
"a or b is infinite or NaN");
318 double r0 = simpleGaussLob(func, a, b);
319 final double h = (b - a) / 2;
320 double r1 = simpleGaussLob(func, a, a + h) + simpleGaussLob(func, a + h, b);
321 double maxi = Math.max(1.0, Math.abs(r1));
322 if (Math.abs(r0 - r1) <= tol * maxi)
327 private static double simpleGaussLob(
MathFunction func,
double a,
double b) {
331 final double h = b - a;
333 for (
int i = 0; i < 5; i++) {
334 sum += Wg[i] * func.
evaluate(a + h * Cg[i]);
361 throw new IllegalArgumentException(
"b < a");
363 T[0] =
new double[1];
364 T[1] =
new double[1];
371 T[0] =
new double[n];
372 T[1] =
new double[n];
376 double res = innerGaussLob(func, a, b, tol, T, s);
378 double[] temp =
new double[n];
379 System.arraycopy(T[0], 0, temp, 0, n);
381 temp =
new double[n];
382 System.arraycopy(T[1], 0, temp, 0, n);
387 private static double innerGaussLob(
MathFunction func,
double a,
double b,
double tol,
double[][] T,
int[] s) {
388 double r0 = simpleGaussLob(func, a, b);
389 final double h = (b - a) / 2;
390 double r1 = simpleGaussLob(func, a, a + h) + simpleGaussLob(func, a + h, b);
391 if (Math.abs(r0 - r1) <= tol) {
394 if (len >= T[0].length) {
395 double[] temp =
new double[2 * len];
396 System.arraycopy(T[0], 0, temp, 0, len);
398 temp =
new double[2 * len];
399 System.arraycopy(T[1], 0, temp, 0, len);
407 return innerGaussLob(func, a, a + h, tol, T, s) + innerGaussLob(func, a + h, b, tol, T, s);
static int NUMINTERVALS
Default number of intervals for Simpson’s integral.
static double derivative(MathFunction func, double x)
Returns the first derivative of the function func evaluated at x.
static double finiteCenteredDifferenceDerivative(MathFunction func, double x, int n, double h)
Computes and returns an estimate of the th derivative of the function using finite centered differen...
static double simpsonIntegral(MathFunction func, double a, double b, int numIntervals)
Computes and returns an approximation of the integral of func over , using the Simpson’s method wit...
static double derivative(MathFunction func, double x, int n)
Returns the th derivative of function func evaluated at x.
static double H
Step length in to compute derivatives.
static double finiteDifferenceDerivative(MathFunction func, double x, int n, double h)
Computes and returns an estimate of the th derivative of the function .
static double gaussLobatto(MathFunction func, double a, double b, double tol)
Computes and returns a numerical approximation of the integral of.
static double[][] removeNaNs(double[] x, double[] y)
Removes any point (NaN, y) or (x, NaN) from x and y, and returns a 2D array containing the filtered p...
static double gaussLobatto(MathFunction func, double a, double b, double tol, double[][] T)
Similar to method gaussLobatto(MathFunction, double,double, double), but also returns in T[0] the sub...
static double integral(MathFunction func, double a, double b)
Returns the integral of the function func over .
static double finiteCenteredDifferenceDerivative(MathFunction func, double x, double h)
Returns , an estimate of the first derivative of using centered differences.
Represents a mathematical function whose th derivative can be computed using derivative(double,...
Represents a mathematical function whose derivative can be computed using derivative(double).
Represents a mathematical function whose integral can be computed by the integral(double,...
This interface should be implemented by classes which represent univariate mathematical functions.
double evaluate(double x)
Returns the value of the function evaluated at .