The following document contains the results of PMD's CPD 4.3.
| File | Line |
|---|---|
| org/apache/commons/math3/optim/nonlinear/scalar/noderiv/BOBYQAOptimizer.java | 51 |
| org/apache/commons/math3/optimization/direct/BOBYQAOptimizer.java | 57 |
extends MultivariateOptimizer {
/** Minimum dimension of the problem: {@value} */
public static final int MINIMUM_PROBLEM_DIMENSION = 2;
/** Default value for {@link #initialTrustRegionRadius}: {@value} . */
public static final double DEFAULT_INITIAL_RADIUS = 10.0;
/** Default value for {@link #stoppingTrustRegionRadius}: {@value} . */
public static final double DEFAULT_STOPPING_RADIUS = 1E-8;
private static final double ZERO = 0d;
private static final double ONE = 1d;
private static final double TWO = 2d;
private static final double TEN = 10d;
private static final double SIXTEEN = 16d;
private static final double TWO_HUNDRED_FIFTY = 250d;
private static final double MINUS_ONE = -ONE;
private static final double HALF = ONE / 2;
private static final double ONE_OVER_FOUR = ONE / 4;
private static final double ONE_OVER_EIGHT = ONE / 8;
private static final double ONE_OVER_TEN = ONE / 10;
private static final double ONE_OVER_A_THOUSAND = ONE / 1000;
/**
* numberOfInterpolationPoints XXX
*/
private final int numberOfInterpolationPoints;
/**
* initialTrustRegionRadius XXX
*/
private double initialTrustRegionRadius;
/**
* stoppingTrustRegionRadius XXX
*/
private final double stoppingTrustRegionRadius;
/** Goal type (minimize or maximize). */
private boolean isMinimize;
/**
* Current best values for the variables to be optimized.
* The vector will be changed in-place to contain the values of the least
* calculated objective function values.
*/
private ArrayRealVector currentBest;
/** Differences between the upper and lower bounds. */
private double[] boundDifference;
/**
* Index of the interpolation point at the trust region center.
*/
private int trustRegionCenterInterpolationPointIndex;
/**
* Last <em>n</em> columns of matrix H (where <em>n</em> is the dimension
* of the problem).
* XXX "bmat" in the original code.
*/
private Array2DRowRealMatrix bMatrix;
/**
* Factorization of the leading <em>npt</em> square submatrix of H, this
* factorization being Z Z<sup>T</sup>, which provides both the correct
* rank and positive semi-definiteness.
* XXX "zmat" in the original code.
*/
private Array2DRowRealMatrix zMatrix;
/**
* Coordinates of the interpolation points relative to {@link #originShift}.
* XXX "xpt" in the original code.
*/
private Array2DRowRealMatrix interpolationPoints;
/**
* Shift of origin that should reduce the contributions from rounding
* errors to values of the model and Lagrange functions.
* XXX "xbase" in the original code.
*/
private ArrayRealVector originShift;
/**
* Values of the objective function at the interpolation points.
* XXX "fval" in the original code.
*/
private ArrayRealVector fAtInterpolationPoints;
/**
* Displacement from {@link #originShift} of the trust region center.
* XXX "xopt" in the original code.
*/
private ArrayRealVector trustRegionCenterOffset;
/**
* Gradient of the quadratic model at {@link #originShift} +
* {@link #trustRegionCenterOffset}.
* XXX "gopt" in the original code.
*/
private ArrayRealVector gradientAtTrustRegionCenter;
/**
* Differences {@link #getLowerBound()} - {@link #originShift}.
* All the components of every {@link #trustRegionCenterOffset} are going
* to satisfy the bounds<br/>
* {@link #getLowerBound() lowerBound}<sub>i</sub> ≤
* {@link #trustRegionCenterOffset}<sub>i</sub>,<br/>
* with appropriate equalities when {@link #trustRegionCenterOffset} is
* on a constraint boundary.
* XXX "sl" in the original code.
*/
private ArrayRealVector lowerDifference;
/**
* Differences {@link #getUpperBound()} - {@link #originShift}
* All the components of every {@link #trustRegionCenterOffset} are going
* to satisfy the bounds<br/>
* {@link #trustRegionCenterOffset}<sub>i</sub> ≤
* {@link #getUpperBound() upperBound}<sub>i</sub>,<br/>
* with appropriate equalities when {@link #trustRegionCenterOffset} is
* on a constraint boundary.
* XXX "su" in the original code.
*/
private ArrayRealVector upperDifference;
/**
* Parameters of the implicit second derivatives of the quadratic model.
* XXX "pq" in the original code.
*/
private ArrayRealVector modelSecondDerivativesParameters;
/**
* Point chosen by function {@link #trsbox(double,ArrayRealVector,
* ArrayRealVector, ArrayRealVector,ArrayRealVector,ArrayRealVector) trsbox}
* or {@link #altmov(int,double) altmov}.
* Usually {@link #originShift} + {@link #newPoint} is the vector of
* variables for the next evaluation of the objective function.
* It also satisfies the constraints indicated in {@link #lowerDifference}
* and {@link #upperDifference}.
* XXX "xnew" in the original code.
*/
private ArrayRealVector newPoint;
/**
* Alternative to {@link #newPoint}, chosen by
* {@link #altmov(int,double) altmov}.
* It may replace {@link #newPoint} in order to increase the denominator
* in the {@link #update(double, double, int) updating procedure}.
* XXX "xalt" in the original code.
*/
private ArrayRealVector alternativeNewPoint;
/**
* Trial step from {@link #trustRegionCenterOffset} which is usually
* {@link #newPoint} - {@link #trustRegionCenterOffset}.
* XXX "d__" in the original code.
*/
private ArrayRealVector trialStepPoint;
/**
* Values of the Lagrange functions at a new point.
* XXX "vlag" in the original code.
*/
private ArrayRealVector lagrangeValuesAtNewPoint;
/**
* Explicit second derivatives of the quadratic model.
* XXX "hq" in the original code.
*/
private ArrayRealVector modelSecondDerivativesValues;
/**
* @param numberOfInterpolationPoints Number of interpolation conditions.
* For a problem of dimension {@code n}, its value must be in the interval
* {@code [n+2, (n+1)(n+2)/2]}.
* Choices that exceed {@code 2n+1} are not recommended.
*/
public BOBYQAOptimizer(int numberOfInterpolationPoints) {
this(numberOfInterpolationPoints,
DEFAULT_INITIAL_RADIUS,
DEFAULT_STOPPING_RADIUS);
}
/**
* @param numberOfInterpolationPoints Number of interpolation conditions.
* For a problem of dimension {@code n}, its value must be in the interval
* {@code [n+2, (n+1)(n+2)/2]}.
* Choices that exceed {@code 2n+1} are not recommended.
* @param initialTrustRegionRadius Initial trust region radius.
* @param stoppingTrustRegionRadius Stopping trust region radius.
*/
public BOBYQAOptimizer(int numberOfInterpolationPoints,
double initialTrustRegionRadius,
double stoppingTrustRegionRadius) {
super(null); // No custom convergence criterion.
this.numberOfInterpolationPoints = numberOfInterpolationPoints;
this.initialTrustRegionRadius = initialTrustRegionRadius;
this.stoppingTrustRegionRadius = stoppingTrustRegionRadius;
}
/** {@inheritDoc} */
@Override
protected PointValuePair doOptimize() {
final double[] lowerBound = getLowerBound();
final double[] upperBound = getUpperBound();
// Validity checks.
setup(lowerBound, upperBound);
isMinimize = (getGoalType() == GoalType.MINIMIZE);
currentBest = new ArrayRealVector(getStartPoint());
final double value = bobyqa(lowerBound, upperBound);
return new PointValuePair(currentBest.getDataRef(),
isMinimize ? value : -value);
}
/**
* This subroutine seeks the least value of a function of many variables,
* by applying a trust region method that forms quadratic models by
* interpolation. There is usually some freedom in the interpolation
* conditions, which is taken up by minimizing the Frobenius norm of
* the change to the second derivative of the model, beginning with the
* zero matrix. The values of the variables are constrained by upper and
* lower bounds. The arguments of the subroutine are as follows.
*
* N must be set to the number of variables and must be at least two.
* NPT is the number of interpolation conditions. Its value must be in
* the interval [N+2,(N+1)(N+2)/2]. Choices that exceed 2*N+1 are not
* recommended.
* Initial values of the variables must be set in X(1),X(2),...,X(N). They
* will be changed to the values that give the least calculated F.
* For I=1,2,...,N, XL(I) and XU(I) must provide the lower and upper
* bounds, respectively, on X(I). The construction of quadratic models
* requires XL(I) to be strictly less than XU(I) for each I. Further,
* the contribution to a model from changes to the I-th variable is
* damaged severely by rounding errors if XU(I)-XL(I) is too small.
* RHOBEG and RHOEND must be set to the initial and final values of a trust
* region radius, so both must be positive with RHOEND no greater than
* RHOBEG. Typically, RHOBEG should be about one tenth of the greatest
* expected change to a variable, while RHOEND should indicate the
* accuracy that is required in the final values of the variables. An
* error return occurs if any of the differences XU(I)-XL(I), I=1,...,N,
* is less than 2*RHOBEG.
* MAXFUN must be set to an upper bound on the number of calls of CALFUN.
* The array W will be used for working space. Its length must be at least
* (NPT+5)*(NPT+N)+3*N*(N+5)/2.
*
* @param lowerBound Lower bounds.
* @param upperBound Upper bounds.
* @return the value of the objective at the optimum.
*/
private double bobyqa(double[] lowerBound,
double[] upperBound) {
printMethod(); // XXX
final int n = currentBest.getDimension();
// Return if there is insufficient space between the bounds. Modify the
// initial X if necessary in order to avoid conflicts between the bounds
// and the construction of the first quadratic model. The lower and upper
// bounds on moves from the updated X are set now, in the ISL and ISU
// partitions of W, in order to provide useful and exact information about
// components of X that become within distance RHOBEG from their bounds.
for (int j = 0; j < n; j++) {
final double boundDiff = boundDifference[j];
lowerDifference.setEntry(j, lowerBound[j] - currentBest.getEntry(j));
upperDifference.setEntry(j, upperBound[j] - currentBest.getEntry(j));
if (lowerDifference.getEntry(j) >= -initialTrustRegionRadius) {
if (lowerDifference.getEntry(j) >= ZERO) {
currentBest.setEntry(j, lowerBound[j]);
lowerDifference.setEntry(j, ZERO);
upperDifference.setEntry(j, boundDiff);
} else {
currentBest.setEntry(j, lowerBound[j] + initialTrustRegionRadius);
lowerDifference.setEntry(j, -initialTrustRegionRadius);
// Computing MAX
final double deltaOne = upperBound[j] - currentBest.getEntry(j);
upperDifference.setEntry(j, Math.max(deltaOne, initialTrustRegionRadius));
}
} else if (upperDifference.getEntry(j) <= initialTrustRegionRadius) {
if (upperDifference.getEntry(j) <= ZERO) {
currentBest.setEntry(j, upperBound[j]);
lowerDifference.setEntry(j, -boundDiff);
upperDifference.setEntry(j, ZERO);
} else {
currentBest.setEntry(j, upperBound[j] - initialTrustRegionRadius);
// Computing MIN
final double deltaOne = lowerBound[j] - currentBest.getEntry(j);
final double deltaTwo = -initialTrustRegionRadius;
lowerDifference.setEntry(j, Math.min(deltaOne, deltaTwo));
upperDifference.setEntry(j, initialTrustRegionRadius);
}
}
}
// Make the call of BOBYQB.
return bobyqb(lowerBound, upperBound);
} // bobyqa
// ----------------------------------------------------------------------------------------
/**
* The arguments N, NPT, X, XL, XU, RHOBEG, RHOEND, IPRINT and MAXFUN
* are identical to the corresponding arguments in SUBROUTINE BOBYQA.
* XBASE holds a shift of origin that should reduce the contributions
* from rounding errors to values of the model and Lagrange functions.
* XPT is a two-dimensional array that holds the coordinates of the
* interpolation points relative to XBASE.
* FVAL holds the values of F at the interpolation points.
* XOPT is set to the displacement from XBASE of the trust region centre.
* GOPT holds the gradient of the quadratic model at XBASE+XOPT.
* HQ holds the explicit second derivatives of the quadratic model.
* PQ contains the parameters of the implicit second derivatives of the
* quadratic model.
* BMAT holds the last N columns of H.
* ZMAT holds the factorization of the leading NPT by NPT submatrix of H,
* this factorization being ZMAT times ZMAT^T, which provides both the
* correct rank and positive semi-definiteness.
* NDIM is the first dimension of BMAT and has the value NPT+N.
* SL and SU hold the differences XL-XBASE and XU-XBASE, respectively.
* All the components of every XOPT are going to satisfy the bounds
* SL(I) .LEQ. XOPT(I) .LEQ. SU(I), with appropriate equalities when
* XOPT is on a constraint boundary.
* XNEW is chosen by SUBROUTINE TRSBOX or ALTMOV. Usually XBASE+XNEW is the
* vector of variables for the next call of CALFUN. XNEW also satisfies
* the SL and SU constraints in the way that has just been mentioned.
* XALT is an alternative to XNEW, chosen by ALTMOV, that may replace XNEW
* in order to increase the denominator in the updating of UPDATE.
* D is reserved for a trial step from XOPT, which is usually XNEW-XOPT.
* VLAG contains the values of the Lagrange functions at a new point X.
* They are part of a product that requires VLAG to be of length NDIM.
* W is a one-dimensional array that is used for working space. Its length
* must be at least 3*NDIM = 3*(NPT+N).
*
* @param lowerBound Lower bounds.
* @param upperBound Upper bounds.
* @return the value of the objective at the optimum.
*/
private double bobyqb(double[] lowerBound,
double[] upperBound) {
printMethod(); // XXX
final int n = currentBest.getDimension();
final int npt = numberOfInterpolationPoints;
final int np = n + 1;
final int nptm = npt - np;
final int nh = n * np / 2;
final ArrayRealVector work1 = new ArrayRealVector(n);
final ArrayRealVector work2 = new ArrayRealVector(npt);
final ArrayRealVector work3 = new ArrayRealVector(npt);
double cauchy = Double.NaN;
double alpha = Double.NaN;
double dsq = Double.NaN;
double crvmin = Double.NaN;
// Set some constants.
// Parameter adjustments
// Function Body
// The call of PRELIM sets the elements of XBASE, XPT, FVAL, GOPT, HQ, PQ,
// BMAT and ZMAT for the first iteration, with the corresponding values of
// of NF and KOPT, which are the number of calls of CALFUN so far and the
// index of the interpolation point at the trust region centre. Then the
// initial XOPT is set too. The branch to label 720 occurs if MAXFUN is
// less than NPT. GOPT will be updated if KOPT is different from KBASE.
trustRegionCenterInterpolationPointIndex = 0;
prelim(lowerBound, upperBound);
double xoptsq = ZERO;
for (int i = 0; i < n; i++) {
trustRegionCenterOffset.setEntry(i, interpolationPoints.getEntry(trustRegionCenterInterpolationPointIndex, i));
// Computing 2nd power
final double deltaOne = trustRegionCenterOffset.getEntry(i);
xoptsq += deltaOne * deltaOne;
}
double fsave = fAtInterpolationPoints.getEntry(0);
final int kbase = 0;
// Complete the settings that are required for the iterative procedure.
int ntrits = 0;
int itest = 0;
int knew = 0;
int nfsav = getEvaluations();
double rho = initialTrustRegionRadius;
double delta = rho;
double diffa = ZERO;
double diffb = ZERO;
double diffc = ZERO;
double f = ZERO;
double beta = ZERO;
double adelt = ZERO;
double denom = ZERO;
double ratio = ZERO;
double dnorm = ZERO;
double scaden = ZERO;
double biglsq = ZERO;
double distsq = ZERO;
// Update GOPT if necessary before the first iteration and after each
// call of RESCUE that makes a call of CALFUN.
int state = 20;
for(;;) switch (state) {
case 20: {
printState(20); // XXX
if (trustRegionCenterInterpolationPointIndex != kbase) {
int ih = 0;
for (int j = 0; j < n; j++) {
for (int i = 0; i <= j; i++) {
if (i < j) {
gradientAtTrustRegionCenter.setEntry(j, gradientAtTrustRegionCenter.getEntry(j) + modelSecondDerivativesValues.getEntry(ih) * trustRegionCenterOffset.getEntry(i));
}
gradientAtTrustRegionCenter.setEntry(i, gradientAtTrustRegionCenter.getEntry(i) + modelSecondDerivativesValues.getEntry(ih) * trustRegionCenterOffset.getEntry(j));
ih++;
}
}
if (getEvaluations() > npt) {
for (int k = 0; k < npt; k++) {
double temp = ZERO;
for (int j = 0; j < n; j++) {
temp += interpolationPoints.getEntry(k, j) * trustRegionCenterOffset.getEntry(j);
}
temp *= modelSecondDerivativesParameters.getEntry(k);
for (int i = 0; i < n; i++) {
gradientAtTrustRegionCenter.setEntry(i, gradientAtTrustRegionCenter.getEntry(i) + temp * interpolationPoints.getEntry(k, i));
}
}
// throw new PathIsExploredException(); // XXX
}
}
// Generate the next point in the trust region that provides a small value
// of the quadratic model subject to the constraints on the variables.
// The int NTRITS is set to the number "trust region" iterations that
// have occurred since the last "alternative" iteration. If the length
// of XNEW-XOPT is less than HALF*RHO, however, then there is a branch to
// label 650 or 680 with NTRITS=-1, instead of calculating F at XNEW.
}
case 60: {
printState(60); // XXX
final ArrayRealVector gnew = new ArrayRealVector(n);
final ArrayRealVector xbdi = new ArrayRealVector(n);
final ArrayRealVector s = new ArrayRealVector(n);
final ArrayRealVector hs = new ArrayRealVector(n);
final ArrayRealVector hred = new ArrayRealVector(n);
final double[] dsqCrvmin = trsbox(delta, gnew, xbdi, s,
hs, hred);
dsq = dsqCrvmin[0];
crvmin = dsqCrvmin[1];
// Computing MIN
double deltaOne = delta;
double deltaTwo = Math.sqrt(dsq);
dnorm = Math.min(deltaOne, deltaTwo);
if (dnorm < HALF * rho) {
ntrits = -1;
// Computing 2nd power
deltaOne = TEN * rho;
distsq = deltaOne * deltaOne;
if (getEvaluations() <= nfsav + 2) {
state = 650; break;
}
// The following choice between labels 650 and 680 depends on whether or
// not our work with the current RHO seems to be complete. Either RHO is
// decreased or termination occurs if the errors in the quadratic model at
// the last three interpolation points compare favourably with predictions
// of likely improvements to the model within distance HALF*RHO of XOPT.
// Computing MAX
deltaOne = Math.max(diffa, diffb);
final double errbig = Math.max(deltaOne, diffc);
final double frhosq = rho * ONE_OVER_EIGHT * rho;
if (crvmin > ZERO &&
errbig > frhosq * crvmin) {
state = 650; break;
}
final double bdtol = errbig / rho;
for (int j = 0; j < n; j++) {
double bdtest = bdtol;
if (newPoint.getEntry(j) == lowerDifference.getEntry(j)) {
bdtest = work1.getEntry(j);
}
if (newPoint.getEntry(j) == upperDifference.getEntry(j)) {
bdtest = -work1.getEntry(j);
}
if (bdtest < bdtol) {
double curv = modelSecondDerivativesValues.getEntry((j + j * j) / 2);
for (int k = 0; k < npt; k++) {
// Computing 2nd power
final double d1 = interpolationPoints.getEntry(k, j);
curv += modelSecondDerivativesParameters.getEntry(k) * (d1 * d1);
}
bdtest += HALF * curv * rho;
if (bdtest < bdtol) {
state = 650; break;
}
// throw new PathIsExploredException(); // XXX
}
}
state = 680; break;
}
++ntrits;
// Severe cancellation is likely to occur if XOPT is too far from XBASE.
// If the following test holds, then XBASE is shifted so that XOPT becomes
// zero. The appropriate changes are made to BMAT and to the second
// derivatives of the current model, beginning with the changes to BMAT
// that do not depend on ZMAT. VLAG is used temporarily for working space.
}
case 90: {
printState(90); // XXX
if (dsq <= xoptsq * ONE_OVER_A_THOUSAND) {
final double fracsq = xoptsq * ONE_OVER_FOUR;
double sumpq = ZERO;
// final RealVector sumVector
// = new ArrayRealVector(npt, -HALF * xoptsq).add(interpolationPoints.operate(trustRegionCenter));
for (int k = 0; k < npt; k++) {
sumpq += modelSecondDerivativesParameters.getEntry(k);
double sum = -HALF * xoptsq;
for (int i = 0; i < n; i++) {
sum += interpolationPoints.getEntry(k, i) * trustRegionCenterOffset.getEntry(i);
}
// sum = sumVector.getEntry(k); // XXX "testAckley" and "testDiffPow" fail.
work2.setEntry(k, sum);
final double temp = fracsq - HALF * sum;
for (int i = 0; i < n; i++) {
work1.setEntry(i, bMatrix.getEntry(k, i));
lagrangeValuesAtNewPoint.setEntry(i, sum * interpolationPoints.getEntry(k, i) + temp * trustRegionCenterOffset.getEntry(i));
final int ip = npt + i;
for (int j = 0; j <= i; j++) {
bMatrix.setEntry(ip, j,
bMatrix.getEntry(ip, j)
+ work1.getEntry(i) * lagrangeValuesAtNewPoint.getEntry(j)
+ lagrangeValuesAtNewPoint.getEntry(i) * work1.getEntry(j));
}
}
}
// Then the revisions of BMAT that depend on ZMAT are calculated.
for (int m = 0; m < nptm; m++) {
double sumz = ZERO;
double sumw = ZERO;
for (int k = 0; k < npt; k++) {
sumz += zMatrix.getEntry(k, m);
lagrangeValuesAtNewPoint.setEntry(k, work2.getEntry(k) * zMatrix.getEntry(k, m));
sumw += lagrangeValuesAtNewPoint.getEntry(k);
}
for (int j = 0; j < n; j++) {
double sum = (fracsq * sumz - HALF * sumw) * trustRegionCenterOffset.getEntry(j);
for (int k = 0; k < npt; k++) {
sum += lagrangeValuesAtNewPoint.getEntry(k) * interpolationPoints.getEntry(k, j);
}
work1.setEntry(j, sum);
for (int k = 0; k < npt; k++) {
bMatrix.setEntry(k, j,
bMatrix.getEntry(k, j)
+ sum * zMatrix.getEntry(k, m));
}
}
for (int i = 0; i < n; i++) {
final int ip = i + npt;
final double temp = work1.getEntry(i);
for (int j = 0; j <= i; j++) {
bMatrix.setEntry(ip, j,
bMatrix.getEntry(ip, j)
+ temp * work1.getEntry(j));
}
}
}
// The following instructions complete the shift, including the changes
// to the second derivative parameters of the quadratic model.
int ih = 0;
for (int j = 0; j < n; j++) {
work1.setEntry(j, -HALF * sumpq * trustRegionCenterOffset.getEntry(j));
for (int k = 0; k < npt; k++) {
work1.setEntry(j, work1.getEntry(j) + modelSecondDerivativesParameters.getEntry(k) * interpolationPoints.getEntry(k, j));
interpolationPoints.setEntry(k, j, interpolationPoints.getEntry(k, j) - trustRegionCenterOffset.getEntry(j));
}
for (int i = 0; i <= j; i++) {
modelSecondDerivativesValues.setEntry(ih,
modelSecondDerivativesValues.getEntry(ih)
+ work1.getEntry(i) * trustRegionCenterOffset.getEntry(j)
+ trustRegionCenterOffset.getEntry(i) * work1.getEntry(j));
bMatrix.setEntry(npt + i, j, bMatrix.getEntry(npt + j, i));
ih++;
}
}
for (int i = 0; i < n; i++) {
originShift.setEntry(i, originShift.getEntry(i) + trustRegionCenterOffset.getEntry(i));
newPoint.setEntry(i, newPoint.getEntry(i) - trustRegionCenterOffset.getEntry(i));
lowerDifference.setEntry(i, lowerDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i));
upperDifference.setEntry(i, upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i));
trustRegionCenterOffset.setEntry(i, ZERO);
}
xoptsq = ZERO;
}
if (ntrits == 0) {
state = 210; break;
}
state = 230; break;
// XBASE is also moved to XOPT by a call of RESCUE. This calculation is
// more expensive than the previous shift, because new matrices BMAT and
// ZMAT are generated from scratch, which may include the replacement of
// interpolation points whose positions seem to be causing near linear
// dependence in the interpolation conditions. Therefore RESCUE is called
// only if rounding errors have reduced by at least a factor of two the
// denominator of the formula for updating the H matrix. It provides a
// useful safeguard, but is not invoked in most applications of BOBYQA.
}
case 210: {
printState(210); // XXX
// Pick two alternative vectors of variables, relative to XBASE, that
// are suitable as new positions of the KNEW-th interpolation point.
// Firstly, XNEW is set to the point on a line through XOPT and another
// interpolation point that minimizes the predicted value of the next
// denominator, subject to ||XNEW - XOPT|| .LEQ. ADELT and to the SL
// and SU bounds. Secondly, XALT is set to the best feasible point on
// a constrained version of the Cauchy step of the KNEW-th Lagrange
// function, the corresponding value of the square of this function
// being returned in CAUCHY. The choice between these alternatives is
// going to be made when the denominator is calculated.
final double[] alphaCauchy = altmov(knew, adelt);
alpha = alphaCauchy[0];
cauchy = alphaCauchy[1];
for (int i = 0; i < n; i++) {
trialStepPoint.setEntry(i, newPoint.getEntry(i) - trustRegionCenterOffset.getEntry(i));
}
// Calculate VLAG and BETA for the current choice of D. The scalar
// product of D with XPT(K,.) is going to be held in W(NPT+K) for
// use when VQUAD is calculated.
}
case 230: {
printState(230); // XXX
for (int k = 0; k < npt; k++) {
double suma = ZERO;
double sumb = ZERO;
double sum = ZERO;
for (int j = 0; j < n; j++) {
suma += interpolationPoints.getEntry(k, j) * trialStepPoint.getEntry(j);
sumb += interpolationPoints.getEntry(k, j) * trustRegionCenterOffset.getEntry(j);
sum += bMatrix.getEntry(k, j) * trialStepPoint.getEntry(j);
}
work3.setEntry(k, suma * (HALF * suma + sumb));
lagrangeValuesAtNewPoint.setEntry(k, sum);
work2.setEntry(k, suma);
}
beta = ZERO;
for (int m = 0; m < nptm; m++) {
double sum = ZERO;
for (int k = 0; k < npt; k++) {
sum += zMatrix.getEntry(k, m) * work3.getEntry(k);
}
beta -= sum * sum;
for (int k = 0; k < npt; k++) {
lagrangeValuesAtNewPoint.setEntry(k, lagrangeValuesAtNewPoint.getEntry(k) + sum * zMatrix.getEntry(k, m));
}
}
dsq = ZERO;
double bsum = ZERO;
double dx = ZERO;
for (int j = 0; j < n; j++) {
// Computing 2nd power
final double d1 = trialStepPoint.getEntry(j);
dsq += d1 * d1;
double sum = ZERO;
for (int k = 0; k < npt; k++) {
sum += work3.getEntry(k) * bMatrix.getEntry(k, j);
}
bsum += sum * trialStepPoint.getEntry(j);
final int jp = npt + j;
for (int i = 0; i < n; i++) {
sum += bMatrix.getEntry(jp, i) * trialStepPoint.getEntry(i);
}
lagrangeValuesAtNewPoint.setEntry(jp, sum);
bsum += sum * trialStepPoint.getEntry(j);
dx += trialStepPoint.getEntry(j) * trustRegionCenterOffset.getEntry(j);
}
beta = dx * dx + dsq * (xoptsq + dx + dx + HALF * dsq) + beta - bsum; // Original
// beta += dx * dx + dsq * (xoptsq + dx + dx + HALF * dsq) - bsum; // XXX "testAckley" and "testDiffPow" fail.
// beta = dx * dx + dsq * (xoptsq + 2 * dx + HALF * dsq) + beta - bsum; // XXX "testDiffPow" fails.
lagrangeValuesAtNewPoint.setEntry(trustRegionCenterInterpolationPointIndex,
lagrangeValuesAtNewPoint.getEntry(trustRegionCenterInterpolationPointIndex) + ONE);
// If NTRITS is zero, the denominator may be increased by replacing
// the step D of ALTMOV by a Cauchy step. Then RESCUE may be called if
// rounding errors have damaged the chosen denominator.
if (ntrits == 0) {
// Computing 2nd power
final double d1 = lagrangeValuesAtNewPoint.getEntry(knew);
denom = d1 * d1 + alpha * beta;
if (denom < cauchy && cauchy > ZERO) {
for (int i = 0; i < n; i++) {
newPoint.setEntry(i, alternativeNewPoint.getEntry(i));
trialStepPoint.setEntry(i, newPoint.getEntry(i) - trustRegionCenterOffset.getEntry(i));
}
cauchy = ZERO; // XXX Useful statement?
state = 230; break;
}
// Alternatively, if NTRITS is positive, then set KNEW to the index of
// the next interpolation point to be deleted to make room for a trust
// region step. Again RESCUE may be called if rounding errors have damaged_
// the chosen denominator, which is the reason for attempting to select
// KNEW before calculating the next value of the objective function.
} else {
final double delsq = delta * delta;
scaden = ZERO;
biglsq = ZERO;
knew = 0;
for (int k = 0; k < npt; k++) {
if (k == trustRegionCenterInterpolationPointIndex) {
continue;
}
double hdiag = ZERO;
for (int m = 0; m < nptm; m++) {
// Computing 2nd power
final double d1 = zMatrix.getEntry(k, m);
hdiag += d1 * d1;
}
// Computing 2nd power
final double d2 = lagrangeValuesAtNewPoint.getEntry(k);
final double den = beta * hdiag + d2 * d2;
distsq = ZERO;
for (int j = 0; j < n; j++) {
// Computing 2nd power
final double d3 = interpolationPoints.getEntry(k, j) - trustRegionCenterOffset.getEntry(j);
distsq += d3 * d3;
}
// Computing MAX
// Computing 2nd power
final double d4 = distsq / delsq;
final double temp = Math.max(ONE, d4 * d4);
if (temp * den > scaden) {
scaden = temp * den;
knew = k;
denom = den;
}
// Computing MAX
// Computing 2nd power
final double d5 = lagrangeValuesAtNewPoint.getEntry(k);
biglsq = Math.max(biglsq, temp * (d5 * d5));
}
}
// Put the variables for the next calculation of the objective function
// in XNEW, with any adjustments for the bounds.
// Calculate the value of the objective function at XBASE+XNEW, unless
// the limit on the number of calculations of F has been reached.
}
case 360: {
printState(360); // XXX
for (int i = 0; i < n; i++) {
// Computing MIN
// Computing MAX
final double d3 = lowerBound[i];
final double d4 = originShift.getEntry(i) + newPoint.getEntry(i);
final double d1 = Math.max(d3, d4);
final double d2 = upperBound[i];
currentBest.setEntry(i, Math.min(d1, d2));
if (newPoint.getEntry(i) == lowerDifference.getEntry(i)) {
currentBest.setEntry(i, lowerBound[i]);
}
if (newPoint.getEntry(i) == upperDifference.getEntry(i)) {
currentBest.setEntry(i, upperBound[i]);
}
}
f = computeObjectiveValue(currentBest.toArray());
if (!isMinimize)
f = -f;
if (ntrits == -1) {
fsave = f;
state = 720; break;
}
// Use the quadratic model to predict the change in F due to the step D,
// and set DIFF to the error of this prediction.
final double fopt = fAtInterpolationPoints.getEntry(trustRegionCenterInterpolationPointIndex);
double vquad = ZERO;
int ih = 0;
for (int j = 0; j < n; j++) {
vquad += trialStepPoint.getEntry(j) * gradientAtTrustRegionCenter.getEntry(j);
for (int i = 0; i <= j; i++) {
double temp = trialStepPoint.getEntry(i) * trialStepPoint.getEntry(j);
if (i == j) {
temp *= HALF;
}
vquad += modelSecondDerivativesValues.getEntry(ih) * temp;
ih++;
}
}
for (int k = 0; k < npt; k++) {
// Computing 2nd power
final double d1 = work2.getEntry(k);
final double d2 = d1 * d1; // "d1" must be squared first to prevent test failures.
vquad += HALF * modelSecondDerivativesParameters.getEntry(k) * d2;
}
final double diff = f - fopt - vquad;
diffc = diffb;
diffb = diffa;
diffa = Math.abs(diff);
if (dnorm > rho) {
nfsav = getEvaluations();
}
// Pick the next value of DELTA after a trust region step.
if (ntrits > 0) {
if (vquad >= ZERO) {
throw new MathIllegalStateException(LocalizedFormats.TRUST_REGION_STEP_FAILED, vquad);
}
ratio = (f - fopt) / vquad;
final double hDelta = HALF * delta;
if (ratio <= ONE_OVER_TEN) {
// Computing MIN
delta = Math.min(hDelta, dnorm);
} else if (ratio <= .7) {
// Computing MAX
delta = Math.max(hDelta, dnorm);
} else {
// Computing MAX
delta = Math.max(hDelta, 2 * dnorm);
}
if (delta <= rho * 1.5) {
delta = rho;
}
// Recalculate KNEW and DENOM if the new F is less than FOPT.
if (f < fopt) {
final int ksav = knew;
final double densav = denom;
final double delsq = delta * delta;
scaden = ZERO;
biglsq = ZERO;
knew = 0;
for (int k = 0; k < npt; k++) {
double hdiag = ZERO;
for (int m = 0; m < nptm; m++) {
// Computing 2nd power
final double d1 = zMatrix.getEntry(k, m);
hdiag += d1 * d1;
}
// Computing 2nd power
final double d1 = lagrangeValuesAtNewPoint.getEntry(k);
final double den = beta * hdiag + d1 * d1;
distsq = ZERO;
for (int j = 0; j < n; j++) {
// Computing 2nd power
final double d2 = interpolationPoints.getEntry(k, j) - newPoint.getEntry(j);
distsq += d2 * d2;
}
// Computing MAX
// Computing 2nd power
final double d3 = distsq / delsq;
final double temp = Math.max(ONE, d3 * d3);
if (temp * den > scaden) {
scaden = temp * den;
knew = k;
denom = den;
}
// Computing MAX
// Computing 2nd power
final double d4 = lagrangeValuesAtNewPoint.getEntry(k);
final double d5 = temp * (d4 * d4);
biglsq = Math.max(biglsq, d5);
}
if (scaden <= HALF * biglsq) {
knew = ksav;
denom = densav;
}
}
}
// Update BMAT and ZMAT, so that the KNEW-th interpolation point can be
// moved. Also update the second derivative terms of the model.
update(beta, denom, knew);
ih = 0;
final double pqold = modelSecondDerivativesParameters.getEntry(knew);
modelSecondDerivativesParameters.setEntry(knew, ZERO);
for (int i = 0; i < n; i++) {
final double temp = pqold * interpolationPoints.getEntry(knew, i);
for (int j = 0; j <= i; j++) {
modelSecondDerivativesValues.setEntry(ih, modelSecondDerivativesValues.getEntry(ih) + temp * interpolationPoints.getEntry(knew, j));
ih++;
}
}
for (int m = 0; m < nptm; m++) {
final double temp = diff * zMatrix.getEntry(knew, m);
for (int k = 0; k < npt; k++) {
modelSecondDerivativesParameters.setEntry(k, modelSecondDerivativesParameters.getEntry(k) + temp * zMatrix.getEntry(k, m));
}
}
// Include the new interpolation point, and make the changes to GOPT at
// the old XOPT that are caused by the updating of the quadratic model.
fAtInterpolationPoints.setEntry(knew, f);
for (int i = 0; i < n; i++) {
interpolationPoints.setEntry(knew, i, newPoint.getEntry(i));
work1.setEntry(i, bMatrix.getEntry(knew, i));
}
for (int k = 0; k < npt; k++) {
double suma = ZERO;
for (int m = 0; m < nptm; m++) {
suma += zMatrix.getEntry(knew, m) * zMatrix.getEntry(k, m);
}
double sumb = ZERO;
for (int j = 0; j < n; j++) {
sumb += interpolationPoints.getEntry(k, j) * trustRegionCenterOffset.getEntry(j);
}
final double temp = suma * sumb;
for (int i = 0; i < n; i++) {
work1.setEntry(i, work1.getEntry(i) + temp * interpolationPoints.getEntry(k, i));
}
}
for (int i = 0; i < n; i++) {
gradientAtTrustRegionCenter.setEntry(i, gradientAtTrustRegionCenter.getEntry(i) + diff * work1.getEntry(i));
}
// Update XOPT, GOPT and KOPT if the new calculated F is less than FOPT.
if (f < fopt) {
trustRegionCenterInterpolationPointIndex = knew;
xoptsq = ZERO;
ih = 0;
for (int j = 0; j < n; j++) {
trustRegionCenterOffset.setEntry(j, newPoint.getEntry(j));
// Computing 2nd power
final double d1 = trustRegionCenterOffset.getEntry(j);
xoptsq += d1 * d1;
for (int i = 0; i <= j; i++) {
if (i < j) {
gradientAtTrustRegionCenter.setEntry(j, gradientAtTrustRegionCenter.getEntry(j) + modelSecondDerivativesValues.getEntry(ih) * trialStepPoint.getEntry(i));
}
gradientAtTrustRegionCenter.setEntry(i, gradientAtTrustRegionCenter.getEntry(i) + modelSecondDerivativesValues.getEntry(ih) * trialStepPoint.getEntry(j));
ih++;
}
}
for (int k = 0; k < npt; k++) {
double temp = ZERO;
for (int j = 0; j < n; j++) {
temp += interpolationPoints.getEntry(k, j) * trialStepPoint.getEntry(j);
}
temp *= modelSecondDerivativesParameters.getEntry(k);
for (int i = 0; i < n; i++) {
gradientAtTrustRegionCenter.setEntry(i, gradientAtTrustRegionCenter.getEntry(i) + temp * interpolationPoints.getEntry(k, i));
}
}
}
// Calculate the parameters of the least Frobenius norm interpolant to
// the current data, the gradient of this interpolant at XOPT being put
// into VLAG(NPT+I), I=1,2,...,N.
if (ntrits > 0) {
for (int k = 0; k < npt; k++) {
lagrangeValuesAtNewPoint.setEntry(k, fAtInterpolationPoints.getEntry(k) - fAtInterpolationPoints.getEntry(trustRegionCenterInterpolationPointIndex));
work3.setEntry(k, ZERO);
}
for (int j = 0; j < nptm; j++) {
double sum = ZERO;
for (int k = 0; k < npt; k++) {
sum += zMatrix.getEntry(k, j) * lagrangeValuesAtNewPoint.getEntry(k);
}
for (int k = 0; k < npt; k++) {
work3.setEntry(k, work3.getEntry(k) + sum * zMatrix.getEntry(k, j));
}
}
for (int k = 0; k < npt; k++) {
double sum = ZERO;
for (int j = 0; j < n; j++) {
sum += interpolationPoints.getEntry(k, j) * trustRegionCenterOffset.getEntry(j);
}
work2.setEntry(k, work3.getEntry(k));
work3.setEntry(k, sum * work3.getEntry(k));
}
double gqsq = ZERO;
double gisq = ZERO;
for (int i = 0; i < n; i++) {
double sum = ZERO;
for (int k = 0; k < npt; k++) {
sum += bMatrix.getEntry(k, i) *
lagrangeValuesAtNewPoint.getEntry(k) + interpolationPoints.getEntry(k, i) * work3.getEntry(k);
}
if (trustRegionCenterOffset.getEntry(i) == lowerDifference.getEntry(i)) {
// Computing MIN
// Computing 2nd power
final double d1 = Math.min(ZERO, gradientAtTrustRegionCenter.getEntry(i));
gqsq += d1 * d1;
// Computing 2nd power
final double d2 = Math.min(ZERO, sum);
gisq += d2 * d2;
} else if (trustRegionCenterOffset.getEntry(i) == upperDifference.getEntry(i)) {
// Computing MAX
// Computing 2nd power
final double d1 = Math.max(ZERO, gradientAtTrustRegionCenter.getEntry(i));
gqsq += d1 * d1;
// Computing 2nd power
final double d2 = Math.max(ZERO, sum);
gisq += d2 * d2;
} else {
// Computing 2nd power
final double d1 = gradientAtTrustRegionCenter.getEntry(i);
gqsq += d1 * d1;
gisq += sum * sum;
}
lagrangeValuesAtNewPoint.setEntry(npt + i, sum);
}
// Test whether to replace the new quadratic model by the least Frobenius
// norm interpolant, making the replacement if the test is satisfied.
++itest;
if (gqsq < TEN * gisq) {
itest = 0;
}
if (itest >= 3) {
for (int i = 0, max = Math.max(npt, nh); i < max; i++) {
if (i < n) {
gradientAtTrustRegionCenter.setEntry(i, lagrangeValuesAtNewPoint.getEntry(npt + i));
}
if (i < npt) {
modelSecondDerivativesParameters.setEntry(i, work2.getEntry(i));
}
if (i < nh) {
modelSecondDerivativesValues.setEntry(i, ZERO);
}
itest = 0;
}
}
}
// If a trust region step has provided a sufficient decrease in F, then
// branch for another trust region calculation. The case NTRITS=0 occurs
// when the new interpolation point was reached by an alternative step.
if (ntrits == 0) {
state = 60; break;
}
if (f <= fopt + ONE_OVER_TEN * vquad) {
state = 60; break;
}
// Alternatively, find out if the interpolation points are close enough
// to the best point so far.
// Computing MAX
// Computing 2nd power
final double d1 = TWO * delta;
// Computing 2nd power
final double d2 = TEN * rho;
distsq = Math.max(d1 * d1, d2 * d2);
}
case 650: {
printState(650); // XXX
knew = -1;
for (int k = 0; k < npt; k++) {
double sum = ZERO;
for (int j = 0; j < n; j++) {
// Computing 2nd power
final double d1 = interpolationPoints.getEntry(k, j) - trustRegionCenterOffset.getEntry(j);
sum += d1 * d1;
}
if (sum > distsq) {
knew = k;
distsq = sum;
}
}
// If KNEW is positive, then ALTMOV finds alternative new positions for
// the KNEW-th interpolation point within distance ADELT of XOPT. It is
// reached via label 90. Otherwise, there is a branch to label 60 for
// another trust region iteration, unless the calculations with the
// current RHO are complete.
if (knew >= 0) {
final double dist = Math.sqrt(distsq);
if (ntrits == -1) {
// Computing MIN
delta = Math.min(ONE_OVER_TEN * delta, HALF * dist);
if (delta <= rho * 1.5) {
delta = rho;
}
}
ntrits = 0;
// Computing MAX
// Computing MIN
final double d1 = Math.min(ONE_OVER_TEN * dist, delta);
adelt = Math.max(d1, rho);
dsq = adelt * adelt;
state = 90; break;
}
if (ntrits == -1) {
state = 680; break;
}
if (ratio > ZERO) {
state = 60; break;
}
if (Math.max(delta, dnorm) > rho) {
state = 60; break;
}
// The calculations with the current value of RHO are complete. Pick the
// next values of RHO and DELTA.
}
case 680: {
printState(680); // XXX
if (rho > stoppingTrustRegionRadius) {
delta = HALF * rho;
ratio = rho / stoppingTrustRegionRadius;
if (ratio <= SIXTEEN) {
rho = stoppingTrustRegionRadius;
} else if (ratio <= TWO_HUNDRED_FIFTY) {
rho = Math.sqrt(ratio) * stoppingTrustRegionRadius;
} else {
rho *= ONE_OVER_TEN;
}
delta = Math.max(delta, rho);
ntrits = 0;
nfsav = getEvaluations();
state = 60; break;
}
// Return from the calculation, after another Newton-Raphson step, if
// it is too short to have been tried before.
if (ntrits == -1) {
state = 360; break;
}
}
case 720: {
printState(720); // XXX
if (fAtInterpolationPoints.getEntry(trustRegionCenterInterpolationPointIndex) <= fsave) {
for (int i = 0; i < n; i++) {
// Computing MIN
// Computing MAX
final double d3 = lowerBound[i];
final double d4 = originShift.getEntry(i) + trustRegionCenterOffset.getEntry(i);
final double d1 = Math.max(d3, d4);
final double d2 = upperBound[i];
currentBest.setEntry(i, Math.min(d1, d2));
if (trustRegionCenterOffset.getEntry(i) == lowerDifference.getEntry(i)) {
currentBest.setEntry(i, lowerBound[i]);
}
if (trustRegionCenterOffset.getEntry(i) == upperDifference.getEntry(i)) {
currentBest.setEntry(i, upperBound[i]);
}
}
f = fAtInterpolationPoints.getEntry(trustRegionCenterInterpolationPointIndex);
}
return f;
}
default: {
throw new MathIllegalStateException(LocalizedFormats.SIMPLE_MESSAGE, "bobyqb");
}}
} // bobyqb
// ----------------------------------------------------------------------------------------
/**
* The arguments N, NPT, XPT, XOPT, BMAT, ZMAT, NDIM, SL and SU all have
* the same meanings as the corresponding arguments of BOBYQB.
* KOPT is the index of the optimal interpolation point.
* KNEW is the index of the interpolation point that is going to be moved.
* ADELT is the current trust region bound.
* XNEW will be set to a suitable new position for the interpolation point
* XPT(KNEW,.). Specifically, it satisfies the SL, SU and trust region
* bounds and it should provide a large denominator in the next call of
* UPDATE. The step XNEW-XOPT from XOPT is restricted to moves along the
* straight lines through XOPT and another interpolation point.
* XALT also provides a large value of the modulus of the KNEW-th Lagrange
* function subject to the constraints that have been mentioned, its main
* difference from XNEW being that XALT-XOPT is a constrained version of
* the Cauchy step within the trust region. An exception is that XALT is
* not calculated if all components of GLAG (see below) are zero.
* ALPHA will be set to the KNEW-th diagonal element of the H matrix.
* CAUCHY will be set to the square of the KNEW-th Lagrange function at
* the step XALT-XOPT from XOPT for the vector XALT that is returned,
* except that CAUCHY is set to zero if XALT is not calculated.
* GLAG is a working space vector of length N for the gradient of the
* KNEW-th Lagrange function at XOPT.
* HCOL is a working space vector of length NPT for the second derivative
* coefficients of the KNEW-th Lagrange function.
* W is a working space vector of length 2N that is going to hold the
* constrained Cauchy step from XOPT of the Lagrange function, followed
* by the downhill version of XALT when the uphill step is calculated.
*
* Set the first NPT components of W to the leading elements of the
* KNEW-th column of the H matrix.
* @param knew
* @param adelt
*/
private double[] altmov(
int knew,
double adelt
) {
printMethod(); // XXX
final int n = currentBest.getDimension();
final int npt = numberOfInterpolationPoints;
final ArrayRealVector glag = new ArrayRealVector(n);
final ArrayRealVector hcol = new ArrayRealVector(npt);
final ArrayRealVector work1 = new ArrayRealVector(n);
final ArrayRealVector work2 = new ArrayRealVector(n);
for (int k = 0; k < npt; k++) {
hcol.setEntry(k, ZERO);
}
for (int j = 0, max = npt - n - 1; j < max; j++) {
final double tmp = zMatrix.getEntry(knew, j);
for (int k = 0; k < npt; k++) {
hcol.setEntry(k, hcol.getEntry(k) + tmp * zMatrix.getEntry(k, j));
}
}
final double alpha = hcol.getEntry(knew);
final double ha = HALF * alpha;
// Calculate the gradient of the KNEW-th Lagrange function at XOPT.
for (int i = 0; i < n; i++) {
glag.setEntry(i, bMatrix.getEntry(knew, i));
}
for (int k = 0; k < npt; k++) {
double tmp = ZERO;
for (int j = 0; j < n; j++) {
tmp += interpolationPoints.getEntry(k, j) * trustRegionCenterOffset.getEntry(j);
}
tmp *= hcol.getEntry(k);
for (int i = 0; i < n; i++) {
glag.setEntry(i, glag.getEntry(i) + tmp * interpolationPoints.getEntry(k, i));
}
}
// Search for a large denominator along the straight lines through XOPT
// and another interpolation point. SLBD and SUBD will be lower and upper
// bounds on the step along each of these lines in turn. PREDSQ will be
// set to the square of the predicted denominator for each line. PRESAV
// will be set to the largest admissible value of PREDSQ that occurs.
double presav = ZERO;
double step = Double.NaN;
int ksav = 0;
int ibdsav = 0;
double stpsav = 0;
for (int k = 0; k < npt; k++) {
if (k == trustRegionCenterInterpolationPointIndex) {
continue;
}
double dderiv = ZERO;
double distsq = ZERO;
for (int i = 0; i < n; i++) {
final double tmp = interpolationPoints.getEntry(k, i) - trustRegionCenterOffset.getEntry(i);
dderiv += glag.getEntry(i) * tmp;
distsq += tmp * tmp;
}
double subd = adelt / Math.sqrt(distsq);
double slbd = -subd;
int ilbd = 0;
int iubd = 0;
final double sumin = Math.min(ONE, subd);
// Revise SLBD and SUBD if necessary because of the bounds in SL and SU.
for (int i = 0; i < n; i++) {
final double tmp = interpolationPoints.getEntry(k, i) - trustRegionCenterOffset.getEntry(i);
if (tmp > ZERO) {
if (slbd * tmp < lowerDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) {
slbd = (lowerDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) / tmp;
ilbd = -i - 1;
}
if (subd * tmp > upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) {
// Computing MAX
subd = Math.max(sumin,
(upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) / tmp);
iubd = i + 1;
}
} else if (tmp < ZERO) {
if (slbd * tmp > upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) {
slbd = (upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) / tmp;
ilbd = i + 1;
}
if (subd * tmp < lowerDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) {
// Computing MAX
subd = Math.max(sumin,
(lowerDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) / tmp);
iubd = -i - 1;
}
}
}
// Seek a large modulus of the KNEW-th Lagrange function when the index
// of the other interpolation point on the line through XOPT is KNEW.
step = slbd;
int isbd = ilbd;
double vlag = Double.NaN;
if (k == knew) {
final double diff = dderiv - ONE;
vlag = slbd * (dderiv - slbd * diff);
final double d1 = subd * (dderiv - subd * diff);
if (Math.abs(d1) > Math.abs(vlag)) {
step = subd;
vlag = d1;
isbd = iubd;
}
final double d2 = HALF * dderiv;
final double d3 = d2 - diff * slbd;
final double d4 = d2 - diff * subd;
if (d3 * d4 < ZERO) {
final double d5 = d2 * d2 / diff;
if (Math.abs(d5) > Math.abs(vlag)) {
step = d2 / diff;
vlag = d5;
isbd = 0;
}
}
// Search along each of the other lines through XOPT and another point.
} else {
vlag = slbd * (ONE - slbd);
final double tmp = subd * (ONE - subd);
if (Math.abs(tmp) > Math.abs(vlag)) {
step = subd;
vlag = tmp;
isbd = iubd;
}
if (subd > HALF) {
if (Math.abs(vlag) < ONE_OVER_FOUR) {
step = HALF;
vlag = ONE_OVER_FOUR;
isbd = 0;
}
}
vlag *= dderiv;
}
// Calculate PREDSQ for the current line search and maintain PRESAV.
final double tmp = step * (ONE - step) * distsq;
final double predsq = vlag * vlag * (vlag * vlag + ha * tmp * tmp);
if (predsq > presav) {
presav = predsq;
ksav = k;
stpsav = step;
ibdsav = isbd;
}
}
// Construct XNEW in a way that satisfies the bound constraints exactly.
for (int i = 0; i < n; i++) {
final double tmp = trustRegionCenterOffset.getEntry(i) + stpsav * (interpolationPoints.getEntry(ksav, i) - trustRegionCenterOffset.getEntry(i));
newPoint.setEntry(i, Math.max(lowerDifference.getEntry(i),
Math.min(upperDifference.getEntry(i), tmp)));
}
if (ibdsav < 0) {
newPoint.setEntry(-ibdsav - 1, lowerDifference.getEntry(-ibdsav - 1));
}
if (ibdsav > 0) {
newPoint.setEntry(ibdsav - 1, upperDifference.getEntry(ibdsav - 1));
}
// Prepare for the iterative method that assembles the constrained Cauchy
// step in W. The sum of squares of the fixed components of W is formed in
// WFIXSQ, and the free components of W are set to BIGSTP.
final double bigstp = adelt + adelt;
int iflag = 0;
double cauchy = Double.NaN;
double csave = ZERO;
while (true) {
double wfixsq = ZERO;
double ggfree = ZERO;
for (int i = 0; i < n; i++) {
final double glagValue = glag.getEntry(i);
work1.setEntry(i, ZERO);
if (Math.min(trustRegionCenterOffset.getEntry(i) - lowerDifference.getEntry(i), glagValue) > ZERO ||
Math.max(trustRegionCenterOffset.getEntry(i) - upperDifference.getEntry(i), glagValue) < ZERO) {
work1.setEntry(i, bigstp);
// Computing 2nd power
ggfree += glagValue * glagValue;
}
}
if (ggfree == ZERO) {
return new double[] { alpha, ZERO };
}
// Investigate whether more components of W can be fixed.
final double tmp1 = adelt * adelt - wfixsq;
if (tmp1 > ZERO) {
step = Math.sqrt(tmp1 / ggfree);
ggfree = ZERO;
for (int i = 0; i < n; i++) {
if (work1.getEntry(i) == bigstp) {
final double tmp2 = trustRegionCenterOffset.getEntry(i) - step * glag.getEntry(i);
if (tmp2 <= lowerDifference.getEntry(i)) {
work1.setEntry(i, lowerDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i));
// Computing 2nd power
final double d1 = work1.getEntry(i);
wfixsq += d1 * d1;
} else if (tmp2 >= upperDifference.getEntry(i)) {
work1.setEntry(i, upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i));
// Computing 2nd power
final double d1 = work1.getEntry(i);
wfixsq += d1 * d1;
} else {
// Computing 2nd power
final double d1 = glag.getEntry(i);
ggfree += d1 * d1;
}
}
}
}
// Set the remaining free components of W and all components of XALT,
// except that W may be scaled later.
double gw = ZERO;
for (int i = 0; i < n; i++) {
final double glagValue = glag.getEntry(i);
if (work1.getEntry(i) == bigstp) {
work1.setEntry(i, -step * glagValue);
final double min = Math.min(upperDifference.getEntry(i),
trustRegionCenterOffset.getEntry(i) + work1.getEntry(i));
alternativeNewPoint.setEntry(i, Math.max(lowerDifference.getEntry(i), min));
} else if (work1.getEntry(i) == ZERO) {
alternativeNewPoint.setEntry(i, trustRegionCenterOffset.getEntry(i));
} else if (glagValue > ZERO) {
alternativeNewPoint.setEntry(i, lowerDifference.getEntry(i));
} else {
alternativeNewPoint.setEntry(i, upperDifference.getEntry(i));
}
gw += glagValue * work1.getEntry(i);
}
// Set CURV to the curvature of the KNEW-th Lagrange function along W.
// Scale W by a factor less than one if that can reduce the modulus of
// the Lagrange function at XOPT+W. Set CAUCHY to the final value of
// the square of this function.
double curv = ZERO;
for (int k = 0; k < npt; k++) {
double tmp = ZERO;
for (int j = 0; j < n; j++) {
tmp += interpolationPoints.getEntry(k, j) * work1.getEntry(j);
}
curv += hcol.getEntry(k) * tmp * tmp;
}
if (iflag == 1) {
curv = -curv;
}
if (curv > -gw &&
curv < -gw * (ONE + Math.sqrt(TWO))) {
final double scale = -gw / curv;
for (int i = 0; i < n; i++) {
final double tmp = trustRegionCenterOffset.getEntry(i) + scale * work1.getEntry(i);
alternativeNewPoint.setEntry(i, Math.max(lowerDifference.getEntry(i),
Math.min(upperDifference.getEntry(i), tmp)));
}
// Computing 2nd power
final double d1 = HALF * gw * scale;
cauchy = d1 * d1;
} else {
// Computing 2nd power
final double d1 = gw + HALF * curv;
cauchy = d1 * d1;
}
// If IFLAG is zero, then XALT is calculated as before after reversing
// the sign of GLAG. Thus two XALT vectors become available. The one that
// is chosen is the one that gives the larger value of CAUCHY.
if (iflag == 0) {
for (int i = 0; i < n; i++) {
glag.setEntry(i, -glag.getEntry(i));
work2.setEntry(i, alternativeNewPoint.getEntry(i));
}
csave = cauchy;
iflag = 1;
} else {
break;
}
}
if (csave > cauchy) {
for (int i = 0; i < n; i++) {
alternativeNewPoint.setEntry(i, work2.getEntry(i));
}
cauchy = csave;
}
return new double[] { alpha, cauchy };
} // altmov
// ----------------------------------------------------------------------------------------
/**
* SUBROUTINE PRELIM sets the elements of XBASE, XPT, FVAL, GOPT, HQ, PQ,
* BMAT and ZMAT for the first iteration, and it maintains the values of
* NF and KOPT. The vector X is also changed by PRELIM.
*
* The arguments N, NPT, X, XL, XU, RHOBEG, IPRINT and MAXFUN are the
* same as the corresponding arguments in SUBROUTINE BOBYQA.
* The arguments XBASE, XPT, FVAL, HQ, PQ, BMAT, ZMAT, NDIM, SL and SU
* are the same as the corresponding arguments in BOBYQB, the elements
* of SL and SU being set in BOBYQA.
* GOPT is usually the gradient of the quadratic model at XOPT+XBASE, but
* it is set by PRELIM to the gradient of the quadratic model at XBASE.
* If XOPT is nonzero, BOBYQB will change it to its usual value later.
* NF is maintaned as the number of calls of CALFUN so far.
* KOPT will be such that the least calculated value of F so far is at
* the point XPT(KOPT,.)+XBASE in the space of the variables.
*
* @param lowerBound Lower bounds.
* @param upperBound Upper bounds.
*/
private void prelim(double[] lowerBound,
double[] upperBound) {
printMethod(); // XXX
final int n = currentBest.getDimension();
final int npt = numberOfInterpolationPoints;
final int ndim = bMatrix.getRowDimension();
final double rhosq = initialTrustRegionRadius * initialTrustRegionRadius;
final double recip = 1d / rhosq;
final int np = n + 1;
// Set XBASE to the initial vector of variables, and set the initial
// elements of XPT, BMAT, HQ, PQ and ZMAT to zero.
for (int j = 0; j < n; j++) {
originShift.setEntry(j, currentBest.getEntry(j));
for (int k = 0; k < npt; k++) {
interpolationPoints.setEntry(k, j, ZERO);
}
for (int i = 0; i < ndim; i++) {
bMatrix.setEntry(i, j, ZERO);
}
}
for (int i = 0, max = n * np / 2; i < max; i++) {
modelSecondDerivativesValues.setEntry(i, ZERO);
}
for (int k = 0; k < npt; k++) {
modelSecondDerivativesParameters.setEntry(k, ZERO);
for (int j = 0, max = npt - np; j < max; j++) {
zMatrix.setEntry(k, j, ZERO);
}
}
// Begin the initialization procedure. NF becomes one more than the number
// of function values so far. The coordinates of the displacement of the
// next initial interpolation point from XBASE are set in XPT(NF+1,.).
int ipt = 0;
int jpt = 0;
double fbeg = Double.NaN;
do {
final int nfm = getEvaluations();
final int nfx = nfm - n;
final int nfmm = nfm - 1;
final int nfxm = nfx - 1;
double stepa = 0;
double stepb = 0;
if (nfm <= 2 * n) {
if (nfm >= 1 &&
nfm <= n) {
stepa = initialTrustRegionRadius;
if (upperDifference.getEntry(nfmm) == ZERO) {
stepa = -stepa;
// throw new PathIsExploredException(); // XXX
}
interpolationPoints.setEntry(nfm, nfmm, stepa);
} else if (nfm > n) {
stepa = interpolationPoints.getEntry(nfx, nfxm);
stepb = -initialTrustRegionRadius;
if (lowerDifference.getEntry(nfxm) == ZERO) {
stepb = Math.min(TWO * initialTrustRegionRadius, upperDifference.getEntry(nfxm));
// throw new PathIsExploredException(); // XXX
}
if (upperDifference.getEntry(nfxm) == ZERO) {
stepb = Math.max(-TWO * initialTrustRegionRadius, lowerDifference.getEntry(nfxm));
// throw new PathIsExploredException(); // XXX
}
interpolationPoints.setEntry(nfm, nfxm, stepb);
}
} else {
final int tmp1 = (nfm - np) / n;
jpt = nfm - tmp1 * n - n;
ipt = jpt + tmp1;
if (ipt > n) {
final int tmp2 = jpt;
jpt = ipt - n;
ipt = tmp2;
// throw new PathIsExploredException(); // XXX
}
final int iptMinus1 = ipt - 1;
final int jptMinus1 = jpt - 1;
interpolationPoints.setEntry(nfm, iptMinus1, interpolationPoints.getEntry(ipt, iptMinus1));
interpolationPoints.setEntry(nfm, jptMinus1, interpolationPoints.getEntry(jpt, jptMinus1));
}
// Calculate the next value of F. The least function value so far and
// its index are required.
for (int j = 0; j < n; j++) {
currentBest.setEntry(j, Math.min(Math.max(lowerBound[j],
originShift.getEntry(j) + interpolationPoints.getEntry(nfm, j)),
upperBound[j]));
if (interpolationPoints.getEntry(nfm, j) == lowerDifference.getEntry(j)) {
currentBest.setEntry(j, lowerBound[j]);
}
if (interpolationPoints.getEntry(nfm, j) == upperDifference.getEntry(j)) {
currentBest.setEntry(j, upperBound[j]);
}
}
final double objectiveValue = computeObjectiveValue(currentBest.toArray());
final double f = isMinimize ? objectiveValue : -objectiveValue;
final int numEval = getEvaluations(); // nfm + 1
fAtInterpolationPoints.setEntry(nfm, f);
if (numEval == 1) {
fbeg = f;
trustRegionCenterInterpolationPointIndex = 0;
} else if (f < fAtInterpolationPoints.getEntry(trustRegionCenterInterpolationPointIndex)) {
trustRegionCenterInterpolationPointIndex = nfm;
}
// Set the nonzero initial elements of BMAT and the quadratic model in the
// cases when NF is at most 2*N+1. If NF exceeds N+1, then the positions
// of the NF-th and (NF-N)-th interpolation points may be switched, in
// order that the function value at the first of them contributes to the
// off-diagonal second derivative terms of the initial quadratic model.
if (numEval <= 2 * n + 1) {
if (numEval >= 2 &&
numEval <= n + 1) {
gradientAtTrustRegionCenter.setEntry(nfmm, (f - fbeg) / stepa);
if (npt < numEval + n) {
final double oneOverStepA = ONE / stepa;
bMatrix.setEntry(0, nfmm, -oneOverStepA);
bMatrix.setEntry(nfm, nfmm, oneOverStepA);
bMatrix.setEntry(npt + nfmm, nfmm, -HALF * rhosq);
// throw new PathIsExploredException(); // XXX
}
} else if (numEval >= n + 2) {
final int ih = nfx * (nfx + 1) / 2 - 1;
final double tmp = (f - fbeg) / stepb;
final double diff = stepb - stepa;
modelSecondDerivativesValues.setEntry(ih, TWO * (tmp - gradientAtTrustRegionCenter.getEntry(nfxm)) / diff);
gradientAtTrustRegionCenter.setEntry(nfxm, (gradientAtTrustRegionCenter.getEntry(nfxm) * stepb - tmp * stepa) / diff);
if (stepa * stepb < ZERO) {
if (f < fAtInterpolationPoints.getEntry(nfm - n)) {
fAtInterpolationPoints.setEntry(nfm, fAtInterpolationPoints.getEntry(nfm - n));
fAtInterpolationPoints.setEntry(nfm - n, f);
if (trustRegionCenterInterpolationPointIndex == nfm) {
trustRegionCenterInterpolationPointIndex = nfm - n;
}
interpolationPoints.setEntry(nfm - n, nfxm, stepb);
interpolationPoints.setEntry(nfm, nfxm, stepa);
}
}
bMatrix.setEntry(0, nfxm, -(stepa + stepb) / (stepa * stepb));
bMatrix.setEntry(nfm, nfxm, -HALF / interpolationPoints.getEntry(nfm - n, nfxm));
bMatrix.setEntry(nfm - n, nfxm,
-bMatrix.getEntry(0, nfxm) - bMatrix.getEntry(nfm, nfxm));
zMatrix.setEntry(0, nfxm, Math.sqrt(TWO) / (stepa * stepb));
zMatrix.setEntry(nfm, nfxm, Math.sqrt(HALF) / rhosq);
// zMatrix.setEntry(nfm, nfxm, Math.sqrt(HALF) * recip); // XXX "testAckley" and "testDiffPow" fail.
zMatrix.setEntry(nfm - n, nfxm,
-zMatrix.getEntry(0, nfxm) - zMatrix.getEntry(nfm, nfxm));
}
// Set the off-diagonal second derivatives of the Lagrange functions and
// the initial quadratic model.
} else {
zMatrix.setEntry(0, nfxm, recip);
zMatrix.setEntry(nfm, nfxm, recip);
zMatrix.setEntry(ipt, nfxm, -recip);
zMatrix.setEntry(jpt, nfxm, -recip);
final int ih = ipt * (ipt - 1) / 2 + jpt - 1;
final double tmp = interpolationPoints.getEntry(nfm, ipt - 1) * interpolationPoints.getEntry(nfm, jpt - 1);
modelSecondDerivativesValues.setEntry(ih, (fbeg - fAtInterpolationPoints.getEntry(ipt) - fAtInterpolationPoints.getEntry(jpt) + f) / tmp);
// throw new PathIsExploredException(); // XXX
}
} while (getEvaluations() < npt);
} // prelim
// ----------------------------------------------------------------------------------------
/**
* A version of the truncated conjugate gradient is applied. If a line
* search is restricted by a constraint, then the procedure is restarted,
* the values of the variables that are at their bounds being fixed. If
* the trust region boundary is reached, then further changes may be made
* to D, each one being in the two dimensional space that is spanned
* by the current D and the gradient of Q at XOPT+D, staying on the trust
* region boundary. Termination occurs when the reduction in Q seems to
* be close to the greatest reduction that can be achieved.
* The arguments N, NPT, XPT, XOPT, GOPT, HQ, PQ, SL and SU have the same
* meanings as the corresponding arguments of BOBYQB.
* DELTA is the trust region radius for the present calculation, which
* seeks a small value of the quadratic model within distance DELTA of
* XOPT subject to the bounds on the variables.
* XNEW will be set to a new vector of variables that is approximately
* the one that minimizes the quadratic model within the trust region
* subject to the SL and SU constraints on the variables. It satisfies
* as equations the bounds that become active during the calculation.
* D is the calculated trial step from XOPT, generated iteratively from an
* initial value of zero. Thus XNEW is XOPT+D after the final iteration.
* GNEW holds the gradient of the quadratic model at XOPT+D. It is updated
* when D is updated.
* xbdi.get( is a working space vector. For I=1,2,...,N, the element xbdi.get((I) is
* set to -1.0, 0.0, or 1.0, the value being nonzero if and only if the
* I-th variable has become fixed at a bound, the bound being SL(I) or
* SU(I) in the case xbdi.get((I)=-1.0 or xbdi.get((I)=1.0, respectively. This
* information is accumulated during the construction of XNEW.
* The arrays S, HS and HRED are also used for working space. They hold the
* current search direction, and the changes in the gradient of Q along S
* and the reduced D, respectively, where the reduced D is the same as D,
* except that the components of the fixed variables are zero.
* DSQ will be set to the square of the length of XNEW-XOPT.
* CRVMIN is set to zero if D reaches the trust region boundary. Otherwise
* it is set to the least curvature of H that occurs in the conjugate
* gradient searches that are not restricted by any constraints. The
* value CRVMIN=-1.0D0 is set, however, if all of these searches are
* constrained.
* @param delta
* @param gnew
* @param xbdi
* @param s
* @param hs
* @param hred
*/
private double[] trsbox(
double delta,
ArrayRealVector gnew,
ArrayRealVector xbdi,
ArrayRealVector s,
ArrayRealVector hs,
ArrayRealVector hred
) {
printMethod(); // XXX
final int n = currentBest.getDimension();
final int npt = numberOfInterpolationPoints;
double dsq = Double.NaN;
double crvmin = Double.NaN;
// Local variables
double ds;
int iu;
double dhd, dhs, cth, shs, sth, ssq, beta=0, sdec, blen;
int iact = -1;
int nact = 0;
double angt = 0, qred;
int isav;
double temp = 0, xsav = 0, xsum = 0, angbd = 0, dredg = 0, sredg = 0;
int iterc;
double resid = 0, delsq = 0, ggsav = 0, tempa = 0, tempb = 0,
redmax = 0, dredsq = 0, redsav = 0, gredsq = 0, rednew = 0;
int itcsav = 0;
double rdprev = 0, rdnext = 0, stplen = 0, stepsq = 0;
int itermax = 0;
// Set some constants.
// Function Body
// The sign of GOPT(I) gives the sign of the change to the I-th variable
// that will reduce Q from its value at XOPT. Thus xbdi.get((I) shows whether
// or not to fix the I-th variable at one of its bounds initially, with
// NACT being set to the number of fixed variables. D and GNEW are also
// set for the first iteration. DELSQ is the upper bound on the sum of
// squares of the free variables. QRED is the reduction in Q so far.
iterc = 0;
nact = 0;
for (int i = 0; i < n; i++) {
xbdi.setEntry(i, ZERO);
if (trustRegionCenterOffset.getEntry(i) <= lowerDifference.getEntry(i)) {
if (gradientAtTrustRegionCenter.getEntry(i) >= ZERO) {
xbdi.setEntry(i, MINUS_ONE);
}
} else if (trustRegionCenterOffset.getEntry(i) >= upperDifference.getEntry(i)) {
if (gradientAtTrustRegionCenter.getEntry(i) <= ZERO) {
xbdi.setEntry(i, ONE);
}
}
if (xbdi.getEntry(i) != ZERO) {
++nact;
}
trialStepPoint.setEntry(i, ZERO);
gnew.setEntry(i, gradientAtTrustRegionCenter.getEntry(i));
}
delsq = delta * delta;
qred = ZERO;
crvmin = MINUS_ONE;
// Set the next search direction of the conjugate gradient method. It is
// the steepest descent direction initially and when the iterations are
// restarted because a variable has just been fixed by a bound, and of
// course the components of the fixed variables are zero. ITERMAX is an
// upper bound on the indices of the conjugate gradient iterations.
int state = 20;
for(;;) {
switch (state) {
case 20: {
printState(20); // XXX
beta = ZERO;
}
case 30: {
printState(30); // XXX
stepsq = ZERO;
for (int i = 0; i < n; i++) {
if (xbdi.getEntry(i) != ZERO) {
s.setEntry(i, ZERO);
} else if (beta == ZERO) {
s.setEntry(i, -gnew.getEntry(i));
} else {
s.setEntry(i, beta * s.getEntry(i) - gnew.getEntry(i));
}
// Computing 2nd power
final double d1 = s.getEntry(i);
stepsq += d1 * d1;
}
if (stepsq == ZERO) {
state = 190; break;
}
if (beta == ZERO) {
gredsq = stepsq;
itermax = iterc + n - nact;
}
if (gredsq * delsq <= qred * 1e-4 * qred) {
state = 190; break;
}
// Multiply the search direction by the second derivative matrix of Q and
// calculate some scalars for the choice of steplength. Then set BLEN to
// the length of the the step to the trust region boundary and STPLEN to
// the steplength, ignoring the simple bounds.
state = 210; break;
}
case 50: {
printState(50); // XXX
resid = delsq;
ds = ZERO;
shs = ZERO;
for (int i = 0; i < n; i++) {
if (xbdi.getEntry(i) == ZERO) {
// Computing 2nd power
final double d1 = trialStepPoint.getEntry(i);
resid -= d1 * d1;
ds += s.getEntry(i) * trialStepPoint.getEntry(i);
shs += s.getEntry(i) * hs.getEntry(i);
}
}
if (resid <= ZERO) {
state = 90; break;
}
temp = Math.sqrt(stepsq * resid + ds * ds);
if (ds < ZERO) {
blen = (temp - ds) / stepsq;
} else {
blen = resid / (temp + ds);
}
stplen = blen;
if (shs > ZERO) {
// Computing MIN
stplen = Math.min(blen, gredsq / shs);
}
// Reduce STPLEN if necessary in order to preserve the simple bounds,
// letting IACT be the index of the new constrained variable.
iact = -1;
for (int i = 0; i < n; i++) {
if (s.getEntry(i) != ZERO) {
xsum = trustRegionCenterOffset.getEntry(i) + trialStepPoint.getEntry(i);
if (s.getEntry(i) > ZERO) {
temp = (upperDifference.getEntry(i) - xsum) / s.getEntry(i);
} else {
temp = (lowerDifference.getEntry(i) - xsum) / s.getEntry(i);
}
if (temp < stplen) {
stplen = temp;
iact = i;
}
}
}
// Update CRVMIN, GNEW and D. Set SDEC to the decrease that occurs in Q.
sdec = ZERO;
if (stplen > ZERO) {
++iterc;
temp = shs / stepsq;
if (iact == -1 && temp > ZERO) {
crvmin = Math.min(crvmin,temp);
if (crvmin == MINUS_ONE) {
crvmin = temp;
}
}
ggsav = gredsq;
gredsq = ZERO;
for (int i = 0; i < n; i++) {
gnew.setEntry(i, gnew.getEntry(i) + stplen * hs.getEntry(i));
if (xbdi.getEntry(i) == ZERO) {
// Computing 2nd power
final double d1 = gnew.getEntry(i);
gredsq += d1 * d1;
}
trialStepPoint.setEntry(i, trialStepPoint.getEntry(i) + stplen * s.getEntry(i));
}
// Computing MAX
final double d1 = stplen * (ggsav - HALF * stplen * shs);
sdec = Math.max(d1, ZERO);
qred += sdec;
}
// Restart the conjugate gradient method if it has hit a new bound.
if (iact >= 0) {
++nact;
xbdi.setEntry(iact, ONE);
if (s.getEntry(iact) < ZERO) {
xbdi.setEntry(iact, MINUS_ONE);
}
// Computing 2nd power
final double d1 = trialStepPoint.getEntry(iact);
delsq -= d1 * d1;
if (delsq <= ZERO) {
state = 190; break;
}
state = 20; break;
}
// If STPLEN is less than BLEN, then either apply another conjugate
// gradient iteration or RETURN.
if (stplen < blen) {
if (iterc == itermax) {
state = 190; break;
}
if (sdec <= qred * .01) {
state = 190; break;
}
beta = gredsq / ggsav;
state = 30; break;
}
}
case 90: {
printState(90); // XXX
crvmin = ZERO;
// Prepare for the alternative iteration by calculating some scalars
// and by multiplying the reduced D by the second derivative matrix of
// Q, where S holds the reduced D in the call of GGMULT.
}
case 100: {
printState(100); // XXX
if (nact >= n - 1) {
state = 190; break;
}
dredsq = ZERO;
dredg = ZERO;
gredsq = ZERO;
for (int i = 0; i < n; i++) {
if (xbdi.getEntry(i) == ZERO) {
// Computing 2nd power
double d1 = trialStepPoint.getEntry(i);
dredsq += d1 * d1;
dredg += trialStepPoint.getEntry(i) * gnew.getEntry(i);
// Computing 2nd power
d1 = gnew.getEntry(i);
gredsq += d1 * d1;
s.setEntry(i, trialStepPoint.getEntry(i));
} else {
s.setEntry(i, ZERO);
}
}
itcsav = iterc;
state = 210; break;
// Let the search direction S be a linear combination of the reduced D
// and the reduced G that is orthogonal to the reduced D.
}
case 120: {
printState(120); // XXX
++iterc;
temp = gredsq * dredsq - dredg * dredg;
if (temp <= qred * 1e-4 * qred) {
state = 190; break;
}
temp = Math.sqrt(temp);
for (int i = 0; i < n; i++) {
if (xbdi.getEntry(i) == ZERO) {
s.setEntry(i, (dredg * trialStepPoint.getEntry(i) - dredsq * gnew.getEntry(i)) / temp);
} else {
s.setEntry(i, ZERO);
}
}
sredg = -temp;
// By considering the simple bounds on the variables, calculate an upper
// bound on the tangent of half the angle of the alternative iteration,
// namely ANGBD, except that, if already a free variable has reached a
// bound, there is a branch back to label 100 after fixing that variable.
angbd = ONE;
iact = -1;
for (int i = 0; i < n; i++) {
if (xbdi.getEntry(i) == ZERO) {
tempa = trustRegionCenterOffset.getEntry(i) + trialStepPoint.getEntry(i) - lowerDifference.getEntry(i);
tempb = upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i) - trialStepPoint.getEntry(i);
if (tempa <= ZERO) {
++nact;
xbdi.setEntry(i, MINUS_ONE);
state = 100; break;
} else if (tempb <= ZERO) {
++nact;
xbdi.setEntry(i, ONE);
state = 100; break;
}
// Computing 2nd power
double d1 = trialStepPoint.getEntry(i);
// Computing 2nd power
double d2 = s.getEntry(i);
ssq = d1 * d1 + d2 * d2;
// Computing 2nd power
d1 = trustRegionCenterOffset.getEntry(i) - lowerDifference.getEntry(i);
temp = ssq - d1 * d1;
if (temp > ZERO) {
temp = Math.sqrt(temp) - s.getEntry(i);
if (angbd * temp > tempa) {
angbd = tempa / temp;
iact = i;
xsav = MINUS_ONE;
}
}
// Computing 2nd power
d1 = upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i);
temp = ssq - d1 * d1;
if (temp > ZERO) {
temp = Math.sqrt(temp) + s.getEntry(i);
if (angbd * temp > tempb) {
angbd = tempb / temp;
iact = i;
xsav = ONE;
}
}
}
}
// Calculate HHD and some curvatures for the alternative iteration.
state = 210; break;
}
case 150: {
printState(150); // XXX
shs = ZERO;
dhs = ZERO;
dhd = ZERO;
for (int i = 0; i < n; i++) {
if (xbdi.getEntry(i) == ZERO) {
shs += s.getEntry(i) * hs.getEntry(i);
dhs += trialStepPoint.getEntry(i) * hs.getEntry(i);
dhd += trialStepPoint.getEntry(i) * hred.getEntry(i);
}
}
// Seek the greatest reduction in Q for a range of equally spaced values
// of ANGT in [0,ANGBD], where ANGT is the tangent of half the angle of
// the alternative iteration.
redmax = ZERO;
isav = -1;
redsav = ZERO;
iu = (int) (angbd * 17. + 3.1);
for (int i = 0; i < iu; i++) {
angt = angbd * i / iu;
sth = (angt + angt) / (ONE + angt * angt);
temp = shs + angt * (angt * dhd - dhs - dhs);
rednew = sth * (angt * dredg - sredg - HALF * sth * temp);
if (rednew > redmax) {
redmax = rednew;
isav = i;
rdprev = redsav;
} else if (i == isav + 1) {
rdnext = rednew;
}
redsav = rednew;
}
// Return if the reduction is zero. Otherwise, set the sine and cosine
// of the angle of the alternative iteration, and calculate SDEC.
if (isav < 0) {
state = 190; break;
}
if (isav < iu) {
temp = (rdnext - rdprev) / (redmax + redmax - rdprev - rdnext);
angt = angbd * (isav + HALF * temp) / iu;
}
cth = (ONE - angt * angt) / (ONE + angt * angt);
sth = (angt + angt) / (ONE + angt * angt);
temp = shs + angt * (angt * dhd - dhs - dhs);
sdec = sth * (angt * dredg - sredg - HALF * sth * temp);
if (sdec <= ZERO) {
state = 190; break;
}
// Update GNEW, D and HRED. If the angle of the alternative iteration
// is restricted by a bound on a free variable, that variable is fixed
// at the bound.
dredg = ZERO;
gredsq = ZERO;
for (int i = 0; i < n; i++) {
gnew.setEntry(i, gnew.getEntry(i) + (cth - ONE) * hred.getEntry(i) + sth * hs.getEntry(i));
if (xbdi.getEntry(i) == ZERO) {
trialStepPoint.setEntry(i, cth * trialStepPoint.getEntry(i) + sth * s.getEntry(i));
dredg += trialStepPoint.getEntry(i) * gnew.getEntry(i);
// Computing 2nd power
final double d1 = gnew.getEntry(i);
gredsq += d1 * d1;
}
hred.setEntry(i, cth * hred.getEntry(i) + sth * hs.getEntry(i));
}
qred += sdec;
if (iact >= 0 && isav == iu) {
++nact;
xbdi.setEntry(iact, xsav);
state = 100; break;
}
// If SDEC is sufficiently small, then RETURN after setting XNEW to
// XOPT+D, giving careful attention to the bounds.
if (sdec > qred * .01) {
state = 120; break;
}
}
case 190: {
printState(190); // XXX
dsq = ZERO;
for (int i = 0; i < n; i++) {
// Computing MAX
// Computing MIN
final double min = Math.min(trustRegionCenterOffset.getEntry(i) + trialStepPoint.getEntry(i),
upperDifference.getEntry(i));
newPoint.setEntry(i, Math.max(min, lowerDifference.getEntry(i)));
if (xbdi.getEntry(i) == MINUS_ONE) {
newPoint.setEntry(i, lowerDifference.getEntry(i));
}
if (xbdi.getEntry(i) == ONE) {
newPoint.setEntry(i, upperDifference.getEntry(i));
}
trialStepPoint.setEntry(i, newPoint.getEntry(i) - trustRegionCenterOffset.getEntry(i));
// Computing 2nd power
final double d1 = trialStepPoint.getEntry(i);
dsq += d1 * d1;
}
return new double[] { dsq, crvmin };
// The following instructions multiply the current S-vector by the second
// derivative matrix of the quadratic model, putting the product in HS.
// They are reached from three different parts of the software above and
// they can be regarded as an external subroutine.
}
case 210: {
printState(210); // XXX
int ih = 0;
for (int j = 0; j < n; j++) {
hs.setEntry(j, ZERO);
for (int i = 0; i <= j; i++) {
if (i < j) {
hs.setEntry(j, hs.getEntry(j) + modelSecondDerivativesValues.getEntry(ih) * s.getEntry(i));
}
hs.setEntry(i, hs.getEntry(i) + modelSecondDerivativesValues.getEntry(ih) * s.getEntry(j));
ih++;
}
}
final RealVector tmp = interpolationPoints.operate(s).ebeMultiply(modelSecondDerivativesParameters);
for (int k = 0; k < npt; k++) {
if (modelSecondDerivativesParameters.getEntry(k) != ZERO) {
for (int i = 0; i < n; i++) {
hs.setEntry(i, hs.getEntry(i) + tmp.getEntry(k) * interpolationPoints.getEntry(k, i));
}
}
}
if (crvmin != ZERO) {
state = 50; break;
}
if (iterc > itcsav) {
state = 150; break;
}
for (int i = 0; i < n; i++) {
hred.setEntry(i, hs.getEntry(i));
}
state = 120; break;
}
default: {
throw new MathIllegalStateException(LocalizedFormats.SIMPLE_MESSAGE, "trsbox");
}}
}
} // trsbox
// ----------------------------------------------------------------------------------------
/**
* The arrays BMAT and ZMAT are updated, as required by the new position
* of the interpolation point that has the index KNEW. The vector VLAG has
* N+NPT components, set on entry to the first NPT and last N components
* of the product Hw in equation (4.11) of the Powell (2006) paper on
* NEWUOA. Further, BETA is set on entry to the value of the parameter
* with that name, and DENOM is set to the denominator of the updating
* formula. Elements of ZMAT may be treated as zero if their moduli are
* at most ZTEST. The first NDIM elements of W are used for working space.
* @param beta
* @param denom
* @param knew
*/
private void update(
double beta,
double denom,
int knew
) {
printMethod(); // XXX
final int n = currentBest.getDimension();
final int npt = numberOfInterpolationPoints;
final int nptm = npt - n - 1;
// XXX Should probably be split into two arrays.
final ArrayRealVector work = new ArrayRealVector(npt + n);
double ztest = ZERO;
for (int k = 0; k < npt; k++) {
for (int j = 0; j < nptm; j++) {
// Computing MAX
ztest = Math.max(ztest, Math.abs(zMatrix.getEntry(k, j)));
}
}
ztest *= 1e-20;
// Apply the rotations that put zeros in the KNEW-th row of ZMAT.
for (int j = 1; j < nptm; j++) {
final double d1 = zMatrix.getEntry(knew, j);
if (Math.abs(d1) > ztest) {
// Computing 2nd power
final double d2 = zMatrix.getEntry(knew, 0);
// Computing 2nd power
final double d3 = zMatrix.getEntry(knew, j);
final double d4 = Math.sqrt(d2 * d2 + d3 * d3);
final double d5 = zMatrix.getEntry(knew, 0) / d4;
final double d6 = zMatrix.getEntry(knew, j) / d4;
for (int i = 0; i < npt; i++) {
final double d7 = d5 * zMatrix.getEntry(i, 0) + d6 * zMatrix.getEntry(i, j);
zMatrix.setEntry(i, j, d5 * zMatrix.getEntry(i, j) - d6 * zMatrix.getEntry(i, 0));
zMatrix.setEntry(i, 0, d7);
}
}
zMatrix.setEntry(knew, j, ZERO);
}
// Put the first NPT components of the KNEW-th column of HLAG into W,
// and calculate the parameters of the updating formula.
for (int i = 0; i < npt; i++) {
work.setEntry(i, zMatrix.getEntry(knew, 0) * zMatrix.getEntry(i, 0));
}
final double alpha = work.getEntry(knew);
final double tau = lagrangeValuesAtNewPoint.getEntry(knew);
lagrangeValuesAtNewPoint.setEntry(knew, lagrangeValuesAtNewPoint.getEntry(knew) - ONE);
// Complete the updating of ZMAT.
final double sqrtDenom = Math.sqrt(denom);
final double d1 = tau / sqrtDenom;
final double d2 = zMatrix.getEntry(knew, 0) / sqrtDenom;
for (int i = 0; i < npt; i++) {
zMatrix.setEntry(i, 0,
d1 * zMatrix.getEntry(i, 0) - d2 * lagrangeValuesAtNewPoint.getEntry(i));
}
// Finally, update the matrix BMAT.
for (int j = 0; j < n; j++) {
final int jp = npt + j;
work.setEntry(jp, bMatrix.getEntry(knew, j));
final double d3 = (alpha * lagrangeValuesAtNewPoint.getEntry(jp) - tau * work.getEntry(jp)) / denom;
final double d4 = (-beta * work.getEntry(jp) - tau * lagrangeValuesAtNewPoint.getEntry(jp)) / denom;
for (int i = 0; i <= jp; i++) {
bMatrix.setEntry(i, j,
bMatrix.getEntry(i, j) + d3 * lagrangeValuesAtNewPoint.getEntry(i) + d4 * work.getEntry(i));
if (i >= npt) {
bMatrix.setEntry(jp, (i - npt), bMatrix.getEntry(i, j));
}
}
}
} // update
/**
* Performs validity checks.
*
* @param lowerBound Lower bounds (constraints) of the objective variables.
* @param upperBound Upperer bounds (constraints) of the objective variables.
*/
private void setup(double[] lowerBound,
double[] upperBound) {
printMethod(); // XXX
double[] init = getStartPoint();
final int dimension = init.length;
// Check problem dimension.
if (dimension < MINIMUM_PROBLEM_DIMENSION) {
throw new NumberIsTooSmallException(dimension, MINIMUM_PROBLEM_DIMENSION, true);
}
// Check number of interpolation points.
final int[] nPointsInterval = { dimension + 2, (dimension + 2) * (dimension + 1) / 2 };
if (numberOfInterpolationPoints < nPointsInterval[0] ||
numberOfInterpolationPoints > nPointsInterval[1]) {
throw new OutOfRangeException(LocalizedFormats.NUMBER_OF_INTERPOLATION_POINTS,
numberOfInterpolationPoints,
nPointsInterval[0],
nPointsInterval[1]);
}
// Initialize bound differences.
boundDifference = new double[dimension];
double requiredMinDiff = 2 * initialTrustRegionRadius;
double minDiff = Double.POSITIVE_INFINITY;
for (int i = 0; i < dimension; i++) {
boundDifference[i] = upperBound[i] - lowerBound[i];
minDiff = Math.min(minDiff, boundDifference[i]);
}
if (minDiff < requiredMinDiff) {
initialTrustRegionRadius = minDiff / 3.0;
}
// Initialize the data structures used by the "bobyqa" method.
bMatrix = new Array2DRowRealMatrix(dimension + numberOfInterpolationPoints,
dimension);
zMatrix = new Array2DRowRealMatrix(numberOfInterpolationPoints,
numberOfInterpolationPoints - dimension - 1);
interpolationPoints = new Array2DRowRealMatrix(numberOfInterpolationPoints,
dimension);
originShift = new ArrayRealVector(dimension);
fAtInterpolationPoints = new ArrayRealVector(numberOfInterpolationPoints);
trustRegionCenterOffset = new ArrayRealVector(dimension);
gradientAtTrustRegionCenter = new ArrayRealVector(dimension);
lowerDifference = new ArrayRealVector(dimension);
upperDifference = new ArrayRealVector(dimension);
modelSecondDerivativesParameters = new ArrayRealVector(numberOfInterpolationPoints);
newPoint = new ArrayRealVector(dimension);
alternativeNewPoint = new ArrayRealVector(dimension);
trialStepPoint = new ArrayRealVector(dimension);
lagrangeValuesAtNewPoint = new ArrayRealVector(dimension + numberOfInterpolationPoints);
modelSecondDerivativesValues = new ArrayRealVector(dimension * (dimension + 1) / 2);
}
/**
* Creates a new array.
*
* @param n Dimension of the returned array.
* @param value Value for each element.
* @return an array containing {@code n} elements set to the given
* {@code value}.
*/
private static double[] fillNewArray(int n,
double value) {
double[] ds = new double[n];
Arrays.fill(ds, value);
return ds;
}
// XXX utility for figuring out call sequence.
private static String caller(int n) {
final Throwable t = new Throwable();
final StackTraceElement[] elements = t.getStackTrace();
final StackTraceElement e = elements[n];
return e.getMethodName() + " (at line " + e.getLineNumber() + ")";
}
// XXX utility for figuring out call sequence.
private static void printState(int s) {
// System.out.println(caller(2) + ": state " + s);
}
// XXX utility for figuring out call sequence.
private static void printMethod() {
// System.out.println(caller(2));
}
/**
* Marker for code paths that are not explored with the current unit tests.
* If the path becomes explored, it should just be removed from the code.
*/
private static class PathIsExploredException extends RuntimeException {
private static final long serialVersionUID = 745350979634801853L;
private static final String PATH_IS_EXPLORED
= "If this exception is thrown, just remove it from the code";
PathIsExploredException() {
super(PATH_IS_EXPLORED + " " + BOBYQAOptimizer.caller(3));
}
}
} | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/nonlinear/scalar/noderiv/CMAESOptimizer.java | 578 |
| org/apache/commons/math3/optimization/direct/CMAESOptimizer.java | 709 |
sigmaArray[i][0] = inputSigma[i];
}
final RealMatrix insigma = new Array2DRowRealMatrix(sigmaArray, false);
sigma = max(insigma); // overall standard deviation
// initialize termination criteria
stopTolUpX = 1e3 * max(insigma);
stopTolX = 1e-11 * max(insigma);
stopTolFun = 1e-12;
stopTolHistFun = 1e-13;
// initialize selection strategy parameters
mu = lambda / 2; // number of parents/points for recombination
logMu2 = Math.log(mu + 0.5);
weights = log(sequence(1, mu, 1)).scalarMultiply(-1).scalarAdd(logMu2);
double sumw = 0;
double sumwq = 0;
for (int i = 0; i < mu; i++) {
double w = weights.getEntry(i, 0);
sumw += w;
sumwq += w * w;
}
weights = weights.scalarMultiply(1 / sumw);
mueff = sumw * sumw / sumwq; // variance-effectiveness of sum w_i x_i
// initialize dynamic strategy parameters and constants
cc = (4 + mueff / dimension) /
(dimension + 4 + 2 * mueff / dimension);
cs = (mueff + 2) / (dimension + mueff + 3.);
damps = (1 + 2 * Math.max(0, Math.sqrt((mueff - 1) /
(dimension + 1)) - 1)) *
Math.max(0.3,
1 - dimension / (1e-6 + maxIterations)) + cs; // minor increment
ccov1 = 2 / ((dimension + 1.3) * (dimension + 1.3) + mueff);
ccovmu = Math.min(1 - ccov1, 2 * (mueff - 2 + 1 / mueff) /
((dimension + 2) * (dimension + 2) + mueff));
ccov1Sep = Math.min(1, ccov1 * (dimension + 1.5) / 3);
ccovmuSep = Math.min(1 - ccov1, ccovmu * (dimension + 1.5) / 3);
chiN = Math.sqrt(dimension) *
(1 - 1 / ((double) 4 * dimension) + 1 / ((double) 21 * dimension * dimension));
// intialize CMA internal values - updated each generation
xmean = MatrixUtils.createColumnRealMatrix(guess); // objective variables
diagD = insigma.scalarMultiply(1 / sigma);
diagC = square(diagD);
pc = zeros(dimension, 1); // evolution paths for C and sigma
ps = zeros(dimension, 1); // B defines the coordinate system
normps = ps.getFrobeniusNorm();
B = eye(dimension, dimension);
D = ones(dimension, 1); // diagonal D defines the scaling
BD = times(B, repmat(diagD.transpose(), dimension, 1));
C = B.multiply(diag(square(D)).multiply(B.transpose())); // covariance
historySize = 10 + (int) (3 * 10 * dimension / (double) lambda);
fitnessHistory = new double[historySize]; // history of fitness values
for (int i = 0; i < historySize; i++) {
fitnessHistory[i] = Double.MAX_VALUE;
}
}
/**
* Update of the evolution paths ps and pc.
*
* @param zmean Weighted row matrix of the gaussian random numbers generating
* the current offspring.
* @param xold xmean matrix of the previous generation.
* @return hsig flag indicating a small correction.
*/
private boolean updateEvolutionPaths(RealMatrix zmean, RealMatrix xold) {
ps = ps.scalarMultiply(1 - cs).add(
B.multiply(zmean).scalarMultiply(
Math.sqrt(cs * (2 - cs) * mueff)));
normps = ps.getFrobeniusNorm();
final boolean hsig = normps /
Math.sqrt(1 - Math.pow(1 - cs, 2 * iterations)) /
chiN < 1.4 + 2 / ((double) dimension + 1);
pc = pc.scalarMultiply(1 - cc);
if (hsig) {
pc = pc.add(xmean.subtract(xold).scalarMultiply(Math.sqrt(cc * (2 - cc) * mueff) / sigma));
}
return hsig;
}
/**
* Update of the covariance matrix C for diagonalOnly > 0
*
* @param hsig Flag indicating a small correction.
* @param bestArz Fitness-sorted matrix of the gaussian random values of the
* current offspring.
*/
private void updateCovarianceDiagonalOnly(boolean hsig,
final RealMatrix bestArz) {
// minor correction if hsig==false
double oldFac = hsig ? 0 : ccov1Sep * cc * (2 - cc);
oldFac += 1 - ccov1Sep - ccovmuSep;
diagC = diagC.scalarMultiply(oldFac) // regard old matrix
.add(square(pc).scalarMultiply(ccov1Sep)) // plus rank one update
.add((times(diagC, square(bestArz).multiply(weights))) // plus rank mu update
.scalarMultiply(ccovmuSep));
diagD = sqrt(diagC); // replaces eig(C)
if (diagonalOnly > 1 &&
iterations > diagonalOnly) {
// full covariance matrix from now on
diagonalOnly = 0;
B = eye(dimension, dimension);
BD = diag(diagD);
C = diag(diagC);
}
}
/**
* Update of the covariance matrix C.
*
* @param hsig Flag indicating a small correction.
* @param bestArx Fitness-sorted matrix of the argument vectors producing the
* current offspring.
* @param arz Unsorted matrix containing the gaussian random values of the
* current offspring.
* @param arindex Indices indicating the fitness-order of the current offspring.
* @param xold xmean matrix of the previous generation.
*/
private void updateCovariance(boolean hsig, final RealMatrix bestArx,
final RealMatrix arz, final int[] arindex,
final RealMatrix xold) {
double negccov = 0;
if (ccov1 + ccovmu > 0) {
final RealMatrix arpos = bestArx.subtract(repmat(xold, 1, mu))
.scalarMultiply(1 / sigma); // mu difference vectors
final RealMatrix roneu = pc.multiply(pc.transpose())
.scalarMultiply(ccov1); // rank one update
// minor correction if hsig==false
double oldFac = hsig ? 0 : ccov1 * cc * (2 - cc);
oldFac += 1 - ccov1 - ccovmu;
if (isActiveCMA) {
// Adapt covariance matrix C active CMA
negccov = (1 - ccovmu) * 0.25 * mueff /
(Math.pow(dimension + 2, 1.5) + 2 * mueff);
// keep at least 0.66 in all directions, small popsize are most
// critical
final double negminresidualvariance = 0.66;
// where to make up for the variance loss
final double negalphaold = 0.5;
// prepare vectors, compute negative updating matrix Cneg
final int[] arReverseIndex = reverse(arindex);
RealMatrix arzneg = selectColumns(arz, MathArrays.copyOf(arReverseIndex, mu));
RealMatrix arnorms = sqrt(sumRows(square(arzneg)));
final int[] idxnorms = sortedIndices(arnorms.getRow(0));
final RealMatrix arnormsSorted = selectColumns(arnorms, idxnorms);
final int[] idxReverse = reverse(idxnorms);
final RealMatrix arnormsReverse = selectColumns(arnorms, idxReverse);
arnorms = divide(arnormsReverse, arnormsSorted);
final int[] idxInv = inverse(idxnorms);
final RealMatrix arnormsInv = selectColumns(arnorms, idxInv);
// check and set learning rate negccov
final double negcovMax = (1 - negminresidualvariance) /
square(arnormsInv).multiply(weights).getEntry(0, 0);
if (negccov > negcovMax) {
negccov = negcovMax;
}
arzneg = times(arzneg, repmat(arnormsInv, dimension, 1));
final RealMatrix artmp = BD.multiply(arzneg);
final RealMatrix Cneg = artmp.multiply(diag(weights)).multiply(artmp.transpose());
oldFac += negalphaold * negccov;
C = C.scalarMultiply(oldFac)
.add(roneu) // regard old matrix
.add(arpos.scalarMultiply( // plus rank one update
ccovmu + (1 - negalphaold) * negccov) // plus rank mu update
.multiply(times(repmat(weights, 1, dimension),
arpos.transpose())))
.subtract(Cneg.scalarMultiply(negccov));
} else {
// Adapt covariance matrix C - nonactive
C = C.scalarMultiply(oldFac) // regard old matrix
.add(roneu) // plus rank one update
.add(arpos.scalarMultiply(ccovmu) // plus rank mu update
.multiply(times(repmat(weights, 1, dimension),
arpos.transpose())));
}
}
updateBD(negccov);
}
/**
* Update B and D from C.
*
* @param negccov Negative covariance factor.
*/
private void updateBD(double negccov) {
if (ccov1 + ccovmu + negccov > 0 &&
(iterations % 1. / (ccov1 + ccovmu + negccov) / dimension / 10.) < 1) {
// to achieve O(N^2)
C = triu(C, 0).add(triu(C, 1).transpose());
// enforce symmetry to prevent complex numbers
final EigenDecomposition eig = new EigenDecomposition(C);
B = eig.getV(); // eigen decomposition, B==normalized eigenvectors
D = eig.getD();
diagD = diag(D);
if (min(diagD) <= 0) {
for (int i = 0; i < dimension; i++) {
if (diagD.getEntry(i, 0) < 0) {
diagD.setEntry(i, 0, 0);
}
}
final double tfac = max(diagD) / 1e14;
C = C.add(eye(dimension, dimension).scalarMultiply(tfac));
diagD = diagD.add(ones(dimension, 1).scalarMultiply(tfac));
}
if (max(diagD) > 1e14 * min(diagD)) {
final double tfac = max(diagD) / 1e14 - min(diagD);
C = C.add(eye(dimension, dimension).scalarMultiply(tfac));
diagD = diagD.add(ones(dimension, 1).scalarMultiply(tfac));
}
diagC = diag(C);
diagD = sqrt(diagD); // D contains standard deviations now
BD = times(B, repmat(diagD.transpose(), dimension, 1)); // O(n^2)
}
}
/**
* Pushes the current best fitness value in a history queue.
*
* @param vals History queue.
* @param val Current best fitness value.
*/
private static void push(double[] vals, double val) {
for (int i = vals.length-1; i > 0; i--) {
vals[i] = vals[i-1];
}
vals[0] = val;
}
/**
* Sorts fitness values.
*
* @param doubles Array of values to be sorted.
* @return a sorted array of indices pointing into doubles.
*/
private int[] sortedIndices(final double[] doubles) {
final DoubleIndex[] dis = new DoubleIndex[doubles.length];
for (int i = 0; i < doubles.length; i++) {
dis[i] = new DoubleIndex(doubles[i], i);
}
Arrays.sort(dis);
final int[] indices = new int[doubles.length];
for (int i = 0; i < doubles.length; i++) {
indices[i] = dis[i].index;
}
return indices;
}
/**
* Used to sort fitness values. Sorting is always in lower value first
* order.
*/
private static class DoubleIndex implements Comparable<DoubleIndex> {
/** Value to compare. */
private final double value;
/** Index into sorted array. */
private final int index;
/**
* @param value Value to compare.
* @param index Index into sorted array.
*/
DoubleIndex(double value, int index) {
this.value = value;
this.index = index;
}
/** {@inheritDoc} */
public int compareTo(DoubleIndex o) {
return Double.compare(value, o.value);
}
/** {@inheritDoc} */
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other instanceof DoubleIndex) {
return Double.compare(value, ((DoubleIndex) other).value) == 0;
}
return false;
}
/** {@inheritDoc} */
@Override
public int hashCode() {
long bits = Double.doubleToLongBits(value);
return (int) ((1438542 ^ (bits >>> 32) ^ bits) & 0xffffffff);
}
}
/**
* Normalizes fitness values to the range [0,1]. Adds a penalty to the
* fitness value if out of range. The penalty is adjusted by calling
* setValueRange().
*/
private class FitnessFunction {
/** Determines the penalty for boundary violations */
private double valueRange;
/**
* Flag indicating whether the objective variables are forced into their
* bounds if defined
*/
private final boolean isRepairMode;
/** Simple constructor.
*/
public FitnessFunction() {
valueRange = 1;
isRepairMode = true;
}
/**
* @param point Normalized objective variables.
* @return the objective value + penalty for violated bounds.
*/
public double value(final double[] point) {
double value;
if (isRepairMode) {
double[] repaired = repair(point);
value = CMAESOptimizer.this.computeObjectiveValue(repaired) +
penalty(point, repaired);
} else {
value = CMAESOptimizer.this.computeObjectiveValue(point);
}
return isMinimize ? value : -value;
}
/**
* @param x Normalized objective variables.
* @return {@code true} if in bounds.
*/
public boolean isFeasible(final double[] x) {
final double[] lB = CMAESOptimizer.this.getLowerBound();
final double[] uB = CMAESOptimizer.this.getUpperBound();
for (int i = 0; i < x.length; i++) {
if (x[i] < lB[i]) {
return false;
}
if (x[i] > uB[i]) {
return false;
}
}
return true;
}
/**
* @param valueRange Adjusts the penalty computation.
*/
public void setValueRange(double valueRange) {
this.valueRange = valueRange;
}
/**
* @param x Normalized objective variables.
* @return the repaired (i.e. all in bounds) objective variables.
*/
private double[] repair(final double[] x) {
final double[] lB = CMAESOptimizer.this.getLowerBound();
final double[] uB = CMAESOptimizer.this.getUpperBound();
final double[] repaired = new double[x.length];
for (int i = 0; i < x.length; i++) {
if (x[i] < lB[i]) {
repaired[i] = lB[i];
} else if (x[i] > uB[i]) {
repaired[i] = uB[i];
} else {
repaired[i] = x[i];
}
}
return repaired;
}
/**
* @param x Normalized objective variables.
* @param repaired Repaired objective variables.
* @return Penalty value according to the violation of the bounds.
*/
private double penalty(final double[] x, final double[] repaired) {
double penalty = 0;
for (int i = 0; i < x.length; i++) {
double diff = Math.abs(x[i] - repaired[i]);
penalty += diff * valueRange;
}
return isMinimize ? penalty : -penalty;
}
}
// -----Matrix utility functions similar to the Matlab build in functions------
/**
* @param m Input matrix
* @return Matrix representing the element-wise logarithm of m.
*/
private static RealMatrix log(final RealMatrix m) {
final double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
for (int r = 0; r < m.getRowDimension(); r++) {
for (int c = 0; c < m.getColumnDimension(); c++) {
d[r][c] = Math.log(m.getEntry(r, c));
}
}
return new Array2DRowRealMatrix(d, false);
}
/**
* @param m Input matrix.
* @return Matrix representing the element-wise square root of m.
*/
private static RealMatrix sqrt(final RealMatrix m) {
final double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
for (int r = 0; r < m.getRowDimension(); r++) {
for (int c = 0; c < m.getColumnDimension(); c++) {
d[r][c] = Math.sqrt(m.getEntry(r, c));
}
}
return new Array2DRowRealMatrix(d, false);
}
/**
* @param m Input matrix.
* @return Matrix representing the element-wise square of m.
*/
private static RealMatrix square(final RealMatrix m) {
final double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
for (int r = 0; r < m.getRowDimension(); r++) {
for (int c = 0; c < m.getColumnDimension(); c++) {
double e = m.getEntry(r, c);
d[r][c] = e * e;
}
}
return new Array2DRowRealMatrix(d, false);
}
/**
* @param m Input matrix 1.
* @param n Input matrix 2.
* @return the matrix where the elements of m and n are element-wise multiplied.
*/
private static RealMatrix times(final RealMatrix m, final RealMatrix n) {
final double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
for (int r = 0; r < m.getRowDimension(); r++) {
for (int c = 0; c < m.getColumnDimension(); c++) {
d[r][c] = m.getEntry(r, c) * n.getEntry(r, c);
}
}
return new Array2DRowRealMatrix(d, false);
}
/**
* @param m Input matrix 1.
* @param n Input matrix 2.
* @return Matrix where the elements of m and n are element-wise divided.
*/
private static RealMatrix divide(final RealMatrix m, final RealMatrix n) {
final double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
for (int r = 0; r < m.getRowDimension(); r++) {
for (int c = 0; c < m.getColumnDimension(); c++) {
d[r][c] = m.getEntry(r, c) / n.getEntry(r, c);
}
}
return new Array2DRowRealMatrix(d, false);
}
/**
* @param m Input matrix.
* @param cols Columns to select.
* @return Matrix representing the selected columns.
*/
private static RealMatrix selectColumns(final RealMatrix m, final int[] cols) {
final double[][] d = new double[m.getRowDimension()][cols.length];
for (int r = 0; r < m.getRowDimension(); r++) {
for (int c = 0; c < cols.length; c++) {
d[r][c] = m.getEntry(r, cols[c]);
}
}
return new Array2DRowRealMatrix(d, false);
}
/**
* @param m Input matrix.
* @param k Diagonal position.
* @return Upper triangular part of matrix.
*/
private static RealMatrix triu(final RealMatrix m, int k) {
final double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
for (int r = 0; r < m.getRowDimension(); r++) {
for (int c = 0; c < m.getColumnDimension(); c++) {
d[r][c] = r <= c - k ? m.getEntry(r, c) : 0;
}
}
return new Array2DRowRealMatrix(d, false);
}
/**
* @param m Input matrix.
* @return Row matrix representing the sums of the rows.
*/
private static RealMatrix sumRows(final RealMatrix m) {
final double[][] d = new double[1][m.getColumnDimension()];
for (int c = 0; c < m.getColumnDimension(); c++) {
double sum = 0;
for (int r = 0; r < m.getRowDimension(); r++) {
sum += m.getEntry(r, c);
}
d[0][c] = sum;
}
return new Array2DRowRealMatrix(d, false);
}
/**
* @param m Input matrix.
* @return the diagonal n-by-n matrix if m is a column matrix or the column
* matrix representing the diagonal if m is a n-by-n matrix.
*/
private static RealMatrix diag(final RealMatrix m) {
if (m.getColumnDimension() == 1) {
final double[][] d = new double[m.getRowDimension()][m.getRowDimension()];
for (int i = 0; i < m.getRowDimension(); i++) {
d[i][i] = m.getEntry(i, 0);
}
return new Array2DRowRealMatrix(d, false);
} else {
final double[][] d = new double[m.getRowDimension()][1];
for (int i = 0; i < m.getColumnDimension(); i++) {
d[i][0] = m.getEntry(i, i);
}
return new Array2DRowRealMatrix(d, false);
}
}
/**
* Copies a column from m1 to m2.
*
* @param m1 Source matrix.
* @param col1 Source column.
* @param m2 Target matrix.
* @param col2 Target column.
*/
private static void copyColumn(final RealMatrix m1, int col1,
RealMatrix m2, int col2) {
for (int i = 0; i < m1.getRowDimension(); i++) {
m2.setEntry(i, col2, m1.getEntry(i, col1));
}
}
/**
* @param n Number of rows.
* @param m Number of columns.
* @return n-by-m matrix filled with 1.
*/
private static RealMatrix ones(int n, int m) {
final double[][] d = new double[n][m];
for (int r = 0; r < n; r++) {
Arrays.fill(d[r], 1);
}
return new Array2DRowRealMatrix(d, false);
}
/**
* @param n Number of rows.
* @param m Number of columns.
* @return n-by-m matrix of 0 values out of diagonal, and 1 values on
* the diagonal.
*/
private static RealMatrix eye(int n, int m) {
final double[][] d = new double[n][m];
for (int r = 0; r < n; r++) {
if (r < m) {
d[r][r] = 1;
}
}
return new Array2DRowRealMatrix(d, false);
}
/**
* @param n Number of rows.
* @param m Number of columns.
* @return n-by-m matrix of zero values.
*/
private static RealMatrix zeros(int n, int m) {
return new Array2DRowRealMatrix(n, m);
}
/**
* @param mat Input matrix.
* @param n Number of row replicates.
* @param m Number of column replicates.
* @return a matrix which replicates the input matrix in both directions.
*/
private static RealMatrix repmat(final RealMatrix mat, int n, int m) {
final int rd = mat.getRowDimension();
final int cd = mat.getColumnDimension();
final double[][] d = new double[n * rd][m * cd];
for (int r = 0; r < n * rd; r++) {
for (int c = 0; c < m * cd; c++) {
d[r][c] = mat.getEntry(r % rd, c % cd);
}
}
return new Array2DRowRealMatrix(d, false);
}
/**
* @param start Start value.
* @param end End value.
* @param step Step size.
* @return a sequence as column matrix.
*/
private static RealMatrix sequence(double start, double end, double step) {
final int size = (int) ((end - start) / step + 1);
final double[][] d = new double[size][1];
double value = start;
for (int r = 0; r < size; r++) {
d[r][0] = value;
value += step;
}
return new Array2DRowRealMatrix(d, false);
}
/**
* @param m Input matrix.
* @return the maximum of the matrix element values.
*/
private static double max(final RealMatrix m) {
double max = -Double.MAX_VALUE;
for (int r = 0; r < m.getRowDimension(); r++) {
for (int c = 0; c < m.getColumnDimension(); c++) {
double e = m.getEntry(r, c);
if (max < e) {
max = e;
}
}
}
return max;
}
/**
* @param m Input matrix.
* @return the minimum of the matrix element values.
*/
private static double min(final RealMatrix m) {
double min = Double.MAX_VALUE;
for (int r = 0; r < m.getRowDimension(); r++) {
for (int c = 0; c < m.getColumnDimension(); c++) {
double e = m.getEntry(r, c);
if (min > e) {
min = e;
}
}
}
return min;
}
/**
* @param m Input array.
* @return the maximum of the array values.
*/
private static double max(final double[] m) {
double max = -Double.MAX_VALUE;
for (int r = 0; r < m.length; r++) {
if (max < m[r]) {
max = m[r];
}
}
return max;
}
/**
* @param m Input array.
* @return the minimum of the array values.
*/
private static double min(final double[] m) {
double min = Double.MAX_VALUE;
for (int r = 0; r < m.length; r++) {
if (min > m[r]) {
min = m[r];
}
}
return min;
}
/**
* @param indices Input index array.
* @return the inverse of the mapping defined by indices.
*/
private static int[] inverse(final int[] indices) {
final int[] inverse = new int[indices.length];
for (int i = 0; i < indices.length; i++) {
inverse[indices[i]] = i;
}
return inverse;
}
/**
* @param indices Input index array.
* @return the indices in inverse order (last is first).
*/
private static int[] reverse(final int[] indices) {
final int[] reverse = new int[indices.length];
for (int i = 0; i < indices.length; i++) {
reverse[i] = indices[indices.length - i - 1];
}
return reverse;
}
/**
* @param size Length of random array.
* @return an array of Gaussian random numbers.
*/
private double[] randn(int size) {
final double[] randn = new double[size];
for (int i = 0; i < size; i++) {
randn[i] = random.nextGaussian();
}
return randn;
}
/**
* @param size Number of rows.
* @param popSize Population size.
* @return a 2-dimensional matrix of Gaussian random numbers.
*/
private RealMatrix randn1(int size, int popSize) {
final double[][] d = new double[size][popSize];
for (int r = 0; r < size; r++) {
for (int c = 0; c < popSize; c++) {
d[r][c] = random.nextGaussian();
}
}
return new Array2DRowRealMatrix(d, false);
}
} | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/nonlinear/vector/jacobian/LevenbergMarquardtOptimizer.java | 508 |
| org/apache/commons/math3/optimization/general/LevenbergMarquardtOptimizer.java | 516 |
setCost(currentCost);
return current;
}
// tests for termination and stringent tolerances
// (2.2204e-16 is the machine epsilon for IEEE754)
if ((FastMath.abs(actRed) <= 2.2204e-16) && (preRed <= 2.2204e-16) && (ratio <= 2.0)) {
throw new ConvergenceException(LocalizedFormats.TOO_SMALL_COST_RELATIVE_TOLERANCE,
costRelativeTolerance);
} else if (delta <= 2.2204e-16 * xNorm) {
throw new ConvergenceException(LocalizedFormats.TOO_SMALL_PARAMETERS_RELATIVE_TOLERANCE,
parRelativeTolerance);
} else if (maxCosine <= 2.2204e-16) {
throw new ConvergenceException(LocalizedFormats.TOO_SMALL_ORTHOGONALITY_TOLERANCE,
orthoTolerance);
}
}
}
}
/**
* Determine the Levenberg-Marquardt parameter.
* <p>This implementation is a translation in Java of the MINPACK
* <a href="http://www.netlib.org/minpack/lmpar.f">lmpar</a>
* routine.</p>
* <p>This method sets the lmPar and lmDir attributes.</p>
* <p>The authors of the original fortran function are:</p>
* <ul>
* <li>Argonne National Laboratory. MINPACK project. March 1980</li>
* <li>Burton S. Garbow</li>
* <li>Kenneth E. Hillstrom</li>
* <li>Jorge J. More</li>
* </ul>
* <p>Luc Maisonobe did the Java translation.</p>
*
* @param qy array containing qTy
* @param delta upper bound on the euclidean norm of diagR * lmDir
* @param diag diagonal matrix
* @param work1 work array
* @param work2 work array
* @param work3 work array
*/
private void determineLMParameter(double[] qy, double delta, double[] diag,
double[] work1, double[] work2, double[] work3) {
final int nC = weightedJacobian[0].length;
// compute and store in x the gauss-newton direction, if the
// jacobian is rank-deficient, obtain a least squares solution
for (int j = 0; j < rank; ++j) {
lmDir[permutation[j]] = qy[j];
}
for (int j = rank; j < nC; ++j) {
lmDir[permutation[j]] = 0;
}
for (int k = rank - 1; k >= 0; --k) {
int pk = permutation[k];
double ypk = lmDir[pk] / diagR[pk];
for (int i = 0; i < k; ++i) {
lmDir[permutation[i]] -= ypk * weightedJacobian[i][pk];
}
lmDir[pk] = ypk;
}
// evaluate the function at the origin, and test
// for acceptance of the Gauss-Newton direction
double dxNorm = 0;
for (int j = 0; j < solvedCols; ++j) {
int pj = permutation[j];
double s = diag[pj] * lmDir[pj];
work1[pj] = s;
dxNorm += s * s;
}
dxNorm = FastMath.sqrt(dxNorm);
double fp = dxNorm - delta;
if (fp <= 0.1 * delta) {
lmPar = 0;
return;
}
// if the jacobian is not rank deficient, the Newton step provides
// a lower bound, parl, for the zero of the function,
// otherwise set this bound to zero
double sum2;
double parl = 0;
if (rank == solvedCols) {
for (int j = 0; j < solvedCols; ++j) {
int pj = permutation[j];
work1[pj] *= diag[pj] / dxNorm;
}
sum2 = 0;
for (int j = 0; j < solvedCols; ++j) {
int pj = permutation[j];
double sum = 0;
for (int i = 0; i < j; ++i) {
sum += weightedJacobian[i][pj] * work1[permutation[i]];
}
double s = (work1[pj] - sum) / diagR[pj];
work1[pj] = s;
sum2 += s * s;
}
parl = fp / (delta * sum2);
}
// calculate an upper bound, paru, for the zero of the function
sum2 = 0;
for (int j = 0; j < solvedCols; ++j) {
int pj = permutation[j];
double sum = 0;
for (int i = 0; i <= j; ++i) {
sum += weightedJacobian[i][pj] * qy[i];
}
sum /= diag[pj];
sum2 += sum * sum;
}
double gNorm = FastMath.sqrt(sum2);
double paru = gNorm / delta;
if (paru == 0) {
// 2.2251e-308 is the smallest positive real for IEE754
paru = 2.2251e-308 / FastMath.min(delta, 0.1);
}
// if the input par lies outside of the interval (parl,paru),
// set par to the closer endpoint
lmPar = FastMath.min(paru, FastMath.max(lmPar, parl));
if (lmPar == 0) {
lmPar = gNorm / dxNorm;
}
for (int countdown = 10; countdown >= 0; --countdown) {
// evaluate the function at the current value of lmPar
if (lmPar == 0) {
lmPar = FastMath.max(2.2251e-308, 0.001 * paru);
}
double sPar = FastMath.sqrt(lmPar);
for (int j = 0; j < solvedCols; ++j) {
int pj = permutation[j];
work1[pj] = sPar * diag[pj];
}
determineLMDirection(qy, work1, work2, work3);
dxNorm = 0;
for (int j = 0; j < solvedCols; ++j) {
int pj = permutation[j];
double s = diag[pj] * lmDir[pj];
work3[pj] = s;
dxNorm += s * s;
}
dxNorm = FastMath.sqrt(dxNorm);
double previousFP = fp;
fp = dxNorm - delta;
// if the function is small enough, accept the current value
// of lmPar, also test for the exceptional cases where parl is zero
if ((FastMath.abs(fp) <= 0.1 * delta) ||
((parl == 0) && (fp <= previousFP) && (previousFP < 0))) {
return;
}
// compute the Newton correction
for (int j = 0; j < solvedCols; ++j) {
int pj = permutation[j];
work1[pj] = work3[pj] * diag[pj] / dxNorm;
}
for (int j = 0; j < solvedCols; ++j) {
int pj = permutation[j];
work1[pj] /= work2[j];
double tmp = work1[pj];
for (int i = j + 1; i < solvedCols; ++i) {
work1[permutation[i]] -= weightedJacobian[i][pj] * tmp;
}
}
sum2 = 0;
for (int j = 0; j < solvedCols; ++j) {
double s = work1[permutation[j]];
sum2 += s * s;
}
double correction = fp / (delta * sum2);
// depending on the sign of the function, update parl or paru.
if (fp > 0) {
parl = FastMath.max(parl, lmPar);
} else if (fp < 0) {
paru = FastMath.min(paru, lmPar);
}
// compute an improved estimate for lmPar
lmPar = FastMath.max(parl, lmPar + correction);
}
}
/**
* Solve a*x = b and d*x = 0 in the least squares sense.
* <p>This implementation is a translation in Java of the MINPACK
* <a href="http://www.netlib.org/minpack/qrsolv.f">qrsolv</a>
* routine.</p>
* <p>This method sets the lmDir and lmDiag attributes.</p>
* <p>The authors of the original fortran function are:</p>
* <ul>
* <li>Argonne National Laboratory. MINPACK project. March 1980</li>
* <li>Burton S. Garbow</li>
* <li>Kenneth E. Hillstrom</li>
* <li>Jorge J. More</li>
* </ul>
* <p>Luc Maisonobe did the Java translation.</p>
*
* @param qy array containing qTy
* @param diag diagonal matrix
* @param lmDiag diagonal elements associated with lmDir
* @param work work array
*/
private void determineLMDirection(double[] qy, double[] diag,
double[] lmDiag, double[] work) {
// copy R and Qty to preserve input and initialize s
// in particular, save the diagonal elements of R in lmDir
for (int j = 0; j < solvedCols; ++j) {
int pj = permutation[j];
for (int i = j + 1; i < solvedCols; ++i) {
weightedJacobian[i][pj] = weightedJacobian[j][permutation[i]];
}
lmDir[j] = diagR[pj];
work[j] = qy[j];
}
// eliminate the diagonal matrix d using a Givens rotation
for (int j = 0; j < solvedCols; ++j) {
// prepare the row of d to be eliminated, locating the
// diagonal element using p from the Q.R. factorization
int pj = permutation[j];
double dpj = diag[pj];
if (dpj != 0) {
Arrays.fill(lmDiag, j + 1, lmDiag.length, 0);
}
lmDiag[j] = dpj;
// the transformations to eliminate the row of d
// modify only a single element of Qty
// beyond the first n, which is initially zero.
double qtbpj = 0;
for (int k = j; k < solvedCols; ++k) {
int pk = permutation[k];
// determine a Givens rotation which eliminates the
// appropriate element in the current row of d
if (lmDiag[k] != 0) {
final double sin;
final double cos;
double rkk = weightedJacobian[k][pk];
if (FastMath.abs(rkk) < FastMath.abs(lmDiag[k])) {
final double cotan = rkk / lmDiag[k];
sin = 1.0 / FastMath.sqrt(1.0 + cotan * cotan);
cos = sin * cotan;
} else {
final double tan = lmDiag[k] / rkk;
cos = 1.0 / FastMath.sqrt(1.0 + tan * tan);
sin = cos * tan;
}
// compute the modified diagonal element of R and
// the modified element of (Qty,0)
weightedJacobian[k][pk] = cos * rkk + sin * lmDiag[k];
final double temp = cos * work[k] + sin * qtbpj;
qtbpj = -sin * work[k] + cos * qtbpj;
work[k] = temp;
// accumulate the tranformation in the row of s
for (int i = k + 1; i < solvedCols; ++i) {
double rik = weightedJacobian[i][pk];
final double temp2 = cos * rik + sin * lmDiag[i];
lmDiag[i] = -sin * rik + cos * lmDiag[i];
weightedJacobian[i][pk] = temp2;
}
}
}
// store the diagonal element of s and restore
// the corresponding diagonal element of R
lmDiag[j] = weightedJacobian[j][permutation[j]];
weightedJacobian[j][permutation[j]] = lmDir[j];
}
// solve the triangular system for z, if the system is
// singular, then obtain a least squares solution
int nSing = solvedCols;
for (int j = 0; j < solvedCols; ++j) {
if ((lmDiag[j] == 0) && (nSing == solvedCols)) {
nSing = j;
}
if (nSing < solvedCols) {
work[j] = 0;
}
}
if (nSing > 0) {
for (int j = nSing - 1; j >= 0; --j) {
int pj = permutation[j];
double sum = 0;
for (int i = j + 1; i < nSing; ++i) {
sum += weightedJacobian[i][pj] * work[i];
}
work[j] = (work[j] - sum) / lmDiag[j];
}
}
// permute the components of z back to components of lmDir
for (int j = 0; j < lmDir.length; ++j) {
lmDir[permutation[j]] = work[j];
}
}
/**
* Decompose a matrix A as A.P = Q.R using Householder transforms.
* <p>As suggested in the P. Lascaux and R. Theodor book
* <i>Analyse numérique matricielle appliquée à
* l'art de l'ingénieur</i> (Masson, 1986), instead of representing
* the Householder transforms with u<sub>k</sub> unit vectors such that:
* <pre>
* H<sub>k</sub> = I - 2u<sub>k</sub>.u<sub>k</sub><sup>t</sup>
* </pre>
* we use <sub>k</sub> non-unit vectors such that:
* <pre>
* H<sub>k</sub> = I - beta<sub>k</sub>v<sub>k</sub>.v<sub>k</sub><sup>t</sup>
* </pre>
* where v<sub>k</sub> = a<sub>k</sub> - alpha<sub>k</sub> e<sub>k</sub>.
* The beta<sub>k</sub> coefficients are provided upon exit as recomputing
* them from the v<sub>k</sub> vectors would be costly.</p>
* <p>This decomposition handles rank deficient cases since the tranformations
* are performed in non-increasing columns norms order thanks to columns
* pivoting. The diagonal elements of the R matrix are therefore also in
* non-increasing absolute values order.</p>
*
* @param jacobian Weighted Jacobian matrix at the current point.
* @exception ConvergenceException if the decomposition cannot be performed
*/
private void qrDecomposition(RealMatrix jacobian) throws ConvergenceException {
// Code in this class assumes that the weighted Jacobian is -(W^(1/2) J),
// hence the multiplication by -1.
weightedJacobian = jacobian.scalarMultiply(-1).getData();
final int nR = weightedJacobian.length;
final int nC = weightedJacobian[0].length;
// initializations
for (int k = 0; k < nC; ++k) {
permutation[k] = k;
double norm2 = 0;
for (int i = 0; i < nR; ++i) {
double akk = weightedJacobian[i][k];
norm2 += akk * akk;
}
jacNorm[k] = FastMath.sqrt(norm2);
}
// transform the matrix column after column
for (int k = 0; k < nC; ++k) {
// select the column with the greatest norm on active components
int nextColumn = -1;
double ak2 = Double.NEGATIVE_INFINITY;
for (int i = k; i < nC; ++i) {
double norm2 = 0;
for (int j = k; j < nR; ++j) {
double aki = weightedJacobian[j][permutation[i]];
norm2 += aki * aki;
}
if (Double.isInfinite(norm2) || Double.isNaN(norm2)) {
throw new ConvergenceException(LocalizedFormats.UNABLE_TO_PERFORM_QR_DECOMPOSITION_ON_JACOBIAN,
nR, nC);
}
if (norm2 > ak2) {
nextColumn = i;
ak2 = norm2;
}
}
if (ak2 <= qrRankingThreshold) {
rank = k;
return;
}
int pk = permutation[nextColumn];
permutation[nextColumn] = permutation[k];
permutation[k] = pk;
// choose alpha such that Hk.u = alpha ek
double akk = weightedJacobian[k][pk];
double alpha = (akk > 0) ? -FastMath.sqrt(ak2) : FastMath.sqrt(ak2);
double betak = 1.0 / (ak2 - akk * alpha);
beta[pk] = betak;
// transform the current column
diagR[pk] = alpha;
weightedJacobian[k][pk] -= alpha;
// transform the remaining columns
for (int dk = nC - 1 - k; dk > 0; --dk) {
double gamma = 0;
for (int j = k; j < nR; ++j) {
gamma += weightedJacobian[j][pk] * weightedJacobian[j][permutation[k + dk]];
}
gamma *= betak;
for (int j = k; j < nR; ++j) {
weightedJacobian[j][permutation[k + dk]] -= gamma * weightedJacobian[j][pk];
}
}
}
rank = solvedCols;
}
/**
* Compute the product Qt.y for some Q.R. decomposition.
*
* @param y vector to multiply (will be overwritten with the result)
*/
private void qTy(double[] y) {
final int nR = weightedJacobian.length;
final int nC = weightedJacobian[0].length;
for (int k = 0; k < nC; ++k) {
int pk = permutation[k];
double gamma = 0;
for (int i = k; i < nR; ++i) {
gamma += weightedJacobian[i][pk] * y[i];
}
gamma *= beta[pk];
for (int i = k; i < nR; ++i) {
y[i] -= gamma * weightedJacobian[i][pk];
}
}
}
} | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/linear/SimplexTableau.java | 64 |
| org/apache/commons/math3/optimization/linear/SimplexTableau.java | 66 |
class SimplexTableau implements Serializable {
/** Column label for negative vars. */
private static final String NEGATIVE_VAR_COLUMN_LABEL = "x-";
/** Default amount of error to accept in floating point comparisons (as ulps). */
private static final int DEFAULT_ULPS = 10;
/** The cut-off threshold to zero-out entries. */
private static final double CUTOFF_THRESHOLD = 1e-12;
/** Serializable version identifier. */
private static final long serialVersionUID = -1369660067587938365L;
/** Linear objective function. */
private final LinearObjectiveFunction f;
/** Linear constraints. */
private final List<LinearConstraint> constraints;
/** Whether to restrict the variables to non-negative values. */
private final boolean restrictToNonNegative;
/** The variables each column represents */
private final List<String> columnLabels = new ArrayList<String>();
/** Simple tableau. */
private transient RealMatrix tableau;
/** Number of decision variables. */
private final int numDecisionVariables;
/** Number of slack variables. */
private final int numSlackVariables;
/** Number of artificial variables. */
private int numArtificialVariables;
/** Amount of error to accept when checking for optimality. */
private final double epsilon;
/** Amount of error to accept in floating point comparisons. */
private final int maxUlps;
/**
* Builds a tableau for a linear problem.
*
* @param f Linear objective function.
* @param constraints Linear constraints.
* @param goalType Optimization goal: either {@link GoalType#MAXIMIZE}
* or {@link GoalType#MINIMIZE}.
* @param restrictToNonNegative Whether to restrict the variables to non-negative values.
* @param epsilon Amount of error to accept when checking for optimality.
*/
SimplexTableau(final LinearObjectiveFunction f,
final Collection<LinearConstraint> constraints,
final GoalType goalType,
final boolean restrictToNonNegative,
final double epsilon) {
this(f, constraints, goalType, restrictToNonNegative, epsilon, DEFAULT_ULPS);
}
/**
* Build a tableau for a linear problem.
* @param f linear objective function
* @param constraints linear constraints
* @param goalType type of optimization goal: either {@link GoalType#MAXIMIZE} or {@link GoalType#MINIMIZE}
* @param restrictToNonNegative whether to restrict the variables to non-negative values
* @param epsilon amount of error to accept when checking for optimality
* @param maxUlps amount of error to accept in floating point comparisons
*/
SimplexTableau(final LinearObjectiveFunction f,
final Collection<LinearConstraint> constraints,
final GoalType goalType,
final boolean restrictToNonNegative,
final double epsilon,
final int maxUlps) {
this.f = f;
this.constraints = normalizeConstraints(constraints);
this.restrictToNonNegative = restrictToNonNegative;
this.epsilon = epsilon;
this.maxUlps = maxUlps;
this.numDecisionVariables = f.getCoefficients().getDimension() +
(restrictToNonNegative ? 0 : 1);
this.numSlackVariables = getConstraintTypeCounts(Relationship.LEQ) +
getConstraintTypeCounts(Relationship.GEQ);
this.numArtificialVariables = getConstraintTypeCounts(Relationship.EQ) +
getConstraintTypeCounts(Relationship.GEQ);
this.tableau = createTableau(goalType == GoalType.MAXIMIZE);
initializeColumnLabels();
}
/**
* Initialize the labels for the columns.
*/
protected void initializeColumnLabels() {
if (getNumObjectiveFunctions() == 2) {
columnLabels.add("W");
}
columnLabels.add("Z");
for (int i = 0; i < getOriginalNumDecisionVariables(); i++) {
columnLabels.add("x" + i);
}
if (!restrictToNonNegative) {
columnLabels.add(NEGATIVE_VAR_COLUMN_LABEL);
}
for (int i = 0; i < getNumSlackVariables(); i++) {
columnLabels.add("s" + i);
}
for (int i = 0; i < getNumArtificialVariables(); i++) {
columnLabels.add("a" + i);
}
columnLabels.add("RHS");
}
/**
* Create the tableau by itself.
* @param maximize if true, goal is to maximize the objective function
* @return created tableau
*/
protected RealMatrix createTableau(final boolean maximize) {
// create a matrix of the correct size
int width = numDecisionVariables + numSlackVariables +
numArtificialVariables + getNumObjectiveFunctions() + 1; // + 1 is for RHS
int height = constraints.size() + getNumObjectiveFunctions();
Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(height, width);
// initialize the objective function rows
if (getNumObjectiveFunctions() == 2) {
matrix.setEntry(0, 0, -1);
}
int zIndex = (getNumObjectiveFunctions() == 1) ? 0 : 1;
matrix.setEntry(zIndex, zIndex, maximize ? 1 : -1);
RealVector objectiveCoefficients =
maximize ? f.getCoefficients().mapMultiply(-1) : f.getCoefficients();
copyArray(objectiveCoefficients.toArray(), matrix.getDataRef()[zIndex]);
matrix.setEntry(zIndex, width - 1,
maximize ? f.getConstantTerm() : -1 * f.getConstantTerm());
if (!restrictToNonNegative) {
matrix.setEntry(zIndex, getSlackVariableOffset() - 1,
getInvertedCoefficientSum(objectiveCoefficients));
}
// initialize the constraint rows
int slackVar = 0;
int artificialVar = 0;
for (int i = 0; i < constraints.size(); i++) {
LinearConstraint constraint = constraints.get(i);
int row = getNumObjectiveFunctions() + i;
// decision variable coefficients
copyArray(constraint.getCoefficients().toArray(), matrix.getDataRef()[row]);
// x-
if (!restrictToNonNegative) {
matrix.setEntry(row, getSlackVariableOffset() - 1,
getInvertedCoefficientSum(constraint.getCoefficients()));
}
// RHS
matrix.setEntry(row, width - 1, constraint.getValue());
// slack variables
if (constraint.getRelationship() == Relationship.LEQ) {
matrix.setEntry(row, getSlackVariableOffset() + slackVar++, 1); // slack
} else if (constraint.getRelationship() == Relationship.GEQ) {
matrix.setEntry(row, getSlackVariableOffset() + slackVar++, -1); // excess
}
// artificial variables
if ((constraint.getRelationship() == Relationship.EQ) ||
(constraint.getRelationship() == Relationship.GEQ)) {
matrix.setEntry(0, getArtificialVariableOffset() + artificialVar, 1);
matrix.setEntry(row, getArtificialVariableOffset() + artificialVar++, 1);
matrix.setRowVector(0, matrix.getRowVector(0).subtract(matrix.getRowVector(row)));
}
}
return matrix;
}
/**
* Get new versions of the constraints which have positive right hand sides.
* @param originalConstraints original (not normalized) constraints
* @return new versions of the constraints
*/
public List<LinearConstraint> normalizeConstraints(Collection<LinearConstraint> originalConstraints) {
List<LinearConstraint> normalized = new ArrayList<LinearConstraint>();
for (LinearConstraint constraint : originalConstraints) {
normalized.add(normalize(constraint));
}
return normalized;
}
/**
* Get a new equation equivalent to this one with a positive right hand side.
* @param constraint reference constraint
* @return new equation
*/
private LinearConstraint normalize(final LinearConstraint constraint) {
if (constraint.getValue() < 0) {
return new LinearConstraint(constraint.getCoefficients().mapMultiply(-1),
constraint.getRelationship().oppositeRelationship(),
-1 * constraint.getValue());
}
return new LinearConstraint(constraint.getCoefficients(),
constraint.getRelationship(), constraint.getValue());
}
/**
* Get the number of objective functions in this tableau.
* @return 2 for Phase 1. 1 for Phase 2.
*/
protected final int getNumObjectiveFunctions() {
return this.numArtificialVariables > 0 ? 2 : 1;
}
/**
* Get a count of constraints corresponding to a specified relationship.
* @param relationship relationship to count
* @return number of constraint with the specified relationship
*/
private int getConstraintTypeCounts(final Relationship relationship) {
int count = 0;
for (final LinearConstraint constraint : constraints) {
if (constraint.getRelationship() == relationship) {
++count;
}
}
return count;
}
/**
* Get the -1 times the sum of all coefficients in the given array.
* @param coefficients coefficients to sum
* @return the -1 times the sum of all coefficients in the given array.
*/
protected static double getInvertedCoefficientSum(final RealVector coefficients) {
double sum = 0;
for (double coefficient : coefficients.toArray()) {
sum -= coefficient;
}
return sum;
}
/**
* Checks whether the given column is basic.
* @param col index of the column to check
* @return the row that the variable is basic in. null if the column is not basic
*/
protected Integer getBasicRow(final int col) {
Integer row = null;
for (int i = 0; i < getHeight(); i++) {
final double entry = getEntry(i, col);
if (Precision.equals(entry, 1d, maxUlps) && (row == null)) {
row = i;
} else if (!Precision.equals(entry, 0d, maxUlps)) {
return null;
}
}
return row;
}
/**
* Removes the phase 1 objective function, positive cost non-artificial variables,
* and the non-basic artificial variables from this tableau.
*/
protected void dropPhase1Objective() {
if (getNumObjectiveFunctions() == 1) {
return;
}
Set<Integer> columnsToDrop = new TreeSet<Integer>();
columnsToDrop.add(0);
// positive cost non-artificial variables
for (int i = getNumObjectiveFunctions(); i < getArtificialVariableOffset(); i++) {
final double entry = tableau.getEntry(0, i);
if (Precision.compareTo(entry, 0d, epsilon) > 0) {
columnsToDrop.add(i);
}
}
// non-basic artificial variables
for (int i = 0; i < getNumArtificialVariables(); i++) {
int col = i + getArtificialVariableOffset();
if (getBasicRow(col) == null) {
columnsToDrop.add(col);
}
}
double[][] matrix = new double[getHeight() - 1][getWidth() - columnsToDrop.size()];
for (int i = 1; i < getHeight(); i++) {
int col = 0;
for (int j = 0; j < getWidth(); j++) {
if (!columnsToDrop.contains(j)) {
matrix[i - 1][col++] = tableau.getEntry(i, j);
}
}
}
// remove the columns in reverse order so the indices are correct
Integer[] drop = columnsToDrop.toArray(new Integer[columnsToDrop.size()]);
for (int i = drop.length - 1; i >= 0; i--) {
columnLabels.remove((int) drop[i]);
}
this.tableau = new Array2DRowRealMatrix(matrix);
this.numArtificialVariables = 0;
}
/**
* @param src the source array
* @param dest the destination array
*/
private void copyArray(final double[] src, final double[] dest) {
System.arraycopy(src, 0, dest, getNumObjectiveFunctions(), src.length);
}
/**
* Returns whether the problem is at an optimal state.
* @return whether the model has been solved
*/
boolean isOptimal() {
for (int i = getNumObjectiveFunctions(); i < getWidth() - 1; i++) {
final double entry = tableau.getEntry(0, i);
if (Precision.compareTo(entry, 0d, epsilon) < 0) {
return false;
}
}
return true;
}
/**
* Get the current solution.
* @return current solution
*/
protected PointValuePair getSolution() {
int negativeVarColumn = columnLabels.indexOf(NEGATIVE_VAR_COLUMN_LABEL);
Integer negativeVarBasicRow = negativeVarColumn > 0 ? getBasicRow(negativeVarColumn) : null;
double mostNegative = negativeVarBasicRow == null ? 0 : getEntry(negativeVarBasicRow, getRhsOffset());
Set<Integer> basicRows = new HashSet<Integer>();
double[] coefficients = new double[getOriginalNumDecisionVariables()];
for (int i = 0; i < coefficients.length; i++) {
int colIndex = columnLabels.indexOf("x" + i);
if (colIndex < 0) {
coefficients[i] = 0;
continue;
}
Integer basicRow = getBasicRow(colIndex);
if (basicRow != null && basicRow == 0) {
// if the basic row is found to be the objective function row
// set the coefficient to 0 -> this case handles unconstrained
// variables that are still part of the objective function
coefficients[i] = 0;
} else if (basicRows.contains(basicRow)) {
// if multiple variables can take a given value
// then we choose the first and set the rest equal to 0
coefficients[i] = 0 - (restrictToNonNegative ? 0 : mostNegative);
} else {
basicRows.add(basicRow);
coefficients[i] =
(basicRow == null ? 0 : getEntry(basicRow, getRhsOffset())) -
(restrictToNonNegative ? 0 : mostNegative);
}
}
return new PointValuePair(coefficients, f.value(coefficients)); | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/nonlinear/scalar/noderiv/CMAESOptimizer.java | 367 |
| org/apache/commons/math3/optimization/direct/CMAESOptimizer.java | 491 |
return super.optimize(optData);
}
/** {@inheritDoc} */
@Override
protected PointValuePair doOptimize() {
checkParameters();
// -------------------- Initialization --------------------------------
isMinimize = getGoalType().equals(GoalType.MINIMIZE);
final FitnessFunction fitfun = new FitnessFunction();
final double[] guess = getStartPoint();
// number of objective variables/problem dimension
dimension = guess.length;
initializeCMA(guess);
iterations = 0;
double bestValue = fitfun.value(guess);
push(fitnessHistory, bestValue);
PointValuePair optimum
= new PointValuePair(getStartPoint(),
isMinimize ? bestValue : -bestValue);
PointValuePair lastResult = null;
// -------------------- Generation Loop --------------------------------
generationLoop:
for (iterations = 1; iterations <= maxIterations; iterations++) {
// Generate and evaluate lambda offspring
final RealMatrix arz = randn1(dimension, lambda);
final RealMatrix arx = zeros(dimension, lambda);
final double[] fitness = new double[lambda];
// generate random offspring
for (int k = 0; k < lambda; k++) {
RealMatrix arxk = null;
for (int i = 0; i < checkFeasableCount + 1; i++) {
if (diagonalOnly <= 0) {
arxk = xmean.add(BD.multiply(arz.getColumnMatrix(k))
.scalarMultiply(sigma)); // m + sig * Normal(0,C)
} else {
arxk = xmean.add(times(diagD,arz.getColumnMatrix(k))
.scalarMultiply(sigma));
}
if (i >= checkFeasableCount ||
fitfun.isFeasible(arxk.getColumn(0))) {
break;
}
// regenerate random arguments for row
arz.setColumn(k, randn(dimension));
}
copyColumn(arxk, 0, arx, k);
try {
fitness[k] = fitfun.value(arx.getColumn(k)); // compute fitness
} catch (TooManyEvaluationsException e) {
break generationLoop;
}
}
// Sort by fitness and compute weighted mean into xmean
final int[] arindex = sortedIndices(fitness);
// Calculate new xmean, this is selection and recombination
final RealMatrix xold = xmean; // for speed up of Eq. (2) and (3)
final RealMatrix bestArx = selectColumns(arx, MathArrays.copyOf(arindex, mu));
xmean = bestArx.multiply(weights);
final RealMatrix bestArz = selectColumns(arz, MathArrays.copyOf(arindex, mu));
final RealMatrix zmean = bestArz.multiply(weights);
final boolean hsig = updateEvolutionPaths(zmean, xold);
if (diagonalOnly <= 0) {
updateCovariance(hsig, bestArx, arz, arindex, xold);
} else {
updateCovarianceDiagonalOnly(hsig, bestArz);
}
// Adapt step size sigma - Eq. (5)
sigma *= Math.exp(Math.min(1, (normps/chiN - 1) * cs / damps));
final double bestFitness = fitness[arindex[0]];
final double worstFitness = fitness[arindex[arindex.length - 1]];
if (bestValue > bestFitness) {
bestValue = bestFitness;
lastResult = optimum;
optimum = new PointValuePair(fitfun.repair(bestArx.getColumn(0)),
isMinimize ? bestFitness : -bestFitness);
if (getConvergenceChecker() != null &&
lastResult != null) {
if (getConvergenceChecker().converged(iterations, optimum, lastResult)) {
break generationLoop;
}
}
}
// handle termination criteria
// Break, if fitness is good enough
if (stopFitness != 0) { // only if stopFitness is defined
if (bestFitness < (isMinimize ? stopFitness : -stopFitness)) {
break generationLoop;
}
}
final double[] sqrtDiagC = sqrt(diagC).getColumn(0);
final double[] pcCol = pc.getColumn(0);
for (int i = 0; i < dimension; i++) {
if (sigma * Math.max(Math.abs(pcCol[i]), sqrtDiagC[i]) > stopTolX) {
break;
}
if (i >= dimension - 1) {
break generationLoop;
}
}
for (int i = 0; i < dimension; i++) {
if (sigma * sqrtDiagC[i] > stopTolUpX) {
break generationLoop;
}
}
final double historyBest = min(fitnessHistory);
final double historyWorst = max(fitnessHistory);
if (iterations > 2 &&
Math.max(historyWorst, worstFitness) -
Math.min(historyBest, bestFitness) < stopTolFun) {
break generationLoop;
}
if (iterations > fitnessHistory.length &&
historyWorst - historyBest < stopTolHistFun) {
break generationLoop;
}
// condition number of the covariance matrix exceeds 1e14
if (max(diagD) / min(diagD) > 1e7) {
break generationLoop;
}
// user defined termination
if (getConvergenceChecker() != null) {
final PointValuePair current
= new PointValuePair(bestArx.getColumn(0),
isMinimize ? bestFitness : -bestFitness);
if (lastResult != null &&
getConvergenceChecker().converged(iterations, current, lastResult)) {
break generationLoop;
}
lastResult = current;
}
// Adjust step size in case of equal function values (flat fitness)
if (bestValue == fitness[arindex[(int)(0.1+lambda/4.)]]) {
sigma = sigma * Math.exp(0.2 + cs / damps);
}
if (iterations > 2 && Math.max(historyWorst, bestFitness) -
Math.min(historyBest, bestFitness) == 0) {
sigma = sigma * Math.exp(0.2 + cs / damps);
}
// store best in history
push(fitnessHistory,bestFitness);
fitfun.setValueRange(worstFitness-bestFitness);
if (generateStatistics) {
statisticsSigmaHistory.add(sigma);
statisticsFitnessHistory.add(bestFitness);
statisticsMeanHistory.add(xmean.transpose());
statisticsDHistory.add(diagD.transpose().scalarMultiply(1E5));
}
}
return optimum;
}
/**
* Scans the list of (required and optional) optimization data that
* characterize the problem.
*
* @param optData Optimization data. The following data will be looked for:
* <ul>
* <li>{@link Sigma}</li>
* <li>{@link PopulationSize}</li>
* </ul>
*/
private void parseOptimizationData(OptimizationData... optData) {
// The existing values (as set by the previous call) are reused if
// not provided in the argument list.
for (OptimizationData data : optData) {
if (data instanceof Sigma) {
inputSigma = ((Sigma) data).getSigma();
continue;
}
if (data instanceof PopulationSize) {
lambda = ((PopulationSize) data).getPopulationSize();
continue;
}
}
}
/**
* Checks dimensions and values of boundaries and inputSigma if defined.
*/
private void checkParameters() {
final double[] init = getStartPoint();
final double[] lB = getLowerBound();
final double[] uB = getUpperBound();
if (inputSigma != null) {
if (inputSigma.length != init.length) {
throw new DimensionMismatchException(inputSigma.length, init.length);
}
for (int i = 0; i < init.length; i++) {
if (inputSigma[i] > uB[i] - lB[i]) { | |
| File | Line |
|---|---|
| org/apache/commons/math3/fitting/GaussianFitter.java | 63 |
| org/apache/commons/math3/optimization/fitting/GaussianFitter.java | 67 |
public GaussianFitter(MultivariateVectorOptimizer optimizer) {
super(optimizer);
}
/**
* Fits a Gaussian function to the observed points.
*
* @param initialGuess First guess values in the following order:
* <ul>
* <li>Norm</li>
* <li>Mean</li>
* <li>Sigma</li>
* </ul>
* @return the parameters of the Gaussian function that best fits the
* observed points (in the same order as above).
* @since 3.0
*/
public double[] fit(double[] initialGuess) {
final Gaussian.Parametric f = new Gaussian.Parametric() {
@Override
public double value(double x, double ... p) {
double v = Double.POSITIVE_INFINITY;
try {
v = super.value(x, p);
} catch (NotStrictlyPositiveException e) { // NOPMD
// Do nothing.
}
return v;
}
@Override
public double[] gradient(double x, double ... p) {
double[] v = { Double.POSITIVE_INFINITY,
Double.POSITIVE_INFINITY,
Double.POSITIVE_INFINITY };
try {
v = super.gradient(x, p);
} catch (NotStrictlyPositiveException e) { // NOPMD
// Do nothing.
}
return v;
}
};
return fit(f, initialGuess);
}
/**
* Fits a Gaussian function to the observed points.
*
* @return the parameters of the Gaussian function that best fits the
* observed points (in the same order as above).
*/
public double[] fit() {
final double[] guess = (new ParameterGuesser(getObservations())).guess();
return fit(guess);
}
/**
* Guesses the parameters {@code norm}, {@code mean}, and {@code sigma}
* of a {@link org.apache.commons.math3.analysis.function.Gaussian.Parametric}
* based on the specified observed points.
*/
public static class ParameterGuesser {
/** Normalization factor. */
private final double norm;
/** Mean. */
private final double mean;
/** Standard deviation. */
private final double sigma;
/**
* Constructs instance with the specified observed points.
*
* @param observations Observed points from which to guess the
* parameters of the Gaussian.
* @throws NullArgumentException if {@code observations} is
* {@code null}.
* @throws NumberIsTooSmallException if there are less than 3
* observations.
*/
public ParameterGuesser(WeightedObservedPoint[] observations) {
if (observations == null) {
throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
}
if (observations.length < 3) {
throw new NumberIsTooSmallException(observations.length, 3, true);
}
final WeightedObservedPoint[] sorted = sortObservations(observations);
final double[] params = basicGuess(sorted);
norm = params[0];
mean = params[1];
sigma = params[2];
}
/**
* Gets an estimation of the parameters.
*
* @return the guessed parameters, in the following order:
* <ul>
* <li>Normalization factor</li>
* <li>Mean</li>
* <li>Standard deviation</li>
* </ul>
*/
public double[] guess() {
return new double[] { norm, mean, sigma };
}
/**
* Sort the observations.
*
* @param unsorted Input observations.
* @return the input observations, sorted.
*/
private WeightedObservedPoint[] sortObservations(WeightedObservedPoint[] unsorted) {
final WeightedObservedPoint[] observations = unsorted.clone();
final Comparator<WeightedObservedPoint> cmp
= new Comparator<WeightedObservedPoint>() {
public int compare(WeightedObservedPoint p1,
WeightedObservedPoint p2) {
if (p1 == null && p2 == null) {
return 0;
}
if (p1 == null) {
return -1;
}
if (p2 == null) {
return 1;
}
if (p1.getX() < p2.getX()) {
return -1;
}
if (p1.getX() > p2.getX()) {
return 1;
}
if (p1.getY() < p2.getY()) {
return -1;
}
if (p1.getY() > p2.getY()) {
return 1;
}
if (p1.getWeight() < p2.getWeight()) {
return -1;
}
if (p1.getWeight() > p2.getWeight()) {
return 1;
}
return 0;
}
};
Arrays.sort(observations, cmp);
return observations;
}
/**
* Guesses the parameters based on the specified observed points.
*
* @param points Observed points, sorted.
* @return the guessed parameters (normalization factor, mean and
* sigma).
*/
private double[] basicGuess(WeightedObservedPoint[] points) {
final int maxYIdx = findMaxY(points);
final double n = points[maxYIdx].getY();
final double m = points[maxYIdx].getX();
double fwhmApprox;
try {
final double halfY = n + ((m - n) / 2);
final double fwhmX1 = interpolateXAtY(points, maxYIdx, -1, halfY);
final double fwhmX2 = interpolateXAtY(points, maxYIdx, 1, halfY);
fwhmApprox = fwhmX2 - fwhmX1;
} catch (OutOfRangeException e) {
// TODO: Exceptions should not be used for flow control.
fwhmApprox = points[points.length - 1].getX() - points[0].getX();
}
final double s = fwhmApprox / (2 * FastMath.sqrt(2 * FastMath.log(2)));
return new double[] { n, m, s };
}
/**
* Finds index of point in specified points with the largest Y.
*
* @param points Points to search.
* @return the index in specified points array.
*/
private int findMaxY(WeightedObservedPoint[] points) {
int maxYIdx = 0;
for (int i = 1; i < points.length; i++) {
if (points[i].getY() > points[maxYIdx].getY()) {
maxYIdx = i;
}
}
return maxYIdx;
}
/**
* Interpolates using the specified points to determine X at the
* specified Y.
*
* @param points Points to use for interpolation.
* @param startIdx Index within points from which to start the search for
* interpolation bounds points.
* @param idxStep Index step for searching interpolation bounds points.
* @param y Y value for which X should be determined.
* @return the value of X for the specified Y.
* @throws ZeroException if {@code idxStep} is 0.
* @throws OutOfRangeException if specified {@code y} is not within the
* range of the specified {@code points}.
*/
private double interpolateXAtY(WeightedObservedPoint[] points,
int startIdx,
int idxStep,
double y)
throws OutOfRangeException {
if (idxStep == 0) {
throw new ZeroException();
}
final WeightedObservedPoint[] twoPoints
= getInterpolationPointsForY(points, startIdx, idxStep, y);
final WeightedObservedPoint p1 = twoPoints[0];
final WeightedObservedPoint p2 = twoPoints[1];
if (p1.getY() == y) {
return p1.getX();
}
if (p2.getY() == y) {
return p2.getX();
}
return p1.getX() + (((y - p1.getY()) * (p2.getX() - p1.getX())) /
(p2.getY() - p1.getY()));
}
/**
* Gets the two bounding interpolation points from the specified points
* suitable for determining X at the specified Y.
*
* @param points Points to use for interpolation.
* @param startIdx Index within points from which to start search for
* interpolation bounds points.
* @param idxStep Index step for search for interpolation bounds points.
* @param y Y value for which X should be determined.
* @return the array containing two points suitable for determining X at
* the specified Y.
* @throws ZeroException if {@code idxStep} is 0.
* @throws OutOfRangeException if specified {@code y} is not within the
* range of the specified {@code points}.
*/
private WeightedObservedPoint[] getInterpolationPointsForY(WeightedObservedPoint[] points,
int startIdx,
int idxStep,
double y)
throws OutOfRangeException {
if (idxStep == 0) {
throw new ZeroException();
}
for (int i = startIdx;
idxStep < 0 ? i + idxStep >= 0 : i + idxStep < points.length;
i += idxStep) {
final WeightedObservedPoint p1 = points[i];
final WeightedObservedPoint p2 = points[i + idxStep];
if (isBetween(y, p1.getY(), p2.getY())) {
if (idxStep < 0) {
return new WeightedObservedPoint[] { p2, p1 };
} else {
return new WeightedObservedPoint[] { p1, p2 };
}
}
}
// Boundaries are replaced by dummy values because the raised
// exception is caught and the message never displayed.
// TODO: Exceptions should not be used for flow control.
throw new OutOfRangeException(y,
Double.NEGATIVE_INFINITY,
Double.POSITIVE_INFINITY);
}
/**
* Determines whether a value is between two other values.
*
* @param value Value to test whether it is between {@code boundary1}
* and {@code boundary2}.
* @param boundary1 One end of the range.
* @param boundary2 Other end of the range.
* @return {@code true} if {@code value} is between {@code boundary1} and
* {@code boundary2} (inclusive), {@code false} otherwise.
*/
private boolean isBetween(double value,
double boundary1,
double boundary2) {
return (value >= boundary1 && value <= boundary2) ||
(value >= boundary2 && value <= boundary1);
}
}
} | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/nonlinear/scalar/noderiv/PowellOptimizer.java | 54 |
| org/apache/commons/math3/optimization/direct/PowellOptimizer.java | 56 |
extends MultivariateOptimizer {
/**
* Minimum relative tolerance.
*/
private static final double MIN_RELATIVE_TOLERANCE = 2 * FastMath.ulp(1d);
/**
* Relative threshold.
*/
private final double relativeThreshold;
/**
* Absolute threshold.
*/
private final double absoluteThreshold;
/**
* Line search.
*/
private final LineSearch line;
/**
* This constructor allows to specify a user-defined convergence checker,
* in addition to the parameters that control the default convergence
* checking procedure.
* <br/>
* The internal line search tolerances are set to the square-root of their
* corresponding value in the multivariate optimizer.
*
* @param rel Relative threshold.
* @param abs Absolute threshold.
* @param checker Convergence checker.
* @throws NotStrictlyPositiveException if {@code abs <= 0}.
* @throws NumberIsTooSmallException if {@code rel < 2 * Math.ulp(1d)}.
*/
public PowellOptimizer(double rel,
double abs,
ConvergenceChecker<PointValuePair> checker) {
this(rel, abs, FastMath.sqrt(rel), FastMath.sqrt(abs), checker);
}
/**
* This constructor allows to specify a user-defined convergence checker,
* in addition to the parameters that control the default convergence
* checking procedure and the line search tolerances.
*
* @param rel Relative threshold for this optimizer.
* @param abs Absolute threshold for this optimizer.
* @param lineRel Relative threshold for the internal line search optimizer.
* @param lineAbs Absolute threshold for the internal line search optimizer.
* @param checker Convergence checker.
* @throws NotStrictlyPositiveException if {@code abs <= 0}.
* @throws NumberIsTooSmallException if {@code rel < 2 * Math.ulp(1d)}.
*/
public PowellOptimizer(double rel,
double abs,
double lineRel,
double lineAbs,
ConvergenceChecker<PointValuePair> checker) {
super(checker);
if (rel < MIN_RELATIVE_TOLERANCE) {
throw new NumberIsTooSmallException(rel, MIN_RELATIVE_TOLERANCE, true);
}
if (abs <= 0) {
throw new NotStrictlyPositiveException(abs);
}
relativeThreshold = rel;
absoluteThreshold = abs;
// Create the line search optimizer.
line = new LineSearch(lineRel,
lineAbs);
}
/**
* The parameters control the default convergence checking procedure.
* <br/>
* The internal line search tolerances are set to the square-root of their
* corresponding value in the multivariate optimizer.
*
* @param rel Relative threshold.
* @param abs Absolute threshold.
* @throws NotStrictlyPositiveException if {@code abs <= 0}.
* @throws NumberIsTooSmallException if {@code rel < 2 * Math.ulp(1d)}.
*/
public PowellOptimizer(double rel,
double abs) {
this(rel, abs, null);
}
/**
* Builds an instance with the default convergence checking procedure.
*
* @param rel Relative threshold.
* @param abs Absolute threshold.
* @param lineRel Relative threshold for the internal line search optimizer.
* @param lineAbs Absolute threshold for the internal line search optimizer.
* @throws NotStrictlyPositiveException if {@code abs <= 0}.
* @throws NumberIsTooSmallException if {@code rel < 2 * Math.ulp(1d)}.
*/
public PowellOptimizer(double rel,
double abs,
double lineRel,
double lineAbs) {
this(rel, abs, lineRel, lineAbs, null);
}
/** {@inheritDoc} */
@Override
protected PointValuePair doOptimize() {
final GoalType goal = getGoalType();
final double[] guess = getStartPoint();
final int n = guess.length;
final double[][] direc = new double[n][n];
for (int i = 0; i < n; i++) {
direc[i][i] = 1;
}
final ConvergenceChecker<PointValuePair> checker
= getConvergenceChecker();
double[] x = guess;
double fVal = computeObjectiveValue(x);
double[] x1 = x.clone();
int iter = 0;
while (true) {
++iter;
double fX = fVal;
double fX2 = 0;
double delta = 0;
int bigInd = 0;
double alphaMin = 0;
for (int i = 0; i < n; i++) {
final double[] d = MathArrays.copyOf(direc[i]);
fX2 = fVal;
final UnivariatePointValuePair optimum = line.search(x, d);
fVal = optimum.getValue();
alphaMin = optimum.getPoint();
final double[][] result = newPointAndDirection(x, d, alphaMin);
x = result[0];
if ((fX2 - fVal) > delta) {
delta = fX2 - fVal;
bigInd = i;
}
}
// Default convergence check.
boolean stop = 2 * (fX - fVal) <=
(relativeThreshold * (FastMath.abs(fX) + FastMath.abs(fVal)) +
absoluteThreshold);
final PointValuePair previous = new PointValuePair(x1, fX);
final PointValuePair current = new PointValuePair(x, fVal);
if (!stop) { // User-defined stopping criteria.
if (checker != null) {
stop = checker.converged(iter, previous, current);
}
}
if (stop) {
if (goal == GoalType.MINIMIZE) {
return (fVal < fX) ? current : previous;
} else {
return (fVal > fX) ? current : previous;
}
}
final double[] d = new double[n];
final double[] x2 = new double[n];
for (int i = 0; i < n; i++) {
d[i] = x[i] - x1[i];
x2[i] = 2 * x[i] - x1[i];
}
x1 = x.clone();
fX2 = computeObjectiveValue(x2);
if (fX > fX2) {
double t = 2 * (fX + fX2 - 2 * fVal);
double temp = fX - fVal - delta;
t *= temp * temp;
temp = fX - fX2;
t -= delta * temp * temp;
if (t < 0.0) {
final UnivariatePointValuePair optimum = line.search(x, d);
fVal = optimum.getValue();
alphaMin = optimum.getPoint();
final double[][] result = newPointAndDirection(x, d, alphaMin);
x = result[0];
final int lastInd = n - 1;
direc[bigInd] = direc[lastInd];
direc[lastInd] = result[1];
}
}
}
}
/**
* Compute a new point (in the original space) and a new direction
* vector, resulting from the line search.
*
* @param p Point used in the line search.
* @param d Direction used in the line search.
* @param optimum Optimum found by the line search.
* @return a 2-element array containing the new point (at index 0) and
* the new direction (at index 1).
*/
private double[][] newPointAndDirection(double[] p,
double[] d,
double optimum) {
final int n = p.length;
final double[] nP = new double[n];
final double[] nD = new double[n];
for (int i = 0; i < n; i++) {
nD[i] = d[i] * optimum;
nP[i] = p[i] + nD[i];
}
final double[][] result = new double[2][];
result[0] = nP;
result[1] = nD;
return result;
}
/**
* Class for finding the minimum of the objective function along a given
* direction.
*/
private class LineSearch extends BrentOptimizer {
/**
* Value that will pass the precondition check for {@link BrentOptimizer}
* but will not pass the convergence check, so that the custom checker
* will always decide when to stop the line search.
*/
private static final double REL_TOL_UNUSED = 1e-15;
/**
* Value that will pass the precondition check for {@link BrentOptimizer}
* but will not pass the convergence check, so that the custom checker
* will always decide when to stop the line search.
*/
private static final double ABS_TOL_UNUSED = Double.MIN_VALUE;
/**
* Automatic bracketing.
*/
private final BracketFinder bracket = new BracketFinder();
/**
* The "BrentOptimizer" default stopping criterion uses the tolerances
* to check the domain (point) values, not the function values.
* We thus create a custom checker to use function values.
*
* @param rel Relative threshold.
* @param abs Absolute threshold.
*/
LineSearch(double rel,
double abs) {
super(REL_TOL_UNUSED,
ABS_TOL_UNUSED,
new SimpleUnivariateValueChecker(rel, abs));
}
/**
* Find the minimum of the function {@code f(p + alpha * d)}.
*
* @param p Starting point.
* @param d Search direction.
* @return the optimum.
* @throws org.apache.commons.math3.exception.TooManyEvaluationsException
* if the number of evaluations is exceeded.
*/
public UnivariatePointValuePair search(final double[] p, final double[] d) {
final int n = p.length;
final UnivariateFunction f = new UnivariateFunction() {
public double value(double alpha) {
final double[] x = new double[n];
for (int i = 0; i < n; i++) {
x[i] = p[i] + alpha * d[i];
}
final double obj = PowellOptimizer.this.computeObjectiveValue(x);
return obj;
}
};
final GoalType goal = PowellOptimizer.this.getGoalType();
bracket.search(f, goal, 0, 1);
// Passing "MAX_VALUE" as a dummy value because it is the enclosing
// class that counts the number of evaluations (and will eventually
// generate the exception).
return optimize(new MaxEval(Integer.MAX_VALUE), | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/nonlinear/scalar/noderiv/NelderMeadSimplex.java | 30 |
| org/apache/commons/math3/optimization/direct/NelderMeadSimplex.java | 33 |
public class NelderMeadSimplex extends AbstractSimplex {
/** Default value for {@link #rho}: {@value}. */
private static final double DEFAULT_RHO = 1;
/** Default value for {@link #khi}: {@value}. */
private static final double DEFAULT_KHI = 2;
/** Default value for {@link #gamma}: {@value}. */
private static final double DEFAULT_GAMMA = 0.5;
/** Default value for {@link #sigma}: {@value}. */
private static final double DEFAULT_SIGMA = 0.5;
/** Reflection coefficient. */
private final double rho;
/** Expansion coefficient. */
private final double khi;
/** Contraction coefficient. */
private final double gamma;
/** Shrinkage coefficient. */
private final double sigma;
/**
* Build a Nelder-Mead simplex with default coefficients.
* The default coefficients are 1.0 for rho, 2.0 for khi and 0.5
* for both gamma and sigma.
*
* @param n Dimension of the simplex.
*/
public NelderMeadSimplex(final int n) {
this(n, 1d);
}
/**
* Build a Nelder-Mead simplex with default coefficients.
* The default coefficients are 1.0 for rho, 2.0 for khi and 0.5
* for both gamma and sigma.
*
* @param n Dimension of the simplex.
* @param sideLength Length of the sides of the default (hypercube)
* simplex. See {@link AbstractSimplex#AbstractSimplex(int,double)}.
*/
public NelderMeadSimplex(final int n, double sideLength) {
this(n, sideLength,
DEFAULT_RHO, DEFAULT_KHI, DEFAULT_GAMMA, DEFAULT_SIGMA);
}
/**
* Build a Nelder-Mead simplex with specified coefficients.
*
* @param n Dimension of the simplex. See
* {@link AbstractSimplex#AbstractSimplex(int,double)}.
* @param sideLength Length of the sides of the default (hypercube)
* simplex. See {@link AbstractSimplex#AbstractSimplex(int,double)}.
* @param rho Reflection coefficient.
* @param khi Expansion coefficient.
* @param gamma Contraction coefficient.
* @param sigma Shrinkage coefficient.
*/
public NelderMeadSimplex(final int n, double sideLength,
final double rho, final double khi,
final double gamma, final double sigma) {
super(n, sideLength);
this.rho = rho;
this.khi = khi;
this.gamma = gamma;
this.sigma = sigma;
}
/**
* Build a Nelder-Mead simplex with specified coefficients.
*
* @param n Dimension of the simplex. See
* {@link AbstractSimplex#AbstractSimplex(int)}.
* @param rho Reflection coefficient.
* @param khi Expansion coefficient.
* @param gamma Contraction coefficient.
* @param sigma Shrinkage coefficient.
*/
public NelderMeadSimplex(final int n,
final double rho, final double khi,
final double gamma, final double sigma) {
this(n, 1d, rho, khi, gamma, sigma);
}
/**
* Build a Nelder-Mead simplex with default coefficients.
* The default coefficients are 1.0 for rho, 2.0 for khi and 0.5
* for both gamma and sigma.
*
* @param steps Steps along the canonical axes representing box edges.
* They may be negative but not zero. See
*/
public NelderMeadSimplex(final double[] steps) {
this(steps, DEFAULT_RHO, DEFAULT_KHI, DEFAULT_GAMMA, DEFAULT_SIGMA);
}
/**
* Build a Nelder-Mead simplex with specified coefficients.
*
* @param steps Steps along the canonical axes representing box edges.
* They may be negative but not zero. See
* {@link AbstractSimplex#AbstractSimplex(double[])}.
* @param rho Reflection coefficient.
* @param khi Expansion coefficient.
* @param gamma Contraction coefficient.
* @param sigma Shrinkage coefficient.
* @throws IllegalArgumentException if one of the steps is zero.
*/
public NelderMeadSimplex(final double[] steps,
final double rho, final double khi,
final double gamma, final double sigma) {
super(steps);
this.rho = rho;
this.khi = khi;
this.gamma = gamma;
this.sigma = sigma;
}
/**
* Build a Nelder-Mead simplex with default coefficients.
* The default coefficients are 1.0 for rho, 2.0 for khi and 0.5
* for both gamma and sigma.
*
* @param referenceSimplex Reference simplex. See
* {@link AbstractSimplex#AbstractSimplex(double[][])}.
*/
public NelderMeadSimplex(final double[][] referenceSimplex) {
this(referenceSimplex, DEFAULT_RHO, DEFAULT_KHI, DEFAULT_GAMMA, DEFAULT_SIGMA);
}
/**
* Build a Nelder-Mead simplex with specified coefficients.
*
* @param referenceSimplex Reference simplex. See
* {@link AbstractSimplex#AbstractSimplex(double[][])}.
* @param rho Reflection coefficient.
* @param khi Expansion coefficient.
* @param gamma Contraction coefficient.
* @param sigma Shrinkage coefficient.
* @throws org.apache.commons.math3.exception.NotStrictlyPositiveException
* if the reference simplex does not contain at least one point.
* @throws org.apache.commons.math3.exception.DimensionMismatchException
* if there is a dimension mismatch in the reference simplex.
*/
public NelderMeadSimplex(final double[][] referenceSimplex,
final double rho, final double khi,
final double gamma, final double sigma) {
super(referenceSimplex);
this.rho = rho;
this.khi = khi;
this.gamma = gamma;
this.sigma = sigma;
}
/** {@inheritDoc} */
@Override
public void iterate(final MultivariateFunction evaluationFunction,
final Comparator<PointValuePair> comparator) {
// The simplex has n + 1 points if dimension is n.
final int n = getDimension();
// Interesting values.
final PointValuePair best = getPoint(0);
final PointValuePair secondBest = getPoint(n - 1);
final PointValuePair worst = getPoint(n);
final double[] xWorst = worst.getPointRef();
// Compute the centroid of the best vertices (dismissing the worst
// point at index n).
final double[] centroid = new double[n];
for (int i = 0; i < n; i++) {
final double[] x = getPoint(i).getPointRef();
for (int j = 0; j < n; j++) {
centroid[j] += x[j];
}
}
final double scaling = 1.0 / n;
for (int j = 0; j < n; j++) {
centroid[j] *= scaling;
}
// compute the reflection point
final double[] xR = new double[n];
for (int j = 0; j < n; j++) {
xR[j] = centroid[j] + rho * (centroid[j] - xWorst[j]);
}
final PointValuePair reflected
= new PointValuePair(xR, evaluationFunction.value(xR), false);
if (comparator.compare(best, reflected) <= 0 &&
comparator.compare(reflected, secondBest) < 0) {
// Accept the reflected point.
replaceWorstPoint(reflected, comparator);
} else if (comparator.compare(reflected, best) < 0) {
// Compute the expansion point.
final double[] xE = new double[n];
for (int j = 0; j < n; j++) {
xE[j] = centroid[j] + khi * (xR[j] - centroid[j]);
}
final PointValuePair expanded
= new PointValuePair(xE, evaluationFunction.value(xE), false);
if (comparator.compare(expanded, reflected) < 0) {
// Accept the expansion point.
replaceWorstPoint(expanded, comparator);
} else {
// Accept the reflected point.
replaceWorstPoint(reflected, comparator);
}
} else {
if (comparator.compare(reflected, worst) < 0) {
// Perform an outside contraction.
final double[] xC = new double[n];
for (int j = 0; j < n; j++) {
xC[j] = centroid[j] + gamma * (xR[j] - centroid[j]);
}
final PointValuePair outContracted
= new PointValuePair(xC, evaluationFunction.value(xC), false);
if (comparator.compare(outContracted, reflected) <= 0) {
// Accept the contraction point.
replaceWorstPoint(outContracted, comparator);
return;
}
} else {
// Perform an inside contraction.
final double[] xC = new double[n];
for (int j = 0; j < n; j++) {
xC[j] = centroid[j] - gamma * (centroid[j] - xWorst[j]);
}
final PointValuePair inContracted
= new PointValuePair(xC, evaluationFunction.value(xC), false);
if (comparator.compare(inContracted, worst) < 0) {
// Accept the contraction point.
replaceWorstPoint(inContracted, comparator);
return;
}
}
// Perform a shrink.
final double[] xSmallest = getPoint(0).getPointRef();
for (int i = 1; i <= n; i++) {
final double[] x = getPoint(i).getPoint();
for (int j = 0; j < n; j++) {
x[j] = xSmallest[j] + sigma * (x[j] - xSmallest[j]);
}
setPoint(i, new PointValuePair(x, Double.NaN, false));
}
evaluate(evaluationFunction, comparator);
}
}
} | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/nonlinear/scalar/noderiv/AbstractSimplex.java | 51 |
| org/apache/commons/math3/optimization/direct/AbstractSimplex.java | 53 |
public abstract class AbstractSimplex implements OptimizationData {
/** Simplex. */
private PointValuePair[] simplex;
/** Start simplex configuration. */
private double[][] startConfiguration;
/** Simplex dimension (must be equal to {@code simplex.length - 1}). */
private final int dimension;
/**
* Build a unit hypercube simplex.
*
* @param n Dimension of the simplex.
*/
protected AbstractSimplex(int n) {
this(n, 1d);
}
/**
* Build a hypercube simplex with the given side length.
*
* @param n Dimension of the simplex.
* @param sideLength Length of the sides of the hypercube.
*/
protected AbstractSimplex(int n,
double sideLength) {
this(createHypercubeSteps(n, sideLength));
}
/**
* The start configuration for simplex is built from a box parallel to
* the canonical axes of the space. The simplex is the subset of vertices
* of a box parallel to the canonical axes. It is built as the path followed
* while traveling from one vertex of the box to the diagonally opposite
* vertex moving only along the box edges. The first vertex of the box will
* be located at the start point of the optimization.
* As an example, in dimension 3 a simplex has 4 vertices. Setting the
* steps to (1, 10, 2) and the start point to (1, 1, 1) would imply the
* start simplex would be: { (1, 1, 1), (2, 1, 1), (2, 11, 1), (2, 11, 3) }.
* The first vertex would be set to the start point at (1, 1, 1) and the
* last vertex would be set to the diagonally opposite vertex at (2, 11, 3).
*
* @param steps Steps along the canonical axes representing box edges. They
* may be negative but not zero.
* @throws NullArgumentException if {@code steps} is {@code null}.
* @throws ZeroException if one of the steps is zero.
*/
protected AbstractSimplex(final double[] steps) {
if (steps == null) {
throw new NullArgumentException();
}
if (steps.length == 0) {
throw new ZeroException();
}
dimension = steps.length;
// Only the relative position of the n final vertices with respect
// to the first one are stored.
startConfiguration = new double[dimension][dimension];
for (int i = 0; i < dimension; i++) {
final double[] vertexI = startConfiguration[i];
for (int j = 0; j < i + 1; j++) {
if (steps[j] == 0) {
throw new ZeroException(LocalizedFormats.EQUAL_VERTICES_IN_SIMPLEX);
}
System.arraycopy(steps, 0, vertexI, 0, j + 1);
}
}
}
/**
* The real initial simplex will be set up by moving the reference
* simplex such that its first point is located at the start point of the
* optimization.
*
* @param referenceSimplex Reference simplex.
* @throws NotStrictlyPositiveException if the reference simplex does not
* contain at least one point.
* @throws DimensionMismatchException if there is a dimension mismatch
* in the reference simplex.
* @throws IllegalArgumentException if one of its vertices is duplicated.
*/
protected AbstractSimplex(final double[][] referenceSimplex) {
if (referenceSimplex.length <= 0) {
throw new NotStrictlyPositiveException(LocalizedFormats.SIMPLEX_NEED_ONE_POINT,
referenceSimplex.length);
}
dimension = referenceSimplex.length - 1;
// Only the relative position of the n final vertices with respect
// to the first one are stored.
startConfiguration = new double[dimension][dimension];
final double[] ref0 = referenceSimplex[0];
// Loop over vertices.
for (int i = 0; i < referenceSimplex.length; i++) {
final double[] refI = referenceSimplex[i];
// Safety checks.
if (refI.length != dimension) {
throw new DimensionMismatchException(refI.length, dimension);
}
for (int j = 0; j < i; j++) {
final double[] refJ = referenceSimplex[j];
boolean allEquals = true;
for (int k = 0; k < dimension; k++) {
if (refI[k] != refJ[k]) {
allEquals = false;
break;
}
}
if (allEquals) {
throw new MathIllegalArgumentException(LocalizedFormats.EQUAL_VERTICES_IN_SIMPLEX,
i, j);
}
}
// Store vertex i position relative to vertex 0 position.
if (i > 0) {
final double[] confI = startConfiguration[i - 1];
for (int k = 0; k < dimension; k++) {
confI[k] = refI[k] - ref0[k];
}
}
}
}
/**
* Get simplex dimension.
*
* @return the dimension of the simplex.
*/
public int getDimension() {
return dimension;
}
/**
* Get simplex size.
* After calling the {@link #build(double[]) build} method, this method will
* will be equivalent to {@code getDimension() + 1}.
*
* @return the size of the simplex.
*/
public int getSize() {
return simplex.length;
}
/**
* Compute the next simplex of the algorithm.
*
* @param evaluationFunction Evaluation function.
* @param comparator Comparator to use to sort simplex vertices from best
* to worst.
* @throws org.apache.commons.math3.exception.TooManyEvaluationsException
* if the algorithm fails to converge.
*/
public abstract void iterate(final MultivariateFunction evaluationFunction,
final Comparator<PointValuePair> comparator);
/**
* Build an initial simplex.
*
* @param startPoint First point of the simplex.
* @throws DimensionMismatchException if the start point does not match
* simplex dimension.
*/
public void build(final double[] startPoint) {
if (dimension != startPoint.length) {
throw new DimensionMismatchException(dimension, startPoint.length);
}
// Set first vertex.
simplex = new PointValuePair[dimension + 1];
simplex[0] = new PointValuePair(startPoint, Double.NaN);
// Set remaining vertices.
for (int i = 0; i < dimension; i++) {
final double[] confI = startConfiguration[i];
final double[] vertexI = new double[dimension];
for (int k = 0; k < dimension; k++) {
vertexI[k] = startPoint[k] + confI[k];
}
simplex[i + 1] = new PointValuePair(vertexI, Double.NaN);
}
}
/**
* Evaluate all the non-evaluated points of the simplex.
*
* @param evaluationFunction Evaluation function.
* @param comparator Comparator to use to sort simplex vertices from best to worst.
* @throws org.apache.commons.math3.exception.TooManyEvaluationsException
* if the maximal number of evaluations is exceeded.
*/
public void evaluate(final MultivariateFunction evaluationFunction,
final Comparator<PointValuePair> comparator) {
// Evaluate the objective function at all non-evaluated simplex points.
for (int i = 0; i < simplex.length; i++) {
final PointValuePair vertex = simplex[i];
final double[] point = vertex.getPointRef();
if (Double.isNaN(vertex.getValue())) {
simplex[i] = new PointValuePair(point, evaluationFunction.value(point), false);
}
}
// Sort the simplex from best to worst.
Arrays.sort(simplex, comparator);
}
/**
* Replace the worst point of the simplex by a new point.
*
* @param pointValuePair Point to insert.
* @param comparator Comparator to use for sorting the simplex vertices
* from best to worst.
*/
protected void replaceWorstPoint(PointValuePair pointValuePair,
final Comparator<PointValuePair> comparator) {
for (int i = 0; i < dimension; i++) {
if (comparator.compare(simplex[i], pointValuePair) > 0) {
PointValuePair tmp = simplex[i];
simplex[i] = pointValuePair;
pointValuePair = tmp;
}
}
simplex[dimension] = pointValuePair;
}
/**
* Get the points of the simplex.
*
* @return all the simplex points.
*/
public PointValuePair[] getPoints() {
final PointValuePair[] copy = new PointValuePair[simplex.length];
System.arraycopy(simplex, 0, copy, 0, simplex.length);
return copy;
}
/**
* Get the simplex point stored at the requested {@code index}.
*
* @param index Location.
* @return the point at location {@code index}.
*/
public PointValuePair getPoint(int index) {
if (index < 0 ||
index >= simplex.length) {
throw new OutOfRangeException(index, 0, simplex.length - 1);
}
return simplex[index];
}
/**
* Store a new point at location {@code index}.
* Note that no deep-copy of {@code point} is performed.
*
* @param index Location.
* @param point New value.
*/
protected void setPoint(int index, PointValuePair point) {
if (index < 0 ||
index >= simplex.length) {
throw new OutOfRangeException(index, 0, simplex.length - 1);
}
simplex[index] = point;
}
/**
* Replace all points.
* Note that no deep-copy of {@code points} is performed.
*
* @param points New Points.
*/
protected void setPoints(PointValuePair[] points) {
if (points.length != simplex.length) {
throw new DimensionMismatchException(points.length, simplex.length);
}
simplex = points;
}
/**
* Create steps for a unit hypercube.
*
* @param n Dimension of the hypercube.
* @param sideLength Length of the sides of the hypercube.
* @return the steps.
*/
private static double[] createHypercubeSteps(int n,
double sideLength) {
final double[] steps = new double[n];
for (int i = 0; i < n; i++) {
steps[i] = sideLength;
}
return steps;
}
} | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/univariate/BrentOptimizer.java | 44 |
| org/apache/commons/math3/optimization/univariate/BrentOptimizer.java | 46 |
public class BrentOptimizer extends UnivariateOptimizer {
/**
* Golden section.
*/
private static final double GOLDEN_SECTION = 0.5 * (3 - FastMath.sqrt(5));
/**
* Minimum relative tolerance.
*/
private static final double MIN_RELATIVE_TOLERANCE = 2 * FastMath.ulp(1d);
/**
* Relative threshold.
*/
private final double relativeThreshold;
/**
* Absolute threshold.
*/
private final double absoluteThreshold;
/**
* The arguments are used implement the original stopping criterion
* of Brent's algorithm.
* {@code abs} and {@code rel} define a tolerance
* {@code tol = rel |x| + abs}. {@code rel} should be no smaller than
* <em>2 macheps</em> and preferably not much less than <em>sqrt(macheps)</em>,
* where <em>macheps</em> is the relative machine precision. {@code abs} must
* be positive.
*
* @param rel Relative threshold.
* @param abs Absolute threshold.
* @param checker Additional, user-defined, convergence checking
* procedure.
* @throws NotStrictlyPositiveException if {@code abs <= 0}.
* @throws NumberIsTooSmallException if {@code rel < 2 * Math.ulp(1d)}.
*/
public BrentOptimizer(double rel,
double abs,
ConvergenceChecker<UnivariatePointValuePair> checker) {
super(checker);
if (rel < MIN_RELATIVE_TOLERANCE) {
throw new NumberIsTooSmallException(rel, MIN_RELATIVE_TOLERANCE, true);
}
if (abs <= 0) {
throw new NotStrictlyPositiveException(abs);
}
relativeThreshold = rel;
absoluteThreshold = abs;
}
/**
* The arguments are used for implementing the original stopping criterion
* of Brent's algorithm.
* {@code abs} and {@code rel} define a tolerance
* {@code tol = rel |x| + abs}. {@code rel} should be no smaller than
* <em>2 macheps</em> and preferably not much less than <em>sqrt(macheps)</em>,
* where <em>macheps</em> is the relative machine precision. {@code abs} must
* be positive.
*
* @param rel Relative threshold.
* @param abs Absolute threshold.
* @throws NotStrictlyPositiveException if {@code abs <= 0}.
* @throws NumberIsTooSmallException if {@code rel < 2 * Math.ulp(1d)}.
*/
public BrentOptimizer(double rel,
double abs) {
this(rel, abs, null);
}
/** {@inheritDoc} */
@Override
protected UnivariatePointValuePair doOptimize() {
final boolean isMinim = getGoalType() == GoalType.MINIMIZE;
final double lo = getMin();
final double mid = getStartValue();
final double hi = getMax();
// Optional additional convergence criteria.
final ConvergenceChecker<UnivariatePointValuePair> checker
= getConvergenceChecker();
double a;
double b;
if (lo < hi) {
a = lo;
b = hi;
} else {
a = hi;
b = lo;
}
double x = mid;
double v = x;
double w = x;
double d = 0;
double e = 0;
double fx = computeObjectiveValue(x);
if (!isMinim) {
fx = -fx;
}
double fv = fx;
double fw = fx;
UnivariatePointValuePair previous = null;
UnivariatePointValuePair current
= new UnivariatePointValuePair(x, isMinim ? fx : -fx);
// Best point encountered so far (which is the initial guess).
UnivariatePointValuePair best = current;
int iter = 0;
while (true) {
final double m = 0.5 * (a + b);
final double tol1 = relativeThreshold * FastMath.abs(x) + absoluteThreshold;
final double tol2 = 2 * tol1;
// Default stopping criterion.
final boolean stop = FastMath.abs(x - m) <= tol2 - 0.5 * (b - a);
if (!stop) {
double p = 0;
double q = 0;
double r = 0;
double u = 0;
if (FastMath.abs(e) > tol1) { // Fit parabola.
r = (x - w) * (fx - fv);
q = (x - v) * (fx - fw);
p = (x - v) * q - (x - w) * r;
q = 2 * (q - r);
if (q > 0) {
p = -p;
} else {
q = -q;
}
r = e;
e = d;
if (p > q * (a - x) &&
p < q * (b - x) &&
FastMath.abs(p) < FastMath.abs(0.5 * q * r)) {
// Parabolic interpolation step.
d = p / q;
u = x + d;
// f must not be evaluated too close to a or b.
if (u - a < tol2 || b - u < tol2) {
if (x <= m) {
d = tol1;
} else {
d = -tol1;
}
}
} else {
// Golden section step.
if (x < m) {
e = b - x;
} else {
e = a - x;
}
d = GOLDEN_SECTION * e;
}
} else {
// Golden section step.
if (x < m) {
e = b - x;
} else {
e = a - x;
}
d = GOLDEN_SECTION * e;
}
// Update by at least "tol1".
if (FastMath.abs(d) < tol1) {
if (d >= 0) {
u = x + tol1;
} else {
u = x - tol1;
}
} else {
u = x + d;
}
double fu = computeObjectiveValue(u);
if (!isMinim) {
fu = -fu;
}
// User-defined convergence checker.
previous = current;
current = new UnivariatePointValuePair(u, isMinim ? fu : -fu);
best = best(best,
best(previous,
current,
isMinim),
isMinim);
if (checker != null) {
if (checker.converged(iter, previous, current)) {
return best;
}
}
// Update a, b, v, w and x.
if (fu <= fx) {
if (u < x) {
b = x;
} else {
a = x;
}
v = w;
fv = fw;
w = x;
fw = fx;
x = u;
fx = fu;
} else {
if (u < x) {
a = u;
} else {
b = u;
}
if (fu <= fw ||
Precision.equals(w, x)) {
v = w;
fv = fw;
w = u;
fw = fu;
} else if (fu <= fv ||
Precision.equals(v, x) ||
Precision.equals(v, w)) {
v = u;
fv = fu;
}
}
} else { // Default termination (Brent's criterion).
return best(best,
best(previous,
current,
isMinim),
isMinim);
}
++iter;
}
}
/**
* Selects the best of two points.
*
* @param a Point and value.
* @param b Point and value.
* @param isMinim {@code true} if the selected point must be the one with
* the lowest value.
* @return the best point, or {@code null} if {@code a} and {@code b} are
* both {@code null}. When {@code a} and {@code b} have the same function
* value, {@code a} is returned.
*/
private UnivariatePointValuePair best(UnivariatePointValuePair a,
UnivariatePointValuePair b,
boolean isMinim) {
if (a == null) {
return b;
}
if (b == null) {
return a;
}
if (isMinim) {
return a.getValue() <= b.getValue() ? a : b;
} else {
return a.getValue() >= b.getValue() ? a : b;
}
}
} | |
| File | Line |
|---|---|
| org/apache/commons/math3/fitting/HarmonicFitter.java | 44 |
| org/apache/commons/math3/optimization/fitting/HarmonicFitter.java | 47 |
public HarmonicFitter(final MultivariateVectorOptimizer optimizer) {
super(optimizer);
}
/**
* Fit an harmonic function to the observed points.
*
* @param initialGuess First guess values in the following order:
* <ul>
* <li>Amplitude</li>
* <li>Angular frequency</li>
* <li>Phase</li>
* </ul>
* @return the parameters of the harmonic function that best fits the
* observed points (in the same order as above).
*/
public double[] fit(double[] initialGuess) {
return fit(new HarmonicOscillator.Parametric(), initialGuess);
}
/**
* Fit an harmonic function to the observed points.
* An initial guess will be automatically computed.
*
* @return the parameters of the harmonic function that best fits the
* observed points (see the other {@link #fit(double[]) fit} method.
* @throws NumberIsTooSmallException if the sample is too short for the
* the first guess to be computed.
* @throws ZeroException if the first guess cannot be computed because
* the abscissa range is zero.
*/
public double[] fit() {
return fit((new ParameterGuesser(getObservations())).guess());
}
/**
* This class guesses harmonic coefficients from a sample.
* <p>The algorithm used to guess the coefficients is as follows:</p>
*
* <p>We know f (t) at some sampling points t<sub>i</sub> and want to find a,
* ω and φ such that f (t) = a cos (ω t + φ).
* </p>
*
* <p>From the analytical expression, we can compute two primitives :
* <pre>
* If2 (t) = ∫ f<sup>2</sup> = a<sup>2</sup> × [t + S (t)] / 2
* If'2 (t) = ∫ f'<sup>2</sup> = a<sup>2</sup> ω<sup>2</sup> × [t - S (t)] / 2
* where S (t) = sin (2 (ω t + φ)) / (2 ω)
* </pre>
* </p>
*
* <p>We can remove S between these expressions :
* <pre>
* If'2 (t) = a<sup>2</sup> ω<sup>2</sup> t - ω<sup>2</sup> If2 (t)
* </pre>
* </p>
*
* <p>The preceding expression shows that If'2 (t) is a linear
* combination of both t and If2 (t): If'2 (t) = A × t + B × If2 (t)
* </p>
*
* <p>From the primitive, we can deduce the same form for definite
* integrals between t<sub>1</sub> and t<sub>i</sub> for each t<sub>i</sub> :
* <pre>
* If2 (t<sub>i</sub>) - If2 (t<sub>1</sub>) = A × (t<sub>i</sub> - t<sub>1</sub>) + B × (If2 (t<sub>i</sub>) - If2 (t<sub>1</sub>))
* </pre>
* </p>
*
* <p>We can find the coefficients A and B that best fit the sample
* to this linear expression by computing the definite integrals for
* each sample points.
* </p>
*
* <p>For a bilinear expression z (x<sub>i</sub>, y<sub>i</sub>) = A × x<sub>i</sub> + B × y<sub>i</sub>, the
* coefficients A and B that minimize a least square criterion
* ∑ (z<sub>i</sub> - z (x<sub>i</sub>, y<sub>i</sub>))<sup>2</sup> are given by these expressions:</p>
* <pre>
*
* ∑y<sub>i</sub>y<sub>i</sub> ∑x<sub>i</sub>z<sub>i</sub> - ∑x<sub>i</sub>y<sub>i</sub> ∑y<sub>i</sub>z<sub>i</sub>
* A = ------------------------
* ∑x<sub>i</sub>x<sub>i</sub> ∑y<sub>i</sub>y<sub>i</sub> - ∑x<sub>i</sub>y<sub>i</sub> ∑x<sub>i</sub>y<sub>i</sub>
*
* ∑x<sub>i</sub>x<sub>i</sub> ∑y<sub>i</sub>z<sub>i</sub> - ∑x<sub>i</sub>y<sub>i</sub> ∑x<sub>i</sub>z<sub>i</sub>
* B = ------------------------
* ∑x<sub>i</sub>x<sub>i</sub> ∑y<sub>i</sub>y<sub>i</sub> - ∑x<sub>i</sub>y<sub>i</sub> ∑x<sub>i</sub>y<sub>i</sub>
* </pre>
* </p>
*
*
* <p>In fact, we can assume both a and ω are positive and
* compute them directly, knowing that A = a<sup>2</sup> ω<sup>2</sup> and that
* B = - ω<sup>2</sup>. The complete algorithm is therefore:</p>
* <pre>
*
* for each t<sub>i</sub> from t<sub>1</sub> to t<sub>n-1</sub>, compute:
* f (t<sub>i</sub>)
* f' (t<sub>i</sub>) = (f (t<sub>i+1</sub>) - f(t<sub>i-1</sub>)) / (t<sub>i+1</sub> - t<sub>i-1</sub>)
* x<sub>i</sub> = t<sub>i</sub> - t<sub>1</sub>
* y<sub>i</sub> = ∫ f<sup>2</sup> from t<sub>1</sub> to t<sub>i</sub>
* z<sub>i</sub> = ∫ f'<sup>2</sup> from t<sub>1</sub> to t<sub>i</sub>
* update the sums ∑x<sub>i</sub>x<sub>i</sub>, ∑y<sub>i</sub>y<sub>i</sub>, ∑x<sub>i</sub>y<sub>i</sub>, ∑x<sub>i</sub>z<sub>i</sub> and ∑y<sub>i</sub>z<sub>i</sub>
* end for
*
* |--------------------------
* \ | ∑y<sub>i</sub>y<sub>i</sub> ∑x<sub>i</sub>z<sub>i</sub> - ∑x<sub>i</sub>y<sub>i</sub> ∑y<sub>i</sub>z<sub>i</sub>
* a = \ | ------------------------
* \| ∑x<sub>i</sub>y<sub>i</sub> ∑x<sub>i</sub>z<sub>i</sub> - ∑x<sub>i</sub>x<sub>i</sub> ∑y<sub>i</sub>z<sub>i</sub>
*
*
* |--------------------------
* \ | ∑x<sub>i</sub>y<sub>i</sub> ∑x<sub>i</sub>z<sub>i</sub> - ∑x<sub>i</sub>x<sub>i</sub> ∑y<sub>i</sub>z<sub>i</sub>
* ω = \ | ------------------------
* \| ∑x<sub>i</sub>x<sub>i</sub> ∑y<sub>i</sub>y<sub>i</sub> - ∑x<sub>i</sub>y<sub>i</sub> ∑x<sub>i</sub>y<sub>i</sub>
*
* </pre>
* </p>
*
* <p>Once we know ω, we can compute:
* <pre>
* fc = ω f (t) cos (ω t) - f' (t) sin (ω t)
* fs = ω f (t) sin (ω t) + f' (t) cos (ω t)
* </pre>
* </p>
*
* <p>It appears that <code>fc = a ω cos (φ)</code> and
* <code>fs = -a ω sin (φ)</code>, so we can use these
* expressions to compute φ. The best estimate over the sample is
* given by averaging these expressions.
* </p>
*
* <p>Since integrals and means are involved in the preceding
* estimations, these operations run in O(n) time, where n is the
* number of measurements.</p>
*/
public static class ParameterGuesser {
/** Amplitude. */
private final double a;
/** Angular frequency. */
private final double omega;
/** Phase. */
private final double phi;
/**
* Simple constructor.
*
* @param observations Sampled observations.
* @throws NumberIsTooSmallException if the sample is too short.
* @throws ZeroException if the abscissa range is zero.
* @throws MathIllegalStateException when the guessing procedure cannot
* produce sensible results.
*/
public ParameterGuesser(WeightedObservedPoint[] observations) {
if (observations.length < 4) {
throw new NumberIsTooSmallException(LocalizedFormats.INSUFFICIENT_OBSERVED_POINTS_IN_SAMPLE,
observations.length, 4, true);
}
final WeightedObservedPoint[] sorted = sortObservations(observations);
final double aOmega[] = guessAOmega(sorted);
a = aOmega[0];
omega = aOmega[1];
phi = guessPhi(sorted);
}
/**
* Gets an estimation of the parameters.
*
* @return the guessed parameters, in the following order:
* <ul>
* <li>Amplitude</li>
* <li>Angular frequency</li>
* <li>Phase</li>
* </ul>
*/
public double[] guess() {
return new double[] { a, omega, phi };
}
/**
* Sort the observations with respect to the abscissa.
*
* @param unsorted Input observations.
* @return the input observations, sorted.
*/
private WeightedObservedPoint[] sortObservations(WeightedObservedPoint[] unsorted) {
final WeightedObservedPoint[] observations = unsorted.clone();
// Since the samples are almost always already sorted, this
// method is implemented as an insertion sort that reorders the
// elements in place. Insertion sort is very efficient in this case.
WeightedObservedPoint curr = observations[0];
for (int j = 1; j < observations.length; ++j) {
WeightedObservedPoint prec = curr;
curr = observations[j];
if (curr.getX() < prec.getX()) {
// the current element should be inserted closer to the beginning
int i = j - 1;
WeightedObservedPoint mI = observations[i];
while ((i >= 0) && (curr.getX() < mI.getX())) {
observations[i + 1] = mI;
if (i-- != 0) {
mI = observations[i];
}
}
observations[i + 1] = curr;
curr = observations[j];
}
}
return observations;
}
/**
* Estimate a first guess of the amplitude and angular frequency.
* This method assumes that the {@link #sortObservations()} method
* has been called previously.
*
* @param observations Observations, sorted w.r.t. abscissa.
* @throws ZeroException if the abscissa range is zero.
* @throws MathIllegalStateException when the guessing procedure cannot
* produce sensible results.
* @return the guessed amplitude (at index 0) and circular frequency
* (at index 1).
*/
private double[] guessAOmega(WeightedObservedPoint[] observations) {
final double[] aOmega = new double[2];
// initialize the sums for the linear model between the two integrals
double sx2 = 0;
double sy2 = 0;
double sxy = 0;
double sxz = 0;
double syz = 0;
double currentX = observations[0].getX();
double currentY = observations[0].getY();
double f2Integral = 0;
double fPrime2Integral = 0;
final double startX = currentX;
for (int i = 1; i < observations.length; ++i) {
// one step forward
final double previousX = currentX;
final double previousY = currentY;
currentX = observations[i].getX();
currentY = observations[i].getY();
// update the integrals of f<sup>2</sup> and f'<sup>2</sup>
// considering a linear model for f (and therefore constant f')
final double dx = currentX - previousX;
final double dy = currentY - previousY;
final double f2StepIntegral =
dx * (previousY * previousY + previousY * currentY + currentY * currentY) / 3;
final double fPrime2StepIntegral = dy * dy / dx;
final double x = currentX - startX;
f2Integral += f2StepIntegral;
fPrime2Integral += fPrime2StepIntegral;
sx2 += x * x;
sy2 += f2Integral * f2Integral;
sxy += x * f2Integral;
sxz += x * fPrime2Integral;
syz += f2Integral * fPrime2Integral;
}
// compute the amplitude and pulsation coefficients
double c1 = sy2 * sxz - sxy * syz;
double c2 = sxy * sxz - sx2 * syz;
double c3 = sx2 * sy2 - sxy * sxy;
if ((c1 / c2 < 0) || (c2 / c3 < 0)) {
final int last = observations.length - 1;
// Range of the observations, assuming that the
// observations are sorted.
final double xRange = observations[last].getX() - observations[0].getX();
if (xRange == 0) {
throw new ZeroException();
}
aOmega[1] = 2 * Math.PI / xRange;
double yMin = Double.POSITIVE_INFINITY;
double yMax = Double.NEGATIVE_INFINITY;
for (int i = 1; i < observations.length; ++i) {
final double y = observations[i].getY();
if (y < yMin) {
yMin = y;
}
if (y > yMax) {
yMax = y;
}
}
aOmega[0] = 0.5 * (yMax - yMin);
} else {
if (c2 == 0) {
// In some ill-conditioned cases (cf. MATH-844), the guesser
// procedure cannot produce sensible results.
throw new MathIllegalStateException(LocalizedFormats.ZERO_DENOMINATOR);
}
aOmega[0] = FastMath.sqrt(c1 / c2);
aOmega[1] = FastMath.sqrt(c2 / c3);
}
return aOmega;
}
/**
* Estimate a first guess of the phase.
*
* @param observations Observations, sorted w.r.t. abscissa.
* @return the guessed phase.
*/
private double guessPhi(WeightedObservedPoint[] observations) {
// initialize the means
double fcMean = 0;
double fsMean = 0;
double currentX = observations[0].getX();
double currentY = observations[0].getY();
for (int i = 1; i < observations.length; ++i) {
// one step forward
final double previousX = currentX;
final double previousY = currentY;
currentX = observations[i].getX();
currentY = observations[i].getY();
final double currentYPrime = (currentY - previousY) / (currentX - previousX);
double omegaX = omega * currentX;
double cosine = FastMath.cos(omegaX);
double sine = FastMath.sin(omegaX);
fcMean += omega * currentY * cosine - currentYPrime * sine;
fsMean += omega * currentY * sine + currentYPrime * cosine;
}
return FastMath.atan2(-fsMean, fcMean);
}
}
} | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/nonlinear/vector/jacobian/LevenbergMarquardtOptimizer.java | 107 |
| org/apache/commons/math3/optimization/general/LevenbergMarquardtOptimizer.java | 110 |
public class LevenbergMarquardtOptimizer
extends AbstractLeastSquaresOptimizer {
/** Number of solved point. */
private int solvedCols;
/** Diagonal elements of the R matrix in the Q.R. decomposition. */
private double[] diagR;
/** Norms of the columns of the jacobian matrix. */
private double[] jacNorm;
/** Coefficients of the Householder transforms vectors. */
private double[] beta;
/** Columns permutation array. */
private int[] permutation;
/** Rank of the jacobian matrix. */
private int rank;
/** Levenberg-Marquardt parameter. */
private double lmPar;
/** Parameters evolution direction associated with lmPar. */
private double[] lmDir;
/** Positive input variable used in determining the initial step bound. */
private final double initialStepBoundFactor;
/** Desired relative error in the sum of squares. */
private final double costRelativeTolerance;
/** Desired relative error in the approximate solution parameters. */
private final double parRelativeTolerance;
/** Desired max cosine on the orthogonality between the function vector
* and the columns of the jacobian. */
private final double orthoTolerance;
/** Threshold for QR ranking. */
private final double qrRankingThreshold;
/** Weighted residuals. */
private double[] weightedResidual;
/** Weighted Jacobian. */
private double[][] weightedJacobian;
/**
* Build an optimizer for least squares problems with default values
* for all the tuning parameters (see the {@link
* #LevenbergMarquardtOptimizer(double,double,double,double,double)
* other contructor}.
* The default values for the algorithm settings are:
* <ul>
* <li>Initial step bound factor: 100</li>
* <li>Cost relative tolerance: 1e-10</li>
* <li>Parameters relative tolerance: 1e-10</li>
* <li>Orthogonality tolerance: 1e-10</li>
* <li>QR ranking threshold: {@link Precision#SAFE_MIN}</li>
* </ul>
*/
public LevenbergMarquardtOptimizer() {
this(100, 1e-10, 1e-10, 1e-10, Precision.SAFE_MIN);
}
/**
* Constructor that allows the specification of a custom convergence
* checker.
* Note that all the usual convergence checks will be <em>disabled</em>.
* The default values for the algorithm settings are:
* <ul>
* <li>Initial step bound factor: 100</li>
* <li>Cost relative tolerance: 1e-10</li>
* <li>Parameters relative tolerance: 1e-10</li>
* <li>Orthogonality tolerance: 1e-10</li>
* <li>QR ranking threshold: {@link Precision#SAFE_MIN}</li>
* </ul>
*
* @param checker Convergence checker.
*/
public LevenbergMarquardtOptimizer(ConvergenceChecker<PointVectorValuePair> checker) {
this(100, checker, 1e-10, 1e-10, 1e-10, Precision.SAFE_MIN);
}
/**
* Constructor that allows the specification of a custom convergence
* checker, in addition to the standard ones.
*
* @param initialStepBoundFactor Positive input variable used in
* determining the initial step bound. This bound is set to the
* product of initialStepBoundFactor and the euclidean norm of
* {@code diag * x} if non-zero, or else to {@code initialStepBoundFactor}
* itself. In most cases factor should lie in the interval
* {@code (0.1, 100.0)}. {@code 100} is a generally recommended value.
* @param checker Convergence checker.
* @param costRelativeTolerance Desired relative error in the sum of
* squares.
* @param parRelativeTolerance Desired relative error in the approximate
* solution parameters.
* @param orthoTolerance Desired max cosine on the orthogonality between
* the function vector and the columns of the Jacobian.
* @param threshold Desired threshold for QR ranking. If the squared norm
* of a column vector is smaller or equal to this threshold during QR
* decomposition, it is considered to be a zero vector and hence the rank
* of the matrix is reduced.
*/
public LevenbergMarquardtOptimizer(double initialStepBoundFactor,
ConvergenceChecker<PointVectorValuePair> checker,
double costRelativeTolerance,
double parRelativeTolerance,
double orthoTolerance,
double threshold) {
super(checker);
this.initialStepBoundFactor = initialStepBoundFactor;
this.costRelativeTolerance = costRelativeTolerance;
this.parRelativeTolerance = parRelativeTolerance;
this.orthoTolerance = orthoTolerance;
this.qrRankingThreshold = threshold;
}
/**
* Build an optimizer for least squares problems with default values
* for some of the tuning parameters (see the {@link
* #LevenbergMarquardtOptimizer(double,double,double,double,double)
* other contructor}.
* The default values for the algorithm settings are:
* <ul>
* <li>Initial step bound factor}: 100</li>
* <li>QR ranking threshold}: {@link Precision#SAFE_MIN}</li>
* </ul>
*
* @param costRelativeTolerance Desired relative error in the sum of
* squares.
* @param parRelativeTolerance Desired relative error in the approximate
* solution parameters.
* @param orthoTolerance Desired max cosine on the orthogonality between
* the function vector and the columns of the Jacobian.
*/
public LevenbergMarquardtOptimizer(double costRelativeTolerance,
double parRelativeTolerance,
double orthoTolerance) {
this(100,
costRelativeTolerance, parRelativeTolerance, orthoTolerance,
Precision.SAFE_MIN);
}
/**
* The arguments control the behaviour of the default convergence checking
* procedure.
* Additional criteria can defined through the setting of a {@link
* ConvergenceChecker}.
*
* @param initialStepBoundFactor Positive input variable used in
* determining the initial step bound. This bound is set to the
* product of initialStepBoundFactor and the euclidean norm of
* {@code diag * x} if non-zero, or else to {@code initialStepBoundFactor}
* itself. In most cases factor should lie in the interval
* {@code (0.1, 100.0)}. {@code 100} is a generally recommended value.
* @param costRelativeTolerance Desired relative error in the sum of
* squares.
* @param parRelativeTolerance Desired relative error in the approximate
* solution parameters.
* @param orthoTolerance Desired max cosine on the orthogonality between
* the function vector and the columns of the Jacobian.
* @param threshold Desired threshold for QR ranking. If the squared norm
* of a column vector is smaller or equal to this threshold during QR
* decomposition, it is considered to be a zero vector and hence the rank
* of the matrix is reduced.
*/
public LevenbergMarquardtOptimizer(double initialStepBoundFactor,
double costRelativeTolerance,
double parRelativeTolerance,
double orthoTolerance,
double threshold) {
super(null); // No custom convergence criterion.
this.initialStepBoundFactor = initialStepBoundFactor;
this.costRelativeTolerance = costRelativeTolerance;
this.parRelativeTolerance = parRelativeTolerance;
this.orthoTolerance = orthoTolerance;
this.qrRankingThreshold = threshold;
}
/** {@inheritDoc} */
@Override
protected PointVectorValuePair doOptimize() {
final int nR = getTarget().length; // Number of observed data.
final double[] currentPoint = getStartPoint();
final int nC = currentPoint.length; // Number of parameters.
// arrays shared with the other private methods
solvedCols = FastMath.min(nR, nC);
diagR = new double[nC];
jacNorm = new double[nC];
beta = new double[nC];
permutation = new int[nC];
lmDir = new double[nC];
// local point
double delta = 0;
double xNorm = 0;
double[] diag = new double[nC];
double[] oldX = new double[nC];
double[] oldRes = new double[nR];
double[] oldObj = new double[nR];
double[] qtf = new double[nR];
double[] work1 = new double[nC];
double[] work2 = new double[nC];
double[] work3 = new double[nC];
final RealMatrix weightMatrixSqrt = getWeightSquareRoot();
// Evaluate the function at the starting point and calculate its norm.
double[] currentObjective = computeObjectiveValue(currentPoint);
double[] currentResiduals = computeResiduals(currentObjective);
PointVectorValuePair current = new PointVectorValuePair(currentPoint, currentObjective);
double currentCost = computeCost(currentResiduals);
// Outer loop.
lmPar = 0;
boolean firstIteration = true;
int iter = 0;
final ConvergenceChecker<PointVectorValuePair> checker = getConvergenceChecker();
while (true) {
++iter;
final PointVectorValuePair previous = current;
// QR decomposition of the jacobian matrix
qrDecomposition(computeWeightedJacobian(currentPoint));
weightedResidual = weightMatrixSqrt.operate(currentResiduals);
for (int i = 0; i < nR; i++) {
qtf[i] = weightedResidual[i];
}
// compute Qt.res
qTy(qtf);
// now we don't need Q anymore,
// so let jacobian contain the R matrix with its diagonal elements
for (int k = 0; k < solvedCols; ++k) {
int pk = permutation[k];
weightedJacobian[k][pk] = diagR[pk];
}
if (firstIteration) {
// scale the point according to the norms of the columns
// of the initial jacobian
xNorm = 0;
for (int k = 0; k < nC; ++k) {
double dk = jacNorm[k];
if (dk == 0) {
dk = 1.0;
}
double xk = dk * currentPoint[k];
xNorm += xk * xk;
diag[k] = dk;
}
xNorm = FastMath.sqrt(xNorm);
// initialize the step bound delta
delta = (xNorm == 0) ? initialStepBoundFactor : (initialStepBoundFactor * xNorm);
}
// check orthogonality between function vector and jacobian columns
double maxCosine = 0;
if (currentCost != 0) {
for (int j = 0; j < solvedCols; ++j) {
int pj = permutation[j];
double s = jacNorm[pj];
if (s != 0) {
double sum = 0;
for (int i = 0; i <= j; ++i) {
sum += weightedJacobian[i][pj] * qtf[i];
}
maxCosine = FastMath.max(maxCosine, FastMath.abs(sum) / (s * currentCost));
}
}
}
if (maxCosine <= orthoTolerance) {
// Convergence has been reached.
setCost(currentCost); | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/univariate/BracketFinder.java | 34 |
| org/apache/commons/math3/optimization/univariate/BracketFinder.java | 36 |
public class BracketFinder {
/** Tolerance to avoid division by zero. */
private static final double EPS_MIN = 1e-21;
/**
* Golden section.
*/
private static final double GOLD = 1.618034;
/**
* Factor for expanding the interval.
*/
private final double growLimit;
/**
* Counter for function evaluations.
*/
private final Incrementor evaluations = new Incrementor();
/**
* Lower bound of the bracket.
*/
private double lo;
/**
* Higher bound of the bracket.
*/
private double hi;
/**
* Point inside the bracket.
*/
private double mid;
/**
* Function value at {@link #lo}.
*/
private double fLo;
/**
* Function value at {@link #hi}.
*/
private double fHi;
/**
* Function value at {@link #mid}.
*/
private double fMid;
/**
* Constructor with default values {@code 100, 50} (see the
* {@link #BracketFinder(double,int) other constructor}).
*/
public BracketFinder() {
this(100, 50);
}
/**
* Create a bracketing interval finder.
*
* @param growLimit Expanding factor.
* @param maxEvaluations Maximum number of evaluations allowed for finding
* a bracketing interval.
*/
public BracketFinder(double growLimit,
int maxEvaluations) {
if (growLimit <= 0) {
throw new NotStrictlyPositiveException(growLimit);
}
if (maxEvaluations <= 0) {
throw new NotStrictlyPositiveException(maxEvaluations);
}
this.growLimit = growLimit;
evaluations.setMaximalCount(maxEvaluations);
}
/**
* Search new points that bracket a local optimum of the function.
*
* @param func Function whose optimum should be bracketed.
* @param goal {@link GoalType Goal type}.
* @param xA Initial point.
* @param xB Initial point.
* @throws TooManyEvaluationsException if the maximum number of evaluations
* is exceeded.
*/
public void search(UnivariateFunction func, GoalType goal, double xA, double xB) {
evaluations.resetCount();
final boolean isMinim = goal == GoalType.MINIMIZE;
double fA = eval(func, xA);
double fB = eval(func, xB);
if (isMinim ?
fA < fB :
fA > fB) {
double tmp = xA;
xA = xB;
xB = tmp;
tmp = fA;
fA = fB;
fB = tmp;
}
double xC = xB + GOLD * (xB - xA);
double fC = eval(func, xC);
while (isMinim ? fC < fB : fC > fB) {
double tmp1 = (xB - xA) * (fB - fC);
double tmp2 = (xB - xC) * (fB - fA);
double val = tmp2 - tmp1;
double denom = Math.abs(val) < EPS_MIN ? 2 * EPS_MIN : 2 * val;
double w = xB - ((xB - xC) * tmp2 - (xB - xA) * tmp1) / denom;
double wLim = xB + growLimit * (xC - xB);
double fW;
if ((w - xC) * (xB - w) > 0) {
fW = eval(func, w);
if (isMinim ?
fW < fC :
fW > fC) {
xA = xB;
xB = w;
fA = fB;
fB = fW;
break;
} else if (isMinim ?
fW > fB :
fW < fB) {
xC = w;
fC = fW;
break;
}
w = xC + GOLD * (xC - xB);
fW = eval(func, w);
} else if ((w - wLim) * (wLim - xC) >= 0) {
w = wLim;
fW = eval(func, w);
} else if ((w - wLim) * (xC - w) > 0) {
fW = eval(func, w);
if (isMinim ?
fW < fC :
fW > fC) {
xB = xC;
xC = w;
w = xC + GOLD * (xC - xB);
fB = fC;
fC =fW;
fW = eval(func, w);
}
} else {
w = xC + GOLD * (xC - xB);
fW = eval(func, w);
}
xA = xB;
fA = fB;
xB = xC;
fB = fC;
xC = w;
fC = fW;
}
lo = xA;
fLo = fA;
mid = xB;
fMid = fB;
hi = xC;
fHi = fC;
if (lo > hi) {
double tmp = lo;
lo = hi;
hi = tmp;
tmp = fLo;
fLo = fHi;
fHi = tmp;
}
}
/**
* @return the number of evalutations.
*/
public int getMaxEvaluations() {
return evaluations.getMaximalCount();
}
/**
* @return the number of evalutations.
*/
public int getEvaluations() {
return evaluations.getCount();
}
/**
* @return the lower bound of the bracket.
* @see #getFLo()
*/
public double getLo() {
return lo;
}
/**
* Get function value at {@link #getLo()}.
* @return function value at {@link #getLo()}
*/
public double getFLo() {
return fLo;
}
/**
* @return the higher bound of the bracket.
* @see #getFHi()
*/
public double getHi() {
return hi;
}
/**
* Get function value at {@link #getHi()}.
* @return function value at {@link #getHi()}
*/
public double getFHi() {
return fHi;
}
/**
* @return a point in the middle of the bracket.
* @see #getFMid()
*/
public double getMid() {
return mid;
}
/**
* Get function value at {@link #getMid()}.
* @return function value at {@link #getMid()}
*/
public double getFMid() {
return fMid;
}
/**
* @param f Function.
* @param x Argument.
* @return {@code f(x)}
* @throws TooManyEvaluationsException if the maximal number of evaluations is
* exceeded.
*/
private double eval(UnivariateFunction f, double x) {
try {
evaluations.incrementCount();
} catch (MaxCountExceededException e) {
throw new TooManyEvaluationsException(e.getMax());
}
return f.value(x);
}
} | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/nonlinear/vector/jacobian/LevenbergMarquardtOptimizer.java | 374 |
| org/apache/commons/math3/optimization/general/LevenbergMarquardtOptimizer.java | 378 |
setCost(currentCost);
return current;
}
// rescale if necessary
for (int j = 0; j < nC; ++j) {
diag[j] = FastMath.max(diag[j], jacNorm[j]);
}
// Inner loop.
for (double ratio = 0; ratio < 1.0e-4;) {
// save the state
for (int j = 0; j < solvedCols; ++j) {
int pj = permutation[j];
oldX[pj] = currentPoint[pj];
}
final double previousCost = currentCost;
double[] tmpVec = weightedResidual;
weightedResidual = oldRes;
oldRes = tmpVec;
tmpVec = currentObjective;
currentObjective = oldObj;
oldObj = tmpVec;
// determine the Levenberg-Marquardt parameter
determineLMParameter(qtf, delta, diag, work1, work2, work3);
// compute the new point and the norm of the evolution direction
double lmNorm = 0;
for (int j = 0; j < solvedCols; ++j) {
int pj = permutation[j];
lmDir[pj] = -lmDir[pj];
currentPoint[pj] = oldX[pj] + lmDir[pj];
double s = diag[pj] * lmDir[pj];
lmNorm += s * s;
}
lmNorm = FastMath.sqrt(lmNorm);
// on the first iteration, adjust the initial step bound.
if (firstIteration) {
delta = FastMath.min(delta, lmNorm);
}
// Evaluate the function at x + p and calculate its norm.
currentObjective = computeObjectiveValue(currentPoint);
currentResiduals = computeResiduals(currentObjective);
current = new PointVectorValuePair(currentPoint, currentObjective);
currentCost = computeCost(currentResiduals);
// compute the scaled actual reduction
double actRed = -1.0;
if (0.1 * currentCost < previousCost) {
double r = currentCost / previousCost;
actRed = 1.0 - r * r;
}
// compute the scaled predicted reduction
// and the scaled directional derivative
for (int j = 0; j < solvedCols; ++j) {
int pj = permutation[j];
double dirJ = lmDir[pj];
work1[j] = 0;
for (int i = 0; i <= j; ++i) {
work1[i] += weightedJacobian[i][pj] * dirJ;
}
}
double coeff1 = 0;
for (int j = 0; j < solvedCols; ++j) {
coeff1 += work1[j] * work1[j];
}
double pc2 = previousCost * previousCost;
coeff1 = coeff1 / pc2;
double coeff2 = lmPar * lmNorm * lmNorm / pc2;
double preRed = coeff1 + 2 * coeff2;
double dirDer = -(coeff1 + coeff2);
// ratio of the actual to the predicted reduction
ratio = (preRed == 0) ? 0 : (actRed / preRed);
// update the step bound
if (ratio <= 0.25) {
double tmp =
(actRed < 0) ? (0.5 * dirDer / (dirDer + 0.5 * actRed)) : 0.5;
if ((0.1 * currentCost >= previousCost) || (tmp < 0.1)) {
tmp = 0.1;
}
delta = tmp * FastMath.min(delta, 10.0 * lmNorm);
lmPar /= tmp;
} else if ((lmPar == 0) || (ratio >= 0.75)) {
delta = 2 * lmNorm;
lmPar *= 0.5;
}
// test for successful iteration.
if (ratio >= 1.0e-4) {
// successful iteration, update the norm
firstIteration = false;
xNorm = 0;
for (int k = 0; k < nC; ++k) {
double xK = diag[k] * currentPoint[k];
xNorm += xK * xK;
}
xNorm = FastMath.sqrt(xNorm);
// tests for convergence.
if (checker != null) {
// we use the vectorial convergence checker
if (checker.converged(iter, previous, current)) {
setCost(currentCost); | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/linear/SimplexTableau.java | 433 |
| org/apache/commons/math3/optimization/linear/SimplexTableau.java | 431 |
return new PointValuePair(coefficients, f.value(coefficients));
}
/**
* Subtracts a multiple of one row from another.
* <p>
* After application of this operation, the following will hold:
* <pre>minuendRow = minuendRow - multiple * subtrahendRow</pre>
*
* @param dividendRow index of the row
* @param divisor value of the divisor
*/
protected void divideRow(final int dividendRow, final double divisor) {
for (int j = 0; j < getWidth(); j++) {
tableau.setEntry(dividendRow, j, tableau.getEntry(dividendRow, j) / divisor);
}
}
/**
* Subtracts a multiple of one row from another.
* <p>
* After application of this operation, the following will hold:
* <pre>minuendRow = minuendRow - multiple * subtrahendRow</pre>
*
* @param minuendRow row index
* @param subtrahendRow row index
* @param multiple multiplication factor
*/
protected void subtractRow(final int minuendRow, final int subtrahendRow,
final double multiple) {
for (int i = 0; i < getWidth(); i++) {
double result = tableau.getEntry(minuendRow, i) - tableau.getEntry(subtrahendRow, i) * multiple;
// cut-off values smaller than the CUTOFF_THRESHOLD, otherwise may lead to numerical instabilities
if (FastMath.abs(result) < CUTOFF_THRESHOLD) {
result = 0.0;
}
tableau.setEntry(minuendRow, i, result);
}
}
/**
* Get the width of the tableau.
* @return width of the tableau
*/
protected final int getWidth() {
return tableau.getColumnDimension();
}
/**
* Get the height of the tableau.
* @return height of the tableau
*/
protected final int getHeight() {
return tableau.getRowDimension();
}
/**
* Get an entry of the tableau.
* @param row row index
* @param column column index
* @return entry at (row, column)
*/
protected final double getEntry(final int row, final int column) {
return tableau.getEntry(row, column);
}
/**
* Set an entry of the tableau.
* @param row row index
* @param column column index
* @param value for the entry
*/
protected final void setEntry(final int row, final int column,
final double value) {
tableau.setEntry(row, column, value);
}
/**
* Get the offset of the first slack variable.
* @return offset of the first slack variable
*/
protected final int getSlackVariableOffset() {
return getNumObjectiveFunctions() + numDecisionVariables;
}
/**
* Get the offset of the first artificial variable.
* @return offset of the first artificial variable
*/
protected final int getArtificialVariableOffset() {
return getNumObjectiveFunctions() + numDecisionVariables + numSlackVariables;
}
/**
* Get the offset of the right hand side.
* @return offset of the right hand side
*/
protected final int getRhsOffset() {
return getWidth() - 1;
}
/**
* Get the number of decision variables.
* <p>
* If variables are not restricted to positive values, this will include 1 extra decision variable to represent
* the absolute value of the most negative variable.
*
* @return number of decision variables
* @see #getOriginalNumDecisionVariables()
*/
protected final int getNumDecisionVariables() {
return numDecisionVariables;
}
/**
* Get the original number of decision variables.
* @return original number of decision variables
* @see #getNumDecisionVariables()
*/
protected final int getOriginalNumDecisionVariables() {
return f.getCoefficients().getDimension();
}
/**
* Get the number of slack variables.
* @return number of slack variables
*/
protected final int getNumSlackVariables() {
return numSlackVariables;
}
/**
* Get the number of artificial variables.
* @return number of artificial variables
*/
protected final int getNumArtificialVariables() {
return numArtificialVariables;
}
/**
* Get the tableau data.
* @return tableau data
*/
protected final double[][] getData() {
return tableau.getData();
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other instanceof SimplexTableau) {
SimplexTableau rhs = (SimplexTableau) other;
return (restrictToNonNegative == rhs.restrictToNonNegative) &&
(numDecisionVariables == rhs.numDecisionVariables) &&
(numSlackVariables == rhs.numSlackVariables) &&
(numArtificialVariables == rhs.numArtificialVariables) &&
(epsilon == rhs.epsilon) &&
(maxUlps == rhs.maxUlps) &&
f.equals(rhs.f) &&
constraints.equals(rhs.constraints) &&
tableau.equals(rhs.tableau);
}
return false;
}
@Override
public int hashCode() {
return Boolean.valueOf(restrictToNonNegative).hashCode() ^
numDecisionVariables ^
numSlackVariables ^
numArtificialVariables ^
Double.valueOf(epsilon).hashCode() ^
maxUlps ^
f.hashCode() ^
constraints.hashCode() ^
tableau.hashCode();
}
/**
* Serialize the instance.
* @param oos stream where object should be written
* @throws IOException if object cannot be written to stream
*/
private void writeObject(ObjectOutputStream oos)
throws IOException {
oos.defaultWriteObject();
MatrixUtils.serializeRealMatrix(tableau, oos);
}
/**
* Deserialize the instance.
* @param ois stream from which the object should be read
* @throws ClassNotFoundException if a class in the stream cannot be found
* @throws IOException if object cannot be read from the stream
*/
private void readObject(ObjectInputStream ois)
throws ClassNotFoundException, IOException {
ois.defaultReadObject();
MatrixUtils.deserializeRealMatrix(this, "tableau", ois);
}
} | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/OpenIntToDoubleHashMap.java | 175 |
| org/apache/commons/math3/util/OpenIntToFieldHashMap.java | 188 |
public double get(final int key) {
final int hash = hashOf(key);
int index = hash & mask;
if (containsKey(key, index)) {
return values[index];
}
if (states[index] == FREE) {
return missingEntries;
}
int j = index;
for (int perturb = perturb(hash); states[index] != FREE; perturb >>= PERTURB_SHIFT) {
j = probe(perturb, j);
index = j & mask;
if (containsKey(key, index)) {
return values[index];
}
}
return missingEntries;
}
/**
* Check if a value is associated with a key.
* @param key key to check
* @return true if a value is associated with key
*/
public boolean containsKey(final int key) {
final int hash = hashOf(key);
int index = hash & mask;
if (containsKey(key, index)) {
return true;
}
if (states[index] == FREE) {
return false;
}
int j = index;
for (int perturb = perturb(hash); states[index] != FREE; perturb >>= PERTURB_SHIFT) {
j = probe(perturb, j);
index = j & mask;
if (containsKey(key, index)) {
return true;
}
}
return false;
}
/**
* Get an iterator over map elements.
* <p>The specialized iterators returned are fail-fast: they throw a
* <code>ConcurrentModificationException</code> when they detect the map
* has been modified during iteration.</p>
* @return iterator over the map elements
*/
public Iterator iterator() {
return new Iterator();
}
/**
* Perturb the hash for starting probing.
* @param hash initial hash
* @return perturbed hash
*/
private static int perturb(final int hash) {
return hash & 0x7fffffff;
}
/**
* Find the index at which a key should be inserted
* @param key key to lookup
* @return index at which key should be inserted
*/
private int findInsertionIndex(final int key) {
return findInsertionIndex(keys, states, key, mask);
}
/**
* Find the index at which a key should be inserted
* @param keys keys table
* @param states states table
* @param key key to lookup
* @param mask bit mask for hash values
* @return index at which key should be inserted
*/
private static int findInsertionIndex(final int[] keys, final byte[] states,
final int key, final int mask) {
final int hash = hashOf(key);
int index = hash & mask;
if (states[index] == FREE) {
return index;
} else if (states[index] == FULL && keys[index] == key) {
return changeIndexSign(index);
}
int perturb = perturb(hash);
int j = index;
if (states[index] == FULL) {
while (true) {
j = probe(perturb, j);
index = j & mask;
perturb >>= PERTURB_SHIFT;
if (states[index] != FULL || keys[index] == key) {
break;
}
}
}
if (states[index] == FREE) {
return index;
} else if (states[index] == FULL) {
// due to the loop exit condition,
// if (states[index] == FULL) then keys[index] == key
return changeIndexSign(index);
}
final int firstRemoved = index;
while (true) {
j = probe(perturb, j);
index = j & mask;
if (states[index] == FREE) {
return firstRemoved;
} else if (states[index] == FULL && keys[index] == key) {
return changeIndexSign(index);
}
perturb >>= PERTURB_SHIFT;
}
}
/**
* Compute next probe for collision resolution
* @param perturb perturbed hash
* @param j previous probe
* @return next probe
*/
private static int probe(final int perturb, final int j) {
return (j << 2) + j + perturb + 1;
}
/**
* Change the index sign
* @param index initial index
* @return changed index
*/
private static int changeIndexSign(final int index) {
return -index - 1;
}
/**
* Get the number of elements stored in the map.
* @return number of elements stored in the map
*/
public int size() {
return size;
}
/**
* Remove the value associated with a key.
* @param key key to which the value is associated
* @return removed value
*/
public double remove(final int key) { | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/nonlinear/scalar/noderiv/MultiDirectionalSimplex.java | 30 |
| org/apache/commons/math3/optimization/direct/MultiDirectionalSimplex.java | 33 |
public class MultiDirectionalSimplex extends AbstractSimplex {
/** Default value for {@link #khi}: {@value}. */
private static final double DEFAULT_KHI = 2;
/** Default value for {@link #gamma}: {@value}. */
private static final double DEFAULT_GAMMA = 0.5;
/** Expansion coefficient. */
private final double khi;
/** Contraction coefficient. */
private final double gamma;
/**
* Build a multi-directional simplex with default coefficients.
* The default values are 2.0 for khi and 0.5 for gamma.
*
* @param n Dimension of the simplex.
*/
public MultiDirectionalSimplex(final int n) {
this(n, 1d);
}
/**
* Build a multi-directional simplex with default coefficients.
* The default values are 2.0 for khi and 0.5 for gamma.
*
* @param n Dimension of the simplex.
* @param sideLength Length of the sides of the default (hypercube)
* simplex. See {@link AbstractSimplex#AbstractSimplex(int,double)}.
*/
public MultiDirectionalSimplex(final int n, double sideLength) {
this(n, sideLength, DEFAULT_KHI, DEFAULT_GAMMA);
}
/**
* Build a multi-directional simplex with specified coefficients.
*
* @param n Dimension of the simplex. See
* {@link AbstractSimplex#AbstractSimplex(int,double)}.
* @param khi Expansion coefficient.
* @param gamma Contraction coefficient.
*/
public MultiDirectionalSimplex(final int n,
final double khi, final double gamma) {
this(n, 1d, khi, gamma);
}
/**
* Build a multi-directional simplex with specified coefficients.
*
* @param n Dimension of the simplex. See
* {@link AbstractSimplex#AbstractSimplex(int,double)}.
* @param sideLength Length of the sides of the default (hypercube)
* simplex. See {@link AbstractSimplex#AbstractSimplex(int,double)}.
* @param khi Expansion coefficient.
* @param gamma Contraction coefficient.
*/
public MultiDirectionalSimplex(final int n, double sideLength,
final double khi, final double gamma) {
super(n, sideLength);
this.khi = khi;
this.gamma = gamma;
}
/**
* Build a multi-directional simplex with default coefficients.
* The default values are 2.0 for khi and 0.5 for gamma.
*
* @param steps Steps along the canonical axes representing box edges.
* They may be negative but not zero. See
*/
public MultiDirectionalSimplex(final double[] steps) {
this(steps, DEFAULT_KHI, DEFAULT_GAMMA);
}
/**
* Build a multi-directional simplex with specified coefficients.
*
* @param steps Steps along the canonical axes representing box edges.
* They may be negative but not zero. See
* {@link AbstractSimplex#AbstractSimplex(double[])}.
* @param khi Expansion coefficient.
* @param gamma Contraction coefficient.
*/
public MultiDirectionalSimplex(final double[] steps,
final double khi, final double gamma) {
super(steps);
this.khi = khi;
this.gamma = gamma;
}
/**
* Build a multi-directional simplex with default coefficients.
* The default values are 2.0 for khi and 0.5 for gamma.
*
* @param referenceSimplex Reference simplex. See
* {@link AbstractSimplex#AbstractSimplex(double[][])}.
*/
public MultiDirectionalSimplex(final double[][] referenceSimplex) {
this(referenceSimplex, DEFAULT_KHI, DEFAULT_GAMMA);
}
/**
* Build a multi-directional simplex with specified coefficients.
*
* @param referenceSimplex Reference simplex. See
* {@link AbstractSimplex#AbstractSimplex(double[][])}.
* @param khi Expansion coefficient.
* @param gamma Contraction coefficient.
* @throws org.apache.commons.math3.exception.NotStrictlyPositiveException
* if the reference simplex does not contain at least one point.
* @throws org.apache.commons.math3.exception.DimensionMismatchException
* if there is a dimension mismatch in the reference simplex.
*/
public MultiDirectionalSimplex(final double[][] referenceSimplex,
final double khi, final double gamma) {
super(referenceSimplex);
this.khi = khi;
this.gamma = gamma;
}
/** {@inheritDoc} */
@Override
public void iterate(final MultivariateFunction evaluationFunction,
final Comparator<PointValuePair> comparator) {
// Save the original simplex.
final PointValuePair[] original = getPoints();
final PointValuePair best = original[0];
// Perform a reflection step.
final PointValuePair reflected = evaluateNewSimplex(evaluationFunction,
original, 1, comparator);
if (comparator.compare(reflected, best) < 0) {
// Compute the expanded simplex.
final PointValuePair[] reflectedSimplex = getPoints();
final PointValuePair expanded = evaluateNewSimplex(evaluationFunction,
original, khi, comparator);
if (comparator.compare(reflected, expanded) <= 0) {
// Keep the reflected simplex.
setPoints(reflectedSimplex);
}
// Keep the expanded simplex.
return;
}
// Compute the contracted simplex.
evaluateNewSimplex(evaluationFunction, original, gamma, comparator);
}
/**
* Compute and evaluate a new simplex.
*
* @param evaluationFunction Evaluation function.
* @param original Original simplex (to be preserved).
* @param coeff Linear coefficient.
* @param comparator Comparator to use to sort simplex vertices from best
* to poorest.
* @return the best point in the transformed simplex.
* @throws org.apache.commons.math3.exception.TooManyEvaluationsException
* if the maximal number of evaluations is exceeded.
*/
private PointValuePair evaluateNewSimplex(final MultivariateFunction evaluationFunction,
final PointValuePair[] original,
final double coeff,
final Comparator<PointValuePair> comparator) {
final double[] xSmallest = original[0].getPointRef();
// Perform a linear transformation on all the simplex points,
// except the first one.
setPoint(0, original[0]);
final int dim = getDimension();
for (int i = 1; i < getSize(); i++) {
final double[] xOriginal = original[i].getPointRef();
final double[] xTransformed = new double[dim];
for (int j = 0; j < dim; j++) {
xTransformed[j] = xSmallest[j] + coeff * (xSmallest[j] - xOriginal[j]);
}
setPoint(i, new PointValuePair(xTransformed, Double.NaN, false));
}
// Evaluate the simplex.
evaluate(evaluationFunction, comparator);
return getPoint(0);
}
} | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/nonlinear/vector/jacobian/GaussNewtonOptimizer.java | 57 |
| org/apache/commons/math3/optimization/general/GaussNewtonOptimizer.java | 88 |
this(true, checker);
}
/**
* @param useLU If {@code true}, the normal equations will be solved
* using LU decomposition, otherwise they will be solved using QR
* decomposition.
* @param checker Convergence checker.
*/
public GaussNewtonOptimizer(final boolean useLU,
ConvergenceChecker<PointVectorValuePair> checker) {
super(checker);
this.useLU = useLU;
}
/** {@inheritDoc} */
@Override
public PointVectorValuePair doOptimize() {
final ConvergenceChecker<PointVectorValuePair> checker
= getConvergenceChecker();
// Computation will be useless without a checker (see "for-loop").
if (checker == null) {
throw new NullArgumentException();
}
final double[] targetValues = getTarget();
final int nR = targetValues.length; // Number of observed data.
final RealMatrix weightMatrix = getWeight();
// Diagonal of the weight matrix.
final double[] residualsWeights = new double[nR];
for (int i = 0; i < nR; i++) {
residualsWeights[i] = weightMatrix.getEntry(i, i);
}
final double[] currentPoint = getStartPoint();
final int nC = currentPoint.length;
// iterate until convergence is reached
PointVectorValuePair current = null;
int iter = 0;
for (boolean converged = false; !converged;) {
++iter;
// evaluate the objective function and its jacobian
PointVectorValuePair previous = current;
// Value of the objective function at "currentPoint".
final double[] currentObjective = computeObjectiveValue(currentPoint);
final double[] currentResiduals = computeResiduals(currentObjective);
final RealMatrix weightedJacobian = computeWeightedJacobian(currentPoint);
current = new PointVectorValuePair(currentPoint, currentObjective);
// build the linear problem
final double[] b = new double[nC];
final double[][] a = new double[nC][nC];
for (int i = 0; i < nR; ++i) {
final double[] grad = weightedJacobian.getRow(i);
final double weight = residualsWeights[i];
final double residual = currentResiduals[i];
// compute the normal equation
final double wr = weight * residual;
for (int j = 0; j < nC; ++j) {
b[j] += wr * grad[j];
}
// build the contribution matrix for measurement i
for (int k = 0; k < nC; ++k) {
double[] ak = a[k];
double wgk = weight * grad[k];
for (int l = 0; l < nC; ++l) {
ak[l] += wgk * grad[l];
}
}
}
try {
// solve the linearized least squares problem
RealMatrix mA = new BlockRealMatrix(a);
DecompositionSolver solver = useLU ?
new LUDecomposition(mA).getSolver() :
new QRDecomposition(mA).getSolver();
final double[] dX = solver.solve(new ArrayRealVector(b, false)).toArray();
// update the estimated parameters
for (int i = 0; i < nC; ++i) {
currentPoint[i] += dX[i];
}
} catch (SingularMatrixException e) {
throw new ConvergenceException(LocalizedFormats.UNABLE_TO_SOLVE_SINGULAR_PROBLEM);
}
// Check convergence.
if (previous != null) {
converged = checker.converged(iter, previous, current);
if (converged) { | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/nonlinear/scalar/MultivariateFunctionMappingAdapter.java | 81 |
| org/apache/commons/math3/optimization/direct/MultivariateFunctionMappingAdapter.java | 80 |
public class MultivariateFunctionMappingAdapter
implements MultivariateFunction {
/** Underlying bounded function. */
private final MultivariateFunction bounded;
/** Mapping functions. */
private final Mapper[] mappers;
/** Simple constructor.
* @param bounded bounded function
* @param lower lower bounds for each element of the input parameters array
* (some elements may be set to {@code Double.NEGATIVE_INFINITY} for
* unbounded values)
* @param upper upper bounds for each element of the input parameters array
* (some elements may be set to {@code Double.POSITIVE_INFINITY} for
* unbounded values)
* @exception DimensionMismatchException if lower and upper bounds are not
* consistent, either according to dimension or to values
*/
public MultivariateFunctionMappingAdapter(final MultivariateFunction bounded,
final double[] lower, final double[] upper) {
// safety checks
MathUtils.checkNotNull(lower);
MathUtils.checkNotNull(upper);
if (lower.length != upper.length) {
throw new DimensionMismatchException(lower.length, upper.length);
}
for (int i = 0; i < lower.length; ++i) {
// note the following test is written in such a way it also fails for NaN
if (!(upper[i] >= lower[i])) {
throw new NumberIsTooSmallException(upper[i], lower[i], true);
}
}
this.bounded = bounded;
this.mappers = new Mapper[lower.length];
for (int i = 0; i < mappers.length; ++i) {
if (Double.isInfinite(lower[i])) {
if (Double.isInfinite(upper[i])) {
// element is unbounded, no transformation is needed
mappers[i] = new NoBoundsMapper();
} else {
// element is simple-bounded on the upper side
mappers[i] = new UpperBoundMapper(upper[i]);
}
} else {
if (Double.isInfinite(upper[i])) {
// element is simple-bounded on the lower side
mappers[i] = new LowerBoundMapper(lower[i]);
} else {
// element is double-bounded
mappers[i] = new LowerUpperBoundMapper(lower[i], upper[i]);
}
}
}
}
/**
* Maps an array from unbounded to bounded.
*
* @param point Unbounded values.
* @return the bounded values.
*/
public double[] unboundedToBounded(double[] point) {
// Map unbounded input point to bounded point.
final double[] mapped = new double[mappers.length];
for (int i = 0; i < mappers.length; ++i) {
mapped[i] = mappers[i].unboundedToBounded(point[i]);
}
return mapped;
}
/**
* Maps an array from bounded to unbounded.
*
* @param point Bounded values.
* @return the unbounded values.
*/
public double[] boundedToUnbounded(double[] point) {
// Map bounded input point to unbounded point.
final double[] mapped = new double[mappers.length];
for (int i = 0; i < mappers.length; ++i) {
mapped[i] = mappers[i].boundedToUnbounded(point[i]);
}
return mapped;
}
/**
* Compute the underlying function value from an unbounded point.
* <p>
* This method simply bounds the unbounded point using the mappings
* set up at construction and calls the underlying function using
* the bounded point.
* </p>
* @param point unbounded value
* @return underlying function value
* @see #unboundedToBounded(double[])
*/
public double value(double[] point) {
return bounded.value(unboundedToBounded(point));
}
/** Mapping interface. */
private interface Mapper {
/**
* Maps a value from unbounded to bounded.
*
* @param y Unbounded value.
* @return the bounded value.
*/
double unboundedToBounded(double y);
/**
* Maps a value from bounded to unbounded.
*
* @param x Bounded value.
* @return the unbounded value.
*/
double boundedToUnbounded(double x);
}
/** Local class for no bounds mapping. */
private static class NoBoundsMapper implements Mapper {
/** {@inheritDoc} */
public double unboundedToBounded(final double y) { | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/linear/SimplexSolver.java | 31 |
| org/apache/commons/math3/optimization/linear/SimplexSolver.java | 36 |
public class SimplexSolver extends LinearOptimizer {
/** Default amount of error to accept for algorithm convergence. */
private static final double DEFAULT_EPSILON = 1.0e-6;
/** Default amount of error to accept in floating point comparisons (as ulps). */
private static final int DEFAULT_ULPS = 10;
/** Amount of error to accept for algorithm convergence. */
private final double epsilon;
/** Amount of error to accept in floating point comparisons (as ulps). */
private final int maxUlps;
/**
* Builds a simplex solver with default settings.
*/
public SimplexSolver() {
this(DEFAULT_EPSILON, DEFAULT_ULPS);
}
/**
* Builds a simplex solver with a specified accepted amount of error.
*
* @param epsilon Amount of error to accept for algorithm convergence.
* @param maxUlps Amount of error to accept in floating point comparisons.
*/
public SimplexSolver(final double epsilon,
final int maxUlps) {
this.epsilon = epsilon;
this.maxUlps = maxUlps;
}
/**
* Returns the column with the most negative coefficient in the objective function row.
*
* @param tableau Simple tableau for the problem.
* @return the column with the most negative coefficient.
*/
private Integer getPivotColumn(SimplexTableau tableau) {
double minValue = 0;
Integer minPos = null;
for (int i = tableau.getNumObjectiveFunctions(); i < tableau.getWidth() - 1; i++) {
final double entry = tableau.getEntry(0, i);
// check if the entry is strictly smaller than the current minimum
// do not use a ulp/epsilon check
if (entry < minValue) {
minValue = entry;
minPos = i;
}
}
return minPos;
}
/**
* Returns the row with the minimum ratio as given by the minimum ratio test (MRT).
*
* @param tableau Simple tableau for the problem.
* @param col Column to test the ratio of (see {@link #getPivotColumn(SimplexTableau)}).
* @return the row with the minimum ratio.
*/
private Integer getPivotRow(SimplexTableau tableau, final int col) {
// create a list of all the rows that tie for the lowest score in the minimum ratio test
List<Integer> minRatioPositions = new ArrayList<Integer>();
double minRatio = Double.MAX_VALUE;
for (int i = tableau.getNumObjectiveFunctions(); i < tableau.getHeight(); i++) {
final double rhs = tableau.getEntry(i, tableau.getWidth() - 1);
final double entry = tableau.getEntry(i, col);
if (Precision.compareTo(entry, 0d, maxUlps) > 0) {
final double ratio = rhs / entry;
// check if the entry is strictly equal to the current min ratio
// do not use a ulp/epsilon check
final int cmp = Double.compare(ratio, minRatio);
if (cmp == 0) {
minRatioPositions.add(i);
} else if (cmp < 0) {
minRatio = ratio;
minRatioPositions = new ArrayList<Integer>();
minRatioPositions.add(i);
}
}
}
if (minRatioPositions.size() == 0) {
return null;
} else if (minRatioPositions.size() > 1) {
// there's a degeneracy as indicated by a tie in the minimum ratio test
// 1. check if there's an artificial variable that can be forced out of the basis
if (tableau.getNumArtificialVariables() > 0) {
for (Integer row : minRatioPositions) {
for (int i = 0; i < tableau.getNumArtificialVariables(); i++) {
int column = i + tableau.getArtificialVariableOffset();
final double entry = tableau.getEntry(row, column);
if (Precision.equals(entry, 1d, maxUlps) && row.equals(tableau.getBasicRow(column))) {
return row;
}
}
}
}
// 2. apply Bland's rule to prevent cycling:
// take the row for which the corresponding basic variable has the smallest index
//
// see http://www.stanford.edu/class/msande310/blandrule.pdf
// see http://en.wikipedia.org/wiki/Bland%27s_rule (not equivalent to the above paper)
//
// Additional heuristic: if we did not get a solution after half of maxIterations
// revert to the simple case of just returning the top-most row
// This heuristic is based on empirical data gathered while investigating MATH-828.
if (getEvaluations() < getMaxEvaluations() / 2) { | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/nonlinear/scalar/MultivariateFunctionPenaltyAdapter.java | 68 |
| org/apache/commons/math3/optimization/direct/MultivariateFunctionPenaltyAdapter.java | 67 |
public class MultivariateFunctionPenaltyAdapter
implements MultivariateFunction {
/** Underlying bounded function. */
private final MultivariateFunction bounded;
/** Lower bounds. */
private final double[] lower;
/** Upper bounds. */
private final double[] upper;
/** Penalty offset. */
private final double offset;
/** Penalty scales. */
private final double[] scale;
/**
* Simple constructor.
* <p>
* When the optimizer provided points are out of range, the value of the
* penalty function will be used instead of the value of the underlying
* function. In order for this penalty to be effective in rejecting this
* point during the optimization process, the penalty function value should
* be defined with care. This value is computed as:
* <pre>
* penalty(point) = offset + ∑<sub>i</sub>[scale[i] * √|point[i]-boundary[i]|]
* </pre>
* where indices i correspond to all the components that violates their boundaries.
* </p>
* <p>
* So when attempting a function minimization, offset should be larger than
* the maximum expected value of the underlying function and scale components
* should all be positive. When attempting a function maximization, offset
* should be lesser than the minimum expected value of the underlying function
* and scale components should all be negative.
* minimization, and lesser than the minimum expected value of the underlying
* function when attempting maximization.
* </p>
* <p>
* These choices for the penalty function have two properties. First, all out
* of range points will return a function value that is worse than the value
* returned by any in range point. Second, the penalty is worse for large
* boundaries violation than for small violations, so the optimizer has an hint
* about the direction in which it should search for acceptable points.
* </p>
* @param bounded bounded function
* @param lower lower bounds for each element of the input parameters array
* (some elements may be set to {@code Double.NEGATIVE_INFINITY} for
* unbounded values)
* @param upper upper bounds for each element of the input parameters array
* (some elements may be set to {@code Double.POSITIVE_INFINITY} for
* unbounded values)
* @param offset base offset of the penalty function
* @param scale scale of the penalty function
* @exception DimensionMismatchException if lower bounds, upper bounds and
* scales are not consistent, either according to dimension or to bounadary
* values
*/
public MultivariateFunctionPenaltyAdapter(final MultivariateFunction bounded,
final double[] lower, final double[] upper,
final double offset, final double[] scale) {
// safety checks
MathUtils.checkNotNull(lower);
MathUtils.checkNotNull(upper);
MathUtils.checkNotNull(scale);
if (lower.length != upper.length) {
throw new DimensionMismatchException(lower.length, upper.length);
}
if (lower.length != scale.length) {
throw new DimensionMismatchException(lower.length, scale.length);
}
for (int i = 0; i < lower.length; ++i) {
// note the following test is written in such a way it also fails for NaN
if (!(upper[i] >= lower[i])) {
throw new NumberIsTooSmallException(upper[i], lower[i], true);
}
}
this.bounded = bounded;
this.lower = lower.clone();
this.upper = upper.clone();
this.offset = offset;
this.scale = scale.clone();
}
/**
* Computes the underlying function value from an unbounded point.
* <p>
* This method simply returns the value of the underlying function
* if the unbounded point already fulfills the bounds, and compute
* a replacement value using the offset and scale if bounds are
* violated, without calling the function at all.
* </p>
* @param point unbounded point
* @return either underlying function value or penalty function value
*/
public double value(double[] point) {
for (int i = 0; i < scale.length; ++i) {
if ((point[i] < lower[i]) || (point[i] > upper[i])) {
// bound violation starting at this component
double sum = 0;
for (int j = i; j < scale.length; ++j) {
final double overshoot;
if (point[j] < lower[j]) {
overshoot = scale[j] * (lower[j] - point[j]);
} else if (point[j] > upper[j]) {
overshoot = scale[j] * (point[j] - upper[j]);
} else {
overshoot = 0;
}
sum += FastMath.sqrt(overshoot);
}
return offset + sum;
}
}
// all boundaries are fulfilled, we are in the expected
// domain of the underlying function
return bounded.value(point);
}
} | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/nonlinear/scalar/LeastSquaresConverter.java | 55 |
| org/apache/commons/math3/optimization/LeastSquaresConverter.java | 58 |
public class LeastSquaresConverter implements MultivariateFunction {
/** Underlying vectorial function. */
private final MultivariateVectorFunction function;
/** Observations to be compared to objective function to compute residuals. */
private final double[] observations;
/** Optional weights for the residuals. */
private final double[] weights;
/** Optional scaling matrix (weight and correlations) for the residuals. */
private final RealMatrix scale;
/**
* Builds a simple converter for uncorrelated residuals with identical
* weights.
*
* @param function vectorial residuals function to wrap
* @param observations observations to be compared to objective function to compute residuals
*/
public LeastSquaresConverter(final MultivariateVectorFunction function,
final double[] observations) {
this.function = function;
this.observations = observations.clone();
this.weights = null;
this.scale = null;
}
/**
* Builds a simple converter for uncorrelated residuals with the
* specified weights.
* <p>
* The scalar objective function value is computed as:
* <pre>
* objective = ∑weight<sub>i</sub>(observation<sub>i</sub>-objective<sub>i</sub>)<sup>2</sup>
* </pre>
* </p>
* <p>
* Weights can be used for example to combine residuals with different standard
* deviations. As an example, consider a residuals array in which even elements
* are angular measurements in degrees with a 0.01° standard deviation and
* odd elements are distance measurements in meters with a 15m standard deviation.
* In this case, the weights array should be initialized with value
* 1.0/(0.01<sup>2</sup>) in the even elements and 1.0/(15.0<sup>2</sup>) in the
* odd elements (i.e. reciprocals of variances).
* </p>
* <p>
* The array computed by the objective function, the observations array and the
* weights array must have consistent sizes or a {@link DimensionMismatchException}
* will be triggered while computing the scalar objective.
* </p>
*
* @param function vectorial residuals function to wrap
* @param observations observations to be compared to objective function to compute residuals
* @param weights weights to apply to the residuals
* @throws DimensionMismatchException if the observations vector and the weights
* vector dimensions do not match (objective function dimension is checked only when
* the {@link #value(double[])} method is called)
*/
public LeastSquaresConverter(final MultivariateVectorFunction function,
final double[] observations,
final double[] weights) {
if (observations.length != weights.length) {
throw new DimensionMismatchException(observations.length, weights.length);
}
this.function = function;
this.observations = observations.clone();
this.weights = weights.clone();
this.scale = null;
}
/**
* Builds a simple converter for correlated residuals with the
* specified weights.
* <p>
* The scalar objective function value is computed as:
* <pre>
* objective = y<sup>T</sup>y with y = scale×(observation-objective)
* </pre>
* </p>
* <p>
* The array computed by the objective function, the observations array and the
* the scaling matrix must have consistent sizes or a {@link DimensionMismatchException}
* will be triggered while computing the scalar objective.
* </p>
*
* @param function vectorial residuals function to wrap
* @param observations observations to be compared to objective function to compute residuals
* @param scale scaling matrix
* @throws DimensionMismatchException if the observations vector and the scale
* matrix dimensions do not match (objective function dimension is checked only when
* the {@link #value(double[])} method is called)
*/
public LeastSquaresConverter(final MultivariateVectorFunction function,
final double[] observations,
final RealMatrix scale) {
if (observations.length != scale.getColumnDimension()) {
throw new DimensionMismatchException(observations.length, scale.getColumnDimension());
}
this.function = function;
this.observations = observations.clone();
this.weights = null;
this.scale = scale.copy();
}
/** {@inheritDoc} */
public double value(final double[] point) {
// compute residuals
final double[] residuals = function.value(point);
if (residuals.length != observations.length) {
throw new DimensionMismatchException(residuals.length, observations.length);
}
for (int i = 0; i < residuals.length; ++i) {
residuals[i] -= observations[i];
}
// compute sum of squares
double sumSquares = 0;
if (weights != null) {
for (int i = 0; i < residuals.length; ++i) {
final double ri = residuals[i];
sumSquares += weights[i] * ri * ri;
}
} else if (scale != null) {
for (final double yi : scale.operate(residuals)) {
sumSquares += yi * yi;
}
} else {
for (final double ri : residuals) {
sumSquares += ri * ri;
}
}
return sumSquares;
}
} | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 684 |
| org/apache/commons/math3/linear/BlockRealMatrix.java | 690 |
final T[] outBlock = out.blocks[outIndex];
final int index = pBlock * blockColumns + qBlock;
final int width = blockWidth(qBlock);
final int heightExcess = iHeight + rowsShift - BLOCK_SIZE;
final int widthExcess = jWidth + columnsShift - BLOCK_SIZE;
if (heightExcess > 0) {
// the submatrix block spans on two blocks rows from the original matrix
if (widthExcess > 0) {
// the submatrix block spans on two blocks columns from the original matrix
final int width2 = blockWidth(qBlock + 1);
copyBlockPart(blocks[index], width,
rowsShift, BLOCK_SIZE,
columnsShift, BLOCK_SIZE,
outBlock, jWidth, 0, 0);
copyBlockPart(blocks[index + 1], width2,
rowsShift, BLOCK_SIZE,
0, widthExcess,
outBlock, jWidth, 0, jWidth - widthExcess);
copyBlockPart(blocks[index + blockColumns], width,
0, heightExcess,
columnsShift, BLOCK_SIZE,
outBlock, jWidth, iHeight - heightExcess, 0);
copyBlockPart(blocks[index + blockColumns + 1], width2,
0, heightExcess,
0, widthExcess,
outBlock, jWidth, iHeight - heightExcess, jWidth - widthExcess);
} else {
// the submatrix block spans on one block column from the original matrix
copyBlockPart(blocks[index], width,
rowsShift, BLOCK_SIZE,
columnsShift, jWidth + columnsShift,
outBlock, jWidth, 0, 0);
copyBlockPart(blocks[index + blockColumns], width,
0, heightExcess,
columnsShift, jWidth + columnsShift,
outBlock, jWidth, iHeight - heightExcess, 0);
}
} else {
// the submatrix block spans on one block row from the original matrix
if (widthExcess > 0) {
// the submatrix block spans on two blocks columns from the original matrix
final int width2 = blockWidth(qBlock + 1);
copyBlockPart(blocks[index], width,
rowsShift, iHeight + rowsShift,
columnsShift, BLOCK_SIZE,
outBlock, jWidth, 0, 0);
copyBlockPart(blocks[index + 1], width2,
rowsShift, iHeight + rowsShift,
0, widthExcess,
outBlock, jWidth, 0, jWidth - widthExcess);
} else {
// the submatrix block spans on one block column from the original matrix
copyBlockPart(blocks[index], width,
rowsShift, iHeight + rowsShift,
columnsShift, jWidth + columnsShift,
outBlock, jWidth, 0, 0);
}
}
++qBlock;
}
++pBlock;
}
return out;
}
/**
* Copy a part of a block into another one
* <p>This method can be called only when the specified part fits in both
* blocks, no verification is done here.</p>
* @param srcBlock source block
* @param srcWidth source block width ({@link #BLOCK_SIZE} or smaller)
* @param srcStartRow start row in the source block
* @param srcEndRow end row (exclusive) in the source block
* @param srcStartColumn start column in the source block
* @param srcEndColumn end column (exclusive) in the source block
* @param dstBlock destination block
* @param dstWidth destination block width ({@link #BLOCK_SIZE} or smaller)
* @param dstStartRow start row in the destination block
* @param dstStartColumn start column in the destination block
*/
private void copyBlockPart(final T[] srcBlock, final int srcWidth, | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/PointVectorValuePair.java | 31 |
| org/apache/commons/math3/optimization/PointVectorValuePair.java | 35 |
public class PointVectorValuePair extends Pair<double[], double[]> implements Serializable {
/** Serializable UID. */
private static final long serialVersionUID = 20120513L;
/**
* Builds a point/objective function value pair.
*
* @param point Point coordinates. This instance will store
* a copy of the array, not the array passed as argument.
* @param value Value of the objective function at the point.
*/
public PointVectorValuePair(final double[] point,
final double[] value) {
this(point, value, true);
}
/**
* Build a point/objective function value pair.
*
* @param point Point coordinates.
* @param value Value of the objective function at the point.
* @param copyArray if {@code true}, the input arrays will be copied,
* otherwise they will be referenced.
*/
public PointVectorValuePair(final double[] point,
final double[] value,
final boolean copyArray) {
super(copyArray ?
((point == null) ? null :
point.clone()) :
point,
copyArray ?
((value == null) ? null :
value.clone()) :
value);
}
/**
* Gets the point.
*
* @return a copy of the stored point.
*/
public double[] getPoint() {
final double[] p = getKey();
return p == null ? null : p.clone();
}
/**
* Gets a reference to the point.
*
* @return a reference to the internal array storing the point.
*/
public double[] getPointRef() {
return getKey();
}
/**
* Gets the value of the objective function.
*
* @return a copy of the stored value of the objective function.
*/
@Override
public double[] getValue() {
final double[] v = super.getValue();
return v == null ? null : v.clone();
}
/**
* Gets a reference to the value of the objective function.
*
* @return a reference to the internal array storing the value of
* the objective function.
*/
public double[] getValueRef() {
return super.getValue();
}
/**
* Replace the instance with a data transfer object for serialization.
* @return data transfer object that will be serialized
*/
private Object writeReplace() {
return new DataTransferObject(getKey(), getValue());
}
/** Internal class used only for serialization. */
private static class DataTransferObject implements Serializable {
/** Serializable UID. */
private static final long serialVersionUID = 20120513L;
/**
* Point coordinates.
* @Serial
*/
private final double[] point;
/**
* Value of the objective function at the point.
* @Serial
*/
private final double[] value;
/** Simple constructor.
* @param point Point coordinates.
* @param value Value of the objective function at the point.
*/
public DataTransferObject(final double[] point, final double[] value) {
this.point = point.clone();
this.value = value.clone();
}
/** Replace the deserialized data transfer object with a {@link PointValuePair}.
* @return replacement {@link PointValuePair}
*/
private Object readResolve() {
return new PointVectorValuePair(point, value, false);
}
}
} | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/linear/LinearConstraint.java | 49 |
| org/apache/commons/math3/optimization/linear/LinearConstraint.java | 53 |
public class LinearConstraint implements Serializable {
/** Serializable version identifier. */
private static final long serialVersionUID = -764632794033034092L;
/** Coefficients of the constraint (left hand side). */
private final transient RealVector coefficients;
/** Relationship between left and right hand sides (=, <=, >=). */
private final Relationship relationship;
/** Value of the constraint (right hand side). */
private final double value;
/**
* Build a constraint involving a single linear equation.
* <p>
* A linear constraint with a single linear equation has one of the forms:
* <ul>
* <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> = v</li>
* <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> <= v</li>
* <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> >= v</li>
* </ul>
* </p>
* @param coefficients The coefficients of the constraint (left hand side)
* @param relationship The type of (in)equality used in the constraint
* @param value The value of the constraint (right hand side)
*/
public LinearConstraint(final double[] coefficients,
final Relationship relationship,
final double value) {
this(new ArrayRealVector(coefficients), relationship, value);
}
/**
* Build a constraint involving a single linear equation.
* <p>
* A linear constraint with a single linear equation has one of the forms:
* <ul>
* <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> = v</li>
* <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> <= v</li>
* <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> >= v</li>
* </ul>
* </p>
* @param coefficients The coefficients of the constraint (left hand side)
* @param relationship The type of (in)equality used in the constraint
* @param value The value of the constraint (right hand side)
*/
public LinearConstraint(final RealVector coefficients,
final Relationship relationship,
final double value) {
this.coefficients = coefficients;
this.relationship = relationship;
this.value = value;
}
/**
* Build a constraint involving two linear equations.
* <p>
* A linear constraint with two linear equation has one of the forms:
* <ul>
* <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> =
* r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
* <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> <=
* r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
* <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> >=
* r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
* </ul>
* </p>
* @param lhsCoefficients The coefficients of the linear expression on the left hand side of the constraint
* @param lhsConstant The constant term of the linear expression on the left hand side of the constraint
* @param relationship The type of (in)equality used in the constraint
* @param rhsCoefficients The coefficients of the linear expression on the right hand side of the constraint
* @param rhsConstant The constant term of the linear expression on the right hand side of the constraint
*/
public LinearConstraint(final double[] lhsCoefficients, final double lhsConstant,
final Relationship relationship,
final double[] rhsCoefficients, final double rhsConstant) {
double[] sub = new double[lhsCoefficients.length];
for (int i = 0; i < sub.length; ++i) {
sub[i] = lhsCoefficients[i] - rhsCoefficients[i];
}
this.coefficients = new ArrayRealVector(sub, false);
this.relationship = relationship;
this.value = rhsConstant - lhsConstant;
}
/**
* Build a constraint involving two linear equations.
* <p>
* A linear constraint with two linear equation has one of the forms:
* <ul>
* <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> =
* r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
* <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> <=
* r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
* <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> >=
* r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
* </ul>
* </p>
* @param lhsCoefficients The coefficients of the linear expression on the left hand side of the constraint
* @param lhsConstant The constant term of the linear expression on the left hand side of the constraint
* @param relationship The type of (in)equality used in the constraint
* @param rhsCoefficients The coefficients of the linear expression on the right hand side of the constraint
* @param rhsConstant The constant term of the linear expression on the right hand side of the constraint
*/
public LinearConstraint(final RealVector lhsCoefficients, final double lhsConstant,
final Relationship relationship,
final RealVector rhsCoefficients, final double rhsConstant) {
this.coefficients = lhsCoefficients.subtract(rhsCoefficients);
this.relationship = relationship;
this.value = rhsConstant - lhsConstant;
}
/**
* Gets the coefficients of the constraint (left hand side).
*
* @return the coefficients of the constraint (left hand side).
*/
public RealVector getCoefficients() {
return coefficients;
}
/**
* Gets the relationship between left and right hand sides.
*
* @return the relationship between left and right hand sides.
*/
public Relationship getRelationship() {
return relationship;
}
/**
* Gets the value of the constraint (right hand side).
*
* @return the value of the constraint (right hand side).
*/
public double getValue() {
return value;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other instanceof LinearConstraint) {
LinearConstraint rhs = (LinearConstraint) other;
return relationship == rhs.relationship && | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/OpenIntToDoubleHashMap.java | 442 |
| org/apache/commons/math3/util/OpenIntToFieldHashMap.java | 455 |
final byte[] newStates = new byte[newLength];
final int newMask = newLength - 1;
for (int i = 0; i < oldLength; ++i) {
if (oldStates[i] == FULL) {
final int key = oldKeys[i];
final int index = findInsertionIndex(newKeys, newStates, key, newMask);
newKeys[index] = key;
newValues[index] = oldValues[i];
newStates[index] = FULL;
}
}
mask = newMask;
keys = newKeys;
values = newValues;
states = newStates;
}
/**
* Check if tables should grow due to increased size.
* @return true if tables should grow
*/
private boolean shouldGrowTable() {
return size > (mask + 1) * LOAD_FACTOR;
}
/**
* Compute the hash value of a key
* @param key key to hash
* @return hash value of the key
*/
private static int hashOf(final int key) {
final int h = key ^ ((key >>> 20) ^ (key >>> 12));
return h ^ (h >>> 7) ^ (h >>> 4);
}
/** Iterator class for the map. */
public class Iterator {
/** Reference modification count. */
private final int referenceCount;
/** Index of current element. */
private int current;
/** Index of next element. */
private int next;
/**
* Simple constructor.
*/
private Iterator() {
// preserve the modification count of the map to detect concurrent modifications later
referenceCount = count;
// initialize current index
next = -1;
try {
advance();
} catch (NoSuchElementException nsee) { // NOPMD
// ignored
}
}
/**
* Check if there is a next element in the map.
* @return true if there is a next element
*/
public boolean hasNext() {
return next >= 0;
}
/**
* Get the key of current entry.
* @return key of current entry
* @exception ConcurrentModificationException if the map is modified during iteration
* @exception NoSuchElementException if there is no element left in the map
*/
public int key()
throws ConcurrentModificationException, NoSuchElementException {
if (referenceCount != count) {
throw new ConcurrentModificationException();
}
if (current < 0) {
throw new NoSuchElementException();
}
return keys[current];
}
/**
* Get the value of current entry.
* @return value of current entry
* @exception ConcurrentModificationException if the map is modified during iteration
* @exception NoSuchElementException if there is no element left in the map
*/
public double value() | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/nonlinear/scalar/noderiv/SimplexOptimizer.java | 122 |
| org/apache/commons/math3/optimization/direct/SimplexOptimizer.java | 177 |
}
/** {@inheritDoc} */
@Override
protected PointValuePair doOptimize() {
if (simplex == null) {
throw new NullArgumentException();
}
// Indirect call to "computeObjectiveValue" in order to update the
// evaluations counter.
final MultivariateFunction evalFunc
= new MultivariateFunction() {
public double value(double[] point) {
return computeObjectiveValue(point);
}
};
final boolean isMinim = getGoalType() == GoalType.MINIMIZE;
final Comparator<PointValuePair> comparator
= new Comparator<PointValuePair>() {
public int compare(final PointValuePair o1,
final PointValuePair o2) {
final double v1 = o1.getValue();
final double v2 = o2.getValue();
return isMinim ? Double.compare(v1, v2) : Double.compare(v2, v1);
}
};
// Initialize search.
simplex.build(getStartPoint());
simplex.evaluate(evalFunc, comparator);
PointValuePair[] previous = null;
int iteration = 0;
final ConvergenceChecker<PointValuePair> checker = getConvergenceChecker();
while (true) {
if (iteration > 0) {
boolean converged = true;
for (int i = 0; i < simplex.getSize(); i++) {
PointValuePair prev = previous[i];
converged = converged &&
checker.converged(iteration, prev, simplex.getPoint(i));
}
if (converged) {
// We have found an optimum.
return simplex.getPoint(0);
}
}
// We still need to search.
previous = simplex.getPoints();
simplex.iterate(evalFunc, comparator);
++iteration;
}
} | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/MathArrays.java | 929 |
| org/apache/commons/math3/util/MathArrays.java | 1031 |
final double a3, final double b3) {
// the code below is split in many additions/subtractions that may
// appear redundant. However, they should NOT be simplified, as they
// do use IEEE754 floating point arithmetic rounding properties.
// as an example, the expression "ca1 - (ca1 - a1)" is NOT the same as "a1"
// The variables naming conventions are that xyzHigh contains the most significant
// bits of xyz and xyzLow contains its least significant bits. So theoretically
// xyz is the sum xyzHigh + xyzLow, but in many cases below, this sum cannot
// be represented in only one double precision number so we preserve two numbers
// to hold it as long as we can, combining the high and low order bits together
// only at the end, after cancellation may have occurred on high order bits
// split a1 and b1 as two 26 bits numbers
final double ca1 = SPLIT_FACTOR * a1;
final double a1High = ca1 - (ca1 - a1);
final double a1Low = a1 - a1High;
final double cb1 = SPLIT_FACTOR * b1;
final double b1High = cb1 - (cb1 - b1);
final double b1Low = b1 - b1High;
// accurate multiplication a1 * b1
final double prod1High = a1 * b1;
final double prod1Low = a1Low * b1Low - (((prod1High - a1High * b1High) - a1Low * b1High) - a1High * b1Low);
// split a2 and b2 as two 26 bits numbers
final double ca2 = SPLIT_FACTOR * a2;
final double a2High = ca2 - (ca2 - a2);
final double a2Low = a2 - a2High;
final double cb2 = SPLIT_FACTOR * b2;
final double b2High = cb2 - (cb2 - b2);
final double b2Low = b2 - b2High;
// accurate multiplication a2 * b2
final double prod2High = a2 * b2;
final double prod2Low = a2Low * b2Low - (((prod2High - a2High * b2High) - a2Low * b2High) - a2High * b2Low);
// split a3 and b3 as two 26 bits numbers
final double ca3 = SPLIT_FACTOR * a3;
final double a3High = ca3 - (ca3 - a3);
final double a3Low = a3 - a3High;
final double cb3 = SPLIT_FACTOR * b3;
final double b3High = cb3 - (cb3 - b3);
final double b3Low = b3 - b3High;
// accurate multiplication a3 * b3
final double prod3High = a3 * b3;
final double prod3Low = a3Low * b3Low - (((prod3High - a3High * b3High) - a3Low * b3High) - a3High * b3Low);
// accurate addition a1 * b1 + a2 * b2
final double s12High = prod1High + prod2High; | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/nonlinear/scalar/noderiv/CMAESOptimizer.java | 223 |
| org/apache/commons/math3/optimization/direct/CMAESOptimizer.java | 346 |
public CMAESOptimizer(int maxIterations,
double stopFitness,
boolean isActiveCMA,
int diagonalOnly,
int checkFeasableCount,
RandomGenerator random,
boolean generateStatistics,
ConvergenceChecker<PointValuePair> checker) {
super(checker);
this.maxIterations = maxIterations;
this.stopFitness = stopFitness;
this.isActiveCMA = isActiveCMA;
this.diagonalOnly = diagonalOnly;
this.checkFeasableCount = checkFeasableCount;
this.random = random;
this.generateStatistics = generateStatistics;
}
/**
* @return History of sigma values.
*/
public List<Double> getStatisticsSigmaHistory() {
return statisticsSigmaHistory;
}
/**
* @return History of mean matrix.
*/
public List<RealMatrix> getStatisticsMeanHistory() {
return statisticsMeanHistory;
}
/**
* @return History of fitness values.
*/
public List<Double> getStatisticsFitnessHistory() {
return statisticsFitnessHistory;
}
/**
* @return History of D matrix.
*/
public List<RealMatrix> getStatisticsDHistory() {
return statisticsDHistory;
}
/**
* Input sigma values.
* They define the initial coordinate-wise standard deviations for
* sampling new search points around the initial guess.
* It is suggested to set them to the estimated distance from the
* initial to the desired optimum.
* Small values induce the search to be more local (and very small
* values are more likely to find a local optimum close to the initial
* guess).
* Too small values might however lead to early termination.
*/
public static class Sigma implements OptimizationData {
/** Sigma values. */
private final double[] sigma;
/**
* @param s Sigma values.
* @throws NotPositiveException if any of the array entries is smaller
* than zero.
*/
public Sigma(double[] s)
throws NotPositiveException {
for (int i = 0; i < s.length; i++) {
if (s[i] < 0) {
throw new NotPositiveException(s[i]);
}
}
sigma = s.clone();
}
/**
* @return the sigma values.
*/
public double[] getSigma() {
return sigma.clone();
}
}
/**
* Population size.
* The number of offspring is the primary strategy parameter.
* In the absence of better clues, a good default could be an
* integer close to {@code 4 + 3 ln(n)}, where {@code n} is the
* number of optimized parameters.
* Increasing the population size improves global search properties
* at the expense of speed (which in general decreases at most
* linearly with increasing population size).
*/
public static class PopulationSize implements OptimizationData {
/** Population size. */
private final int lambda;
/**
* @param size Population size.
* @throws NotStrictlyPositiveException if {@code size <= 0}.
*/
public PopulationSize(int size)
throws NotStrictlyPositiveException {
if (size <= 0) {
throw new NotStrictlyPositiveException(size);
}
lambda = size;
}
/**
* @return the population size.
*/
public int getPopulationSize() {
return lambda;
}
}
/**
* {@inheritDoc}
*
* @param optData Optimization data. The following data will be looked for:
* <ul>
* <li>{@link org.apache.commons.math3.optim.MaxEval}</li>
* <li>{@link org.apache.commons.math3.optim.InitialGuess}</li>
* <li>{@link org.apache.commons.math3.optim.SimpleBounds}</li>
* <li>{@link org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction}</li>
* <li>{@link Sigma}</li>
* <li>{@link PopulationSize}</li>
* </ul>
* @return {@inheritDoc}
* @throws TooManyEvaluationsException if the maximal number of
* evaluations is exceeded.
* @throws DimensionMismatchException if the initial guess, target, and weight
* arguments have inconsistent dimensions.
*/
@Override | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/nonlinear/scalar/MultivariateFunctionMappingAdapter.java | 206 |
| org/apache/commons/math3/optimization/direct/MultivariateFunctionMappingAdapter.java | 211 |
public double unboundedToBounded(final double y) {
return y;
}
/** {@inheritDoc} */
public double boundedToUnbounded(final double x) {
return x;
}
}
/** Local class for lower bounds mapping. */
private static class LowerBoundMapper implements Mapper {
/** Low bound. */
private final double lower;
/**
* Simple constructor.
*
* @param lower lower bound
*/
public LowerBoundMapper(final double lower) {
this.lower = lower;
}
/** {@inheritDoc} */
public double unboundedToBounded(final double y) {
return lower + FastMath.exp(y);
}
/** {@inheritDoc} */
public double boundedToUnbounded(final double x) {
return FastMath.log(x - lower);
}
}
/** Local class for upper bounds mapping. */
private static class UpperBoundMapper implements Mapper {
/** Upper bound. */
private final double upper;
/** Simple constructor.
* @param upper upper bound
*/
public UpperBoundMapper(final double upper) {
this.upper = upper;
}
/** {@inheritDoc} */
public double unboundedToBounded(final double y) {
return upper - FastMath.exp(-y);
}
/** {@inheritDoc} */
public double boundedToUnbounded(final double x) {
return -FastMath.log(upper - x);
}
}
/** Local class for lower and bounds mapping. */
private static class LowerUpperBoundMapper implements Mapper {
/** Function from unbounded to bounded. */
private final UnivariateFunction boundingFunction;
/** Function from bounded to unbounded. */
private final UnivariateFunction unboundingFunction;
/**
* Simple constructor.
*
* @param lower lower bound
* @param upper upper bound
*/
public LowerUpperBoundMapper(final double lower, final double upper) {
boundingFunction = new Sigmoid(lower, upper);
unboundingFunction = new Logit(lower, upper);
}
/** {@inheritDoc} */
public double unboundedToBounded(final double y) {
return boundingFunction.value(y);
}
/** {@inheritDoc} */
public double boundedToUnbounded(final double x) {
return unboundingFunction.value(x);
}
}
} | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/BlockRealMatrix.java | 1395 |
| org/apache/commons/math3/linear/BlockRealMatrix.java | 1425 |
public double walkInRowOrder(final RealMatrixChangingVisitor visitor,
final int startRow, final int endRow,
final int startColumn, final int endColumn)
throws OutOfRangeException, NumberIsTooSmallException {
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) { | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/BlockRealMatrix.java | 1505 |
| org/apache/commons/math3/linear/BlockRealMatrix.java | 1536 |
public double walkInOptimizedOrder(final RealMatrixChangingVisitor visitor,
final int startRow, final int endRow,
final int startColumn,
final int endColumn)
throws OutOfRangeException, NumberIsTooSmallException {
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 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];
for (int p = pStart; p < pEnd; ++p) {
int k = (p - p0) * jWidth + qStart - q0;
for (int q = qStart; q < qEnd; ++q) { | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 1407 |
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 1437 |
public T walkInRowOrder(final FieldMatrixChangingVisitor<T> visitor,
final int startRow, final int endRow,
final int startColumn, final int endColumn)
throws OutOfRangeException, NumberIsTooSmallException {
checkSubMatrixIndex(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 T[] block = blocks[iBlock * blockColumns + jBlock];
int k = (p - p0) * jWidth + qStart - q0;
for (int q = qStart; q < qEnd; ++q) { | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 1517 |
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 1547 |
public T walkInOptimizedOrder(final FieldMatrixChangingVisitor<T> visitor,
final int startRow, final int endRow,
final int startColumn, final int endColumn)
throws OutOfRangeException, NumberIsTooSmallException {
checkSubMatrixIndex(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 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 T[] block = blocks[iBlock * blockColumns + jBlock];
for (int p = pStart; p < pEnd; ++p) {
int k = (p - p0) * jWidth + qStart - q0;
for (int q = qStart; q < qEnd; ++q) { | |
| File | Line |
|---|---|
| org/apache/commons/math3/fitting/CurveFitter.java | 64 |
| org/apache/commons/math3/optimization/fitting/CurveFitter.java | 81 |
this.optimizer = optimizer;
observations = new ArrayList<WeightedObservedPoint>();
}
/** Add an observed (x,y) point to the sample with unit weight.
* <p>Calling this method is equivalent to call
* {@code addObservedPoint(1.0, x, y)}.</p>
* @param x abscissa of the point
* @param y observed value of the point at x, after fitting we should
* have f(x) as close as possible to this value
* @see #addObservedPoint(double, double, double)
* @see #addObservedPoint(WeightedObservedPoint)
* @see #getObservations()
*/
public void addObservedPoint(double x, double y) {
addObservedPoint(1.0, x, y);
}
/** Add an observed weighted (x,y) point to the sample.
* @param weight weight of the observed point in the fit
* @param x abscissa of the point
* @param y observed value of the point at x, after fitting we should
* have f(x) as close as possible to this value
* @see #addObservedPoint(double, double)
* @see #addObservedPoint(WeightedObservedPoint)
* @see #getObservations()
*/
public void addObservedPoint(double weight, double x, double y) {
observations.add(new WeightedObservedPoint(weight, x, y));
}
/** Add an observed weighted (x,y) point to the sample.
* @param observed observed point to add
* @see #addObservedPoint(double, double)
* @see #addObservedPoint(double, double, double)
* @see #getObservations()
*/
public void addObservedPoint(WeightedObservedPoint observed) {
observations.add(observed);
}
/** Get the observed points.
* @return observed points
* @see #addObservedPoint(double, double)
* @see #addObservedPoint(double, double, double)
* @see #addObservedPoint(WeightedObservedPoint)
*/
public WeightedObservedPoint[] getObservations() {
return observations.toArray(new WeightedObservedPoint[observations.size()]);
}
/**
* Remove all observations.
*/
public void clearObservations() {
observations.clear();
}
/**
* Fit a curve.
* This method compute the coefficients of the curve that best
* fit the sample of observed points previously given through calls
* to the {@link #addObservedPoint(WeightedObservedPoint)
* addObservedPoint} method.
*
* @param f parametric function to fit.
* @param initialGuess first guess of the function parameters.
* @return the fitted parameters.
* @throws org.apache.commons.math3.exception.DimensionMismatchException
* if the start point dimension is wrong.
*/
public double[] fit(T f, final double[] initialGuess) {
return fit(Integer.MAX_VALUE, f, initialGuess);
}
/**
* Fit a curve.
* This method compute the coefficients of the curve that best
* fit the sample of observed points previously given through calls
* to the {@link #addObservedPoint(WeightedObservedPoint)
* addObservedPoint} method.
*
* @param f parametric function to fit.
* @param initialGuess first guess of the function parameters.
* @param maxEval Maximum number of function evaluations.
* @return the fitted parameters.
* @throws org.apache.commons.math3.exception.TooManyEvaluationsException
* if the number of allowed evaluations is exceeded.
* @throws org.apache.commons.math3.exception.DimensionMismatchException
* if the start point dimension is wrong.
* @since 3.0
*/
public double[] fit(int maxEval, T f,
final double[] initialGuess) {
// Prepare least squares problem.
double[] target = new double[observations.size()];
double[] weights = new double[observations.size()];
int i = 0;
for (WeightedObservedPoint point : observations) {
target[i] = point.getY();
weights[i] = point.getWeight();
++i;
}
// Input to the optimizer: the model and its Jacobian.
final TheoreticalValuesFunction model = new TheoreticalValuesFunction(f); | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/FieldLUDecomposition.java | 304 |
| org/apache/commons/math3/linear/FieldLUDecomposition.java | 351 |
throw new DimensionMismatchException(b.getDimension(), m);
}
if (singular) {
throw new SingularMatrixException();
}
@SuppressWarnings("unchecked") // field is of type T
final T[] bp = (T[]) Array.newInstance(field.getRuntimeClass(), m);
// Apply permutations to b
for (int row = 0; row < m; row++) {
bp[row] = b.getEntry(pivot[row]);
}
// Solve LY = b
for (int col = 0; col < m; col++) {
final T bpCol = bp[col];
for (int i = col + 1; i < m; i++) {
bp[i] = bp[i].subtract(bpCol.multiply(lu[i][col]));
}
}
// Solve UX = Y
for (int col = m - 1; col >= 0; col--) {
bp[col] = bp[col].divide(lu[col][col]);
final T bpCol = bp[col];
for (int i = 0; i < col; i++) {
bp[i] = bp[i].subtract(bpCol.multiply(lu[i][col]));
}
}
return new ArrayFieldVector<T>(field, bp, false); | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/PointValuePair.java | 31 |
| org/apache/commons/math3/optimization/PointValuePair.java | 35 |
public class PointValuePair extends Pair<double[], Double> implements Serializable {
/** Serializable UID. */
private static final long serialVersionUID = 20120513L;
/**
* Builds a point/objective function value pair.
*
* @param point Point coordinates. This instance will store
* a copy of the array, not the array passed as argument.
* @param value Value of the objective function at the point.
*/
public PointValuePair(final double[] point,
final double value) {
this(point, value, true);
}
/**
* Builds a point/objective function value pair.
*
* @param point Point coordinates.
* @param value Value of the objective function at the point.
* @param copyArray if {@code true}, the input array will be copied,
* otherwise it will be referenced.
*/
public PointValuePair(final double[] point,
final double value,
final boolean copyArray) {
super(copyArray ? ((point == null) ? null :
point.clone()) :
point,
value);
}
/**
* Gets the point.
*
* @return a copy of the stored point.
*/
public double[] getPoint() {
final double[] p = getKey();
return p == null ? null : p.clone();
}
/**
* Gets a reference to the point.
*
* @return a reference to the internal array storing the point.
*/
public double[] getPointRef() {
return getKey();
}
/**
* Replace the instance with a data transfer object for serialization.
* @return data transfer object that will be serialized
*/
private Object writeReplace() {
return new DataTransferObject(getKey(), getValue());
}
/** Internal class used only for serialization. */
private static class DataTransferObject implements Serializable {
/** Serializable UID. */
private static final long serialVersionUID = 20120513L;
/**
* Point coordinates.
* @Serial
*/
private final double[] point;
/**
* Value of the objective function at the point.
* @Serial
*/
private final double value;
/** Simple constructor.
* @param point Point coordinates.
* @param value Value of the objective function at the point.
*/
public DataTransferObject(final double[] point, final double value) {
this.point = point.clone();
this.value = value;
}
/** Replace the deserialized data transfer object with a {@link PointValuePair}.
* @return replacement {@link PointValuePair}
*/
private Object readResolve() {
return new PointValuePair(point, value, false);
}
}
} | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/SimplePointChecker.java | 66 |
| org/apache/commons/math3/optimization/SimplePointChecker.java | 78 |
public SimplePointChecker(final double relativeThreshold,
final double absoluteThreshold) {
super(relativeThreshold, absoluteThreshold);
maxIterationCount = ITERATION_CHECK_DISABLED;
}
/**
* Builds an instance with specified thresholds.
* In order to perform only relative checks, the absolute tolerance
* must be set to a negative value. In order to perform only absolute
* checks, the relative tolerance must be set to a negative value.
*
* @param relativeThreshold Relative tolerance threshold.
* @param absoluteThreshold Absolute tolerance threshold.
* @param maxIter Maximum iteration count.
* @throws NotStrictlyPositiveException if {@code maxIter <= 0}.
*
* @since 3.1
*/
public SimplePointChecker(final double relativeThreshold,
final double absoluteThreshold,
final int maxIter) {
super(relativeThreshold, absoluteThreshold);
if (maxIter <= 0) {
throw new NotStrictlyPositiveException(maxIter);
}
maxIterationCount = maxIter;
}
/**
* Check if the optimization algorithm has converged considering the
* last two points.
* This method may be called several times 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 arguments satify the convergence criterion.
*/
@Override
public boolean converged(final int iteration,
final PAIR previous,
final PAIR current) {
if (maxIterationCount != ITERATION_CHECK_DISABLED) {
if (iteration >= maxIterationCount) {
return true;
}
}
final double[] p = previous.getKey();
final double[] c = current.getKey();
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;
}
} | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/SimpleVectorValueChecker.java | 65 |
| org/apache/commons/math3/optimization/SimpleVectorValueChecker.java | 76 |
public SimpleVectorValueChecker(final double relativeThreshold,
final double absoluteThreshold) {
super(relativeThreshold, absoluteThreshold);
maxIterationCount = ITERATION_CHECK_DISABLED;
}
/**
* Builds an instance with specified tolerance thresholds and
* iteration count.
*
* In order to perform only relative checks, the absolute tolerance
* must be set to a negative value. In order to perform only absolute
* checks, the relative tolerance must be set to a negative value.
*
* @param relativeThreshold Relative tolerance threshold.
* @param absoluteThreshold Absolute tolerance threshold.
* @param maxIter Maximum iteration count.
* @throws NotStrictlyPositiveException if {@code maxIter <= 0}.
*
* @since 3.1
*/
public SimpleVectorValueChecker(final double relativeThreshold,
final double absoluteThreshold,
final int maxIter) {
super(relativeThreshold, absoluteThreshold);
if (maxIter <= 0) {
throw new NotStrictlyPositiveException(maxIter);
}
maxIterationCount = maxIter;
}
/**
* Check if the optimization algorithm has converged considering the
* last two points.
* This method may be called several times 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 arguments satify the convergence criterion.
*/
@Override
public boolean converged(final int iteration,
final PointVectorValuePair previous,
final PointVectorValuePair current) {
if (maxIterationCount != ITERATION_CHECK_DISABLED) {
if (iteration >= maxIterationCount) {
return true;
}
}
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;
}
} | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/differentiation/DSCompiler.java | 1225 |
| org/apache/commons/math3/analysis/differentiation/DSCompiler.java | 1282 |
p[0] = -1;
final double x2 = x * x;
final double f = 1.0 / (1 - x2);
double coeff = FastMath.sqrt(f);
function[1] = coeff * p[0];
for (int n = 2; n <= order; ++n) {
// update and evaluate polynomial P_n(x)
double v = 0;
p[n - 1] = (n - 1) * p[n - 2];
for (int k = n - 1; k >= 0; k -= 2) {
v = v * x2 + p[k];
if (k > 2) {
p[k - 2] = (k - 1) * p[k - 1] + (2 * n - k) * p[k - 3];
} else if (k == 2) {
p[0] = p[1];
}
}
if ((n & 0x1) == 0) {
v *= x;
}
coeff *= f;
function[n] = coeff * v;
}
}
// apply function composition
compose(operand, operandOffset, function, result, resultOffset);
}
/** Compute arc sine of a derivative structure.
* @param operand array holding the operand
* @param operandOffset offset of the operand in its array
* @param result array where result must be stored (for
* arc sine the result array <em>cannot</em> be the input
* array)
* @param resultOffset offset of the result in its array
*/
public void asin(final double[] operand, final int operandOffset, | |
| File | Line |
|---|---|
| org/apache/commons/math3/random/Well44497a.java | 78 |
| org/apache/commons/math3/random/Well44497b.java | 78 |
public Well44497a(long seed) {
super(K, M1, M2, M3, seed);
}
/** {@inheritDoc} */
@Override
protected int next(final int bits) {
final int indexRm1 = iRm1[index];
final int indexRm2 = iRm2[index];
final int v0 = v[index];
final int vM1 = v[i1[index]];
final int vM2 = v[i2[index]];
final int vM3 = v[i3[index]];
// the values below include the errata of the original article
final int z0 = (0xFFFF8000 & v[indexRm1]) ^ (0x00007FFF & v[indexRm2]);
final int z1 = (v0 ^ (v0 << 24)) ^ (vM1 ^ (vM1 >>> 30));
final int z2 = (vM2 ^ (vM2 << 10)) ^ (vM3 << 26);
final int z3 = z1 ^ z2;
final int z2Prime = ((z2 << 9) ^ (z2 >>> 23)) & 0xfbffffff;
final int z2Second = ((z2 & 0x00020000) != 0) ? (z2Prime ^ 0xb729fcec) : z2Prime; | |
| File | Line |
|---|---|
| org/apache/commons/math3/ode/nonstiff/EmbeddedRungeKuttaIntegrator.java | 264 |
| org/apache/commons/math3/ode/nonstiff/RungeKuttaIntegrator.java | 135 |
for (int k = 1; k < stages; ++k) {
for (int j = 0; j < y0.length; ++j) {
double sum = a[k-1][0] * yDotK[0][j];
for (int l = 1; l < k; ++l) {
sum += a[k-1][l] * yDotK[l][j];
}
yTmp[j] = y[j] + stepSize * sum;
}
computeDerivatives(stepStart + c[k-1] * stepSize, yTmp, yDotK[k]);
}
// estimate the state at the end of the step
for (int j = 0; j < y0.length; ++j) {
double sum = b[0] * yDotK[0][j];
for (int l = 1; l < stages; ++l) {
sum += b[l] * yDotK[l][j];
}
yTmp[j] = y[j] + stepSize * sum;
} | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 796 |
| org/apache/commons/math3/linear/BlockRealMatrix.java | 802 |
for (final T[] subRow : subMatrix) {
if (subRow.length != refLength) {
throw new DimensionMismatchException(refLength, subRow.length);
}
}
// compute blocks bounds
final int blockStartRow = row / BLOCK_SIZE;
final int blockEndRow = (endRow + BLOCK_SIZE) / BLOCK_SIZE;
final int blockStartColumn = column / BLOCK_SIZE;
final int blockEndColumn = (endColumn + BLOCK_SIZE) / BLOCK_SIZE;
// perform copy block-wise, to ensure good cache behavior
for (int iBlock = blockStartRow; iBlock < blockEndRow; ++iBlock) {
final int iHeight = blockHeight(iBlock);
final int firstRow = iBlock * BLOCK_SIZE;
final int iStart = FastMath.max(row, firstRow);
final int iEnd = FastMath.min(endRow + 1, firstRow + iHeight);
for (int jBlock = blockStartColumn; jBlock < blockEndColumn; ++jBlock) {
final int jWidth = blockWidth(jBlock);
final int firstColumn = jBlock * BLOCK_SIZE;
final int jStart = FastMath.max(column, firstColumn);
final int jEnd = FastMath.min(endColumn + 1, firstColumn + jWidth);
final int jLength = jEnd - jStart;
// handle one block, row by row
final T[] block = blocks[iBlock * blockColumns + jBlock]; | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/FunctionUtils.java | 615 |
| org/apache/commons/math3/analysis/FunctionUtils.java | 738 |
public DerivativeStructure value(final DerivativeStructure[] t)
throws DimensionMismatchException, NumberIsTooLargeException {
// check parameters and orders limits
final int parameters = t[0].getFreeParameters();
final int order = t[0].getOrder();
final int n = t.length;
if (order > 1) {
throw new NumberIsTooLargeException(order, 1, true);
}
// check all elements in the array are consistent
for (int i = 0; i < n; ++i) {
if (t[i].getFreeParameters() != parameters) {
throw new DimensionMismatchException(t[i].getFreeParameters(), parameters);
}
if (t[i].getOrder() != order) {
throw new DimensionMismatchException(t[i].getOrder(), order);
}
}
// delegate computation to underlying function
final double[] point = new double[n];
for (int i = 0; i < n; ++i) {
point[i] = t[i].getValue();
}
final double value = f.value(point); | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/solvers/BracketingNthOrderBrentSolver.java | 294 |
| org/apache/commons/math3/dfp/BracketingNthOrderBrentSolverDFP.java | 339 |
if (Precision.equals(nextY, 0.0, 1)) {
// we have found an exact root, since it is not an approximation
// we don't need to bother about the allowed solutions setting
return nextX;
}
if ((nbPoints > 2) && (end - start != nbPoints)) {
// we have been forced to ignore some points to keep bracketing,
// they are probably too far from the root, drop them from now on
nbPoints = end - start;
System.arraycopy(x, start, x, 0, nbPoints);
System.arraycopy(y, start, y, 0, nbPoints);
signChangeIndex -= start;
} else if (nbPoints == x.length) {
// we have to drop one point in order to insert the new one
nbPoints--;
// keep the tightest bracketing interval as centered as possible
if (signChangeIndex >= (x.length + 1) / 2) {
// we drop the lowest point, we have to shift the arrays and the index
System.arraycopy(x, 1, x, 0, nbPoints);
System.arraycopy(y, 1, y, 0, nbPoints);
--signChangeIndex;
}
}
// insert the last computed point
//(by construction, we know it lies inside the tightest bracketing interval)
System.arraycopy(x, signChangeIndex, x, signChangeIndex + 1, nbPoints - signChangeIndex);
x[signChangeIndex] = nextX;
System.arraycopy(y, signChangeIndex, y, signChangeIndex + 1, nbPoints - signChangeIndex);
y[signChangeIndex] = nextY;
++nbPoints;
// update the bracketing interval
if (nextY * yA <= 0) { | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/BaseMultivariateOptimizer.java | 124 |
| org/apache/commons/math3/optimization/direct/BaseAbstractMultivariateOptimizer.java | 275 |
private void checkParameters() {
if (start != null) {
final int dim = start.length;
if (lowerBound != null) {
if (lowerBound.length != dim) {
throw new DimensionMismatchException(lowerBound.length, dim);
}
for (int i = 0; i < dim; i++) {
final double v = start[i];
final double lo = lowerBound[i];
if (v < lo) {
throw new NumberIsTooSmallException(v, lo, true);
}
}
}
if (upperBound != null) {
if (upperBound.length != dim) {
throw new DimensionMismatchException(upperBound.length, dim);
}
for (int i = 0; i < dim; i++) {
final double v = start[i];
final double hi = upperBound[i];
if (v > hi) {
throw new NumberIsTooLargeException(v, hi, true);
}
}
} | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/MathArrays.java | 849 |
| org/apache/commons/math3/util/MathArrays.java | 929 |
| org/apache/commons/math3/util/MathArrays.java | 1031 |
final double a2, final double b2) {
// the code below is split in many additions/subtractions that may
// appear redundant. However, they should NOT be simplified, as they
// use IEEE754 floating point arithmetic rounding properties.
// as an example, the expression "ca1 - (ca1 - a1)" is NOT the same as "a1"
// The variable naming conventions are that xyzHigh contains the most significant
// bits of xyz and xyzLow contains its least significant bits. So theoretically
// xyz is the sum xyzHigh + xyzLow, but in many cases below, this sum cannot
// be represented in only one double precision number so we preserve two numbers
// to hold it as long as we can, combining the high and low order bits together
// only at the end, after cancellation may have occurred on high order bits
// split a1 and b1 as two 26 bits numbers
final double ca1 = SPLIT_FACTOR * a1;
final double a1High = ca1 - (ca1 - a1);
final double a1Low = a1 - a1High;
final double cb1 = SPLIT_FACTOR * b1;
final double b1High = cb1 - (cb1 - b1);
final double b1Low = b1 - b1High;
// accurate multiplication a1 * b1
final double prod1High = a1 * b1;
final double prod1Low = a1Low * b1Low - (((prod1High - a1High * b1High) - a1Low * b1High) - a1High * b1Low);
// split a2 and b2 as two 26 bits numbers
final double ca2 = SPLIT_FACTOR * a2;
final double a2High = ca2 - (ca2 - a2);
final double a2Low = a2 - a2High;
final double cb2 = SPLIT_FACTOR * b2;
final double b2High = cb2 - (cb2 - b2);
final double b2Low = b2 - b2High;
// accurate multiplication a2 * b2
final double prod2High = a2 * b2;
final double prod2Low = a2Low * b2Low - (((prod2High - a2High * b2High) - a2Low * b2High) - a2High * b2Low);
// accurate addition a1 * b1 + a2 * b2
final double s12High = prod1High + prod2High; | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 29 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1535 |
private static final double[] EXP_INT_A = new double[] {
+0.0d,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN, | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1489 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 2995 |
+8.218407798110846E307d,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
};
/** Exponential evaluated at integer values,
* exp(x) = expIntTableA[x + EXP_INT_TABLE_MAX_INDEX] + expIntTableB[x+EXP_INT_TABLE_MAX_INDEX]
*/
private static final double[] EXP_INT_B = new double[] { | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/SimpleValueChecker.java | 64 |
| org/apache/commons/math3/optimization/SimpleValueChecker.java | 75 |
public SimpleValueChecker(final double relativeThreshold,
final double absoluteThreshold) {
super(relativeThreshold, absoluteThreshold);
maxIterationCount = ITERATION_CHECK_DISABLED;
}
/**
* Builds an instance with specified thresholds.
*
* In order to perform only relative checks, the absolute tolerance
* must be set to a negative value. In order to perform only absolute
* checks, the relative tolerance must be set to a negative value.
*
* @param relativeThreshold relative tolerance threshold
* @param absoluteThreshold absolute tolerance threshold
* @param maxIter Maximum iteration count.
* @throws NotStrictlyPositiveException if {@code maxIter <= 0}.
*
* @since 3.1
*/
public SimpleValueChecker(final double relativeThreshold,
final double absoluteThreshold,
final int maxIter) {
super(relativeThreshold, absoluteThreshold);
if (maxIter <= 0) {
throw new NotStrictlyPositiveException(maxIter);
}
maxIterationCount = maxIter;
}
/**
* 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 PointValuePair previous,
final PointValuePair current) {
if (maxIterationCount != ITERATION_CHECK_DISABLED) {
if (iteration >= maxIterationCount) {
return true;
}
}
final double p = previous.getValue();
final double c = current.getValue();
final double difference = FastMath.abs(p - c);
final double size = FastMath.max(FastMath.abs(p), FastMath.abs(c));
return difference <= size * getRelativeThreshold() ||
difference <= getAbsoluteThreshold();
}
} | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/univariate/SimpleUnivariateValueChecker.java | 66 |
| org/apache/commons/math3/optimization/univariate/SimpleUnivariateValueChecker.java | 78 |
public SimpleUnivariateValueChecker(final double relativeThreshold,
final double absoluteThreshold) {
super(relativeThreshold, absoluteThreshold);
maxIterationCount = ITERATION_CHECK_DISABLED;
}
/**
* Builds an instance with specified thresholds.
*
* In order to perform only relative checks, the absolute tolerance
* must be set to a negative value. In order to perform only absolute
* checks, the relative tolerance must be set to a negative value.
*
* @param relativeThreshold relative tolerance threshold
* @param absoluteThreshold absolute tolerance threshold
* @param maxIter Maximum iteration count.
* @throws NotStrictlyPositiveException if {@code maxIter <= 0}.
*
* @since 3.1
*/
public SimpleUnivariateValueChecker(final double relativeThreshold,
final double absoluteThreshold,
final int maxIter) {
super(relativeThreshold, absoluteThreshold);
if (maxIter <= 0) {
throw new NotStrictlyPositiveException(maxIter);
}
maxIterationCount = maxIter;
}
/**
* 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 UnivariatePointValuePair previous,
final UnivariatePointValuePair current) {
if (maxIterationCount != ITERATION_CHECK_DISABLED) {
if (iteration >= maxIterationCount) {
return true;
}
}
final double p = previous.getValue();
final double c = current.getValue();
final double difference = FastMath.abs(p - c);
final double size = FastMath.max(FastMath.abs(p), FastMath.abs(c));
return difference <= size * getRelativeThreshold() ||
difference <= getAbsoluteThreshold();
}
} | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 325 |
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 397 |
checkAdditionCompatible(m);
final BlockFieldMatrix<T> out = new BlockFieldMatrix<T>(getField(), rows, columns);
// perform addition block-wise, to ensure good cache behavior
int blockIndex = 0;
for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) {
for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) {
// perform addition on the current block
final T[] outBlock = out.blocks[blockIndex];
final T[] tBlock = blocks[blockIndex];
final int pStart = iBlock * BLOCK_SIZE;
final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows);
final int qStart = jBlock * BLOCK_SIZE;
final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns);
int k = 0;
for (int p = pStart; p < pEnd; ++p) {
for (int q = qStart; q < qEnd; ++q) {
outBlock[k] = tBlock[k].add(m.getEntry(p, q)); | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 1411 |
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 1441 |
| org/apache/commons/math3/linear/BlockRealMatrix.java | 1399 |
| org/apache/commons/math3/linear/BlockRealMatrix.java | 1429 |
checkSubMatrixIndex(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 T[] block = blocks[iBlock * blockColumns + jBlock]; | |
| File | Line |
|---|---|
| org/apache/commons/math3/random/Well19937a.java | 78 |
| org/apache/commons/math3/random/Well19937c.java | 78 |
public Well19937a(long seed) {
super(K, M1, M2, M3, seed);
}
/** {@inheritDoc} */
@Override
protected int next(final int bits) {
final int indexRm1 = iRm1[index];
final int indexRm2 = iRm2[index];
final int v0 = v[index];
final int vM1 = v[i1[index]];
final int vM2 = v[i2[index]];
final int vM3 = v[i3[index]];
final int z0 = (0x80000000 & v[indexRm1]) ^ (0x7FFFFFFF & v[indexRm2]);
final int z1 = (v0 ^ (v0 << 25)) ^ (vM1 ^ (vM1 >>> 27));
final int z2 = (vM2 >>> 9) ^ (vM3 ^ (vM3 >>> 1));
final int z3 = z1 ^ z2; | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 30 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1489 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1536 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 2995 |
+0.0d,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN, | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 31 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1489 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1536 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 2995 |
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN, | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 30 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1490 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1537 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 2996 |
+0.0d,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN, | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/nonlinear/scalar/gradient/NonLinearConjugateGradientOptimizer.java | 233 |
| org/apache/commons/math3/optimization/general/NonLinearConjugateGradientOptimizer.java | 188 |
final UnivariateFunction lsf = new LineSearchFunction(point, searchDirection);
final double uB = findUpperBound(lsf, 0, initialStep);
// XXX Last parameters is set to a value close to zero in order to
// work around the divergence problem in the "testCircleFitting"
// unit test (see MATH-439).
final double step = solver.solve(maxEval, lsf, 0, uB, 1e-15);
maxEval -= solver.getEvaluations(); // Subtract used up evaluations.
// Validate new point.
for (int i = 0; i < point.length; ++i) {
point[i] += step * searchDirection[i];
}
r = computeObjectiveGradient(point);
if (goal == GoalType.MINIMIZE) {
for (int i = 0; i < n; ++i) {
r[i] = -r[i];
}
}
// Compute beta.
final double deltaOld = delta;
final double[] newSteepestDescent = preconditioner.precondition(point, r);
delta = 0;
for (int i = 0; i < n; ++i) {
delta += r[i] * newSteepestDescent[i];
}
final double beta; | |
| File | Line |
|---|---|
| org/apache/commons/math3/genetics/CycleCrossover.java | 102 |
| org/apache/commons/math3/genetics/OrderedCrossover.java | 70 |
@SuppressWarnings("unchecked")
public ChromosomePair crossover(final Chromosome first, final Chromosome second)
throws DimensionMismatchException, MathIllegalArgumentException {
if (!(first instanceof AbstractListChromosome<?> && second instanceof AbstractListChromosome<?>)) {
throw new MathIllegalArgumentException(LocalizedFormats.INVALID_FIXED_LENGTH_CHROMOSOME);
}
return mate((AbstractListChromosome<T>) first, (AbstractListChromosome<T>) second);
}
/**
* Helper for {@link #crossover(Chromosome, Chromosome)}. Performs the actual crossover.
*
* @param first the first chromosome
* @param second the second chromosome
* @return the pair of new chromosomes that resulted from the crossover
* @throws DimensionMismatchException if the length of the two chromosomes is different
*/
protected ChromosomePair mate(final AbstractListChromosome<T> first, final AbstractListChromosome<T> second)
throws DimensionMismatchException {
final int length = first.getLength();
if (length != second.getLength()) {
throw new DimensionMismatchException(second.getLength(), length);
}
// array representations of the parents
final List<T> parent1Rep = first.getRepresentation();
final List<T> parent2Rep = second.getRepresentation();
// and of the children: do a crossover copy to simplify the later processing
final List<T> child1Rep = new ArrayList<T>(second.getRepresentation()); | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/BlockRealMatrix.java | 309 |
| org/apache/commons/math3/linear/BlockRealMatrix.java | 377 |
MatrixUtils.checkAdditionCompatible(this, m);
final BlockRealMatrix out = new BlockRealMatrix(rows, columns);
// perform addition block-wise, to ensure good cache behavior
int blockIndex = 0;
for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) {
for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) {
// perform addition on the current block
final double[] outBlock = out.blocks[blockIndex];
final double[] tBlock = blocks[blockIndex];
final int pStart = iBlock * BLOCK_SIZE;
final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows);
final int qStart = jBlock * BLOCK_SIZE;
final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns);
int k = 0;
for (int p = pStart; p < pEnd; ++p) {
for (int q = qStart; q < qEnd; ++q) {
outBlock[k] = tBlock[k] + m.getEntry(p, q); | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 32 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1489 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1536 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 2995 |
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN, | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 30 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1491 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1538 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 2997 |
+0.0d,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN, | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 1359 |
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 1383 |
public T walkInRowOrder(final FieldMatrixChangingVisitor<T> visitor) {
visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
for (int iBlock = 0; iBlock < blockRows; ++iBlock) {
final int pStart = iBlock * BLOCK_SIZE;
final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows);
for (int p = pStart; p < pEnd; ++p) {
for (int jBlock = 0; jBlock < blockColumns; ++jBlock) {
final int jWidth = blockWidth(jBlock);
final int qStart = jBlock * BLOCK_SIZE;
final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns);
final T[] block = blocks[iBlock * blockColumns + jBlock];
int k = (p - pStart) * jWidth;
for (int q = qStart; q < qEnd; ++q) { | |
| File | Line |
|---|---|
| org/apache/commons/math3/ode/nonstiff/AdamsBashforthIntegrator.java | 279 |
| org/apache/commons/math3/ode/nonstiff/AdamsMoultonIntegrator.java | 297 |
if (!isLastStep) {
// prepare next step
interpolator.storeTime(stepStart);
if (resetOccurred) {
// some events handler has triggered changes that
// invalidate the derivatives, we need to restart from scratch
start(stepStart, y, t);
interpolator.reinitialize(stepStart, stepSize, scaled, nordsieck);
}
// stepsize control for next step
final double factor = computeStepGrowShrinkFactor(error);
final double scaledH = stepSize * factor;
final double nextT = stepStart + scaledH;
final boolean nextIsLast = forward ? (nextT >= t) : (nextT <= t);
hNew = filterStep(scaledH, forward, nextIsLast);
final double filteredNextT = stepStart + hNew;
final boolean filteredNextIsLast = forward ? (filteredNextT >= t) : (filteredNextT <= t);
if (filteredNextIsLast) {
hNew = t - stepStart;
}
interpolator.rescale(hNew);
}
} while (!isLastStep);
// dispatch results
equations.setTime(stepStart);
equations.setCompleteState(y);
resetInternalState();
} | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/linear/LinearObjectiveFunction.java | 105 |
| org/apache/commons/math3/optimization/linear/LinearObjectiveFunction.java | 102 |
public double value(final RealVector point) {
return coefficients.dotProduct(point) + constantTerm;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other instanceof LinearObjectiveFunction) {
LinearObjectiveFunction rhs = (LinearObjectiveFunction) other;
return (constantTerm == rhs.constantTerm) && coefficients.equals(rhs.coefficients);
}
return false;
}
@Override
public int hashCode() {
return Double.valueOf(constantTerm).hashCode() ^ coefficients.hashCode();
}
/**
* Serialize the instance.
* @param oos stream where object should be written
* @throws IOException if object cannot be written to stream
*/
private void writeObject(ObjectOutputStream oos)
throws IOException {
oos.defaultWriteObject();
MatrixUtils.serializeRealVector(coefficients, oos);
}
/**
* Deserialize the instance.
* @param ois stream from which the object should be read
* @throws ClassNotFoundException if a class in the stream cannot be found
* @throws IOException if object cannot be read from the stream
*/
private void readObject(ObjectInputStream ois)
throws ClassNotFoundException, IOException {
ois.defaultReadObject();
MatrixUtils.deserializeRealVector(this, "coefficients", ois);
}
} | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/BlockRealMatrix.java | 1347 |
| org/apache/commons/math3/linear/BlockRealMatrix.java | 1371 |
public double walkInRowOrder(final RealMatrixChangingVisitor visitor) {
visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
for (int iBlock = 0; iBlock < blockRows; ++iBlock) {
final int pStart = iBlock * BLOCK_SIZE;
final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows);
for (int p = pStart; p < pEnd; ++p) {
for (int jBlock = 0; jBlock < blockColumns; ++jBlock) {
final int jWidth = blockWidth(jBlock);
final int qStart = jBlock * BLOCK_SIZE;
final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns);
final double[] block = blocks[iBlock * blockColumns + jBlock];
int k = (p - pStart) * jWidth;
for (int q = qStart; q < qEnd; ++q) { | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 1521 |
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 1551 |
| org/apache/commons/math3/linear/BlockRealMatrix.java | 1510 |
| org/apache/commons/math3/linear/BlockRealMatrix.java | 1541 |
checkSubMatrixIndex(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 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 T[] block = blocks[iBlock * blockColumns + jBlock]; | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 33 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1489 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1536 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 2995 |
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN, | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 30 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1492 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1539 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 2998 |
+0.0d,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 50 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 62 |
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 1232 |
| org/apache/commons/math3/linear/BlockRealMatrix.java | 1225 |
final T[] tBlock = blocks[jBlock * blockColumns + iBlock];
final int pStart = iBlock * BLOCK_SIZE;
final int pEnd = FastMath.min(pStart + BLOCK_SIZE, columns);
final int qStart = jBlock * BLOCK_SIZE;
final int qEnd = FastMath.min(qStart + BLOCK_SIZE, rows);
int k = 0;
for (int p = pStart; p < pEnd; ++p) {
final int lInc = pEnd - pStart;
int l = p - pStart;
for (int q = qStart; q < qEnd; ++q) {
outBlock[k] = tBlock[l];
++k;
l+= lInc;
}
}
// go to next block
++blockIndex;
}
}
return out;
}
/** {@inheritDoc} */
@Override
public int getRowDimension() {
return rows;
}
/** {@inheritDoc} */
@Override
public int getColumnDimension() {
return columns;
}
/** {@inheritDoc} */
@Override
public T[] operate(final T[] v) throws DimensionMismatchException { | |
| File | Line |
|---|---|
| org/apache/commons/math3/ode/nonstiff/DormandPrince853StepInterpolator.java | 377 |
| org/apache/commons/math3/ode/nonstiff/DormandPrince853StepInterpolator.java | 391 |
eta * (v[1][i] +
theta * (v[2][i] +
eta * (v[3][i] +
theta * (v[4][i] +
eta * (v[5][i] +
theta * (v[6][i])))))));
interpolatedDerivatives[i] = v[0][i] + dot1 * v[1][i] + dot2 * v[2][i] +
dot3 * v[3][i] + dot4 * v[4][i] +
dot5 * v[5][i] + dot6 * v[6][i];
}
} else { | |
| File | Line |
|---|---|
| org/apache/commons/math3/stat/regression/MillerUpdatingRegression.java | 971 |
| org/apache/commons/math3/stat/regression/MillerUpdatingRegression.java | 1081 |
int idx1 = 0;
int idx2;
int _i;
int _j;
for (int i = 0; i < beta.length; i++) {
_i = newIndices[i];
for (int j = 0; j <= i; j++, idx1++) {
_j = newIndices[j];
if (_i > _j) {
idx2 = _i * (_i + 1) / 2 + _j;
} else {
idx2 = _j * (_j + 1) / 2 + _i;
}
covNew[idx1] = cov[idx2];
}
}
return new RegressionResults(
betaNew, new double[][]{covNew}, true, this.nobs, rnk,
this.sumy, this.sumsqy, this.sserr, this.hasIntercept, false);
}
} | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 34 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1489 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1536 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 2995 |
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN, | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 30 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1493 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1540 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 2999 |
+0.0d,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN, | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/OpenIntToDoubleHashMap.java | 135 |
| org/apache/commons/math3/util/OpenIntToFieldHashMap.java | 148 |
System.arraycopy(source.values, 0, values, 0, length);
states = new byte[length];
System.arraycopy(source.states, 0, states, 0, length);
missingEntries = source.missingEntries;
size = source.size;
mask = source.mask;
count = source.count;
}
/**
* Compute the capacity needed for a given size.
* @param expectedSize expected size of the map
* @return capacity to use for the specified size
*/
private static int computeCapacity(final int expectedSize) {
if (expectedSize == 0) {
return 1;
}
final int capacity = (int) FastMath.ceil(expectedSize / LOAD_FACTOR);
final int powerOfTwo = Integer.highestOneBit(capacity);
if (powerOfTwo == capacity) {
return capacity;
}
return nextPowerOfTwo(capacity);
}
/**
* Find the smallest power of two greater than the input value
* @param i input value
* @return smallest power of two greater than the input value
*/
private static int nextPowerOfTwo(final int i) {
return Integer.highestOneBit(i) << 1;
}
/**
* Get the stored value associated with the given key
* @param key key associated with the data
* @return data associated with the key
*/
public double get(final int key) { | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/OpenIntToDoubleHashMap.java | 349 |
| org/apache/commons/math3/util/OpenIntToFieldHashMap.java | 362 |
public double remove(final int key) {
final int hash = hashOf(key);
int index = hash & mask;
if (containsKey(key, index)) {
return doRemove(index);
}
if (states[index] == FREE) {
return missingEntries;
}
int j = index;
for (int perturb = perturb(hash); states[index] != FREE; perturb >>= PERTURB_SHIFT) {
j = probe(perturb, j);
index = j & mask;
if (containsKey(key, index)) {
return doRemove(index);
}
}
return missingEntries;
}
/**
* Check if the tables contain an element associated with specified key
* at specified index.
* @param key key to check
* @param index index to check
* @return true if an element is associated with key at index
*/
private boolean containsKey(final int key, final int index) {
return (key != 0 || states[index] == FULL) && keys[index] == key;
}
/**
* Remove an element at specified index.
* @param index index of the element to remove
* @return removed value
*/
private double doRemove(int index) { | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 35 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1489 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1536 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 2995 |
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN, | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 30 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1494 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1541 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 3000 |
+0.0d,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN, | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/nonlinear/scalar/gradient/NonLinearConjugateGradientOptimizer.java | 202 |
| org/apache/commons/math3/optimization/general/NonLinearConjugateGradientOptimizer.java | 157 |
for (int i = 0; i < n; i++) {
r[i] = -r[i];
}
}
// Initial search direction.
double[] steepestDescent = preconditioner.precondition(point, r);
double[] searchDirection = steepestDescent.clone();
double delta = 0;
for (int i = 0; i < n; ++i) {
delta += r[i] * searchDirection[i];
}
PointValuePair current = null;
int iter = 0;
int maxEval = getMaxEvaluations();
while (true) {
++iter;
final double objective = computeObjectiveValue(point);
PointValuePair previous = current;
current = new PointValuePair(point, objective);
if (previous != null) {
if (checker.converged(iter, previous, current)) {
// We have found an optimum.
return current;
}
}
// Find the optimal step in the search direction.
final UnivariateFunction lsf = new LineSearchFunction(point, searchDirection); | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/nonlinear/scalar/gradient/NonLinearConjugateGradientOptimizer.java | 312 |
| org/apache/commons/math3/optimization/general/NonLinearConjugateGradientOptimizer.java | 238 |
}
}
}
/**
* Finds 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.
*/
private double findUpperBound(final UnivariateFunction 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);
}
/** Default identity preconditioner. */
public static class IdentityPreconditioner implements Preconditioner {
/** {@inheritDoc} */
public double[] precondition(double[] variables, double[] r) {
return r.clone();
}
}
/**
* Internal class for line search.
* <p>
* The function represented by this class is the dot product of
* the objective function gradient and the search direction. Its
* value is zero when the gradient is orthogonal to the search
* direction, i.e. when the objective function value is a local
* extremum along the search direction.
* </p>
*/
private class LineSearchFunction implements UnivariateFunction {
/** Current point. */
private final double[] currentPoint; | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 1467 |
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 1492 |
public T walkInOptimizedOrder(final FieldMatrixChangingVisitor<T> visitor) {
visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
int blockIndex = 0;
for (int iBlock = 0; iBlock < blockRows; ++iBlock) {
final int pStart = iBlock * BLOCK_SIZE;
final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows);
for (int jBlock = 0; jBlock < blockColumns; ++jBlock) {
final int qStart = jBlock * BLOCK_SIZE;
final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns);
final T[] block = blocks[blockIndex];
int k = 0;
for (int p = pStart; p < pEnd; ++p) {
for (int q = qStart; q < qEnd; ++q) { | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 36 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1489 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1536 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 2995 |
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN, | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 30 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1495 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1542 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 3001 |
+0.0d,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN, | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/BlockRealMatrix.java | 1455 |
| org/apache/commons/math3/linear/BlockRealMatrix.java | 1480 |
public double walkInOptimizedOrder(final RealMatrixChangingVisitor visitor) {
visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
int blockIndex = 0;
for (int iBlock = 0; iBlock < blockRows; ++iBlock) {
final int pStart = iBlock * BLOCK_SIZE;
final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows);
for (int jBlock = 0; jBlock < blockColumns; ++jBlock) {
final int qStart = jBlock * BLOCK_SIZE;
final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns);
final double[] block = blocks[blockIndex];
int k = 0;
for (int p = pStart; p < pEnd; ++p) {
for (int q = qStart; q < qEnd; ++q) { | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/univariate/MultiStartUnivariateOptimizer.java | 197 |
| org/apache/commons/math3/optimization/univariate/UnivariateMultiStartOptimizer.java | 172 |
sortPairs(getGoalType());
if (optima[0] == null) {
throw lastException; // Cannot be null if starts >= 1.
}
// Return the point with the best objective function value.
return optima[0];
}
/**
* Sort the optima from best to worst, followed by {@code null} elements.
*
* @param goal Goal type.
*/
private void sortPairs(final GoalType goal) {
Arrays.sort(optima, new Comparator<UnivariatePointValuePair>() {
public int compare(final UnivariatePointValuePair o1,
final UnivariatePointValuePair o2) {
if (o1 == null) {
return (o2 == null) ? 0 : 1;
} else if (o2 == null) {
return -1;
}
final double v1 = o1.getValue();
final double v2 = o2.getValue();
return (goal == GoalType.MINIMIZE) ?
Double.compare(v1, v2) : Double.compare(v2, v1);
}
});
}
} | |
| File | Line |
|---|---|
| org/apache/commons/math3/transform/FastFourierTransformer.java | 256 |
| org/apache/commons/math3/transform/FastFourierTransformer.java | 285 |
if (type == TransformType.INVERSE) {
for (int i0 = 0; i0 < n; i0 += 4) {
final int i1 = i0 + 1;
final int i2 = i0 + 2;
final int i3 = i0 + 3;
final double srcR0 = dataR[i0];
final double srcI0 = dataI[i0];
final double srcR1 = dataR[i2];
final double srcI1 = dataI[i2];
final double srcR2 = dataR[i1];
final double srcI2 = dataI[i1];
final double srcR3 = dataR[i3];
final double srcI3 = dataI[i3];
// 4-term DFT
// X_0 = x_0 + x_1 + x_2 + x_3
dataR[i0] = srcR0 + srcR1 + srcR2 + srcR3;
dataI[i0] = srcI0 + srcI1 + srcI2 + srcI3;
// X_1 = x_0 - x_2 + j * (x_3 - x_1)
dataR[i1] = srcR0 - srcR2 + (srcI3 - srcI1); | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 37 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1489 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1536 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 2995 |
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN, | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 30 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1496 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1543 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 3002 |
+0.0d,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/BicubicSplineInterpolatingFunction.java | 163 |
| org/apache/commons/math3/analysis/interpolation/BicubicSplineInterpolatingFunction.java | 258 |
final int i = searchIndex(x, xval);
if (i == -1) {
throw new OutOfRangeException(x, xval[0], xval[xval.length - 1]);
}
final int j = searchIndex(y, yval);
if (j == -1) {
throw new OutOfRangeException(y, yval[0], yval[yval.length - 1]);
}
final double xN = (x - xval[i]) / (xval[i + 1] - xval[i]);
final double yN = (y - yval[j]) / (yval[j + 1] - yval[j]);
return splines[i][j].value(xN, yN); | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 1562 |
| org/apache/commons/math3/linear/BlockRealMatrix.java | 1552 |
final T[] block = blocks[iBlock * blockColumns + jBlock];
for (int p = pStart; p < pEnd; ++p) {
int k = (p - p0) * jWidth + qStart - q0;
for (int q = qStart; q < qEnd; ++q) {
visitor.visit(p, q, block[k]);
++k;
}
}
}
}
return visitor.end();
}
/**
* Get the height of a block.
* @param blockRow row index (in block sense) of the block
* @return height (number of rows) of the block
*/
private int blockHeight(final int blockRow) {
return (blockRow == blockRows - 1) ? rows - blockRow * BLOCK_SIZE : BLOCK_SIZE;
}
/**
* Get the width of a block.
* @param blockColumn column index (in block sense) of the block
* @return width (number of columns) of the block
*/
private int blockWidth(final int blockColumn) {
return (blockColumn == blockColumns - 1) ? columns - blockColumn * BLOCK_SIZE : BLOCK_SIZE;
}
} | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 49 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 50 |
{ 2,-2,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 65 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 66 |
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,-2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/geometry/euclidean/threed/OutlineExtractor.java | 130 |
| org/apache/commons/math3/geometry/euclidean/threed/PolyhedronsSet.java | 156 |
projected = new PolygonsSet(new BSPTree<Euclidean2D>(Boolean.FALSE));
}
/** {@inheritDoc} */
public Order visitOrder(final BSPTree<Euclidean3D> node) {
return Order.MINUS_SUB_PLUS;
}
/** {@inheritDoc} */
public void visitInternalNode(final BSPTree<Euclidean3D> node) {
@SuppressWarnings("unchecked")
final BoundaryAttribute<Euclidean3D> attribute =
(BoundaryAttribute<Euclidean3D>) node.getAttribute();
if (attribute.getPlusOutside() != null) {
addContribution(attribute.getPlusOutside(), false);
}
if (attribute.getPlusInside() != null) {
addContribution(attribute.getPlusInside(), true);
}
}
/** {@inheritDoc} */
public void visitLeafNode(final BSPTree<Euclidean3D> node) {
}
/** Add he contribution of a boundary facet.
* @param facet boundary facet
* @param reversed if true, the facet has the inside on its plus side
*/
private void addContribution(final SubHyperplane<Euclidean3D> facet, final boolean reversed) { | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/linear/SimplexSolver.java | 141 |
| org/apache/commons/math3/optimization/linear/SimplexSolver.java | 143 |
if (getEvaluations() < getMaxEvaluations() / 2) {
Integer minRow = null;
int minIndex = tableau.getWidth();
final int varStart = tableau.getNumObjectiveFunctions();
final int varEnd = tableau.getWidth() - 1;
for (Integer row : minRatioPositions) {
for (int i = varStart; i < varEnd && !row.equals(minRow); i++) {
final Integer basicRow = tableau.getBasicRow(i);
if (basicRow != null && basicRow.equals(row)) {
if (i < minIndex) {
minIndex = i;
minRow = row;
}
}
}
}
return minRow;
}
}
return minRatioPositions.get(0);
}
/**
* Runs one iteration of the Simplex method on the given model.
*
* @param tableau Simple tableau for the problem.
* @throws TooManyIterationsException if the allowed number of iterations has been exhausted.
* @throws UnboundedSolutionException if the model is found not to have a bounded solution.
*/
protected void doIteration(final SimplexTableau tableau)
throws TooManyIterationsException, | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 38 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1489 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1536 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 2995 |
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN, | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 30 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1497 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1544 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 3003 |
+0.0d,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN, | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/OpenIntToDoubleHashMap.java | 541 |
| org/apache/commons/math3/util/OpenIntToFieldHashMap.java | 554 |
public double value()
throws ConcurrentModificationException, NoSuchElementException {
if (referenceCount != count) {
throw new ConcurrentModificationException();
}
if (current < 0) {
throw new NoSuchElementException();
}
return values[current];
}
/**
* Advance iterator one step further.
* @exception ConcurrentModificationException if the map is modified during iteration
* @exception NoSuchElementException if there is no element left in the map
*/
public void advance()
throws ConcurrentModificationException, NoSuchElementException {
if (referenceCount != count) {
throw new ConcurrentModificationException();
}
// advance on step
current = next;
// prepare next step
try {
while (states[++next] != FULL) { // NOPMD
// nothing to do
}
} catch (ArrayIndexOutOfBoundsException e) {
next = -2;
if (current < 0) {
throw new NoSuchElementException();
}
}
}
}
/**
* Read a serialized object.
* @param stream input stream
* @throws IOException if object cannot be read
* @throws ClassNotFoundException if the class corresponding
* to the serialized object cannot be found
*/
private void readObject(final ObjectInputStream stream)
throws IOException, ClassNotFoundException {
stream.defaultReadObject();
count = 0;
} | |
| File | Line |
|---|---|
| org/apache/commons/math3/ode/nonstiff/GillStepInterpolator.java | 117 |
| org/apache/commons/math3/ode/nonstiff/ThreeEighthesStepInterpolator.java | 110 |
final double coeff4 = s * (-3 * theta + fourTheta2);
for (int i = 0; i < interpolatedState.length; ++i) {
final double yDot1 = yDotK[0][i];
final double yDot2 = yDotK[1][i];
final double yDot3 = yDotK[2][i];
final double yDot4 = yDotK[3][i];
interpolatedState[i] =
previousState[i] + coeff1 * yDot1 + coeff2 * yDot2 + coeff3 * yDot3 + coeff4 * yDot4;
interpolatedDerivatives[i] =
coeffDot1 * yDot1 + coeffDot2 * yDot2 + coeffDot3 * yDot3 + coeffDot4 * yDot4;
}
} else {
final double s = oneMinusThetaH / 6.0; | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 49 |
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 62 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 65 |
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 39 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1489 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1536 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 2995 |
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN, | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 30 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1498 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1545 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 3004 |
+0.0d,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN, | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathCalc.java | 206 |
| org/apache/commons/math3/util/FastMathCalc.java | 251 |
static double slowCos(final double x, final double result[]) {
final double xs[] = new double[2];
final double ys[] = new double[2];
final double facts[] = new double[2];
final double as[] = new double[2];
split(x, xs);
ys[0] = ys[1] = 0.0;
for (int i = FACT.length-1; i >= 0; i--) {
splitMult(xs, ys, as);
ys[0] = as[0]; ys[1] = as[1];
if ( (i & 1) != 0) { // skip odd entries | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/MatrixUtils.java | 928 |
| org/apache/commons/math3/linear/MatrixUtils.java | 973 |
public static void solveLowerTriangularSystem(RealMatrix rm, RealVector b)
throws DimensionMismatchException, MathArithmeticException,
NonSquareMatrixException {
if ((rm == null) || (b == null) || ( rm.getRowDimension() != b.getDimension())) {
throw new DimensionMismatchException(
(rm == null) ? 0 : rm.getRowDimension(),
(b == null) ? 0 : b.getDimension());
}
if( rm.getColumnDimension() != rm.getRowDimension() ){
throw new NonSquareMatrixException(rm.getRowDimension(),
rm.getColumnDimension());
}
int rows = rm.getRowDimension();
for( int i = 0 ; i < rows ; i++ ){ | |
| File | Line |
|---|---|
| org/apache/commons/math3/ode/nonstiff/GillStepInterpolator.java | 134 |
| org/apache/commons/math3/ode/nonstiff/ThreeEighthesStepInterpolator.java | 128 |
final double coeff4 = s * (1 + theta + fourTheta2);
for (int i = 0; i < interpolatedState.length; ++i) {
final double yDot1 = yDotK[0][i];
final double yDot2 = yDotK[1][i];
final double yDot3 = yDotK[2][i];
final double yDot4 = yDotK[3][i];
interpolatedState[i] =
currentState[i] - coeff1 * yDot1 - coeff2 * yDot2 - coeff3 * yDot3 - coeff4 * yDot4;
interpolatedDerivatives[i] =
coeffDot1 * yDot1 + coeffDot2 * yDot2 + coeffDot3 * yDot3 + coeffDot4 * yDot4;
}
}
}
} | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 40 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1489 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1536 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 2995 |
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN, | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 30 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1499 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1546 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 3005 |
+0.0d,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN, | |
| File | Line |
|---|---|
| org/apache/commons/math3/stat/descriptive/MultivariateSummaryStatistics.java | 385 |
| org/apache/commons/math3/stat/descriptive/SummaryStatistics.java | 385 |
stat.getCovariance().equals( getCovariance());
}
/**
* Returns hash code based on values of statistics
*
* @return hash code
*/
@Override
public int hashCode() {
int result = 31 + MathUtils.hash(getGeometricMean());
result = result * 31 + MathUtils.hash(getGeometricMean());
result = result * 31 + MathUtils.hash(getMax());
result = result * 31 + MathUtils.hash(getMean());
result = result * 31 + MathUtils.hash(getMin());
result = result * 31 + MathUtils.hash(getN());
result = result * 31 + MathUtils.hash(getSum());
result = result * 31 + MathUtils.hash(getSumSq()); | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathCalc.java | 219 |
| org/apache/commons/math3/util/FastMathCalc.java | 263 |
if ( (i & 1) != 0) { // skip odd entries
continue;
}
split(FACT[i], as);
splitReciprocal(as, facts);
if ( (i & 2) != 0 ) { // alternate terms are negative
facts[0] = -facts[0];
facts[1] = -facts[1];
}
splitAdd(ys, facts, as);
ys[0] = as[0]; ys[1] = as[1];
}
if (result != null) {
result[0] = ys[0];
result[1] = ys[1];
}
return ys[0] + ys[1];
}
/**
* For x between 0 and pi/4 compute sine using Taylor expansion:
* sin(x) = x - x^3/3! + x^5/5! - x^7/7! ...
* @param x number from which sine is requested
* @param result placeholder where to put the result in extended precision
* (may be null)
* @return sin(x)
*/
static double slowSin(final double x, final double result[]) { | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/BlockRealMatrix.java | 1395 |
| org/apache/commons/math3/linear/BlockRealMatrix.java | 1505 |
public double walkInRowOrder(final RealMatrixChangingVisitor visitor,
final int startRow, final int endRow,
final int startColumn, final int endColumn)
throws OutOfRangeException, NumberIsTooSmallException {
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) { | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/BlockRealMatrix.java | 1425 |
| org/apache/commons/math3/linear/BlockRealMatrix.java | 1536 |
public double walkInRowOrder(final RealMatrixPreservingVisitor visitor,
final int startRow, final int endRow,
final int startColumn, final int endColumn)
throws OutOfRangeException, NumberIsTooSmallException {
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) { | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 41 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1489 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1536 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 2995 |
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN, | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 30 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1500 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1547 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 3006 |
+0.0d,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN, | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 1407 |
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 1517 |
public T walkInRowOrder(final FieldMatrixChangingVisitor<T> visitor,
final int startRow, final int endRow,
final int startColumn, final int endColumn)
throws OutOfRangeException, NumberIsTooSmallException {
checkSubMatrixIndex(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) { | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 1437 |
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 1547 |
public T walkInRowOrder(final FieldMatrixPreservingVisitor<T> visitor,
final int startRow, final int endRow,
final int startColumn, final int endColumn)
throws OutOfRangeException, NumberIsTooSmallException {
checkSubMatrixIndex(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) { | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathCalc.java | 206 |
| org/apache/commons/math3/util/FastMathCalc.java | 251 |
| org/apache/commons/math3/util/FastMathCalc.java | 295 |
static double slowCos(final double x, final double result[]) {
final double xs[] = new double[2];
final double ys[] = new double[2];
final double facts[] = new double[2];
final double as[] = new double[2];
split(x, xs);
ys[0] = ys[1] = 0.0;
for (int i = FACT.length-1; i >= 0; i--) {
splitMult(xs, ys, as);
ys[0] = as[0]; ys[1] = as[1]; | |
| File | Line |
|---|---|
| org/apache/commons/math3/stat/inference/ChiSquareTest.java | 81 |
| org/apache/commons/math3/stat/inference/GTest.java | 77 |
public double chiSquare(final double[] expected, final long[] observed)
throws NotPositiveException, NotStrictlyPositiveException,
DimensionMismatchException {
if (expected.length < 2) {
throw new DimensionMismatchException(expected.length, 2);
}
if (expected.length != observed.length) {
throw new DimensionMismatchException(expected.length, observed.length);
}
MathArrays.checkPositive(expected);
MathArrays.checkNonNegative(observed);
double sumExpected = 0d;
double sumObserved = 0d;
for (int i = 0; i < observed.length; i++) {
sumExpected += expected[i];
sumObserved += observed[i];
}
double ratio = 1.0d; | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 52 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 64 |
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-3,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-2,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 79 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 82 |
{ 0,0,0,0,0,0,0,0,-3,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-2,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 849 |
| org/apache/commons/math3/linear/BlockRealMatrix.java | 855 |
final T[] block = blocks[iBlock * blockColumns + jBlock];
final int available = outBlock.length - outIndex;
if (jWidth > available) {
System.arraycopy(block, iRow * jWidth, outBlock, outIndex, available);
outBlock = out.blocks[++outBlockIndex];
System.arraycopy(block, iRow * jWidth, outBlock, 0, jWidth - available);
outIndex = jWidth - available;
} else {
System.arraycopy(block, iRow * jWidth, outBlock, outIndex, jWidth);
outIndex += jWidth;
}
}
return out;
}
/** {@inheritDoc} */
@Override
public void setRowMatrix(final int row, final FieldMatrix<T> matrix) | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/BlockRealMatrix.java | 1425 |
| org/apache/commons/math3/linear/BlockRealMatrix.java | 1505 |
public double walkInRowOrder(final RealMatrixPreservingVisitor visitor,
final int startRow, final int endRow,
final int startColumn, final int endColumn)
throws OutOfRangeException, NumberIsTooSmallException {
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) { | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/BlockRealMatrix.java | 1395 |
| org/apache/commons/math3/linear/BlockRealMatrix.java | 1536 |
public double walkInRowOrder(final RealMatrixChangingVisitor visitor,
final int startRow, final int endRow,
final int startColumn, final int endColumn)
throws OutOfRangeException, NumberIsTooSmallException {
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) { | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 47 |
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 50 |
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 49 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 62 |
{ 2,-2,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 62 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 66 |
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 65 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 66 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 67 |
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,-2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 1437 |
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 1517 |
public T walkInRowOrder(final FieldMatrixPreservingVisitor<T> visitor,
final int startRow, final int endRow,
final int startColumn, final int endColumn)
throws OutOfRangeException, NumberIsTooSmallException {
checkSubMatrixIndex(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) { | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 1407 |
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 1547 |
public T walkInRowOrder(final FieldMatrixChangingVisitor<T> visitor,
final int startRow, final int endRow,
final int startColumn, final int endColumn)
throws OutOfRangeException, NumberIsTooSmallException {
checkSubMatrixIndex(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) { | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 1359 |
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 1383 |
| org/apache/commons/math3/linear/BlockRealMatrix.java | 1347 |
| org/apache/commons/math3/linear/BlockRealMatrix.java | 1371 |
public T walkInRowOrder(final FieldMatrixChangingVisitor<T> visitor) {
visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
for (int iBlock = 0; iBlock < blockRows; ++iBlock) {
final int pStart = iBlock * BLOCK_SIZE;
final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows);
for (int p = pStart; p < pEnd; ++p) {
for (int jBlock = 0; jBlock < blockColumns; ++jBlock) {
final int jWidth = blockWidth(jBlock);
final int qStart = jBlock * BLOCK_SIZE;
final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns);
final T[] block = blocks[iBlock * blockColumns + jBlock]; | |
| File | Line |
|---|---|
| org/apache/commons/math3/geometry/VectorFormat.java | 112 |
| org/apache/commons/math3/linear/RealVectorFormat.java | 105 |
protected VectorFormat(final String prefix, final String suffix,
final String separator, final NumberFormat format) {
this.prefix = prefix;
this.suffix = suffix;
this.separator = separator;
trimmedPrefix = prefix.trim();
trimmedSuffix = suffix.trim();
trimmedSeparator = separator.trim();
this.format = format;
}
/**
* Get the set of locales for which point/vector formats are available.
* <p>This is the same set as the {@link NumberFormat} set.</p>
* @return available point/vector format locales.
*/
public static Locale[] getAvailableLocales() {
return NumberFormat.getAvailableLocales();
}
/**
* Get the format prefix.
* @return format prefix.
*/
public String getPrefix() {
return prefix;
}
/**
* Get the format suffix.
* @return format suffix.
*/
public String getSuffix() {
return suffix;
}
/**
* Get the format separator between components.
* @return format separator.
*/
public String getSeparator() {
return separator;
}
/**
* Get the components format.
* @return components format.
*/
public NumberFormat getFormat() {
return format;
}
/**
* Formats a {@link Vector} object to produce a string.
* @param vector the object to format.
* @return a formatted string.
*/
public String format(Vector<S> vector) { | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMath.java | 418 |
| org/apache/commons/math3/util/FastMath.java | 497 |
exp(x, 0.0, hiPrec);
double ya = hiPrec[0] + hiPrec[1];
double yb = -(ya - hiPrec[0] - hiPrec[1]);
double temp = ya * HEX_40000000;
double yaa = ya + temp - temp;
double yab = ya - yaa;
// recip = 1/y
double recip = 1.0/ya;
temp = recip * HEX_40000000;
double recipa = recip + temp - temp;
double recipb = recip - recipa;
// Correct for rounding in division
recipb += (1.0 - yaa*recipa - yaa*recipb - yab*recipa - yab*recipb) * recip;
// Account for yb
recipb += -yb * recip * recip; | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMath.java | 2299 |
| org/apache/commons/math3/util/FastMath.java | 2364 |
}
if (xa != xa || xa == Double.POSITIVE_INFINITY) {
return Double.NaN;
}
/* Perform any argument reduction */
double xb = 0;
if (xa > 3294198.0) {
// PI * (2**20)
// Argument too big for CodyWaite reduction. Must use
// PayneHanek.
double reduceResults[] = new double[3];
reducePayneHanek(xa, reduceResults);
quadrant = ((int) reduceResults[0]) & 3;
xa = reduceResults[1];
xb = reduceResults[2];
} else if (xa > 1.5707963267948966) {
final CodyWaite cw = new CodyWaite(xa);
quadrant = cw.getK() & 3;
xa = cw.getRemA();
xb = cw.getRemB();
} | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 42 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1489 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1536 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 2995 |
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN, | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 30 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1501 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1548 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 3007 |
+0.0d,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 48 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 49 |
{ -3,3,0,0,0,0,0,0,-2,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 2,-2,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 53 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 65 |
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,-2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 67 |
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 95 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 98 |
{ 0,0,0,0,0,0,0,0,2,0,0,0,-2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/integration/IterativeLegendreGaussIntegrator.java | 94 |
| org/apache/commons/math3/analysis/integration/LegendreGaussIntegrator.java | 191 |
public IterativeLegendreGaussIntegrator(final int n,
final int minimalIterationCount,
final int maximalIterationCount) {
this(n, DEFAULT_RELATIVE_ACCURACY, DEFAULT_ABSOLUTE_ACCURACY,
minimalIterationCount, maximalIterationCount);
}
/** {@inheritDoc} */
@Override
protected double doIntegrate()
throws TooManyEvaluationsException, MaxCountExceededException {
// Compute first estimate with a single step.
double oldt = stage(1);
int n = 2;
while (true) {
// Improve integral with a larger number of steps.
final double t = stage(n);
// Estimate the error.
final double delta = FastMath.abs(t - oldt);
final double limit =
FastMath.max(getAbsoluteAccuracy(),
getRelativeAccuracy() * (FastMath.abs(oldt) + FastMath.abs(t)) * 0.5);
// check convergence
if (iterations.getCount() + 1 >= getMinimalIterationCount() && | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 47 |
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 47 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 48 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 49 |
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 47 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 50 |
{ 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 51 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 62 |
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 50 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 62 |
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 51 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 63 |
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 50 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 65 |
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 63 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 66 |
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 67 |
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 67 |
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 66 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 67 |
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 65 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 66 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 67 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 68 |
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,-2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 47 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 49 |
{ 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 54 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 58 |
{ -3,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,-2,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,-3,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-2,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 47 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 67 |
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 70 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 74 |
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-3,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-2,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-3,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,-2,0,-1,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 666 |
| org/apache/commons/math3/linear/BlockRealMatrix.java | 672 |
new BlockFieldMatrix<T>(getField(), endRow - startRow + 1, endColumn - startColumn + 1);
// compute blocks shifts
final int blockStartRow = startRow / BLOCK_SIZE;
final int rowsShift = startRow % BLOCK_SIZE;
final int blockStartColumn = startColumn / BLOCK_SIZE;
final int columnsShift = startColumn % BLOCK_SIZE;
// perform extraction block-wise, to ensure good cache behavior
int pBlock = blockStartRow;
for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) {
final int iHeight = out.blockHeight(iBlock);
int qBlock = blockStartColumn;
for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) {
final int jWidth = out.blockWidth(jBlock);
// handle one block of the output matrix
final int outIndex = iBlock * out.blockColumns + jBlock;
final T[] outBlock = out.blocks[outIndex]; | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 47 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 48 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 49 |
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 49 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 50 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 54 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 58 |
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 47 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 67 |
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 47 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 67 |
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 62 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 65 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 66 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 70 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 74 |
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 43 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1489 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1536 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 2995 |
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN, | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 30 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1502 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1549 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 3008 |
+0.0d,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 47 |
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 47 |
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 48 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 49 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 67 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 67 |
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 163 |
| org/apache/commons/math3/linear/BlockRealMatrix.java | 160 |
} else {
// reference existing array
blocks = blockData;
}
int index = 0;
for (int iBlock = 0; iBlock < blockRows; ++iBlock) {
final int iHeight = blockHeight(iBlock);
for (int jBlock = 0; jBlock < blockColumns; ++jBlock, ++index) {
if (blockData[index].length != iHeight * blockWidth(jBlock)) {
throw new DimensionMismatchException(blockData[index].length,
iHeight * blockWidth(jBlock));
}
if (copyArray) {
blocks[index] = blockData[index].clone();
}
}
}
}
/**
* Convert a data array from raw layout to blocks layout.
* <p>
* Raw layout is the straightforward layout where element at row i and
* column j is in array element <code>rawData[i][j]</code>. Blocks layout
* is the layout used in {@link BlockFieldMatrix} instances, where the matrix
* is split in square blocks (except at right and bottom side where blocks may
* be rectangular to fit matrix size) and each block is stored in a flattened
* one-dimensional array.
* </p>
* <p>
* This method creates an array in blocks layout from an input array in raw layout.
* It can be used to provide the array argument of the {@link
* #BlockFieldMatrix(int, int, FieldElement[][], boolean)}
* constructor.
* </p>
* @param <T> Type of the field elements.
* @param rawData Data array in raw layout.
* @return a new data array containing the same entries but in blocks layout
* @throws DimensionMismatchException if {@code rawData} is not rectangular
* (not all rows have the same length).
* @see #createBlocksLayout(Field, int, int)
* @see #BlockFieldMatrix(int, int, FieldElement[][], boolean)
*/
public static <T extends FieldElement<T>> T[][] toBlocksLayout(final T[][] rawData) | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/linear/SimplexSolver.java | 174 |
| org/apache/commons/math3/optimization/linear/SimplexSolver.java | 174 |
incrementIterationCount();
Integer pivotCol = getPivotColumn(tableau);
Integer pivotRow = getPivotRow(tableau, pivotCol);
if (pivotRow == null) {
throw new UnboundedSolutionException();
}
// set the pivot element to 1
double pivotVal = tableau.getEntry(pivotRow, pivotCol);
tableau.divideRow(pivotRow, pivotVal);
// set the rest of the pivot column to 0
for (int i = 0; i < tableau.getHeight(); i++) {
if (i != pivotRow) {
final double multiplier = tableau.getEntry(i, pivotCol);
tableau.subtractRow(i, pivotRow, multiplier);
}
}
}
/**
* Solves Phase 1 of the Simplex method.
*
* @param tableau Simple tableau for the problem.
* @throws TooManyIterationsException if the allowed number of iterations has been exhausted.
* @throws UnboundedSolutionException if the model is found not to have a bounded solution.
* @throws NoFeasibleSolutionException if there is no feasible solution?
*/
protected void solvePhase1(final SimplexTableau tableau)
throws TooManyIterationsException, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 50 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 61 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 62 |
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 47 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 48 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 49 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 67 |
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 47 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 48 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 49 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 67 |
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 47 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 48 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 49 |
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 47 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 48 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 49 |
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMath.java | 629 |
| org/apache/commons/math3/util/FastMath.java | 668 |
temp = da + yb;
db += -(temp - da - yb);
da = temp;
temp = da * HEX_40000000;
double daa = da + temp - temp;
double dab = da - daa;
// ratio = na/da
double ratio = na/da;
temp = ratio * HEX_40000000;
double ratioa = ratio + temp - temp;
double ratiob = ratio - ratioa;
// Correct for rounding in division
ratiob += (na - daa*ratioa - daa*ratiob - dab*ratioa - dab*ratiob) / da;
// Account for nb
ratiob += nb / da;
// Account for db
ratiob += -db * na / da / da;
result = ratioa + ratiob;
} | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 47 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 48 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 49 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 67 |
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 47 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 48 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 49 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 67 |
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 225 |
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 274 |
final T[][] blocks = buildArray(field, blockRows * blockColumns, -1);
int blockIndex = 0;
for (int iBlock = 0; iBlock < blockRows; ++iBlock) {
final int pStart = iBlock * BLOCK_SIZE;
final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows);
final int iHeight = pEnd - pStart;
for (int jBlock = 0; jBlock < blockColumns; ++jBlock) {
final int qStart = jBlock * BLOCK_SIZE;
final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns);
final int jWidth = qEnd - qStart; | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 44 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1489 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1536 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 2995 |
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN, | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 30 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1503 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1550 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 3009 |
+0.0d,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 47 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 48 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 49 |
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 47 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 48 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 49 |
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 906 |
| org/apache/commons/math3/linear/BlockRealMatrix.java | 912 |
final T[] block = blocks[iBlock * blockColumns + jBlock];
final int available = mBlock.length - mIndex;
if (jWidth > available) {
System.arraycopy(mBlock, mIndex, block, iRow * jWidth, available);
mBlock = matrix.blocks[++mBlockIndex];
System.arraycopy(mBlock, 0, block, iRow * jWidth, jWidth - available);
mIndex = jWidth - available;
} else {
System.arraycopy(mBlock, mIndex, block, iRow * jWidth, jWidth);
mIndex += jWidth;
}
}
}
/** {@inheritDoc} */
@Override
public FieldMatrix<T> getColumnMatrix(final int column) | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 47 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 48 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 49 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 67 |
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 47 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 48 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 49 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 67 |
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/Array2DRowFieldMatrix.java | 231 |
| org/apache/commons/math3/linear/Array2DRowFieldMatrix.java | 259 |
checkAdditionCompatible(m);
final int rowCount = getRowDimension();
final int columnCount = getColumnDimension();
final T[][] outData = buildArray(getField(), rowCount, columnCount);
for (int row = 0; row < rowCount; row++) {
final T[] dataRow = data[row];
final T[] mRow = m.data[row];
final T[] outDataRow = outData[row];
for (int col = 0; col < columnCount; col++) {
outDataRow[col] = dataRow[col].add(mRow[col]); | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/BlockRealMatrix.java | 217 |
| org/apache/commons/math3/linear/BlockRealMatrix.java | 261 |
final double[][] blocks = new double[blockRows * blockColumns][];
int blockIndex = 0;
for (int iBlock = 0; iBlock < blockRows; ++iBlock) {
final int pStart = iBlock * BLOCK_SIZE;
final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows);
final int iHeight = pEnd - pStart;
for (int jBlock = 0; jBlock < blockColumns; ++jBlock) {
final int qStart = jBlock * BLOCK_SIZE;
final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns);
final int jWidth = qEnd - qStart; | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 47 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 48 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 49 |
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 47 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 48 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 49 |
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/Array2DRowRealMatrix.java | 165 |
| org/apache/commons/math3/linear/Array2DRowRealMatrix.java | 192 |
MatrixUtils.checkAdditionCompatible(this, m);
final int rowCount = getRowDimension();
final int columnCount = getColumnDimension();
final double[][] outData = new double[rowCount][columnCount];
for (int row = 0; row < rowCount; row++) {
final double[] dataRow = data[row];
final double[] mRow = m.data[row];
final double[] outDataRow = outData[row];
for (int col = 0; col < columnCount; col++) {
outDataRow[col] = dataRow[col] + mRow[col]; | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 47 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 48 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 49 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 67 |
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 47 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 48 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 49 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 67 |
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/optim/nonlinear/vector/jacobian/LevenbergMarquardtOptimizer.java | 482 |
| org/apache/commons/math3/optimization/general/LevenbergMarquardtOptimizer.java | 488 |
setCost(currentCost);
return current;
}
}
} else {
// failed iteration, reset the previous values
currentCost = previousCost;
for (int j = 0; j < solvedCols; ++j) {
int pj = permutation[j];
currentPoint[pj] = oldX[pj];
}
tmpVec = weightedResidual;
weightedResidual = oldRes;
oldRes = tmpVec;
tmpVec = currentObjective;
currentObjective = oldObj;
oldObj = tmpVec;
// Reset "current" to previous values.
current = new PointVectorValuePair(currentPoint, currentObjective);
}
// Default convergence criteria.
if ((FastMath.abs(actRed) <= costRelativeTolerance &&
preRed <= costRelativeTolerance &&
ratio <= 2.0) ||
delta <= parRelativeTolerance * xNorm) {
setCost(currentCost); | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 45 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1489 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1536 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 2995 |
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN, | |
| File | Line |
|---|---|
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 30 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1504 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 1551 |
| org/apache/commons/math3/util/FastMathLiteralArrays.java | 3010 |
+0.0d,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN,
Double.NaN, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 47 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 48 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 49 |
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 46 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 47 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 48 |
| org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.java | 49 |
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/ArrayFieldVector.java | 355 |
| org/apache/commons/math3/linear/ArrayFieldVector.java | 380 |
public ArrayFieldVector(T[] v1, T[] v2)
throws NullArgumentException, ZeroException {
if (v1 == null || v2 == null) {
throw new NullArgumentException();
}
if (v1.length + v2.length == 0) {
throw new ZeroException(LocalizedFormats.VECTOR_MUST_HAVE_AT_LEAST_ONE_ELEMENT);
}
data = buildArray(v1.length + v2.length);
System.arraycopy(v1, 0, data, 0, v1.length);
System.arraycopy(v2, 0, data, v1.length, v2.length); | |
| File | Line |
|---|---|
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 371 |
| org/apache/commons/math3/linear/BlockFieldMatrix.java | 441 |
checkAdditionCompatible(m);
final BlockFieldMatrix<T> out = new BlockFieldMatrix<T>(getField(), rows, columns);
// perform addition block-wise, to ensure good cache behavior
for (int blockIndex = 0; blockIndex < out.blocks.length; ++blockIndex) {
final T[] outBlock = out.blocks[blockIndex];
final T[] tBlock = blocks[blockIndex];
final T[] mBlock = m.blocks[blockIndex];
for (int k = 0; k < outBlock.length; ++k) {
outBlock[k] = tBlock[k].add(mBlock[k]); | |