Skip to content

Commit

Permalink
Merge 495a8a2 into e29fd60
Browse files Browse the repository at this point in the history
  • Loading branch information
arturobernalg committed Apr 13, 2021
2 parents e29fd60 + 495a8a2 commit 3bfe940
Show file tree
Hide file tree
Showing 17 changed files with 47 additions and 75 deletions.
Expand Up @@ -180,8 +180,7 @@ public void addInPlace(final SparseGradient a) {
/** {@inheritDoc} */
@Override
public SparseGradient add(final double c) {
final SparseGradient out = new SparseGradient(value + c, derivatives);
return out;
return new SparseGradient(value + c, derivatives);
}

/** {@inheritDoc} */
Expand Down
Expand Up @@ -89,9 +89,8 @@ public double integrate(UnivariateFunction f) {
final double w = getWeight(iMax);

final double y = w * f.value(0d) - c;
final double t = s + y;

s = t;
s = s + y;
}

return s;
Expand Down
Expand Up @@ -142,9 +142,7 @@ public double value(double x,

final PolynomialSplineFunction spline = interpolator.interpolate(yArray, interpArray);

double returnValue = spline.value(y);

return returnValue;
return spline.value(y);
}

/**
Expand Down
Expand Up @@ -50,8 +50,7 @@ public Chromosome mutate(Chromosome original) throws MathIllegalArgumentExceptio
// and change it
newRepr.set(geneIndex, origChrom.getRepresentation().get(geneIndex) == 0 ? 1 : 0);

Chromosome newChrom = origChrom.newFixedLengthChromosome(newRepr);
return newChrom;
return origChrom.newFixedLengthChromosome(newRepr);
}

}
Expand Up @@ -159,11 +159,9 @@ protected boolean isSame(final Chromosome another) {

// two different representations can still encode the same permutation
// the ordering is what counts
List<Integer> thisPerm = this.baseSeqPermutation;
List<Integer> anotherPerm = anotherRk.baseSeqPermutation;

for (int i=0; i<getLength(); i++) {
if (thisPerm.get(i) != anotherPerm.get(i)) {
if (this.baseSeqPermutation.get(i) != anotherRk.baseSeqPermutation.get(i)) {
return false;
}
}
Expand Down
Expand Up @@ -162,9 +162,8 @@ public RealVector solveInPlace(final RealLinearOperator a,
// p and x are constructed as copies of x0, since presumably, the type
// of x is optimized for the calculation of the matrix-vector product
// A.x.
final RealVector x = x0;
final RealVector xro = RealVector.unmodifiableRealVector(x);
final RealVector p = x.copy();
final RealVector xro = RealVector.unmodifiableRealVector(x0);
final RealVector p = x0.copy();
RealVector q = a.operate(p);

final RealVector r = b.combine(1, -1, q);
Expand All @@ -182,7 +181,7 @@ public RealVector solveInPlace(final RealLinearOperator a,
manager.fireInitializationEvent(evt);
if (rnorm <= rmax) {
manager.fireTerminationEvent(evt);
return x;
return x0;
}
double rhoPrev = 0.;
while (true) {
Expand Down Expand Up @@ -218,7 +217,7 @@ public RealVector solveInPlace(final RealLinearOperator a,
throw e;
}
final double alpha = rhoNext / pq;
x.combineToSelf(1., alpha, p);
x0.combineToSelf(1., alpha, p);
r.combineToSelf(1., -alpha, q);
rhoPrev = rhoNext;
rnorm = r.getNorm();
Expand All @@ -227,7 +226,7 @@ public RealVector solveInPlace(final RealLinearOperator a,
manager.fireIterationPerformedEvent(evt);
if (rnorm <= rmax) {
manager.fireTerminationEvent(evt);
return x;
return x0;
}
}
}
Expand Down
Expand Up @@ -61,10 +61,8 @@ public class OpenMapRealMatrix extends AbstractRealMatrix
public OpenMapRealMatrix(int rowDimension, int columnDimension)
throws NotStrictlyPositiveException, NumberIsTooLargeException {
super(rowDimension, columnDimension);
long lRow = rowDimension;
long lCol = columnDimension;
if (lRow * lCol >= Integer.MAX_VALUE) {
throw new NumberIsTooLargeException(lRow * lCol, Integer.MAX_VALUE, false);
if ((long) rowDimension * (long) columnDimension >= Integer.MAX_VALUE) {
throw new NumberIsTooLargeException((long) rowDimension * (long) columnDimension, Integer.MAX_VALUE, false);
}
this.rows = rowDimension;
this.columns = columnDimension;
Expand Down
Expand Up @@ -63,16 +63,14 @@ public QuasiSigmoidDecayFunction(double initValue,
throw new NotStrictlyPositiveException(numCall);
}

final double k = initValue;
final double m = numCall;
final double b = 4 * slope / initValue;
final double q = 1;
final double a = 0;
final double n = 1;
sigmoid = new Logistic(k, m, b, q, a, n);
sigmoid = new Logistic(initValue, (double) numCall, b, q, a, n);

final double y0 = sigmoid.value(0);
scale = k / y0;
scale = initValue / y0;
}

/**
Expand Down
Expand Up @@ -371,13 +371,12 @@ public void computeDerivatives(final double t, final double[] y, final double[]
final double[] dFdYi = dFdY[i];
for (int j = 0; j < stateDim; ++j) {
double s = 0;
final int startIndex = j;
int zIndex = startIndex;
int zIndex = j;
for (int l = 0; l < stateDim; ++l) {
s += dFdYi[l] * z[zIndex];
zIndex += stateDim;
}
zDot[startIndex + i * stateDim] = s;
zDot[j + i * stateDim] = s;
}
}

Expand Down
Expand Up @@ -136,28 +136,27 @@ protected FieldODEStateAndDerivative<T> computeInterpolatedStateAndDerivatives(f
final T[] interpolatedState;
final T[] interpolatedDerivatives;
if (getGlobalPreviousState() != null && theta.getReal() <= 0.5) {
final T f1 = thetaH;
final T f2 = f1.multiply(eta);
final T f2 = thetaH.multiply(eta);
final T f3 = f2.multiply(theta);
final T f4 = f3.multiply(eta);
final T coeff0 = f1.multiply(a70).
final T coeff0 = thetaH.multiply(a70).
subtract(f2.multiply(a70.subtract(1))).
add(f3.multiply(a70.multiply(2).subtract(1))).
add(f4.multiply(d0));
final T coeff1 = time.getField().getZero();
final T coeff2 = f1.multiply(a72).
final T coeff2 = thetaH.multiply(a72).
subtract(f2.multiply(a72)).
add(f3.multiply(a72.multiply(2))).
add(f4.multiply(d2));
final T coeff3 = f1.multiply(a73).
final T coeff3 = thetaH.multiply(a73).
subtract(f2.multiply(a73)).
add(f3.multiply(a73.multiply(2))).
add(f4.multiply(d3));
final T coeff4 = f1.multiply(a74).
final T coeff4 = thetaH.multiply(a74).
subtract(f2.multiply(a74)).
add(f3.multiply(a74.multiply(2))).
add(f4.multiply(d4));
final T coeff5 = f1.multiply(a75).
final T coeff5 = thetaH.multiply(a75).
subtract(f2.multiply(a75)).
add(f3.multiply(a75.multiply(2))).
add(f4.multiply(d5));
Expand Down
Expand Up @@ -234,8 +234,7 @@ protected FieldODEStateAndDerivative<T> computeInterpolatedStateAndDerivatives(f


if (getGlobalPreviousState() != null && theta.getReal() <= 0.5) {
final T f0 = thetaH;
final T f1 = f0.multiply(eta);
final T f1 = thetaH.multiply(eta);
final T f2 = f1.multiply(theta);
final T f3 = f2.multiply(eta);
final T f4 = f3.multiply(theta);
Expand All @@ -244,7 +243,7 @@ protected FieldODEStateAndDerivative<T> computeInterpolatedStateAndDerivatives(f
final T[] p = MathArrays.buildArray(time.getField(), 16);
final T[] q = MathArrays.buildArray(time.getField(), 16);
for (int i = 0; i < p.length; ++i) {
p[i] = f0.multiply(d[0][i]).
p[i] = thetaH.multiply(d[0][i]).
add(f1.multiply(d[1][i])).
add(f2.multiply(d[2][i])).
add(f3.multiply(d[3][i])).
Expand Down
Expand Up @@ -193,26 +193,24 @@ protected FieldODEStateAndDerivative<T> computeInterpolatedStateAndDerivatives(f

if (getGlobalPreviousState() != null && theta.getReal() <= 0.5) {

final T s = thetaH;
final T coeff1 = s.multiply(theta.multiply(theta.multiply(theta.multiply(theta.multiply( 21 / 5.0).add( -47 / 4.0)).add( 12 )).add( -27 / 5.0)).add(1));
final T coeff1 = thetaH.multiply(theta.multiply(theta.multiply(theta.multiply(theta.multiply( 21 / 5.0).add( -47 / 4.0)).add( 12 )).add( -27 / 5.0)).add(1));
final T coeff2 = time.getField().getZero();
final T coeff3 = s.multiply(theta.multiply(theta.multiply(theta.multiply(theta.multiply( 112 / 5.0).add(-152 / 3.0)).add( 320 / 9.0 )).add(-104 / 15.0)));
final T coeff4 = s.multiply(theta.multiply(theta.multiply(theta.multiply(theta.multiply(-567 / 25.0).add( 243 / 5.0)).add( -162 / 5.0 )).add( 162 / 25.0)));
final T coeff5 = s.multiply(theta.multiply(theta.multiply(theta.multiply(theta.multiply(c5a.divide(25)).add(c5b.divide(60))).add(c5c.divide(90))).add(c5d.divide(300))));
final T coeff6 = s.multiply(theta.multiply(theta.multiply(theta.multiply(theta.multiply(c6a.divide(25)).add(c6b.divide(60))).add(c6c.divide(90))).add(c6d.divide(300))));
final T coeff7 = s.multiply(theta.multiply(theta.multiply(theta.multiply( 3 / 4.0 ).add( -1 )).add( 3 / 10.0)));
final T coeff3 = thetaH.multiply(theta.multiply(theta.multiply(theta.multiply(theta.multiply( 112 / 5.0).add(-152 / 3.0)).add( 320 / 9.0 )).add(-104 / 15.0)));
final T coeff4 = thetaH.multiply(theta.multiply(theta.multiply(theta.multiply(theta.multiply(-567 / 25.0).add( 243 / 5.0)).add( -162 / 5.0 )).add( 162 / 25.0)));
final T coeff5 = thetaH.multiply(theta.multiply(theta.multiply(theta.multiply(theta.multiply(c5a.divide(25)).add(c5b.divide(60))).add(c5c.divide(90))).add(c5d.divide(300))));
final T coeff6 = thetaH.multiply(theta.multiply(theta.multiply(theta.multiply(theta.multiply(c6a.divide(25)).add(c6b.divide(60))).add(c6c.divide(90))).add(c6d.divide(300))));
final T coeff7 = thetaH.multiply(theta.multiply(theta.multiply(theta.multiply( 3 / 4.0 ).add( -1 )).add( 3 / 10.0)));
interpolatedState = previousStateLinearCombination(coeff1, coeff2, coeff3, coeff4, coeff5, coeff6, coeff7);
interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot2, coeffDot3, coeffDot4, coeffDot5, coeffDot6, coeffDot7);
} else {

final T s = oneMinusThetaH;
final T coeff1 = s.multiply(theta.multiply(theta.multiply(theta.multiply(theta.multiply( -21 / 5.0).add( 151 / 20.0)).add( -89 / 20.0)).add( 19 / 20.0)).add(- 1 / 20.0));
final T coeff1 = oneMinusThetaH.multiply(theta.multiply(theta.multiply(theta.multiply(theta.multiply( -21 / 5.0).add( 151 / 20.0)).add( -89 / 20.0)).add( 19 / 20.0)).add(- 1 / 20.0));
final T coeff2 = time.getField().getZero();
final T coeff3 = s.multiply(theta.multiply(theta.multiply(theta.multiply(theta.multiply(-112 / 5.0).add( 424 / 15.0)).add( -328 / 45.0)).add( -16 / 45.0)).add(-16 / 45.0));
final T coeff4 = s.multiply(theta.multiply(theta.multiply(theta.multiply(theta.multiply( 567 / 25.0).add( -648 / 25.0)).add( 162 / 25.0))));
final T coeff5 = s.multiply(theta.multiply(theta.multiply(theta.multiply(theta.multiply(d5a.divide(25)).add(d5b.divide(300))).add(d5c.divide(900))).add( -49 / 180.0)).add(-49 / 180.0));
final T coeff6 = s.multiply(theta.multiply(theta.multiply(theta.multiply(theta.multiply(d6a.divide(25)).add(d6b.divide(300))).add(d6c.divide(900))).add( -49 / 180.0)).add(-49 / 180.0));
final T coeff7 = s.multiply( theta.multiply(theta.multiply(theta.multiply( -3 / 4.0 ).add( 1 / 4.0)).add( -1 / 20.0)).add( -1 / 20.0));
final T coeff3 = oneMinusThetaH.multiply(theta.multiply(theta.multiply(theta.multiply(theta.multiply(-112 / 5.0).add( 424 / 15.0)).add( -328 / 45.0)).add( -16 / 45.0)).add(-16 / 45.0));
final T coeff4 = oneMinusThetaH.multiply(theta.multiply(theta.multiply(theta.multiply(theta.multiply( 567 / 25.0).add( -648 / 25.0)).add( 162 / 25.0))));
final T coeff5 = oneMinusThetaH.multiply(theta.multiply(theta.multiply(theta.multiply(theta.multiply(d5a.divide(25)).add(d5b.divide(300))).add(d5c.divide(900))).add( -49 / 180.0)).add(-49 / 180.0));
final T coeff6 = oneMinusThetaH.multiply(theta.multiply(theta.multiply(theta.multiply(theta.multiply(d6a.divide(25)).add(d6b.divide(300))).add(d6c.divide(900))).add( -49 / 180.0)).add(-49 / 180.0));
final T coeff7 = oneMinusThetaH.multiply( theta.multiply(theta.multiply(theta.multiply( -3 / 4.0 ).add( 1 / 4.0)).add( -1 / 20.0)).add( -1 / 20.0));
interpolatedState = currentStateLinearCombination(coeff1, coeff2, coeff3, coeff4, coeff5, coeff6, coeff7);
interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot2, coeffDot3, coeffDot4, coeffDot5, coeffDot6, coeffDot7);
}
Expand Down
Expand Up @@ -119,8 +119,7 @@ public double value(double alpha) {
for (int i = 0; i < n; i++) {
x[i] = startPoint[i] + alpha * direction[i];
}
final double obj = mainOptimizer.computeObjectiveValue(x);
return obj;
return mainOptimizer.computeObjectiveValue(x);
}
};

Expand Down
Expand Up @@ -1818,7 +1818,6 @@ private double[] trsbox(
printMethod(); // XXX

final int n = currentBest.getDimension();
final int npt = numberOfInterpolationPoints;

double dsq = Double.NaN;
double crvmin = Double.NaN;
Expand Down Expand Up @@ -2258,7 +2257,7 @@ private double[] trsbox(
}
}
final RealVector tmp = interpolationPoints.operate(s).ebeMultiply(modelSecondDerivativesParameters);
for (int k = 0; k < npt; k++) {
for (int k = 0; k < numberOfInterpolationPoints; 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));
Expand Down
Expand Up @@ -190,10 +190,9 @@ public double evaluate(final double[] values,final int begin, final int length)
accum3 /= variance * FastMath.sqrt(variance);

// Get N
double n0 = length;

// Calculate skewness
skew = (n0 / ((n0 - 1) * (n0 - 2))) * accum3;
skew = ((double) length / (((double) length - 1) * ((double) length - 2))) * accum3;
}
return skew;
}
Expand Down
Expand Up @@ -163,14 +163,13 @@ public double kolmogorovSmirnovTest(ContinuousDistribution distribution, double[
public double kolmogorovSmirnovStatistic(ContinuousDistribution distribution, double[] data) {
checkArray(data);
final int n = data.length;
final double nd = n;
final double[] dataCopy = new double[n];
System.arraycopy(data, 0, dataCopy, 0, n);
Arrays.sort(dataCopy);
double d = 0d;
for (int i = 1; i <= n; i++) {
final double yi = distribution.cumulativeProbability(dataCopy[i - 1]);
final double currD = FastMath.max(yi - (i - 1) / nd, i / nd - yi);
final double currD = FastMath.max(yi - (i - 1) / (double) n, i / (double) n - yi);
if (currD > d) {
d = currD;
}
Expand Down Expand Up @@ -962,9 +961,7 @@ public double exactP(double d, int n, int m, boolean strict) {
* \(D_{n,m}\) greater than {@code d}
*/
public double approximateP(double d, int n, int m) {
final double dm = m;
final double dn = n;
return 1 - ksSum(d * FastMath.sqrt((dm * dn) / (dm + dn)),
return 1 - ksSum(d * FastMath.sqrt(((double) m * (double) n) / ((double) m + (double) n)),
KS_SUM_CAUCHY_CRITERION, MAXIMUM_PARTIAL_SUM_COUNT);
}

Expand Down
17 changes: 6 additions & 11 deletions src/main/java/org/apache/commons/math4/util/FastMath.java
Expand Up @@ -673,9 +673,6 @@ public static double tanh(double x) {
double ya = hiPrec[0] + hiPrec[1];
double yb = -(ya - hiPrec[0] - hiPrec[1]);

/* Numerator */
double na = ya;
double nb = yb;

/* Denominator */
double da = 2.0 + ya;
Expand All @@ -689,18 +686,18 @@ public static double tanh(double x) {
double dab = da - daa;

// ratio = na/da
double ratio = na/da;
double ratio = ya /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;
ratiob += (ya - daa*ratioa - daa*ratiob - dab*ratioa - dab*ratiob) / da;

// Account for nb
ratiob += nb / da;
ratiob += yb / da;
// Account for db
ratiob += -db * na / da / da;
ratiob += -db * ya / da / da;

result = ratioa + ratiob;
}
Expand Down Expand Up @@ -1596,9 +1593,8 @@ public static double pow(final double x, final double y) {
z = z * lnb + 1.0;
z *= lnb;

final double result = exp(lna, z, null);
//result = result + result * z;
return result;
return exp(lna, z, null);

}
}
Expand Down Expand Up @@ -2791,9 +2787,8 @@ public static double atan2(double y, double x) {
}

// Call atan
final double result = atan(ra, rb, x < 0);

return result;
return atan(ra, rb, x < 0);
}

/** Compute the arc sine of a number.
Expand Down

0 comments on commit 3bfe940

Please sign in to comment.