jhplot.math
Class Numeric
- java.lang.Object
-
- jhplot.math.Numeric
-
public final class Numeric extends java.lang.Object
Do some numerical calculations. Note: The origin of this code is unknown to me, but I suspect most of it was written by Daniel Lemire and Mark Hale for jScience project (licensed under the GNU).
-
-
Constructor Summary
Constructors Constructor and Description Numeric()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method and Description static double
adaptive(F1D func, double a, double b)
In adaptive quadrature we estimate the area under a curve in the interval from a to b twice, one using Q1 and once using Q2.static double[][]
differentiate(F1D func, double[] x, double[] dx)
Numerical differentiation in multiple dimensions.static double[]
differentiate(int N, F1D func, double a, double b)
Numerical differentiation.static double[]
euler(double[] y, F1D func, double dt)
Uses the Euler method to solve an ODE.static double
gaussian4(int N, F1D func, double a, double b)
Numerical integration using the Gaussian integration formula (4 points).static double
gaussian8(int N, F1D func, double a, double b)
Numerical integration using the Gaussian integration formula (8 points).static double[]
leapFrog(double[] y, F1D func, double dt)
Uses the Leap-Frog method to solve an ODE.static double[]
metropolis(double[] list, F1D func, double dx)
The Metropolis algorithm.static double
richardson(int N, F1D func, double a, double b)
Numerical integration using the Richardson extrapolation.static double[]
rungeKutta2(double[] y, F1D func, double dt)
Uses the 2nd order Runge-Kutta method to solve an ODE.static double[]
rungeKutta4(double[] y, F1D func, double dt)
Uses the 4th order Runge-Kutta method to solve an ODE.static double
simpson(int N, F1D func, double a, double b)
Numerical integration using Simpson's rule.static double[]
solveCubic(double a, double b, double c, double d)
Calculates the roots of the cubic equation, a must be different from 0 (or use quadratic equation solver).static double[]
solveLinear(double a, double b)
Calculates the roots of the linear equation, a must be different from 0.static double[]
solveQuadratic(double a, double b, double c)
Calculates the roots of the quadratic equation, a must be different from 0 (or use linear equation solver).static double[]
solveQuartic(double a, double b, double c, double d, double e)
Calculates the roots of the quartics equation, a must be different from 0 (or use cubic equation solver).static double
trapezium(int N, F1D func, double a, double b)
Numerical integration using the trapezium rule.static double
trapezium2D(int N, F2D func, double a1, double b1, double a2, double b2)
Numerical integration using the trapezium rule.
-
-
-
Method Detail
-
solveLinear
public static double[] solveLinear(double a, double b)
Calculates the roots of the linear equation, a must be different from 0. ax+b=0.- Returns:
- an array containing the root.
-
solveQuadratic
public static double[] solveQuadratic(double a, double b, double c)
Calculates the roots of the quadratic equation, a must be different from 0 (or use linear equation solver). Furthermore, if b*b-4.0*a*c < 0 then the solution is not defined on R. ax2+bx+c=0.- Returns:
- an array containing the two roots.
-
solveCubic
public static double[] solveCubic(double a, double b, double c, double d)
Calculates the roots of the cubic equation, a must be different from 0 (or use quadratic equation solver). Furthermore, there may be only one real solution. ax3+bx2+cx+d=0.- Returns:
- an array containing the three roots.
-
solveQuartic
public static double[] solveQuartic(double a, double b, double c, double d, double e)
Calculates the roots of the quartics equation, a must be different from 0 (or use cubic equation solver). ax4+bx3+cx2+dx+e=0. Furthermore, there may be no roots in R.- Returns:
- an array containing the four roots.
-
euler
public static double[] euler(double[] y, F1D func, double dt)
Uses the Euler method to solve an ODE.- Parameters:
y
- an array to be filled with y values, set y[0] to initial condition.func
- dy/dt as a function of y.dt
- step size.- Returns:
- y.
-
leapFrog
public static double[] leapFrog(double[] y, F1D func, double dt)
Uses the Leap-Frog method to solve an ODE.- Parameters:
y
- an array to be filled with y values, set y[0], y[1] to initial conditions.func
- dy/dt as a function of y.dt
- step size.- Returns:
- y.
-
rungeKutta2
public static double[] rungeKutta2(double[] y, F1D func, double dt)
Uses the 2nd order Runge-Kutta method to solve an ODE.- Parameters:
y
- an array to be filled with y values, set y[0] to initial condition.func
- dy/dt as a function of y.dt
- step size.- Returns:
- y.
-
rungeKutta4
public static double[] rungeKutta4(double[] y, F1D func, double dt)
Uses the 4th order Runge-Kutta method to solve an ODE.- Parameters:
y
- an array to be filled with y values, set y[0] to initial condition.func
- dy/dt as a function of y.dt
- step size.- Returns:
- y.
-
trapezium
public static double trapezium(int N, F1D func, double a, double b)
Numerical integration using the trapezium rule.- Parameters:
N
- the number of strips to use.func
- a function.a
- the first ordinate.b
- the last ordinate.
-
trapezium2D
public static double trapezium2D(int N, F2D func, double a1, double b1, double a2, double b2)
Numerical integration using the trapezium rule.- Parameters:
N
- the number of strips to use.func
- a function.a1
- the first ordinate in X.b1
- the last ordinate in X.a2
- the first ordinate in Y.b2
- the last ordinate in Y.
-
simpson
public static double simpson(int N, F1D func, double a, double b)
Numerical integration using Simpson's rule.- Parameters:
N
- the number of strip pairs to use.func
- a function.a
- the first ordinate.b
- the last ordinate.
-
richardson
public static double richardson(int N, F1D func, double a, double b)
Numerical integration using the Richardson extrapolation.- Parameters:
N
- the number of strip pairs to use (lower value).func
- a function.a
- the first ordinate.b
- the last ordinate.
-
adaptive
public static double adaptive(F1D func, double a, double b)
In adaptive quadrature we estimate the area under a curve in the interval from a to b twice, one using Q1 and once using Q2. If these two estimates are sufficiently close, we estimate the area using Q = Q2 + (Q2 - Q1)/ 15. Otherwise, we divide up the interval into two equal subintervals from a to c and c to b, where c is the midpoint (a + b) / 2. The iterations are finished if Math.abs(Q2 - Q1) <= EPSILON, EPSILON = 1E-6- Parameters:
F1D
- input functiona
- min Xb
- max X
-
gaussian4
public static double gaussian4(int N, F1D func, double a, double b)
Numerical integration using the Gaussian integration formula (4 points).- Parameters:
N
- the number of strips to use.func
- a function.a
- the first ordinate.b
- the last ordinate.
-
gaussian8
public static double gaussian8(int N, F1D func, double a, double b)
Numerical integration using the Gaussian integration formula (8 points).- Parameters:
N
- the number of strips to use.func
- a function.a
- the first ordinate.b
- the last ordinate.
-
differentiate
public static double[] differentiate(int N, F1D func, double a, double b)
Numerical differentiation.- Parameters:
N
- the number of points to use.func
- a function.a
- the first ordinate.b
- the last ordinate.
-
differentiate
public static double[][] differentiate(F1D func, double[] x, double[] dx)
Numerical differentiation in multiple dimensions.- Parameters:
func
- a function.x
- coordinates at which to differentiate about.dx
- step size.- Returns:
- an array Mij=dfi/dxj.
-
metropolis
public static double[] metropolis(double[] list, F1D func, double dx)
The Metropolis algorithm.- Parameters:
list
- an array to be filled with values distributed according to func, set list[0] to initial value.func
- distribution function.dx
- step size.- Returns:
- list.
-
-
DMelt 3.0 © DataMelt by jWork.ORG