本文整理汇总了Java中org.apache.commons.math.util.FastMath.max方法的典型用法代码示例。如果您正苦于以下问题:Java FastMath.max方法的具体用法?Java FastMath.max怎么用?Java FastMath.max使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.math.util.FastMath
的用法示例。
在下文中一共展示了FastMath.max方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: estimateError
import org.apache.commons.math.util.FastMath; //导入方法依赖的package包/类
/** {@inheritDoc} */
@Override
protected double estimateError(final double[][] yDotK,
final double[] y0, final double[] y1,
final double h) {
double error = 0;
for (int j = 0; j < mainSetDimension; ++j) {
final double errSum = E1 * yDotK[0][j] + E3 * yDotK[2][j] +
E4 * yDotK[3][j] + E5 * yDotK[4][j] +
E6 * yDotK[5][j] + E7 * yDotK[6][j];
final double yScale = FastMath.max(FastMath.abs(y0[j]), FastMath.abs(y1[j]));
final double tol = (vecAbsoluteTolerance == null) ?
(scalAbsoluteTolerance + scalRelativeTolerance * yScale) :
(vecAbsoluteTolerance[j] + vecRelativeTolerance[j] * yScale);
final double ratio = h * errSum / tol;
error += ratio * ratio;
}
return FastMath.sqrt(error / mainSetDimension);
}
示例2: testSinFunction
import org.apache.commons.math.util.FastMath; //导入方法依赖的package包/类
@Test
public void testSinFunction() throws MathException {
UnivariateRealFunction f = new SinFunction();
UnivariateRealIntegratorImpl integrator = new LegendreGaussIntegrator(5, 64);
integrator.setAbsoluteAccuracy(1.0e-10);
integrator.setRelativeAccuracy(1.0e-14);
integrator.setMinimalIterationCount(2);
integrator.setMaximalIterationCount(15);
double min, max, expected, result, tolerance;
min = 0; max = FastMath.PI; expected = 2;
tolerance = FastMath.max(integrator.getAbsoluteAccuracy(),
FastMath.abs(expected * integrator.getRelativeAccuracy()));
result = integrator.integrate(f, min, max);
Assert.assertEquals(expected, result, tolerance);
min = -FastMath.PI/3; max = 0; expected = -0.5;
tolerance = FastMath.max(integrator.getAbsoluteAccuracy(),
FastMath.abs(expected * integrator.getRelativeAccuracy()));
result = integrator.integrate(f, min, max);
Assert.assertEquals(expected, result, tolerance);
}
示例3: testLinearFunction
import org.apache.commons.math.util.FastMath; //导入方法依赖的package包/类
/**
* Test of solver for the linear function.
*/
@Test
public void testLinearFunction() {
double min, max, expected, result, tolerance;
// p(x) = 4x - 1
double coefficients[] = { -1.0, 4.0 };
PolynomialFunction f = new PolynomialFunction(coefficients);
LaguerreSolver solver = new LaguerreSolver();
min = 0.0; max = 1.0; expected = 0.25;
tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
FastMath.abs(expected * solver.getRelativeAccuracy()));
result = solver.solve(100, f, min, max);
Assert.assertEquals(expected, result, tolerance);
}
示例4: testConsistency
import org.apache.commons.math.util.FastMath; //导入方法依赖的package包/类
/**
* Verifies that probability computations are consistent
*/
public void testConsistency() throws Exception {
for (int i=1; i < cumulativeTestPoints.length; i++) {
// check that cdf(x, x) = 0
TestUtils.assertEquals(0d,
distribution.cumulativeProbability
(cumulativeTestPoints[i], cumulativeTestPoints[i]), tolerance);
// check that P(a < X < b) = P(X < b) - P(X < a)
double upper = FastMath.max(cumulativeTestPoints[i], cumulativeTestPoints[i -1]);
double lower = FastMath.min(cumulativeTestPoints[i], cumulativeTestPoints[i -1]);
double diff = distribution.cumulativeProbability(upper) -
distribution.cumulativeProbability(lower);
double direct = distribution.cumulativeProbability(lower, upper);
TestUtils.assertEquals("Inconsistent cumulative probabilities for ("
+ lower + "," + upper + ")", diff, direct, tolerance);
}
}
示例5: converged
import org.apache.commons.math.util.FastMath; //导入方法依赖的package包/类
/**
* Check if the optimization algorithm has converged considering the
* last two points.
* This method may be called several time from the same algorithm
* iteration with different points. This can be detected by checking the
* iteration number at each call if needed. Each time this method is
* called, the previous and current point correspond to points with the
* same role at each iteration, so they can be compared. As an example,
* simplex-based algorithms call this method for all points of the simplex,
* not only for the best or worst ones.
*
* @param iteration Index of current iteration
* @param previous Best point in the previous iteration.
* @param current Best point in the current iteration.
* @return {@code true} if the algorithm has converged.
*/
@Override
public boolean converged(final int iteration,
final VectorialPointValuePair previous,
final VectorialPointValuePair current) {
final double[] p = previous.getValueRef();
final double[] c = current.getValueRef();
for (int i = 0; i < p.length; ++i) {
final double pi = p[i];
final double ci = c[i];
final double difference = FastMath.abs(pi - ci);
final double size = FastMath.max(FastMath.abs(pi), FastMath.abs(ci));
if (difference > size * getRelativeThreshold() &&
difference > getAbsoluteThreshold()) {
return false;
}
}
return true;
}
示例6: add
import org.apache.commons.math.util.FastMath; //导入方法依赖的package包/类
/**
* Add a polynomial to the instance.
*
* @param p Polynomial to add.
* @return a new polynomial which is the sum of the instance and {@code p}.
*/
public PolynomialFunction add(final PolynomialFunction p) {
// identify the lowest degree polynomial
final int lowLength = FastMath.min(coefficients.length, p.coefficients.length);
final int highLength = FastMath.max(coefficients.length, p.coefficients.length);
// build the coefficients array
double[] newCoefficients = new double[highLength];
for (int i = 0; i < lowLength; ++i) {
newCoefficients[i] = coefficients[i] + p.coefficients[i];
}
System.arraycopy((coefficients.length < p.coefficients.length) ?
p.coefficients : coefficients,
lowLength,
newCoefficients, lowLength,
highLength - lowLength);
return new PolynomialFunction(newCoefficients);
}
示例7: walkInRowOrder
import org.apache.commons.math.util.FastMath; //导入方法依赖的package包/类
/** {@inheritDoc} */
@Override
public double walkInRowOrder(final RealMatrixChangingVisitor visitor,
final int startRow, final int endRow,
final int startColumn, final int endColumn) {
MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn);
visitor.start(rows, columns, startRow, endRow, startColumn, endColumn);
for (int iBlock = startRow / BLOCK_SIZE; iBlock < 1 + endRow / BLOCK_SIZE; ++iBlock) {
final int p0 = iBlock * BLOCK_SIZE;
final int pStart = FastMath.max(startRow, p0);
final int pEnd = FastMath.min((iBlock + 1) * BLOCK_SIZE, 1 + endRow);
for (int p = pStart; p < pEnd; ++p) {
for (int jBlock = startColumn / BLOCK_SIZE; jBlock < 1 + endColumn / BLOCK_SIZE; ++jBlock) {
final int jWidth = blockWidth(jBlock);
final int q0 = jBlock * BLOCK_SIZE;
final int qStart = FastMath.max(startColumn, q0);
final int qEnd = FastMath.min((jBlock + 1) * BLOCK_SIZE, 1 + endColumn);
final double[] block = blocks[iBlock * blockColumns + jBlock];
int k = (p - p0) * jWidth + qStart - q0;
for (int q = qStart; q < qEnd; ++q) {
block[k] = visitor.visit(p, q, block[k]);
++k;
}
}
}
}
return visitor.end();
}
示例8: testQuinticFunction
import org.apache.commons.math.util.FastMath; //导入方法依赖的package包/类
/**
* Test of solver for the quintic function.
*/
@Test
public void testQuinticFunction() {
double min, max, expected, result, tolerance;
// p(x) = x^5 - x^4 - 12x^3 + x^2 - x - 12 = (x+1)(x+3)(x-4)(x^2-x+1)
double coefficients[] = { -12.0, -1.0, 1.0, -12.0, -1.0, 1.0 };
PolynomialFunction f = new PolynomialFunction(coefficients);
LaguerreSolver solver = new LaguerreSolver();
min = -2.0; max = 2.0; expected = -1.0;
tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
FastMath.abs(expected * solver.getRelativeAccuracy()));
result = solver.solve(100, f, min, max);
Assert.assertEquals(expected, result, tolerance);
min = -5.0; max = -2.5; expected = -3.0;
tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
FastMath.abs(expected * solver.getRelativeAccuracy()));
result = solver.solve(100, f, min, max);
Assert.assertEquals(expected, result, tolerance);
min = 3.0; max = 6.0; expected = 4.0;
tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
FastMath.abs(expected * solver.getRelativeAccuracy()));
result = solver.solve(100, f, min, max);
Assert.assertEquals(expected, result, tolerance);
}
示例9: estimateError
import org.apache.commons.math.util.FastMath; //导入方法依赖的package包/类
/** {@inheritDoc} */
@Override
protected double estimateError(final double[][] yDotK,
final double[] y0, final double[] y1,
final double h) {
double error1 = 0;
double error2 = 0;
for (int j = 0; j < mainSetDimension; ++j) {
final double errSum1 = E1_01 * yDotK[0][j] + E1_06 * yDotK[5][j] +
E1_07 * yDotK[6][j] + E1_08 * yDotK[7][j] +
E1_09 * yDotK[8][j] + E1_10 * yDotK[9][j] +
E1_11 * yDotK[10][j] + E1_12 * yDotK[11][j];
final double errSum2 = E2_01 * yDotK[0][j] + E2_06 * yDotK[5][j] +
E2_07 * yDotK[6][j] + E2_08 * yDotK[7][j] +
E2_09 * yDotK[8][j] + E2_10 * yDotK[9][j] +
E2_11 * yDotK[10][j] + E2_12 * yDotK[11][j];
final double yScale = FastMath.max(FastMath.abs(y0[j]), FastMath.abs(y1[j]));
final double tol = (vecAbsoluteTolerance == null) ?
(scalAbsoluteTolerance + scalRelativeTolerance * yScale) :
(vecAbsoluteTolerance[j] + vecRelativeTolerance[j] * yScale);
final double ratio1 = errSum1 / tol;
error1 += ratio1 * ratio1;
final double ratio2 = errSum2 / tol;
error2 += ratio2 * ratio2;
}
double den = error1 + 0.01 * error2;
if (den <= 0.0) {
den = 1.0;
}
return FastMath.abs(h) * error1 / FastMath.sqrt(mainSetDimension * den);
}
示例10: getLInfDistance
import org.apache.commons.math.util.FastMath; //导入方法依赖的package包/类
/** {@inheritDoc} */
@Override
public double getLInfDistance(RealVector v) {
if (v instanceof ArrayRealVector) {
return getLInfDistance(((ArrayRealVector) v).data);
} else {
checkVectorDimensions(v);
double max = 0;
for (int i = 0; i < data.length; ++i) {
final double delta = data[i] - v.getEntry(i);
max = FastMath.max(max, FastMath.abs(delta));
}
return max;
}
}
示例11: getLInfDistance
import org.apache.commons.math.util.FastMath; //导入方法依赖的package包/类
/** {@inheritDoc} */
public double getLInfDistance(RealVector v) {
checkVectorDimensions(v);
double d = 0;
Iterator<Entry> it = iterator();
Entry e;
while (it.hasNext() && (e = it.next()) != null) {
d = FastMath.max(FastMath.abs(e.getValue() - v.getEntry(e.getIndex())), d);
}
return d;
}
示例12: findUpperBound
import org.apache.commons.math.util.FastMath; //导入方法依赖的package包/类
/**
* Find the upper bound b ensuring bracketing of a root between a and b.
*
* @param f function whose root must be bracketed.
* @param a lower bound of the interval.
* @param h initial step to try.
* @return b such that f(a) and f(b) have opposite signs.
* @throws MathIllegalStateException if no bracket can be found.
* @throws org.apache.commons.math.exception.MathUserException if the
* function throws one.
*/
private double findUpperBound(final UnivariateRealFunction f,
final double a, final double h) {
final double yA = f.value(a);
double yB = yA;
for (double step = h; step < Double.MAX_VALUE; step *= FastMath.max(2, yA / yB)) {
final double b = a + step;
yB = f.value(b);
if (yA * yB <= 0) {
return b;
}
}
throw new MathIllegalStateException(LocalizedFormats.UNABLE_TO_BRACKET_OPTIMUM_IN_LINE_SEARCH);
}
示例13: solve
import org.apache.commons.math.util.FastMath; //导入方法依赖的package包/类
/**
* Find a real root in the given interval.
*
* @param min Lower bound for the interval.
* @param max Upper bound for the interval.
* @param fMin function value at the lower bound.
* @param fMax function value at the upper bound.
* @return the point at which the function value is zero.
*/
private double solve(double min, double max,
double fMin, double fMax) {
final double relativeAccuracy = getRelativeAccuracy();
final double absoluteAccuracy = getAbsoluteAccuracy();
final double functionValueAccuracy = getFunctionValueAccuracy();
// [x0, x2] is the bracketing interval in each iteration
// x1 is the last approximation and an interpolation point in (x0, x2)
// x is the new root approximation and new x1 for next round
// d01, d12, d012 are divided differences
double x0 = min;
double y0 = fMin;
double x2 = max;
double y2 = fMax;
double x1 = 0.5 * (x0 + x2);
double y1 = computeObjectiveValue(x1);
double oldx = Double.POSITIVE_INFINITY;
while (true) {
// Muller's method employs quadratic interpolation through
// x0, x1, x2 and x is the zero of the interpolating parabola.
// Due to bracketing condition, this parabola must have two
// real roots and we choose one in [x0, x2] to be x.
final double d01 = (y1 - y0) / (x1 - x0);
final double d12 = (y2 - y1) / (x2 - x1);
final double d012 = (d12 - d01) / (x2 - x0);
final double c1 = d01 + (x1 - x0) * d012;
final double delta = c1 * c1 - 4 * y1 * d012;
final double xplus = x1 + (-2.0 * y1) / (c1 + FastMath.sqrt(delta));
final double xminus = x1 + (-2.0 * y1) / (c1 - FastMath.sqrt(delta));
// xplus and xminus are two roots of parabola and at least
// one of them should lie in (x0, x2)
final double x = isSequence(x0, xplus, x2) ? xplus : xminus;
final double y = computeObjectiveValue(x);
// check for convergence
final double tolerance = FastMath.max(relativeAccuracy * FastMath.abs(x), absoluteAccuracy);
if (FastMath.abs(x - oldx) <= tolerance ||
FastMath.abs(y) <= functionValueAccuracy) {
return x;
}
// Bisect if convergence is too slow. Bisection would waste
// our calculation of x, hopefully it won't happen often.
// the real number equality test x == x1 is intentional and
// completes the proximity tests above it
boolean bisect = (x < x1 && (x1 - x0) > 0.95 * (x2 - x0)) ||
(x > x1 && (x2 - x1) > 0.95 * (x2 - x0)) ||
(x == x1);
// prepare the new bracketing interval for next iteration
if (!bisect) {
x0 = x < x1 ? x0 : x1;
y0 = x < x1 ? y0 : y1;
x2 = x > x1 ? x2 : x1;
y2 = x > x1 ? y2 : y1;
x1 = x; y1 = y;
oldx = x;
} else {
double xm = 0.5 * (x0 + x2);
double ym = computeObjectiveValue(xm);
if (MathUtils.sign(y0) + MathUtils.sign(ym) == 0.0) {
x2 = xm; y2 = ym;
} else {
x0 = xm; y0 = ym;
}
x1 = 0.5 * (x0 + x2);
y1 = computeObjectiveValue(x1);
oldx = Double.POSITIVE_INFINITY;
}
}
}
示例14: doSolve
import org.apache.commons.math.util.FastMath; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
protected double doSolve() {
double min = getMin();
double max = getMax();
// [x1, x2] is the bracketing interval in each iteration
// x3 is the midpoint of [x1, x2]
// x is the new root approximation and an endpoint of the new interval
double x1 = min;
double y1 = computeObjectiveValue(x1);
double x2 = max;
double y2 = computeObjectiveValue(x2);
// check for zeros before verifying bracketing
if (y1 == 0) {
return min;
}
if (y2 == 0) {
return max;
}
verifyBracketing(min, max);
final double absoluteAccuracy = getAbsoluteAccuracy();
final double functionValueAccuracy = getFunctionValueAccuracy();
final double relativeAccuracy = getRelativeAccuracy();
double oldx = Double.POSITIVE_INFINITY;
while (true) {
// calculate the new root approximation
final double x3 = 0.5 * (x1 + x2);
final double y3 = computeObjectiveValue(x3);
if (FastMath.abs(y3) <= functionValueAccuracy) {
return x3;
}
final double delta = 1 - (y1 * y2) / (y3 * y3); // delta > 1 due to bracketing
final double correction = (MathUtils.sign(y2) * MathUtils.sign(y3)) *
(x3 - x1) / FastMath.sqrt(delta);
final double x = x3 - correction; // correction != 0
final double y = computeObjectiveValue(x);
// check for convergence
final double tolerance = FastMath.max(relativeAccuracy * FastMath.abs(x), absoluteAccuracy);
if (FastMath.abs(x - oldx) <= tolerance) {
return x;
}
if (FastMath.abs(y) <= functionValueAccuracy) {
return x;
}
// prepare the new interval for next iteration
// Ridders' method guarantees x1 < x < x2
if (correction > 0.0) { // x1 < x < x3
if (MathUtils.sign(y1) + MathUtils.sign(y) == 0.0) {
x2 = x;
y2 = y;
} else {
x1 = x;
x2 = x3;
y1 = y;
y2 = y3;
}
} else { // x3 < x < x2
if (MathUtils.sign(y2) + MathUtils.sign(y) == 0.0) {
x1 = x;
y1 = y;
} else {
x1 = x3;
x2 = x;
y1 = y3;
y2 = y;
}
}
oldx = x;
}
}
示例15: CholeskyDecompositionImpl
import org.apache.commons.math.util.FastMath; //导入方法依赖的package包/类
/**
* Calculates the Cholesky decomposition of the given matrix.
* @param matrix the matrix to decompose
* @param relativeSymmetryThreshold threshold above which off-diagonal
* elements are considered too different and matrix not symmetric
* @param absolutePositivityThreshold threshold below which diagonal
* elements are considered null and matrix not positive definite
* @throws NonSquareMatrixException if the matrix is not square.
* @throws NonSymmetricMatrixException if the matrix is not symmetric.
* @throws NonPositiveDefiniteMatrixException if the matrix is not
* strictly positive definite.
* @see #CholeskyDecompositionImpl(RealMatrix)
* @see #DEFAULT_RELATIVE_SYMMETRY_THRESHOLD
* @see #DEFAULT_ABSOLUTE_POSITIVITY_THRESHOLD
*/
public CholeskyDecompositionImpl(final RealMatrix matrix,
final double relativeSymmetryThreshold,
final double absolutePositivityThreshold) {
if (!matrix.isSquare()) {
throw new NonSquareMatrixException(matrix.getRowDimension(),
matrix.getColumnDimension());
}
final int order = matrix.getRowDimension();
lTData = matrix.getData();
cachedL = null;
cachedLT = null;
// check the matrix before transformation
for (int i = 0; i < order; ++i) {
final double[] lI = lTData[i];
// check off-diagonal elements (and reset them to 0)
for (int j = i + 1; j < order; ++j) {
final double[] lJ = lTData[j];
final double lIJ = lI[j];
final double lJI = lJ[i];
final double maxDelta =
relativeSymmetryThreshold * FastMath.max(FastMath.abs(lIJ), FastMath.abs(lJI));
if (FastMath.abs(lIJ - lJI) > maxDelta) {
throw new NonSymmetricMatrixException(i, j, relativeSymmetryThreshold);
}
lJ[i] = 0;
}
}
// transform the matrix
for (int i = 0; i < order; ++i) {
final double[] ltI = lTData[i];
// check diagonal element
if (ltI[i] < absolutePositivityThreshold) {
throw new NonPositiveDefiniteMatrixException(i, absolutePositivityThreshold);
}
ltI[i] = FastMath.sqrt(ltI[i]);
final double inverse = 1.0 / ltI[i];
for (int q = order - 1; q > i; --q) {
ltI[q] *= inverse;
final double[] ltQ = lTData[q];
for (int p = q; p < order; ++p) {
ltQ[p] -= ltI[q] * ltI[p];
}
}
}
}