Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,7 @@ public SparseGradient add(final SparseGradient a) {
final SparseGradient out = new SparseGradient(value + a.value, derivatives);
for (Map.Entry<Integer, Double> entry : a.derivatives.entrySet()) {
final int id = entry.getKey();
final Double old = out.derivatives.get(id);
if (old == null) {
out.derivatives.put(id, entry.getValue());
} else {
out.derivatives.put(id, old + entry.getValue());
}
out.derivatives.merge(id, entry.getValue(), Double::sum);
}

return out;
Expand All @@ -161,12 +156,7 @@ public void addInPlace(final SparseGradient a) {
value += a.value;
for (final Map.Entry<Integer, Double> entry : a.derivatives.entrySet()) {
final int id = entry.getKey();
final Double old = derivatives.get(id);
if (old == null) {
derivatives.put(id, entry.getValue());
} else {
derivatives.put(id, old + entry.getValue());
}
derivatives.merge(id, entry.getValue(), Double::sum);
}
}

Expand Down Expand Up @@ -210,12 +200,7 @@ public SparseGradient multiply(final SparseGradient a) {
}
for (Map.Entry<Integer, Double> entry : a.derivatives.entrySet()) {
final int id = entry.getKey();
final Double old = out.derivatives.get(id);
if (old == null) {
out.derivatives.put(id, value * entry.getValue());
} else {
out.derivatives.put(id, old + value * entry.getValue());
}
out.derivatives.merge(id, value * entry.getValue(), Double::sum);
}
return out;
}
Expand All @@ -234,17 +219,10 @@ public SparseGradient multiply(final SparseGradient a) {
*/
public void multiplyInPlace(final SparseGradient a) {
// Derivatives.
for (Map.Entry<Integer, Double> entry : derivatives.entrySet()) {
derivatives.put(entry.getKey(), a.value * entry.getValue());
}
derivatives.replaceAll((k, v) -> a.value * v);
for (Map.Entry<Integer, Double> entry : a.derivatives.entrySet()) {
final int id = entry.getKey();
final Double old = derivatives.get(id);
if (old == null) {
derivatives.put(id, value * entry.getValue());
} else {
derivatives.put(id, old + value * entry.getValue());
}
derivatives.merge(id, value * entry.getValue(), Double::sum);
}
value *= a.value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ protected double doSolve() {

double root = Double.NaN;
try {
root = rf.findRoot(arg -> computeObjectiveValue(arg),
root = rf.findRoot(this::computeObjectiveValue,
min, initial, max);
} catch (IllegalArgumentException e) {
// Redundant calls in order to throw the expected exceptions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package org.apache.commons.math4.legacy.fitting;

import java.util.Collections;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
Expand Down Expand Up @@ -206,7 +205,7 @@ public int compare(WeightedObservedPoint p1,
*/
protected List<WeightedObservedPoint> sortObservations(Collection<WeightedObservedPoint> unsorted) {
final List<WeightedObservedPoint> observations = new ArrayList<>(unsorted);
Collections.sort(observations, CMP);
observations.sort(CMP);
return observations;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public static final List<Double> identityPermutation(final int l) {
public static <S> List<Double> comparatorPermutation(final List<S> data,
final Comparator<S> comparator) {
List<S> sortedData = new ArrayList<>(data);
Collections.sort(sortedData, comparator);
sortedData.sort(comparator);

return inducedPermutation(data, sortedData);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ public interface ClusterEvaluator {
static <T extends Clusterable> ClusterRanking ranking(ClusterEvaluator eval) {
return eval.isBetterScore(1, 2) ?
clusters -> 1 / eval.score(clusters) :
clusters -> eval.score(clusters);
eval::score;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ private AdamsNordsieckFieldTransformer(final Field<T> field, final int n) {
// Nordsieck to multistep, then shifting rows to represent step advance
// then applying inverse transform
T[][] shiftedP = bigP.getData();
for (int i = shiftedP.length - 1; i > 0; --i) {
// shift rows
shiftedP[i] = shiftedP[i - 1];
// shift rows
if (shiftedP.length - 1 > 0) {
System.arraycopy(shiftedP, 0, shiftedP, 1, shiftedP.length - 1);
}
shiftedP[0] = MathArrays.buildArray(field, rows);
Arrays.fill(shiftedP[0], field.getZero());
Expand All @@ -190,11 +190,7 @@ private AdamsNordsieckFieldTransformer(final Field<T> field, final int n) {
getInstance(final Field<T> field, final int nSteps) {
synchronized(CACHE) {
Map<Field<? extends RealFieldElement<?>>,
AdamsNordsieckFieldTransformer<? extends RealFieldElement<?>>> map = CACHE.get(nSteps);
if (map == null) {
map = new HashMap<>();
CACHE.put(nSteps, map);
}
AdamsNordsieckFieldTransformer<? extends RealFieldElement<?>>> map = CACHE.computeIfAbsent(nSteps, k -> new HashMap<>());
@SuppressWarnings("unchecked")
AdamsNordsieckFieldTransformer<T> t = (AdamsNordsieckFieldTransformer<T>) map.get(field);
if (t == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.apache.commons.math4.legacy.optim.nonlinear.scalar;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.Supplier;
Expand Down Expand Up @@ -65,7 +64,7 @@ public MultiStartMultivariateOptimizer(final MultivariateOptimizer optimizer,
*/
@Override
public PointValuePair[] getOptima() {
Collections.sort(optima, getPairComparator());
optima.sort(getPairComparator());
return optima.toArray(new PointValuePair[0]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.Comparator;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.function.UnaryOperator;
import java.util.function.DoublePredicate;
import org.apache.commons.rng.UniformRandomProvider;
Expand Down Expand Up @@ -132,7 +131,7 @@ private Simplex transform(Simplex original,
for (int k = 1; k < size; k++) {
// Perform reflections of the "k" worst points.
final List<PointValuePair> reflected = reflectPoints(original, k, eval);
Collections.sort(reflected, comp);
reflected.sort(comp);

// Check whether the best of the reflected points is better than the
// current overall best.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Collections;
import java.util.function.UnaryOperator;
import java.util.function.DoublePredicate;

Expand Down Expand Up @@ -218,7 +217,7 @@ public Simplex evaluate(MultivariateFunction function,
newPoints.add(new PointValuePair(coord, value, false));
}

Collections.sort(newPoints, comparator);
newPoints.sort(comparator);
return new Simplex(newPoints);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.List;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Collections;
import java.util.Objects;
import java.util.function.UnaryOperator;
import java.util.function.IntSupplier;
Expand Down Expand Up @@ -273,7 +272,7 @@ protected PointValuePair doOptimize() {
// Additional optimizations.
// Reference to counter in the "main" search in order to retrieve
// the total number of evaluations in the "best list" search.
final IntSupplier evalCount = () -> getEvaluations();
final IntSupplier evalCount = this::getEvaluations;

return bestListSearch(evalFunc,
comparator,
Expand Down Expand Up @@ -425,7 +424,7 @@ private static void keepIfBetter(PointValuePair candidate,

// Store better candidate and reorder the list.
list.set(last, candidate);
Collections.sort(list, comp);
list.sort(comp);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,25 +139,22 @@ public Supplier<double[]> gaussian(UniformRandomProvider rng) {
* @return a generator of vectors with correlated components.
*/
private Supplier<double[]> with(final ContinuousSampler sampler) {
return new Supplier<double[]>() {
@Override
public double[] get() {
// Uncorrelated vector.
final double[] uncorrelated = new double[lengthUncorrelated];
for (int i = 0; i < lengthUncorrelated; i++) {
uncorrelated[i] = sampler.sample();
}
return () -> {
// Uncorrelated vector.
final double[] uncorrelated = new double[lengthUncorrelated];
for (int i = 0; i < lengthUncorrelated; i++) {
uncorrelated[i] = sampler.sample();
}

// Correlated vector.
final double[] correlated = mean.clone();
for (int i = 0; i < correlated.length; i++) {
for (int j = 0; j < lengthUncorrelated; j++) {
correlated[i] += root.getEntry(i, j) * uncorrelated[j];
}
// Correlated vector.
final double[] correlated = mean.clone();
for (int i = 0; i < correlated.length; i++) {
for (int j = 0; j < lengthUncorrelated; j++) {
correlated[i] += root.getEntry(i, j) * uncorrelated[j];
}

return correlated;
}

return correlated;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import org.apache.commons.math4.neuralnet.internal.NeuralNetException;
Expand Down Expand Up @@ -94,15 +93,15 @@ public List<Neuron> rank(double[] features,
list.add(p);
if (list.size() > 1) {
// Sort if there is more than 1 element.
Collections.sort(list, PairNeuronDouble.COMPARATOR);
list.sort(PairNeuronDouble.COMPARATOR);
}
} else {
final int last = list.size() - 1;
if (PairNeuronDouble.COMPARATOR.compare(p, list.get(last)) < 0) {
list.set(last, p); // Replace worst entry.
if (last > 0) {
// Sort if there is more than 1 element.
Collections.sort(list, PairNeuronDouble.COMPARATOR);
list.sort(PairNeuronDouble.COMPARATOR);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private UnaryOperator<double[]> create(final Norm normalization,
} else {
return normalization == Norm.ORTHO ?
f -> TransformUtils.scaleInPlace(fct(f), Math.sqrt(2d / (f.length - 1))) :
f -> fct(f);
this::fct;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ private UnaryOperator<double[]> create(final boolean inverse) {
if (inverse) {
return f -> TransformUtils.scaleInPlace(fht(f), 1d / f.length);
} else {
return f -> fht(f);
return this::fht;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ private UnaryOperator<double[]> create(final Norm normalization,
} else {
return normalization == Norm.ORTHO ?
f -> TransformUtils.scaleInPlace(fst(f), Math.sqrt(2d / f.length)) :
f -> fst(f);
this::fst;
}
}

Expand Down