Update userguide code examples.

This commit is contained in:
Gilles Sadowski 2020-02-18 17:18:00 +01:00
parent 5c93c639c8
commit 4d5983aa87
9 changed files with 184 additions and 159 deletions

View File

@ -20,7 +20,7 @@
<parent>
<groupId>org.apache.commons</groupId>
<artifactId>commons-parent</artifactId>
<version>49</version>
<version>50</version>
</parent>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math4</artifactId>

View File

@ -36,11 +36,11 @@ import javax.swing.JLabel;
import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.rng.simple.RandomSource;
import org.apache.commons.rng.sampling.ListSampler;
import org.apache.commons.statistics.distribution.ContinuousDistribution;
import org.apache.commons.statistics.distribution.UniformContinuousDistribution;
import org.apache.commons.statistics.distribution.NormalDistribution;
import org.apache.commons.geometry.euclidean.twod.Vector2D;
import org.apache.commons.math4.distribution.RealDistribution;
import org.apache.commons.math4.distribution.UniformRealDistribution;
import org.apache.commons.math4.distribution.NormalDistribution;
import org.apache.commons.math4.geometry.euclidean.twod.Cartesian2D;
import org.apache.commons.math4.ml.clustering.CentroidCluster;
import org.apache.commons.math4.ml.clustering.Cluster;
import org.apache.commons.math4.ml.clustering.Clusterable;
@ -61,7 +61,7 @@ import org.apache.commons.math4.userguide.ExampleUtils.ExampleFrame;
*/
public class ClusterAlgorithmComparison {
public static List<Cartesian2D> makeCircles(int samples,
public static List<Vector2D> makeCircles(int samples,
boolean shuffle,
double noise,
double factor,
@ -70,14 +70,14 @@ public class ClusterAlgorithmComparison {
throw new IllegalArgumentException();
}
RealDistribution.Sampler dist = new NormalDistribution(0.0, noise).createSampler(rng);
ContinuousDistribution.Sampler dist = new NormalDistribution(0.0, noise).createSampler(rng);
List<Cartesian2D> points = new ArrayList<Cartesian2D>();
List<Vector2D> points = new ArrayList<>();
double range = 2.0 * FastMath.PI;
double step = range / (samples / 2.0 + 1);
for (double angle = 0; angle < range; angle += step) {
Cartesian2D outerCircle = new Cartesian2D(FastMath.cos(angle), FastMath.sin(angle));
Cartesian2D innerCircle = outerCircle.scalarMultiply(factor);
Vector2D outerCircle = Vector2D.of(FastMath.cos(angle), FastMath.sin(angle));
Vector2D innerCircle = outerCircle.multiply(factor);
points.add(outerCircle.add(generateNoiseVector(dist)));
points.add(innerCircle.add(generateNoiseVector(dist)));
@ -90,26 +90,26 @@ public class ClusterAlgorithmComparison {
return points;
}
public static List<Cartesian2D> makeMoons(int samples,
public static List<Vector2D> makeMoons(int samples,
boolean shuffle,
double noise,
UniformRandomProvider rng) {
RealDistribution.Sampler dist = new NormalDistribution(0.0, noise).createSampler(rng);
ContinuousDistribution.Sampler dist = new NormalDistribution(0.0, noise).createSampler(rng);
int nSamplesOut = samples / 2;
int nSamplesIn = samples - nSamplesOut;
List<Cartesian2D> points = new ArrayList<Cartesian2D>();
List<Vector2D> points = new ArrayList<>();
double range = FastMath.PI;
double step = range / (nSamplesOut / 2.0);
for (double angle = 0; angle < range; angle += step) {
Cartesian2D outerCircle = new Cartesian2D(FastMath.cos(angle), FastMath.sin(angle));
Vector2D outerCircle = Vector2D.of(FastMath.cos(angle), FastMath.sin(angle));
points.add(outerCircle.add(generateNoiseVector(dist)));
}
step = range / (nSamplesIn / 2.0);
for (double angle = 0; angle < range; angle += step) {
Cartesian2D innerCircle = new Cartesian2D(1 - FastMath.cos(angle), 1 - FastMath.sin(angle) - 0.5);
Vector2D innerCircle = Vector2D.of(1 - FastMath.cos(angle), 1 - FastMath.sin(angle) - 0.5);
points.add(innerCircle.add(generateNoiseVector(dist)));
}
@ -120,19 +120,19 @@ public class ClusterAlgorithmComparison {
return points;
}
public static List<Cartesian2D> makeBlobs(int samples,
public static List<Vector2D> makeBlobs(int samples,
int centers,
double clusterStd,
double min,
double max,
boolean shuffle,
UniformRandomProvider rng) {
RealDistribution.Sampler uniform = new UniformRealDistribution(min, max).createSampler(rng);
RealDistribution.Sampler gauss = new NormalDistribution(0.0, clusterStd).createSampler(rng);
ContinuousDistribution.Sampler uniform = new UniformContinuousDistribution(min, max).createSampler(rng);
ContinuousDistribution.Sampler gauss = new NormalDistribution(0.0, clusterStd).createSampler(rng);
Cartesian2D[] centerPoints = new Cartesian2D[centers];
Vector2D[] centerPoints = new Vector2D[centers];
for (int i = 0; i < centers; i++) {
centerPoints[i] = new Cartesian2D(uniform.sample(), uniform.sample());
centerPoints[i] = Vector2D.of(uniform.sample(), uniform.sample());
}
int[] nSamplesPerCenter = new int[centers];
@ -143,7 +143,7 @@ public class ClusterAlgorithmComparison {
nSamplesPerCenter[i]++;
}
List<Cartesian2D> points = new ArrayList<Cartesian2D>();
List<Vector2D> points = new ArrayList<>();
for (int i = 0; i < centers; i++) {
for (int j = 0; j < nSamplesPerCenter[i]; j++) {
points.add(centerPoints[i].add(generateNoiseVector(gauss)));
@ -157,26 +157,26 @@ public class ClusterAlgorithmComparison {
return points;
}
public static List<Cartesian2D> makeRandom(int samples) {
public static List<Vector2D> makeRandom(int samples) {
SobolSequenceGenerator generator = new SobolSequenceGenerator(2);
generator.skipTo(999999);
List<Cartesian2D> points = new ArrayList<Cartesian2D>();
List<Vector2D> points = new ArrayList<>();
for (double i = 0; i < samples; i++) {
double[] vector = generator.nextVector();
vector[0] = vector[0] * 2 - 1;
vector[1] = vector[1] * 2 - 1;
Cartesian2D point = new Cartesian2D(vector);
Vector2D point = Vector2D.of(vector);
points.add(point);
}
return points;
}
public static Cartesian2D generateNoiseVector(RealDistribution.Sampler distribution) {
return new Cartesian2D(distribution.sample(), distribution.sample());
public static Vector2D generateNoiseVector(ContinuousDistribution.Sampler distribution) {
return Vector2D.of(distribution.sample(), distribution.sample());
}
public static List<DoublePoint> normalize(final List<Cartesian2D> input,
public static List<DoublePoint> normalize(final List<Vector2D> input,
double minX,
double maxX,
double minY,
@ -184,7 +184,7 @@ public class ClusterAlgorithmComparison {
double rangeX = maxX - minX;
double rangeY = maxY - minY;
List<DoublePoint> points = new ArrayList<DoublePoint>();
for (Cartesian2D p : input) {
for (Vector2D p : input) {
double[] arr = p.toArray();
arr[0] = (arr[0] - minX) / rangeX * 2 - 1;
arr[1] = (arr[1] - minY) / rangeY * 2 - 1;

View File

@ -33,14 +33,14 @@ import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import org.apache.commons.math4.distribution.BinomialDistribution;
import org.apache.commons.math4.distribution.GeometricDistribution;
import org.apache.commons.math4.distribution.HypergeometricDistribution;
import org.apache.commons.math4.distribution.IntegerDistribution;
import org.apache.commons.math4.distribution.PascalDistribution;
import org.apache.commons.math4.distribution.PoissonDistribution;
import org.apache.commons.math4.distribution.UniformIntegerDistribution;
import org.apache.commons.math4.distribution.ZipfDistribution;
import org.apache.commons.statistics.distribution.BinomialDistribution;
import org.apache.commons.statistics.distribution.GeometricDistribution;
import org.apache.commons.statistics.distribution.HypergeometricDistribution;
import org.apache.commons.statistics.distribution.DiscreteDistribution;
import org.apache.commons.statistics.distribution.PascalDistribution;
import org.apache.commons.statistics.distribution.PoissonDistribution;
import org.apache.commons.statistics.distribution.UniformDiscreteDistribution;
import org.apache.commons.statistics.distribution.ZipfDistribution;
import org.apache.commons.math4.userguide.ExampleUtils.ExampleFrame;
import com.xeiam.xchart.Chart;
@ -56,7 +56,7 @@ import com.xeiam.xchart.XChartPanel;
*/
public class IntegerDistributionComparison {
public static void addPDFSeries(Chart chart, IntegerDistribution distribution, String desc, int lowerBound, int upperBound) {
public static void addPDFSeries(Chart chart, DiscreteDistribution distribution, String desc, int lowerBound, int upperBound) {
// generates Log data
List<Number> xData = new ArrayList<Number>();
List<Number> yData = new ArrayList<Number>();
@ -78,7 +78,7 @@ public class IntegerDistributionComparison {
series.setLineStyle(new BasicStroke(1.2f));
}
public static void addCDFSeries(Chart chart, IntegerDistribution distribution, String desc,
public static void addCDFSeries(Chart chart, DiscreteDistribution distribution, String desc,
int lowerBound, int upperBound) {
// generates Log data
List<Number> xData = new ArrayList<Number>();
@ -120,7 +120,7 @@ public class IntegerDistributionComparison {
}
public static JComponent createComponent(String distributionName, int minX, int maxX, String[] seriesText,
IntegerDistribution... series) {
DiscreteDistribution... series) {
JComponent container = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS));
@ -128,14 +128,14 @@ public class IntegerDistributionComparison {
Chart chart = createChart("PDF", minX, maxX, LegendPosition.InsideNE);
int i = 0;
for (IntegerDistribution d : series) {
for (DiscreteDistribution d : series) {
addPDFSeries(chart, d, seriesText[i++], minX, maxX);
}
container.add(new XChartPanel(chart));
chart = createChart("CDF", minX, maxX, LegendPosition.InsideSE);
i = 0;
for (IntegerDistribution d : series) {
for (DiscreteDistribution d : series) {
addCDFSeries(chart, d, seriesText[i++], minX, maxX);
}
container.add(new XChartPanel(chart));
@ -207,9 +207,9 @@ public class IntegerDistributionComparison {
c.gridx++;
comp = createComponent("Uniform", 0, 30,
new String[] { "l=1,u=10", "l=5,u=20", "l=1,u=25" },
new UniformIntegerDistribution(1, 10),
new UniformIntegerDistribution(5, 20),
new UniformIntegerDistribution(1, 25));
new UniformDiscreteDistribution(1, 10),
new UniformDiscreteDistribution(5, 20),
new UniformDiscreteDistribution(1, 25));
container.add(comp, c);
c.gridx++;

View File

@ -35,7 +35,7 @@ import javax.swing.JTextArea;
import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.rng.simple.RandomSource;
import org.apache.commons.math4.geometry.euclidean.twod.Cartesian2D;
import org.apache.commons.geometry.euclidean.twod.Vector2D;
import org.apache.commons.math4.random.HaltonSequenceGenerator;
import org.apache.commons.math4.random.RandomVectorGenerator;
import org.apache.commons.math4.random.SobolSequenceGenerator;
@ -50,11 +50,11 @@ import org.apache.commons.math4.userguide.ExampleUtils.ExampleFrame;
*/
public class LowDiscrepancyGeneratorComparison {
public static List<Cartesian2D> makeCircle(int samples, final RandomVectorGenerator generator) {
List<Cartesian2D> points = new ArrayList<Cartesian2D>();
public static List<Vector2D> makeCircle(int samples, final RandomVectorGenerator generator) {
List<Vector2D> points = new ArrayList<>();
for (double i = 0; i < samples; i++) {
double[] vector = generator.nextVector();
Cartesian2D point = new Cartesian2D(vector);
Vector2D point = Vector2D.of(vector);
points.add(point);
}
@ -62,8 +62,8 @@ public class LowDiscrepancyGeneratorComparison {
points = normalize(points);
// now test if the sample is within the unit circle
List<Cartesian2D> circlePoints = new ArrayList<Cartesian2D>();
for (Cartesian2D p : points) {
List<Vector2D> circlePoints = new ArrayList<>();
for (Vector2D p : points) {
double criteria = FastMath.pow(p.getX(), 2) + FastMath.pow(p.getY(), 2);
if (criteria < 1.0) {
circlePoints.add(p);
@ -73,22 +73,22 @@ public class LowDiscrepancyGeneratorComparison {
return circlePoints;
}
public static List<Cartesian2D> makeRandom(int samples, RandomVectorGenerator generator) {
List<Cartesian2D> points = new ArrayList<Cartesian2D>();
public static List<Vector2D> makeRandom(int samples, RandomVectorGenerator generator) {
List<Vector2D> points = new ArrayList<>();
for (double i = 0; i < samples; i++) {
double[] vector = generator.nextVector();
Cartesian2D point = new Cartesian2D(vector);
Vector2D point = Vector2D.of(vector);
points.add(point);
}
return normalize(points);
}
public static List<Cartesian2D> normalize(final List<Cartesian2D> input) {
public static List<Vector2D> normalize(final List<Vector2D> input) {
// find the mininum and maximum x value in the dataset
double minX = Double.MAX_VALUE;
double maxX = Double.MIN_VALUE;
for (Cartesian2D p : input) {
for (Vector2D p : input) {
minX = FastMath.min(minX, p.getX());
maxX = FastMath.max(maxX, p.getX());
}
@ -106,13 +106,13 @@ public class LowDiscrepancyGeneratorComparison {
double rangeX = maxX - minX;
double rangeY = maxY - minY;
List<Cartesian2D> points = new ArrayList<Cartesian2D>();
for (Cartesian2D p : input) {
List<Vector2D> points = new ArrayList<>();
for (Vector2D p : input) {
double[] arr = p.toArray();
// normalize to the range [-1, 1]
arr[0] = (arr[0] - minX) / rangeX * 2 - 1;
arr[1] = (arr[1] - minY) / rangeY * 2 - 1;
points.add(new Cartesian2D(arr));
points.add(Vector2D.of(arr));
}
return points;
}
@ -177,7 +177,7 @@ public class LowDiscrepancyGeneratorComparison {
c.gridx = 1;
for (Pair<String, RandomVectorGenerator> pair : generators) {
List<Cartesian2D> points = null;
List<Vector2D> points = null;
int samples = datasets[type];
switch (type) {
case 0:
@ -207,9 +207,9 @@ public class LowDiscrepancyGeneratorComparison {
private static double PAD = 10;
private List<Cartesian2D> points;
private List<Vector2D> points;
public Plot(final List<Cartesian2D> points) {
public Plot(final List<Vector2D> points) {
this.points = points;
}
@ -228,8 +228,8 @@ public class LowDiscrepancyGeneratorComparison {
g2.setPaint(Color.black);
g2.drawRect(0, 0, w - 1, h - 1);
for (Cartesian2D point : points) {
Cartesian2D p = transform(point, w, h);
for (Vector2D point : points) {
Vector2D p = transform(point, w, h);
double[] arr = p.toArray();
g2.draw(new Rectangle2D.Double(arr[0] - 1, arr[1] - 1, 2, 2));
}
@ -240,10 +240,10 @@ public class LowDiscrepancyGeneratorComparison {
return new Dimension(140, 140);
}
private Cartesian2D transform(Cartesian2D point, int width, int height) {
private Vector2D transform(Vector2D point, int width, int height) {
double[] arr = point.toArray();
return new Cartesian2D(new double[] { PAD + (arr[0] + 1) / 2.0 * (width - 2 * PAD),
height - PAD - (arr[1] + 1) / 2.0 * (height - 2 * PAD) });
return Vector2D.of(PAD + (arr[0] + 1) / 2.0 * (width - 2 * PAD),
height - PAD - (arr[1] + 1) / 2.0 * (height - 2 * PAD));
}
}

View File

@ -33,19 +33,19 @@ import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import org.apache.commons.math4.distribution.BetaDistribution;
import org.apache.commons.math4.distribution.CauchyDistribution;
import org.apache.commons.math4.distribution.ChiSquaredDistribution;
import org.apache.commons.math4.distribution.ExponentialDistribution;
import org.apache.commons.math4.distribution.FDistribution;
import org.apache.commons.math4.distribution.GammaDistribution;
import org.apache.commons.math4.distribution.LevyDistribution;
import org.apache.commons.math4.distribution.LogNormalDistribution;
import org.apache.commons.math4.distribution.NormalDistribution;
import org.apache.commons.math4.distribution.ParetoDistribution;
import org.apache.commons.math4.distribution.RealDistribution;
import org.apache.commons.math4.distribution.TDistribution;
import org.apache.commons.math4.distribution.WeibullDistribution;
import org.apache.commons.statistics.distribution.BetaDistribution;
import org.apache.commons.statistics.distribution.CauchyDistribution;
import org.apache.commons.statistics.distribution.ChiSquaredDistribution;
import org.apache.commons.statistics.distribution.ExponentialDistribution;
import org.apache.commons.statistics.distribution.FDistribution;
import org.apache.commons.statistics.distribution.GammaDistribution;
import org.apache.commons.statistics.distribution.LevyDistribution;
import org.apache.commons.statistics.distribution.LogNormalDistribution;
import org.apache.commons.statistics.distribution.NormalDistribution;
import org.apache.commons.statistics.distribution.ParetoDistribution;
import org.apache.commons.statistics.distribution.ContinuousDistribution;
import org.apache.commons.statistics.distribution.TDistribution;
import org.apache.commons.statistics.distribution.WeibullDistribution;
import org.apache.commons.math4.util.FastMath;
import org.apache.commons.math4.userguide.ExampleUtils.ExampleFrame;
@ -62,7 +62,7 @@ import com.xeiam.xchart.XChartPanel;
*/
public class RealDistributionComparison {
public static void addPDFSeries(Chart chart, RealDistribution distribution, String desc, int lowerBound, int upperBound) {
public static void addPDFSeries(Chart chart, ContinuousDistribution distribution, String desc, int lowerBound, int upperBound) {
// generates Log data
List<Number> xData = new ArrayList<Number>();
List<Number> yData = new ArrayList<Number>();
@ -86,7 +86,7 @@ public class RealDistributionComparison {
series.setLineStyle(new BasicStroke(1.2f));
}
public static void addCDFSeries(Chart chart, RealDistribution distribution, String desc, int lowerBound, int upperBound) {
public static void addCDFSeries(Chart chart, ContinuousDistribution distribution, String desc, int lowerBound, int upperBound) {
// generates Log data
List<Number> xData = new ArrayList<Number>();
List<Number> yData = new ArrayList<Number>();
@ -128,7 +128,7 @@ public class RealDistributionComparison {
return chart;
}
public static JComponent createComponent(String distributionName, int minX, int maxX, String[] seriesText, RealDistribution... series) {
public static JComponent createComponent(String distributionName, int minX, int maxX, String[] seriesText, ContinuousDistribution... series) {
JComponent container = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS));
@ -136,14 +136,14 @@ public class RealDistributionComparison {
Chart chart = createChart("PDF", minX, maxX, LegendPosition.InsideNE);
int i = 0;
for (RealDistribution d : series) {
for (ContinuousDistribution d : series) {
addPDFSeries(chart, d, seriesText[i++], minX, maxX);
}
container.add(new XChartPanel(chart));
chart = createChart("CDF", minX, maxX, LegendPosition.InsideSE);
i = 0;
for (RealDistribution d : series) {
for (ContinuousDistribution d : series) {
addCDFSeries(chart, d, seriesText[i++], minX, maxX);
}
container.add(new XChartPanel(chart));
@ -175,7 +175,7 @@ public class RealDistributionComparison {
comp = createComponent("Normal", -5, 5,
new String[] { "μ=0,σ\u00B2=0.2", "μ=0,σ\u00B2=1", "μ=0,σ\u00B2=5", "μ=-2,σ\u00B2=0.5" },
new NormalDistribution(0, FastMath.sqrt(0.2)),
new NormalDistribution(),
new NormalDistribution(0, 1),
new NormalDistribution(0, FastMath.sqrt(5)),
new NormalDistribution(-2, FastMath.sqrt(0.5)));
container.add(comp, c);

View File

@ -31,19 +31,21 @@ import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.rng.simple.RandomSource;
import org.apache.commons.geometry.core.precision.EpsilonDoublePrecisionContext;
import org.apache.commons.geometry.euclidean.twod.Segment;
import org.apache.commons.geometry.euclidean.twod.Vector2D;
import org.apache.commons.geometry.enclosing.Encloser;
import org.apache.commons.geometry.enclosing.EnclosingBall;
import org.apache.commons.geometry.enclosing.WelzlEncloser;
import org.apache.commons.geometry.enclosing.euclidean.twod.DiskGenerator;
import org.apache.commons.geometry.enclosing.euclidean.twod.WelzlEncloser2D;
import org.apache.commons.geometry.hull.euclidean.twod.ConvexHull2D;
import org.apache.commons.geometry.hull.euclidean.twod.ConvexHullGenerator2D;
import org.apache.commons.geometry.hull.euclidean.twod.MonotoneChain;
import org.apache.commons.math4.geometry.enclosing.Encloser;
import org.apache.commons.math4.geometry.enclosing.EnclosingBall;
import org.apache.commons.math4.geometry.enclosing.WelzlEncloser;
import org.apache.commons.math4.geometry.euclidean.twod.DiskGenerator;
import org.apache.commons.math4.geometry.euclidean.twod.Euclidean2D;
import org.apache.commons.math4.geometry.euclidean.twod.Segment;
import org.apache.commons.math4.geometry.euclidean.twod.Cartesian2D;
import org.apache.commons.math4.geometry.euclidean.twod.hull.ConvexHull2D;
import org.apache.commons.math4.geometry.euclidean.twod.hull.ConvexHullGenerator2D;
import org.apache.commons.math4.geometry.euclidean.twod.hull.MonotoneChain;
import org.apache.commons.math4.util.FastMath;
import org.apache.commons.math4.userguide.ExampleUtils;
import org.apache.commons.math4.userguide.ExampleUtils.ExampleFrame;
@ -65,39 +67,39 @@ import org.piccolo2d.nodes.PText;
*/
public class GeometryExample {
public static List<Cartesian2D> createRandomPoints(int size) {
public static List<Vector2D> createRandomPoints(int size) {
final UniformRandomProvider random = RandomSource.create(RandomSource.MT);
// create the cloud container
List<Cartesian2D> points = new ArrayList<Cartesian2D>(size);
List<Vector2D> points = new ArrayList<>(size);
// fill the cloud with a random distribution of points
for (int i = 0; i < size; i++) {
points.add(new Cartesian2D(FastMath.round(random.nextDouble() * 400 + 100),
points.add(Vector2D.of(FastMath.round(random.nextDouble() * 400 + 100),
FastMath.round(random.nextDouble() * 400 + 100)));
}
return points;
}
public static List<Cartesian2D> createCircle(int samples) {
List<Cartesian2D> points = new ArrayList<Cartesian2D>();
final Cartesian2D center = new Cartesian2D(300, 300);
public static List<Vector2D> createCircle(int samples) {
List<Vector2D> points = new ArrayList<>();
final Vector2D center = Vector2D.of(300, 300);
double range = 2.0 * FastMath.PI;
double step = range / (samples + 1);
for (double angle = 0; angle < range; angle += step) {
Cartesian2D circle = new Cartesian2D(FastMath.cos(angle), FastMath.sin(angle));
points.add(circle.scalarMultiply(200).add(center));
Vector2D circle = Vector2D.of(FastMath.cos(angle), FastMath.sin(angle));
points.add(circle.multiply(200).add(center));
}
return points;
}
public static List<Cartesian2D> createCross() {
List<Cartesian2D> points = new ArrayList<Cartesian2D>();
public static List<Vector2D> createCross() {
List<Vector2D> points = new ArrayList<>();
for (int i = 100; i < 500; i += 10) {
points.add(new Cartesian2D(300, i));
points.add(new Cartesian2D(i, 300));
points.add(Vector2D.of(300, i));
points.add(Vector2D.of(i, 300));
}
return points;
@ -150,7 +152,7 @@ public class GeometryExample {
@SuppressWarnings("serial")
public static class Display extends ExampleFrame {
private List<Cartesian2D> points;
private List<Vector2D> points;
private PCanvas canvas;
private JComponent container;
private JComponent controlPanel;
@ -223,7 +225,7 @@ public class GeometryExample {
public void paintConvexHull() {
PNode pointSet = new PNode();
for (Cartesian2D point : points) {
for (Vector2D point : points) {
final PNode node = PPath.createEllipse(point.getX() - 1, point.getY() - 1, 2, 2);
node.addAttribute("tooltip", point);
node.setPaint(Color.gray);
@ -232,11 +234,11 @@ public class GeometryExample {
canvas.getLayer().addChild(pointSet);
ConvexHullGenerator2D generator = new MonotoneChain(true, 1e-6);
ConvexHullGenerator2D generator = new MonotoneChain(true, new EpsilonDoublePrecisionContext(1e-6));
ConvexHull2D hull = generator.generate(points); //AklToussaintHeuristic.reducePoints(points));
PNode hullNode = new PNode();
for (Cartesian2D vertex : hull.getVertices()) {
for (Vector2D vertex : hull.getVertices()) {
final PPath node = PPath.createEllipse(vertex.getX() - 1, vertex.getY() - 1, 2, 2);
node.addAttribute("tooltip", vertex);
node.setPaint(Color.red);
@ -244,9 +246,9 @@ public class GeometryExample {
hullNode.addChild(node);
}
for (Segment line : hull.getLineSegments()) {
final PPath node = PPath.createLine(line.getStart().getX(), line.getStart().getY(),
line.getEnd().getX(), line.getEnd().getY());
for (Segment line : hull.getPath().getSegments()) {
final PPath node = PPath.createLine(line.getStartPoint().getX(), line.getStartPoint().getY(),
line.getEndPoint().getX(), line.getEndPoint().getY());
node.setPickable(false);
node.setPaint(Color.red);
node.setStrokePaint(Color.red);
@ -255,9 +257,8 @@ public class GeometryExample {
canvas.getLayer().addChild(hullNode);
Encloser<Euclidean2D, Cartesian2D> encloser =
new WelzlEncloser<Euclidean2D, Cartesian2D>(1e-10, new DiskGenerator());
EnclosingBall<Euclidean2D, Cartesian2D> ball = encloser.enclose(points);
WelzlEncloser2D encloser = new WelzlEncloser2D(new EpsilonDoublePrecisionContext(1e-10));
EnclosingBall<Vector2D> ball = encloser.enclose(points);
final double radius = ball.getRadius();
PPath ballCenter =

View File

@ -19,12 +19,12 @@ package org.apache.commons.math4.userguide.sofm;
import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.rng.simple.RandomSource;
import org.apache.commons.math4.geometry.euclidean.threed.Cartesian3D;
import org.apache.commons.math4.geometry.euclidean.threed.Rotation;
import org.apache.commons.math4.random.UnitSphereRandomVectorGenerator;
import org.apache.commons.math4.distribution.RealDistribution;
import org.apache.commons.math4.distribution.UniformRealDistribution;
import org.apache.commons.rng.sampling.UnitSphereSampler;
import org.apache.commons.geometry.euclidean.threed.Vector3D;
import org.apache.commons.geometry.euclidean.threed.rotation.Rotation3D;
import org.apache.commons.geometry.euclidean.threed.rotation.QuaternionRotation;
import org.apache.commons.statistics.distribution.ContinuousDistribution;
import org.apache.commons.statistics.distribution.UniformContinuousDistribution;
/**
* Class that creates two intertwined rings.
@ -32,7 +32,7 @@ import org.apache.commons.math4.distribution.UniformRealDistribution;
*/
public class ChineseRings {
/** Points in the two rings. */
private final Cartesian3D[] points;
private final Vector3D[] points;
/**
* @param orientationRing1 Vector othogonal to the plane containing the
@ -44,7 +44,7 @@ public class ChineseRings {
* @param numPointsRing1 Number of points in the first ring.
* @param numPointsRing2 Number of points in the second ring.
*/
public ChineseRings(Cartesian3D orientationRing1,
public ChineseRings(Vector3D orientationRing1,
double radiusRing1,
double halfWidthRing1,
double radiusRing2,
@ -52,63 +52,62 @@ public class ChineseRings {
int numPointsRing1,
int numPointsRing2) {
// First ring (centered at the origin).
final Cartesian3D[] firstRing = new Cartesian3D[numPointsRing1];
final Vector3D[] firstRing = new Vector3D[numPointsRing1];
// Second ring (centered around the first ring).
final Cartesian3D[] secondRing = new Cartesian3D[numPointsRing2];
// Create two rings lying in xy-plane.
final UnitSphereRandomVectorGenerator unit
= new UnitSphereRandomVectorGenerator(2);
final Vector3D[] secondRing = new Vector3D[numPointsRing2];
final UniformRandomProvider rng = RandomSource.create(RandomSource.WELL_19937_C);
final RealDistribution.Sampler radius1
= new UniformRealDistribution(radiusRing1 - halfWidthRing1,
radiusRing1 + halfWidthRing1).createSampler(rng);
final RealDistribution.Sampler widthRing1
= new UniformRealDistribution(-halfWidthRing1, halfWidthRing1).createSampler(rng);
// Create two rings lying in xy-plane.
final UnitSphereSampler unit = new UnitSphereSampler(2, rng);
final ContinuousDistribution.Sampler radius1
= new UniformContinuousDistribution(radiusRing1 - halfWidthRing1,
radiusRing1 + halfWidthRing1).createSampler(rng);
final ContinuousDistribution.Sampler widthRing1
= new UniformContinuousDistribution(-halfWidthRing1, halfWidthRing1).createSampler(rng);
for (int i = 0; i < numPointsRing1; i++) {
final double[] v = unit.nextVector();
final double r = radius1.sample();
// First ring is in the xy-plane, centered at (0, 0, 0).
firstRing[i] = new Cartesian3D(v[0] * r,
v[1] * r,
widthRing1.sample());
firstRing[i] = Vector3D.of(v[0] * r,
v[1] * r,
widthRing1.sample());
}
final RealDistribution.Sampler radius2
= new UniformRealDistribution(radiusRing2 - halfWidthRing2,
radiusRing2 + halfWidthRing2).createSampler(rng);
final RealDistribution.Sampler widthRing2
= new UniformRealDistribution(-halfWidthRing2, halfWidthRing2).createSampler(rng);
final ContinuousDistribution.Sampler radius2
= new UniformContinuousDistribution(radiusRing2 - halfWidthRing2,
radiusRing2 + halfWidthRing2).createSampler(rng);
final ContinuousDistribution.Sampler widthRing2
= new UniformContinuousDistribution(-halfWidthRing2, halfWidthRing2).createSampler(rng);
for (int i = 0; i < numPointsRing2; i++) {
final double[] v = unit.nextVector();
final double r = radius2.sample();
// Second ring is in the xz-plane, centered at (radiusRing1, 0, 0).
secondRing[i] = new Cartesian3D(radiusRing1 + v[0] * r,
widthRing2.sample(),
v[1] * r);
secondRing[i] = Vector3D.of(radiusRing1 + v[0] * r,
widthRing2.sample(),
v[1] * r);
}
// Move first and second rings into position.
final Rotation rot = new Rotation(Cartesian3D.PLUS_K,
orientationRing1.normalize());
final Rotation3D rot = QuaternionRotation.createVectorRotation(Vector3D.Unit.PLUS_Z,
orientationRing1.normalize());
int count = 0;
points = new Cartesian3D[numPointsRing1 + numPointsRing2];
points = new Vector3D[numPointsRing1 + numPointsRing2];
for (int i = 0; i < numPointsRing1; i++) {
points[count++] = rot.applyTo(firstRing[i]);
points[count++] = rot.apply(firstRing[i]);
}
for (int i = 0; i < numPointsRing2; i++) {
points[count++] = rot.applyTo(secondRing[i]);
points[count++] = rot.apply(secondRing[i]);
}
}
/**
* Gets all the points.
*/
public Cartesian3D[] getPoints() {
public Vector3D[] getPoints() {
return points.clone();
}
}

View File

@ -23,6 +23,7 @@ import java.io.IOException;
import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.rng.simple.RandomSource;
import org.apache.commons.geometry.euclidean.threed.Vector3D;
import org.apache.commons.math4.ml.neuralnet.SquareNeighbourhood;
import org.apache.commons.math4.ml.neuralnet.FeatureInitializer;
@ -38,7 +39,6 @@ import org.apache.commons.math4.ml.neuralnet.sofm.KohonenTrainingTask;
import org.apache.commons.math4.ml.distance.DistanceMeasure;
import org.apache.commons.math4.ml.distance.EuclideanDistance;
import org.apache.commons.math4.stat.descriptive.SummaryStatistics;
import org.apache.commons.math4.geometry.euclidean.threed.Cartesian3D;
import org.apache.commons.math4.util.FastMath;
import org.apache.commons.math4.exception.MathUnsupportedOperationException;
@ -61,7 +61,7 @@ public class ChineseRingsClassifier {
private final DistanceMeasure distance = new EuclideanDistance();
public static void main(String[] args) {
final ChineseRings rings = new ChineseRings(new Cartesian3D(1, 2, 3),
final ChineseRings rings = new ChineseRings(Vector3D.of(1, 2, 3),
25, 2,
20, 1,
2000, 1500);
@ -185,7 +185,7 @@ public class ChineseRingsClassifier {
new SummaryStatistics(),
new SummaryStatistics()
};
for (Cartesian3D p : rings.getPoints()) {
for (Vector3D p : rings.getPoints()) {
centre[0].addValue(p.getX());
centre[1].addValue(p.getY());
centre[2].addValue(p.getZ());
@ -220,7 +220,7 @@ public class ChineseRingsClassifier {
public Iterator<double[]> iterator() {
return new Iterator<double[]>() {
/** Data. */
final Cartesian3D[] points = rings.getPoints();
final Vector3D[] points = rings.getPoints();
/** Number of samples. */
private int n = 0;
@ -253,7 +253,7 @@ public class ChineseRingsClassifier {
private Iterator<double[]> createRandomIterator(final long numSamples) {
return new Iterator<double[]>() {
/** Data. */
final Cartesian3D[] points = rings.getPoints();
final Vector3D[] points = rings.getPoints();
/** RNG. */
final UniformRandomProvider rng = RandomSource.create(RandomSource.KISS);
/** Number of samples. */

View File

@ -111,10 +111,35 @@
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-rng-client-api</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-rng-simple</artifactId>
<version>1.0</version>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-geometry-euclidean</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-geometry-enclosing</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-geometry-hull</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-statistics-distribution</artifactId>
<version>0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>