public class NumericalMethods
extends java.lang.Object
Constructor and Description |
---|
NumericalMethods() |
Modifier and Type | Method and Description |
---|---|
static double |
average(double[] data)
Computes the arithemetic mean of all values in an array.
|
double |
average(double[] x,
double[] data,
double startX,
double endX)
Computes the arithmetic mean of a range of values in an array.
|
static double |
average(double[] data,
int start,
int end)
Computes the arithemetic mean of a range of values in an array.
|
static int |
binarySearch(double[] data,
double x,
int length)
Performs a binary search on an ordered list and returns the nearest
value less than the serach value.
|
static long |
computeCombinations(int n,
int r) |
static java.awt.geom.Point2D |
computeIntersection(java.awt.geom.Point2D P1,
java.awt.geom.Point2D P2,
java.awt.geom.Point2D P3,
java.awt.geom.Point2D P4)
Given two line segments defined by four points, this method computes the
intersection.
|
static long |
computePermutations(int n,
int r) |
double[] |
cubicSpline(double[] xIn,
double[] yIn,
double[] xOut)
This method performs an interpolation using a cubic spline.
|
static double[] |
derivative(double[] xArray,
double[] yArray,
int order)
Computes the nth order derivative of a set of
(x, y) points.
|
static double |
evaluatePolynomial(double x,
double[] coefficients)
Given an array of coefficients C0, C1, ..., Cn and a value for x,
compute y = C0 + C1 * x + C2 * x^2 + ... + Cn * x^n.
|
static int |
findLargestElement(double[] data)
Performs a search on an unordered list and returns the index of the
largest value in the array
|
static int |
findNearestElement(double[] data,
double value)
Performs a search on an ordered list and retrieves the element nearest
to the search value.
|
static int |
findNearestElementGreaterThan(double[] data,
double value)
Performs a search on an ordered list and returns the nearest value
greater than the serach value.
|
static int |
findNearestElementLessThan(double[] data,
double value)
Performs a search on an ordered list and returns the nearest value less
than the serach value.
|
static int |
findSmallestElement(double[] data)
Performs a search on an unordered list and returns the index of the
smallest value in the array
|
double |
integrate(double[] x,
double[] y,
double startX,
double endX,
int integraltype)
Perform numerical integration using the three composite quadrature
rules (Rectangular, Trapezoid, and Simpson) on the function whose
number is sent as a parameter.
|
static double |
integrate(double[] x,
double[] y,
int integraltype)
Perform numerical integration using the three composite quadrature
rules (Rectangular, Trapezoid, and Simpson) on the function whose
number is sent as a parameter.
|
static double |
integrate(double[] x,
double[] y,
int startX,
int endX,
int integraltype)
Perform numerical integration using the three composite quadrature
rules (Rectangular, Trapezoid, and Simpson) on the function whose
number is sent as a parameter.
|
static double[] |
interpolateSpectrumLagrange(double[] xIn,
double[] yIn,
double[] xOut,
int order,
boolean trim)
Performs a LaGrange interpolation over the provided data.
|
static double |
linearInterpolation(double targetX,
double x2,
double x1,
double y2,
double y1) |
static double[] |
linearSpline(double[] xIn,
double[] yIn,
double[] xOut)
This method performs an interpolation using a linear spline.
|
static double |
standardDeviation(double[] data)
Computes the stadard deviation of all values in an array.
|
double |
standardDeviation(double[] x,
double[] data,
double startX,
double endX)
Computes the standard deviation of a range of values in an array.
|
static double |
standardDeviation(double[] data,
int start,
int end)
Computes the standard deviation of a range of values in an array.
|
static double |
standardDeviationOnInterval(double[] domain,
double[] range,
double start,
double end)
Given an array of doubles, domain, and an array of doubles, range, this
function computes the standard deviation of the values of range over
a given interval in domain.
|
static double[] |
twoSidedDerivative(double[] xArray,
double[] yArray) |
static double[] |
twoSidedDerivative(double[] xArray,
double[] yArray,
int order)
Computes first order derivative of a set of points.
|
public static int findNearestElement(double[] data, double value)
data
- the ordered array to search.value
- the search value.public static int findNearestElementLessThan(double[] data, double value)
data
- the ordered array to search.value
- the search value.public static int findNearestElementGreaterThan(double[] data, double value)
data
- the ordered array to search.value
- the search value.public static int binarySearch(double[] data, double x, int length)
data
- the ordered array to search.x
- the search value.public static int findSmallestElement(double[] data)
data
- the ordered array to search.public static int findLargestElement(double[] data)
data
- the ordered array to search.public static double average(double[] data)
data
- array to be averaged.public static double average(double[] data, int start, int end)
data
- array containing elements to be averaged.start
- index of the first array element to average.end
- index of the last array element to average (inclusive).public double average(double[] x, double[] data, double startX, double endX)
data
- Array of Y values to averagex
- Array of X values corresponding to data arraystartX
- Minimum X value to average. It is not necessary for this
value to occur in the X array.endX
- Maximum X value to average. It is not necessary for this
value to occur in the X array.public static double standardDeviation(double[] data)
data
- array for which the standard deviation is to be computed.public static double standardDeviation(double[] data, int start, int end)
data
- array for which the standard deviation is to be computed.start
- index of the first array element to include.end
- index of the last array element to include (inclusive).public double standardDeviation(double[] x, double[] data, double startX, double endX)
data
- Array of Y values to averagex
- Array of X values corresponding to data arraystartX
- Minimum X value to consider as part of the standard
deviation. It is not necessary for this value to occur in the X array.endX
- Maximum X value to consider as part of the standard
deviation. It is not necessary for this value to occur in the X array.public static long computeCombinations(int n, int r)
public static long computePermutations(int n, int r)
public double integrate(double[] x, double[] y, double startX, double endX, int integraltype)
x
- the abscissas.y
- array of y values.startX
- the lower limit of the range of the integration.endX
- the upper limit of the range of the integration.integraltype
- the integral type.
public static double integrate(double[] x, double[] y, int startX, int endX, int integraltype)
x
- the abscissas.y
- array of y values.startX
- the lower limit of the range of the integration.endX
- the upper limit of the range of the integration.integraltype
- the integral type.
public static double integrate(double[] x, double[] y, int integraltype)
x
- the abscissas.y
- array of y values.integraltype
- the integral type.
public static double evaluatePolynomial(double x, double[] coefficients) throws java.lang.IllegalArgumentException
x
- array of x values.coefficients
- Polynomial coefficients in increasing exponential
order, starting with the constant (intercept) term.java.lang.IllegalArgumentException
public double[] cubicSpline(double[] xIn, double[] yIn, double[] xOut)
This method does cubic interpolation on (xIn, yIn) pairs to produce (xOut, yOut) pairs. Given (X, Y) pairs defined by xIn and yIn, and given a new baseline xOut array, this computes the corresponding Y values for each element in xOut.
xIn
- xIn data points. The length of this array must match the
length of yIn.yIn
- yIn data points. The length of this array must match the
length of xIn.xOut
- New X values for which to interpolate Y values.public static double[] linearSpline(double[] xIn, double[] yIn, double[] xOut)
xIn
- xIn data points. The length of this array must match the
length of yIn.yIn
- yIn data points. The length of this array must match the
length of xIn.xOut
- New X values for which to interpolate Y values.public static java.awt.geom.Point2D computeIntersection(java.awt.geom.Point2D P1, java.awt.geom.Point2D P2, java.awt.geom.Point2D P3, java.awt.geom.Point2D P4)
Note that this will work on any two line segments, even if the X values are equal for one line (i.e. where delta X is zero). This will return null if the lines are parallel (or coincident) or if the line segments do not intersect.
The derivation on this is ugly. Start with equations defining the
lines A and B in parametric form:
For A,
x_A(s) = x_A0 + (x_A1 - x_A0)s and y_A(s) = y_A0 + (y_A1 - y_A0)s (Equation 1)Do the same for B, but use parameter t instead of s:
x_B(t) = x_B0 + (x_B1 - x_B0)t and y_B(t) = y_B0 + (y_B1 - y_B0)t (Equation 2)Set the x and y equations equal:
x_A0 + (x_A1 - x_A0)s = x_B0 + (x_B1 - x_B0)t (Equation 3)
y_A0 + (y_A1 - y_A0)s = y_B0 + (y_B1 - y_B0)t (Equation 4)Solve Equation 3 for one parameter (in this case, t):
t = [x_A0 + (x_A1 - x_A0)s - x_B0]/(x_B1 - x_B0)Plug that into the second equation:
y_A0 + (y_A1 - y_A0)s = y_B0 + (y_B1 - y_B0){[x_A0 + (x_A1 - x_A0)s - x_B0]/(x_B1 - x_B0)}Solve for s. (wave hands here)
Plug this value of s back into Equation 1 to get the intersection in x and y. If s or t are outside of the range of [0, 1] then the lines do not actually intersect (the segments do not, but the infinite lines that they lie on do). This will return null in that case because it is important to know that the line segments do not overlap.
Line A is from point P1 to P2, and line B is from P3 to P4.
Taken from http://astronomy.swin.edu.au/~pbourke/geometry/lineline2d/ (page no longer available.)
P1
- Line A start point.P2
- Line A end point.P3
- Line B start point.P4
- Line B end point.public static double[] derivative(double[] xArray, double[] yArray, int order)
xArray
- array of x values.yArray
- array of y values.order
- the order of the derivative to take.public static double[] twoSidedDerivative(double[] xArray, double[] yArray, int order)
xArray
- array of x values.yArray
- array of y values.order
- Allows higher order derivatives to be taken in one function call.
The order parameter is optional, and if not specified, defaults to 1.public static double[] twoSidedDerivative(double[] xArray, double[] yArray)
public static double[] interpolateSpectrumLagrange(double[] xIn, double[] yIn, double[] xOut, int order, boolean trim)
xIn
- The known X-Values.yIn
- The known Y-Values.xOut
- The desired X-Values.order
- The order.trim
- Whether or not to trim.public static double linearInterpolation(double targetX, double x2, double x1, double y2, double y1) throws java.lang.IllegalArgumentException
java.lang.IllegalArgumentException
public static double standardDeviationOnInterval(double[] domain, double[] range, double start, double end)
domain
- the values that correspond to an independent variable.range
- the values that correspond to a variable dependent on the domain value.start
- the starting value of the interval of interest.end
- the ending value of the interval of interest.Copyright 2004-2006 Ocean Optics, Inc. All Rights Reserved.