Use stream API in place of explicit loop.

Closes #154.
This commit is contained in:
PeterAlfredLee 2020-06-24 17:08:29 +08:00 committed by Gilles Sadowski
parent 0d937abf8c
commit 15dad0b047
5 changed files with 16 additions and 54 deletions

View File

@ -16,6 +16,7 @@
*/
package org.apache.commons.math4.fitting;
import java.util.Arrays;
import java.util.Collection;
import org.apache.commons.math4.analysis.MultivariateMatrixFunction;
@ -101,13 +102,7 @@ public abstract class AbstractCurveFitter {
public TheoreticalValuesFunction(final ParametricUnivariateFunction f,
final Collection<WeightedObservedPoint> observations) {
this.f = f;
final int len = observations.size();
this.points = new double[len];
int i = 0;
for (WeightedObservedPoint obs : observations) {
this.points[i++] = obs.getX();
}
this.points = observations.stream().mapToDouble(WeightedObservedPoint::getX).toArray();
}
/**
@ -118,13 +113,7 @@ public abstract class AbstractCurveFitter {
/** {@inheritDoc} */
@Override
public double[] value(double[] p) {
final int len = points.length;
final double[] values = new double[len];
for (int i = 0; i < len; i++) {
values[i] = f.value(points[i], p);
}
return values;
return Arrays.stream(points).map(point -> f.value(point, p)).toArray();
}
};
}

View File

@ -26,6 +26,8 @@ import org.apache.commons.math4.linear.RealMatrix;
import org.apache.commons.math4.linear.RealVector;
import org.apache.commons.math4.util.Pair;
import java.util.Arrays;
/**
* A MultivariateJacobianFunction (a thing that requires a derivative)
* combined with the thing that can find derivatives.
@ -88,10 +90,6 @@ public class DifferentiatorVectorMultivariateJacobianFunction implements Multiva
DerivativeStructure[] derivatives = differentiator
.differentiate(univariateVectorFunction)
.value(new DerivativeStructure(1, 1, 0, atParameterValue));
double[] derivativesOut = new double[derivatives.length];
for(int index=0;index<derivatives.length;index++) {
derivativesOut[index] = derivatives[index].getPartialDerivative(1);
}
return derivativesOut;
return Arrays.stream(derivatives).mapToDouble(derivative -> derivative.getPartialDerivative(1)).toArray();
}
}

View File

@ -23,6 +23,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.math4.exception.NotPositiveException;
import org.apache.commons.math4.ml.distance.DistanceMeasure;
@ -200,13 +201,8 @@ public class DBSCANClusterer<T extends Clusterable> extends Clusterer<T> {
* @return the List of neighbors
*/
private List<T> getNeighbors(final T point, final Collection<T> points) {
final List<T> neighbors = new ArrayList<>();
for (final T neighbor : points) {
if (point != neighbor && distance(neighbor, point) <= eps) {
neighbors.add(neighbor);
}
}
return neighbors;
return points.stream().filter(neighbor -> point != neighbor && distance(neighbor, point) <= eps)
.collect(Collectors.toList());
}
/**
@ -218,11 +214,7 @@ public class DBSCANClusterer<T extends Clusterable> extends Clusterer<T> {
*/
private List<T> merge(final List<T> one, final List<T> two) {
final Set<T> oneSet = new HashSet<>(one);
for (T item : two) {
if (!oneSet.contains(item)) {
one.add(item);
}
}
two.stream().filter(item -> !oneSet.contains(item)).forEach(one::add);
return one;
}
}

View File

@ -31,6 +31,7 @@ import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;
import org.apache.commons.math4.exception.DimensionMismatchException;
import org.apache.commons.math4.exception.MathIllegalStateException;
@ -216,12 +217,8 @@ public class Network
* this network.
*/
public void deleteNeuron(Neuron neuron) {
final Collection<Neuron> neighbours = getNeighbours(neuron);
// Delete links to from neighbours.
for (Neuron n : neighbours) {
deleteLink(n, neuron);
}
getNeighbours(neuron).forEach(neighbour -> deleteLink(neighbour, neuron));
// Remove neuron.
neuronMap.remove(neuron.getIdentifier());
@ -357,22 +354,13 @@ public class Network
public Collection<Neuron> getNeighbours(Iterable<Neuron> neurons,
Iterable<Neuron> exclude) {
final Set<Long> idList = new HashSet<>();
neurons.forEach(n -> idList.addAll(linkMap.get(n.getIdentifier())));
for (Neuron n : neurons) {
idList.addAll(linkMap.get(n.getIdentifier()));
}
if (exclude != null) {
for (Neuron n : exclude) {
idList.remove(n.getIdentifier());
}
exclude.forEach(n -> idList.remove(n.getIdentifier()));
}
final List<Neuron> neuronList = new ArrayList<>();
for (Long id : idList) {
neuronList.add(getNeuron(id));
}
return neuronList;
return idList.stream().map(this::getNeuron).collect(Collectors.toList());
}
/**

View File

@ -850,12 +850,7 @@ public final class StatUtils {
}
List<Double> list = freq.getMode();
// Convert the list to an array of primitive double
double[] modes = new double[list.size()];
int i = 0;
for(Double c : list) {
modes[i++] = c.doubleValue();
}
return modes;
return list.stream().mapToDouble(Double::doubleValue).toArray();
}
}