Removed "Commons Math" code examples.
Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/a960a5ca Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/a960a5ca Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/a960a5ca Branch: refs/heads/master Commit: a960a5ca73b1f5b8bdce7fc75502b75e6d945aff Parents: bd596be Author: Gilles Sadowski <[email protected]> Authored: Sun Apr 30 17:32:32 2017 +0200 Committer: Gilles Sadowski <[email protected]> Committed: Sun Apr 30 17:32:32 2017 +0200 ---------------------------------------------------------------------- src/userguide/README | 8 - .../userguide/ClusterAlgorithmComparison.java | 304 ----- .../commons/math4/userguide/ExampleUtils.java | 114 -- .../userguide/FastMathTestPerformance.java | 1142 ------------------ .../IntegerDistributionComparison.java | 239 ---- .../LowDiscrepancyGeneratorComparison.java | 253 ---- .../userguide/RealDistributionComparison.java | 309 ----- .../clustering/ImageClusteringExample.java | 203 ---- .../userguide/filter/CannonballExample.java | 321 ----- .../filter/ConstantVoltageExample.java | 240 ---- .../userguide/genetics/HelloWorldExample.java | 187 --- .../genetics/ImageEvolutionExample.java | 230 ---- .../math4/userguide/genetics/Polygon.java | 121 -- .../userguide/genetics/PolygonChromosome.java | 135 --- .../genetics/RandomPolygonMutation.java | 49 - .../userguide/geometry/GeometryExample.java | 280 ----- .../math4/userguide/sofm/ChineseRings.java | 109 -- .../userguide/sofm/ChineseRingsClassifier.java | 335 ----- src/userguide/pom.xml | 85 -- src/userguide/resources/ColorfulBird.jpg | Bin 98227 -> 0 bytes src/userguide/resources/feather-small.gif | Bin 7128 -> 0 bytes src/userguide/resources/monalisa.png | Bin 29270 -> 0 bytes src/userguide/resources/references.txt | 4 - 23 files changed, 4668 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/a960a5ca/src/userguide/README ---------------------------------------------------------------------- diff --git a/src/userguide/README b/src/userguide/README deleted file mode 100644 index a6bcedd..0000000 --- a/src/userguide/README +++ /dev/null @@ -1,8 +0,0 @@ -This directory contains source code that is not part of the Apache -Commons Math library. It contains syntactically correct and working -examples of use. - -In order to run one of the applications (a class that must contain a -"main" method), you would type (in a shell console) a command similar -to the following: - $ mvn -q exec:java -Dexec.mainClass=org.apache.commons.complex.userguide.sofm.ChineseRingsClassifier http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/a960a5ca/src/userguide/java/org/apache/commons/math4/userguide/ClusterAlgorithmComparison.java ---------------------------------------------------------------------- diff --git a/src/userguide/java/org/apache/commons/math4/userguide/ClusterAlgorithmComparison.java b/src/userguide/java/org/apache/commons/math4/userguide/ClusterAlgorithmComparison.java deleted file mode 100644 index dba2ed7..0000000 --- a/src/userguide/java/org/apache/commons/math4/userguide/ClusterAlgorithmComparison.java +++ /dev/null @@ -1,304 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.complex.userguide; - -import java.awt.Color; -import java.awt.Dimension; -import java.awt.Graphics; -import java.awt.Graphics2D; -import java.awt.GridBagConstraints; -import java.awt.GridBagLayout; -import java.awt.Insets; -import java.awt.RenderingHints; -import java.awt.Shape; -import java.awt.geom.Ellipse2D; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -import javax.swing.JComponent; -import javax.swing.JLabel; - -import org.apache.commons.complex.distribution.NormalDistribution; -import org.apache.commons.complex.geometry.euclidean.twod.Vector2D; -import org.apache.commons.complex.ml.clustering.CentroidCluster; -import org.apache.commons.complex.ml.clustering.Cluster; -import org.apache.commons.complex.ml.clustering.Clusterable; -import org.apache.commons.complex.ml.clustering.Clusterer; -import org.apache.commons.complex.ml.clustering.DBSCANClusterer; -import org.apache.commons.complex.ml.clustering.DoublePoint; -import org.apache.commons.complex.ml.clustering.FuzzyKMeansClusterer; -import org.apache.commons.complex.ml.clustering.KMeansPlusPlusClusterer; -import org.apache.commons.complex.random.RandomAdaptor; -import org.apache.commons.complex.random.RandomGenerator; -import org.apache.commons.complex.random.SobolSequenceGenerator; -import org.apache.commons.complex.random.Well19937c; -import org.apache.commons.complex.util.FastMath; -import org.apache.commons.complex.util.Pair; -import org.apache.commons.complex.userguide.ExampleUtils.ExampleFrame; - -/** - * Plots clustering results for various algorithms and datasets. - * Based on - * <a href="http://scikit-learn.org/stable/auto_examples/cluster/plot_cluster_comparison.html">scikit learn</a>. - */ -public class ClusterAlgorithmComparison { - - public static List<Vector2D> makeCircles(int samples, boolean shuffle, double noise, double factor, final RandomGenerator random) { - if (factor < 0 || factor > 1) { - throw new IllegalArgumentException(); - } - - NormalDistribution dist = new NormalDistribution(random, 0.0, noise, 1e-9); - - List<Vector2D> points = new ArrayList<Vector2D>(); - double range = 2.0 * FastMath.PI; - double step = range / (samples / 2.0 + 1); - for (double angle = 0; angle < range; angle += step) { - Vector2D outerCircle = new Vector2D(FastMath.cos(angle), FastMath.sin(angle)); - Vector2D innerCircle = outerCircle.scalarMultiply(factor); - - points.add(outerCircle.add(generateNoiseVector(dist))); - points.add(innerCircle.add(generateNoiseVector(dist))); - } - - if (shuffle) { - Collections.shuffle(points, new RandomAdaptor(random)); - } - - return points; - } - - public static List<Vector2D> makeMoons(int samples, boolean shuffle, double noise, RandomGenerator random) { - NormalDistribution dist = new NormalDistribution(random, 0.0, noise, 1e-9); - - int nSamplesOut = samples / 2; - int nSamplesIn = samples - nSamplesOut; - - List<Vector2D> points = new ArrayList<Vector2D>(); - double range = FastMath.PI; - double step = range / (nSamplesOut / 2.0); - for (double angle = 0; angle < range; angle += step) { - Vector2D outerCircle = new Vector2D(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) { - Vector2D innerCircle = new Vector2D(1 - FastMath.cos(angle), 1 - FastMath.sin(angle) - 0.5); - points.add(innerCircle.add(generateNoiseVector(dist))); - } - - if (shuffle) { - Collections.shuffle(points, new RandomAdaptor(random)); - } - - return points; - } - - public static List<Vector2D> makeBlobs(int samples, int centers, double clusterStd, - double min, double max, boolean shuffle, RandomGenerator random) { - - NormalDistribution dist = new NormalDistribution(random, 0.0, clusterStd, 1e-9); - - double range = max - min; - Vector2D[] centerPoints = new Vector2D[centers]; - for (int i = 0; i < centers; i++) { - double x = random.nextDouble() * range + min; - double y = random.nextDouble() * range + min; - centerPoints[i] = new Vector2D(x, y); - } - - int[] nSamplesPerCenter = new int[centers]; - int count = samples / centers; - Arrays.fill(nSamplesPerCenter, count); - - for (int i = 0; i < samples % centers; i++) { - nSamplesPerCenter[i]++; - } - - List<Vector2D> points = new ArrayList<Vector2D>(); - for (int i = 0; i < centers; i++) { - for (int j = 0; j < nSamplesPerCenter[i]; j++) { - Vector2D point = new Vector2D(dist.sample(), dist.sample()); - points.add(point.add(centerPoints[i])); - } - } - - if (shuffle) { - Collections.shuffle(points, new RandomAdaptor(random)); - } - - return points; - } - - public static List<Vector2D> makeRandom(int samples) { - SobolSequenceGenerator generator = new SobolSequenceGenerator(2); - generator.skipTo(999999); - List<Vector2D> points = new ArrayList<Vector2D>(); - for (double i = 0; i < samples; i++) { - double[] vector = generator.nextVector(); - vector[0] = vector[0] * 2 - 1; - vector[1] = vector[1] * 2 - 1; - Vector2D point = new Vector2D(vector); - points.add(point); - } - - return points; - } - - public static Vector2D generateNoiseVector(NormalDistribution distribution) { - return new Vector2D(distribution.sample(), distribution.sample()); - } - - public static List<DoublePoint> normalize(final List<Vector2D> input, double minX, double maxX, double minY, double maxY) { - double rangeX = maxX - minX; - double rangeY = maxY - minY; - List<DoublePoint> points = new ArrayList<DoublePoint>(); - for (Vector2D p : input) { - double[] arr = p.toArray(); - arr[0] = (arr[0] - minX) / rangeX * 2 - 1; - arr[1] = (arr[1] - minY) / rangeY * 2 - 1; - points.add(new DoublePoint(arr)); - } - return points; - } - - @SuppressWarnings("serial") - public static class Display extends ExampleFrame { - - public Display() { - setTitle("Commons-Math: Cluster algorithm comparison"); - setSize(800, 800); - - setLayout(new GridBagLayout()); - - int nSamples = 1500; - - RandomGenerator rng = new Well19937c(0); - List<List<DoublePoint>> datasets = new ArrayList<List<DoublePoint>>(); - - datasets.add(normalize(makeCircles(nSamples, true, 0.04, 0.5, rng), -1, 1, -1, 1)); - datasets.add(normalize(makeMoons(nSamples, true, 0.04, rng), -1, 2, -1, 1)); - datasets.add(normalize(makeBlobs(nSamples, 3, 1.0, -10, 10, true, rng), -12, 12, -12, 12)); - datasets.add(normalize(makeRandom(nSamples), -1, 1, -1, 1)); - - List<Pair<String, Clusterer<DoublePoint>>> algorithms = new ArrayList<Pair<String, Clusterer<DoublePoint>>>(); - - algorithms.add(new Pair<String, Clusterer<DoublePoint>>("KMeans\n(k=2)", new KMeansPlusPlusClusterer<DoublePoint>(2))); - algorithms.add(new Pair<String, Clusterer<DoublePoint>>("KMeans\n(k=3)", new KMeansPlusPlusClusterer<DoublePoint>(3))); - algorithms.add(new Pair<String, Clusterer<DoublePoint>>("FuzzyKMeans\n(k=3, fuzzy=2)", new FuzzyKMeansClusterer<DoublePoint>(3, 2))); - algorithms.add(new Pair<String, Clusterer<DoublePoint>>("FuzzyKMeans\n(k=3, fuzzy=10)", new FuzzyKMeansClusterer<DoublePoint>(3, 10))); - algorithms.add(new Pair<String, Clusterer<DoublePoint>>("DBSCAN\n(eps=.1, min=3)", new DBSCANClusterer<DoublePoint>(0.1, 3))); - - GridBagConstraints c = new GridBagConstraints(); - c.fill = GridBagConstraints.VERTICAL; - c.gridx = 0; - c.gridy = 0; - c.insets = new Insets(2, 2, 2, 2); - - for (Pair<String, Clusterer<DoublePoint>> pair : algorithms) { - JLabel text = new JLabel("<html><body>" + pair.getFirst().replace("\n", "<br>")); - add(text, c); - c.gridx++; - } - c.gridy++; - - for (List<DoublePoint> dataset : datasets) { - c.gridx = 0; - for (Pair<String, Clusterer<DoublePoint>> pair : algorithms) { - long start = System.currentTimeMillis(); - List<? extends Cluster<DoublePoint>> clusters = pair.getSecond().cluster(dataset); - long end = System.currentTimeMillis(); - add(new ClusterPlot(clusters, end - start), c); - c.gridx++; - } - c.gridy++; - } - } - - } - - @SuppressWarnings("serial") - public static class ClusterPlot extends JComponent { - - private static double PAD = 10; - - private List<? extends Cluster<DoublePoint>> clusters; - private long duration; - - public ClusterPlot(final List<? extends Cluster<DoublePoint>> clusters, long duration) { - this.clusters = clusters; - this.duration = duration; - } - - @Override - protected void paintComponent(Graphics g) { - super.paintComponent(g); - Graphics2D g2 = (Graphics2D)g; - g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, - RenderingHints.VALUE_ANTIALIAS_ON); - - int w = getWidth(); - int h = getHeight(); - - g2.clearRect(0, 0, w, h); - - g2.setPaint(Color.black); - g2.drawRect(0, 0, w - 1, h - 1); - - int index = 0; - Color[] colors = new Color[] { Color.red, Color.blue, Color.green.darker() }; - for (Cluster<DoublePoint> cluster : clusters) { - g2.setPaint(colors[index++]); - for (DoublePoint point : cluster.getPoints()) { - Clusterable p = transform(point, w, h); - double[] arr = p.getPoint(); - g2.fill(new Ellipse2D.Double(arr[0] - 1, arr[1] - 1, 3, 3)); - } - - if (cluster instanceof CentroidCluster) { - Clusterable p = transform(((CentroidCluster<?>) cluster).getCenter(), w, h); - double[] arr = p.getPoint(); - Shape s = new Ellipse2D.Double(arr[0] - 4, arr[1] - 4, 8, 8); - g2.fill(s); - g2.setPaint(Color.black); - g2.draw(s); - } - } - - g2.setPaint(Color.black); - g2.drawString(String.format("%.2f s", duration / 1e3), w - 40, h - 5); - } - - @Override - public Dimension getPreferredSize() { - return new Dimension(150, 150); - } - - private Clusterable transform(Clusterable point, int width, int height) { - double[] arr = point.getPoint(); - return new DoublePoint(new double[] { PAD + (arr[0] + 1) / 2.0 * (width - 2 * PAD), - height - PAD - (arr[1] + 1) / 2.0 * (height - 2 * PAD) }); - } - } - - public static void main(String[] args) { - ExampleUtils.showExampleFrame(new Display()); - } -} http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/a960a5ca/src/userguide/java/org/apache/commons/math4/userguide/ExampleUtils.java ---------------------------------------------------------------------- diff --git a/src/userguide/java/org/apache/commons/math4/userguide/ExampleUtils.java b/src/userguide/java/org/apache/commons/math4/userguide/ExampleUtils.java deleted file mode 100644 index 1b57361..0000000 --- a/src/userguide/java/org/apache/commons/math4/userguide/ExampleUtils.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.complex.userguide; - -import java.awt.Component; -import java.awt.Graphics2D; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.event.InputEvent; -import java.awt.event.KeyEvent; -import java.awt.image.BufferedImage; -import java.io.File; -import java.io.IOException; - -import javax.imageio.ImageIO; -import javax.swing.JFileChooser; -import javax.swing.JFrame; -import javax.swing.JMenu; -import javax.swing.JMenuBar; -import javax.swing.JMenuItem; -import javax.swing.KeyStroke; -import javax.swing.SwingUtilities; - -public class ExampleUtils { - - @SuppressWarnings("serial") - public static class ExampleFrame extends JFrame { - - /** - * Returns the main panel which should be printed by the screenshot action. - * <p> - * By default, it returns the content pane of this frame, but can be overriden - * in case the frame has a global scroll pane which would cut off any offscreen content. - * - * @return the main panel to print - */ - public Component getMainPanel() { - return getContentPane(); - } - } - - public static void showExampleFrame(final ExampleFrame frame) { - Runnable r = new Runnable() { - public void run() { - JMenuItem screenshot = new JMenuItem("Screenshot (png)"); - screenshot.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_0, InputEvent.CTRL_DOWN_MASK)); - screenshot.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent ae) { - JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir")); - if (fileChooser.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION) { - File file = fileChooser.getSelectedFile(); - BufferedImage img = getScreenShot(frame.getMainPanel()); - try { - // write the image as a PNG - ImageIO.write(img, "png", file); - } catch (Exception e) { - e.printStackTrace(); - } - } - } - }); - - JMenuItem exit = new JMenuItem("Exit"); - exit.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - System.exit(0); - } - }); - - JMenu menu = new JMenu("File"); - menu.add(screenshot); - menu.add(exit); - JMenuBar mb = new JMenuBar(); - mb.add(menu); - frame.setJMenuBar(mb); - - frame.setLocationRelativeTo(null); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.setVisible(true); - } - }; - SwingUtilities.invokeLater(r); - } - - private static BufferedImage getScreenShot(Component component) { - BufferedImage image = new BufferedImage(component.getWidth(), component.getHeight(), BufferedImage.TYPE_INT_RGB); - // call the Component's paint method, using the Graphics object of the image. - component.paint(image.getGraphics()); - return image; - } - - public static BufferedImage resizeImage(BufferedImage originalImage, int width, int height, int type) throws IOException { - BufferedImage resizedImage = new BufferedImage(width, height, type); - Graphics2D g = resizedImage.createGraphics(); - g.drawImage(originalImage, 0, 0, width, height, null); - g.dispose(); - return resizedImage; - } - -} http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/a960a5ca/src/userguide/java/org/apache/commons/math4/userguide/FastMathTestPerformance.java ---------------------------------------------------------------------- diff --git a/src/userguide/java/org/apache/commons/math4/userguide/FastMathTestPerformance.java b/src/userguide/java/org/apache/commons/math4/userguide/FastMathTestPerformance.java deleted file mode 100644 index 1657174..0000000 --- a/src/userguide/java/org/apache/commons/math4/userguide/FastMathTestPerformance.java +++ /dev/null @@ -1,1142 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.complex.userguide; - -import org.apache.commons.complex.PerfTestUtils; -import org.apache.commons.complex.util.FastMath; - -/** - * Performance benchmark for FastMath. - * - */ -public class FastMathTestPerformance { - private static final int RUNS = Integer.parseInt(System.getProperty("testRuns","10000000")); - private static final double F1 = 1d / RUNS; - - // Header format - private static final String FMT_HDR = "%-13s %13s %13s %13s Runs=%d Java %s (%s) %s (%s)"; - // Detail format - private static final String FMT_DTL = "%-13s %6d %6.1f %6d %6.4f %6d %6.4f"; - - public static void main(String[] args) { - System.out.println(String.format(FMT_HDR, - "Name","StrictMath","FastMath","Math",RUNS, - System.getProperty("java.version"), - System.getProperty("java.runtime.version","?"), - System.getProperty("java.vm.name"), - System.getProperty("java.vm.version") - )); - testAbs(); - testAcos(); - testAsin(); - testAtan(); - testAtan2(); - testCbrt(); - testCos(); - testCosh(); - testExp(); - testExpm1(); - testHypot(); - testLog(); - testLog10(); - testLog1p(); - testPow(); - testSin(); - testSinh(); - testSqrt(); - testTan(); - testTanh(); - testIEEEremainder(); - - } - - @SuppressWarnings("boxing") - private static void report(String name, long strictMathTime, long fastMathTime, long mathTime) { - long unitTime = strictMathTime; - System.out.println(String.format(FMT_DTL, - name, - strictMathTime / RUNS, (double) strictMathTime / unitTime, - fastMathTime / RUNS, (double) fastMathTime / unitTime, - mathTime / RUNS, (double) mathTime / unitTime - )); - } - - private static void assertTrue(boolean condition) { - if (!condition) { - System.err.println("assertion failed!"); - System.exit(1); - } - } - private static void testLog() { - double x = 0; - long time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += StrictMath.log(0.01 + i); - } - long strictMath = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += FastMath.log(0.01 + i); - } - long fastTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += Math.log(0.01 + i); - } - long mathTime = System.nanoTime() - time; - - report("log",strictMath,fastTime,mathTime); - assertTrue(!Double.isNaN(x)); - } - - private static void testLog10() { - double x = 0; - long time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += StrictMath.log10(0.01 + i); - } - long strictMath = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += FastMath.log10(0.01 + i); - } - long fastTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += Math.log10(0.01 + i); - } - long mathTime = System.nanoTime() - time; - - report("log10",strictMath,fastTime,mathTime); - assertTrue(!Double.isNaN(x)); - } - - private static void testLog1p() { - double x = 0; - long time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += StrictMath.log1p(-0.9 + i); - } - long strictMath = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += FastMath.log1p(-0.9 + i); - } - long fastTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += Math.log1p(-0.9 + i); - } - long mathTime = System.nanoTime() - time; - - report("log1p",strictMath,fastTime,mathTime); - assertTrue(!Double.isNaN(x)); - } - - private static void testPow() { - double x = 0; - long time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += StrictMath.pow(0.01 + i * F1, i * F1); - } - long strictTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += FastMath.pow(0.01 + i * F1, i * F1); - } - long fastTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += Math.pow(0.01 + i * F1, i * F1); - } - long mathTime = System.nanoTime() - time; - report("pow",strictTime,fastTime,mathTime); - assertTrue(!Double.isNaN(x)); - } - - private static void testExp() { - double x = 0; - long time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += StrictMath.exp(100 * i * F1); - } - long strictTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += FastMath.exp(100 * i * F1); - } - long fastTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += Math.exp(100 * i * F1); - } - long mathTime = System.nanoTime() - time; - - report("exp",strictTime,fastTime,mathTime); - assertTrue(!Double.isNaN(x)); - } - - private static void testSin() { - double x = 0; - long time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += StrictMath.sin(100 * (i - RUNS/2) * F1); - } - long strictTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += FastMath.sin(100 * (i - RUNS/2) * F1); - } - long fastTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += Math.sin(100 * (i - RUNS/2) * F1); - } - long mathTime = System.nanoTime() - time; - - report("sin",strictTime,fastTime,mathTime); - assertTrue(!Double.isNaN(x)); - } - - private static void testAsin() { - double x = 0; - long time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += StrictMath.asin(0.999 * (i - RUNS/2) * F1); - } - long strictTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += FastMath.asin(0.999 * (i - RUNS/2) * F1); - } - long fastTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += Math.asin(0.999 * (i - RUNS/2) * F1); - } - long mathTime = System.nanoTime() - time; - - report("asin",strictTime,fastTime,mathTime); - assertTrue(!Double.isNaN(x)); - } - - private static void testCos() { - double x = 0; - long time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += StrictMath.cos(100 * (i - RUNS/2) * F1); - } - long strictTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += FastMath.cos(100 * (i - RUNS/2) * F1); - } - long fastTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += Math.cos(100 * (i - RUNS/2) * F1); - } - long mathTime = System.nanoTime() - time; - - report("cos",strictTime,fastTime,mathTime); - assertTrue(!Double.isNaN(x)); - } - - private static void testAcos() { - double x = 0; - long time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += StrictMath.acos(0.999 * (i - RUNS/2) * F1); - } - long strictTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += FastMath.acos(0.999 * (i - RUNS/2) * F1); - } - long fastTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += Math.acos(0.999 * (i - RUNS/2) * F1); - } - long mathTime = System.nanoTime() - time; - report("acos",strictTime,fastTime,mathTime); - assertTrue(!Double.isNaN(x)); - } - - private static void testTan() { - double x = 0; - long time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += StrictMath.tan(100 * (i - RUNS/2) * F1); - } - long strictTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += FastMath.tan(100 * (i - RUNS/2) * F1); - } - long fastTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += Math.tan(100 * (i - RUNS/2) * F1); - } - long mathTime = System.nanoTime() - time; - - report("tan",strictTime,fastTime,mathTime); - assertTrue(!Double.isNaN(x)); - } - - private static void testAtan() { - double x = 0; - long time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += StrictMath.atan(100 * (i - RUNS/2) * F1); - } - long strictTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += FastMath.atan(100 * (i - RUNS/2) * F1); - } - long fastTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += Math.atan(100 * (i - RUNS/2) * F1); - } - long mathTime = System.nanoTime() - time; - - report("atan",strictTime,fastTime,mathTime); - assertTrue(!Double.isNaN(x)); - } - - private static void testAtan2() { - double x = 0; - long time = System.nanoTime(); - int max = (int) FastMath.floor(FastMath.sqrt(RUNS)); - for (int i = 0; i < max; i++) { - for (int j = 0; j < max; j++) { - x += StrictMath.atan2((i - max/2) * (100.0 / max), (j - max/2) * (100.0 / max)); - } - } - long strictTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < max; i++) { - for (int j = 0; j < max; j++) { - x += FastMath.atan2((i - max/2) * (100.0 / max), (j - max/2) * (100.0 / max)); - } - } - long fastTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < max; i++) { - for (int j = 0; j < max; j++) { - x += Math.atan2((i - max/2) * (100.0 / max), (j - max/2) * (100.0 / max)); - } - } - long mathTime = System.nanoTime() - time; - - report("atan2",strictTime,fastTime,mathTime); - assertTrue(!Double.isNaN(x)); - } - - private static void testHypot() { - double x = 0; - long time = System.nanoTime(); - int max = (int) FastMath.floor(FastMath.sqrt(RUNS)); - for (int i = 0; i < max; i++) { - for (int j = 0; j < max; j++) { - x += StrictMath.atan2((i - max/2) * (100.0 / max), (j - max/2) * (100.0 / max)); - } - } - long strictTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < max; i++) { - for (int j = 0; j < max; j++) { - x += FastMath.atan2((i - max/2) * (100.0 / max), (j - max/2) * (100.0 / max)); - } - } - long fastTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < max; i++) { - for (int j = 0; j < max; j++) { - x += Math.atan2((i - max/2) * (100.0 / max), (j - max/2) * (100.0 / max)); - } - } - long mathTime = System.nanoTime() - time; - - report("hypot",strictTime,fastTime,mathTime); - assertTrue(!Double.isNaN(x)); - } - - private static void testIEEEremainder() { - double x = 0; - long time = System.nanoTime(); - int max = (int) FastMath.floor(FastMath.sqrt(RUNS)); - for (int i = 0; i < max; i++) { - for (int j = 0; j < max; j++) { - x += StrictMath.IEEEremainder((i - max/2) * (100.0 / max), (j + 1) * (100.0 / max)); - } - } - long strictTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < max; i++) { - for (int j = 0; j < max; j++) { - x += FastMath.IEEEremainder((i - max/2) * (100.0 / max), (j + 1) * (100.0 / max)); - } - } - long fastTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < max; i++) { - for (int j = 0; j < max; j++) { - x += Math.IEEEremainder((i - max/2) * (100.0 / max), (j + 1) * (100.0 / max)); - } - } - long mathTime = System.nanoTime() - time; - - report("IEEEremainder",strictTime,fastTime,mathTime); - assertTrue(!Double.isNaN(x)); - } - - private static void testCbrt() { - double x = 0; - long time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += StrictMath.cbrt(100 * i * F1); - } - long strictTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += FastMath.cbrt(100 * i * F1); - } - long fastTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += Math.cbrt(100 * i * F1); - } - long mathTime = System.nanoTime() - time; - - report("cbrt",strictTime,fastTime,mathTime); - assertTrue(!Double.isNaN(x)); - } - - private static void testSqrt() { - double x = 0; - long time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += StrictMath.sqrt(100 * i * F1); - } - long strictTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += FastMath.sqrt(100 * i * F1); - } - long fastTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += Math.sqrt(100 * i * F1); - } - long mathTime = System.nanoTime() - time; - - report("sqrt",strictTime,fastTime,mathTime); - assertTrue(!Double.isNaN(x)); - } - - private static void testCosh() { - double x = 0; - long time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += StrictMath.cosh(100 * (i - RUNS/2) * F1); - } - long strictTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += FastMath.cosh(100 * (i - RUNS/2) * F1); - } - long fastTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += Math.cosh(100 * (i - RUNS/2) * F1); - } - long mathTime = System.nanoTime() - time; - - report("cosh",strictTime,fastTime,mathTime); - assertTrue(!Double.isNaN(x)); - } - - private static void testSinh() { - double x = 0; - long time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += StrictMath.sinh(100 * (i - RUNS/2) * F1); - } - long strictTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += FastMath.sinh(100 * (i - RUNS/2) * F1); - } - long fastTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += Math.sinh(100 * (i - RUNS/2) * F1); - } - long mathTime = System.nanoTime() - time; - - report("sinh",strictTime,fastTime,mathTime); - assertTrue(!Double.isNaN(x)); - } - - private static void testTanh() { - double x = 0; - long time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += StrictMath.tanh(100 * (i - RUNS/2) * F1); - } - long strictTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += FastMath.tanh(100 * (i - RUNS/2) * F1); - } - long fastTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += Math.tanh(100 * (i - RUNS/2) * F1); - } - long mathTime = System.nanoTime() - time; - - report("tanh",strictTime,fastTime,mathTime); - assertTrue(!Double.isNaN(x)); - } - - private static void testExpm1() { - double x = 0; - long time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += StrictMath.expm1(100 * (i - RUNS/2) * F1); - } - long strictTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += FastMath.expm1(100 * (i - RUNS/2) * F1); - } - long fastTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += Math.expm1(100 * (i - RUNS/2) * F1); - } - long mathTime = System.nanoTime() - time; - report("expm1",strictTime,fastTime,mathTime); - assertTrue(!Double.isNaN(x)); - } - - private static void testAbs() { - double x = 0; - long time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += StrictMath.abs(i * (1 - 0.5 * RUNS)); - } - long strictTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += FastMath.abs(i * (1 - 0.5 * RUNS)); - } - long fastTime = System.nanoTime() - time; - - x = 0; - time = System.nanoTime(); - for (int i = 0; i < RUNS; i++) { - x += Math.abs(i * (1 - 0.5 * RUNS)); - } - long mathTime = System.nanoTime() - time; - - report("abs",strictTime,fastTime,mathTime); - assertTrue(!Double.isNaN(x)); - } - - @SuppressWarnings("boxing") - private static void testSimpleBenchmark() { - final String SM = "StrictMath"; - final String M = "Math"; - final String FM = "FastMath"; - - final int maxWidth = 15; - final int numStat = 100; - final int numCall = RUNS / numStat; - - final double x = Math.random(); - final double y = Math.random(); - - PerfTestUtils.timeAndReport("log", - maxWidth, - numCall, - numStat, - false, - new PerfTestUtils.RunTest(SM) { - @Override - public Double call() throws Exception { - return StrictMath.log(x); - } - }, - new PerfTestUtils.RunTest(M) { - @Override - public Double call() throws Exception { - return Math.log(x); - } - }, - new PerfTestUtils.RunTest(FM) { - @Override - public Double call() throws Exception { - return FastMath.log(x); - } - }); - - PerfTestUtils.timeAndReport("log10", - maxWidth, - numCall, - numStat, - false, - new PerfTestUtils.RunTest(SM) { - @Override - public Double call() throws Exception { - return StrictMath.log10(x); - } - }, - new PerfTestUtils.RunTest(M) { - @Override - public Double call() throws Exception { - return Math.log10(x); - } - }, - new PerfTestUtils.RunTest(FM) { - @Override - public Double call() throws Exception { - return FastMath.log10(x); - } - }); - - PerfTestUtils.timeAndReport("log1p", - maxWidth, - numCall, - numStat, - false, - new PerfTestUtils.RunTest(SM) { - @Override - public Double call() throws Exception { - return StrictMath.log1p(x); - } - }, - new PerfTestUtils.RunTest(M) { - @Override - public Double call() throws Exception { - return Math.log1p(x); - } - }, - new PerfTestUtils.RunTest(FM) { - @Override - public Double call() throws Exception { - return FastMath.log1p(x); - } - }); - - PerfTestUtils.timeAndReport("pow", - maxWidth, - numCall, - numStat, - false, - new PerfTestUtils.RunTest(SM) { - @Override - public Double call() throws Exception { - return StrictMath.pow(x, y); - } - }, - new PerfTestUtils.RunTest(M) { - @Override - public Double call() throws Exception { - return Math.pow(x, y); - } - }, - new PerfTestUtils.RunTest(FM) { - @Override - public Double call() throws Exception { - return FastMath.pow(x, y); - } - }); - - PerfTestUtils.timeAndReport("exp", - maxWidth, - numCall, - numStat, - false, - new PerfTestUtils.RunTest(SM) { - @Override - public Double call() throws Exception { - return StrictMath.exp(x); - } - }, - new PerfTestUtils.RunTest(M) { - @Override - public Double call() throws Exception { - return Math.exp(x); - } - }, - new PerfTestUtils.RunTest(FM) { - @Override - public Double call() throws Exception { - return FastMath.exp(x); - } - }); - - PerfTestUtils.timeAndReport("sin", - maxWidth, - numCall, - numStat, - false, - new PerfTestUtils.RunTest(SM) { - @Override - public Double call() throws Exception { - return StrictMath.sin(x); - } - }, - new PerfTestUtils.RunTest(M) { - @Override - public Double call() throws Exception { - return Math.sin(x); - } - }, - new PerfTestUtils.RunTest(FM) { - @Override - public Double call() throws Exception { - return FastMath.sin(x); - } - }); - - PerfTestUtils.timeAndReport("asin", - maxWidth, - numCall, - numStat, - false, - new PerfTestUtils.RunTest(SM) { - @Override - public Double call() throws Exception { - return StrictMath.asin(x); - } - }, - new PerfTestUtils.RunTest(M) { - @Override - public Double call() throws Exception { - return Math.asin(x); - } - }, - new PerfTestUtils.RunTest(FM) { - @Override - public Double call() throws Exception { - return FastMath.asin(x); - } - }); - - PerfTestUtils.timeAndReport("cos", - maxWidth, - numCall, - numStat, - false, - new PerfTestUtils.RunTest(SM) { - @Override - public Double call() throws Exception { - return StrictMath.cos(x); - } - }, - new PerfTestUtils.RunTest(M) { - @Override - public Double call() throws Exception { - return Math.cos(x); - } - }, - new PerfTestUtils.RunTest(FM) { - @Override - public Double call() throws Exception { - return FastMath.cos(x); - } - }); - - PerfTestUtils.timeAndReport("acos", - maxWidth, - numCall, - numStat, - false, - new PerfTestUtils.RunTest(SM) { - @Override - public Double call() throws Exception { - return StrictMath.acos(x); - } - }, - new PerfTestUtils.RunTest(M) { - @Override - public Double call() throws Exception { - return Math.acos(x); - } - }, - new PerfTestUtils.RunTest(FM) { - @Override - public Double call() throws Exception { - return FastMath.acos(x); - } - }); - - PerfTestUtils.timeAndReport("tan", - maxWidth, - numCall, - numStat, - false, - new PerfTestUtils.RunTest(SM) { - @Override - public Double call() throws Exception { - return StrictMath.tan(x); - } - }, - new PerfTestUtils.RunTest(M) { - @Override - public Double call() throws Exception { - return Math.tan(x); - } - }, - new PerfTestUtils.RunTest(FM) { - @Override - public Double call() throws Exception { - return FastMath.tan(x); - } - }); - - PerfTestUtils.timeAndReport("atan", - maxWidth, - numCall, - numStat, - false, - new PerfTestUtils.RunTest(SM) { - @Override - public Double call() throws Exception { - return StrictMath.atan(x); - } - }, - new PerfTestUtils.RunTest(M) { - @Override - public Double call() throws Exception { - return Math.atan(x); - } - }, - new PerfTestUtils.RunTest(FM) { - @Override - public Double call() throws Exception { - return FastMath.atan(x); - } - }); - - PerfTestUtils.timeAndReport("atan2", - maxWidth, - numCall, - numStat, - false, - new PerfTestUtils.RunTest(SM) { - @Override - public Double call() throws Exception { - return StrictMath.atan2(x, y); - } - }, - new PerfTestUtils.RunTest(M) { - @Override - public Double call() throws Exception { - return Math.atan2(x, y); - } - }, - new PerfTestUtils.RunTest(FM) { - @Override - public Double call() throws Exception { - return FastMath.atan2(x, y); - } - }); - - PerfTestUtils.timeAndReport("hypot", - maxWidth, - numCall, - numStat, - false, - new PerfTestUtils.RunTest(SM) { - @Override - public Double call() throws Exception { - return StrictMath.hypot(x, y); - } - }, - new PerfTestUtils.RunTest(M) { - @Override - public Double call() throws Exception { - return Math.hypot(x, y); - } - }, - new PerfTestUtils.RunTest(FM) { - @Override - public Double call() throws Exception { - return FastMath.hypot(x, y); - } - }); - - - PerfTestUtils.timeAndReport("cbrt", - maxWidth, - numCall, - numStat, - false, - new PerfTestUtils.RunTest(SM) { - @Override - public Double call() throws Exception { - return StrictMath.cbrt(x); - } - }, - new PerfTestUtils.RunTest(M) { - @Override - public Double call() throws Exception { - return Math.cbrt(x); - } - }, - new PerfTestUtils.RunTest(FM) { - @Override - public Double call() throws Exception { - return FastMath.cbrt(x); - } - }); - - PerfTestUtils.timeAndReport("sqrt", - maxWidth, - numCall, - numStat, - false, - new PerfTestUtils.RunTest(SM) { - @Override - public Double call() throws Exception { - return StrictMath.sqrt(x); - } - }, - new PerfTestUtils.RunTest(M) { - @Override - public Double call() throws Exception { - return Math.sqrt(x); - } - }, - new PerfTestUtils.RunTest(FM) { - @Override - public Double call() throws Exception { - return FastMath.sqrt(x); - } - }); - - PerfTestUtils.timeAndReport("cosh", - maxWidth, - numCall, - numStat, - false, - new PerfTestUtils.RunTest(SM) { - @Override - public Double call() throws Exception { - return StrictMath.cosh(x); - } - }, - new PerfTestUtils.RunTest(M) { - @Override - public Double call() throws Exception { - return Math.cosh(x); - } - }, - new PerfTestUtils.RunTest(FM) { - @Override - public Double call() throws Exception { - return FastMath.cosh(x); - } - }); - - PerfTestUtils.timeAndReport("sinh", - maxWidth, - numCall, - numStat, - false, - new PerfTestUtils.RunTest(SM) { - @Override - public Double call() throws Exception { - return StrictMath.sinh(x); - } - }, - new PerfTestUtils.RunTest(M) { - @Override - public Double call() throws Exception { - return Math.sinh(x); - } - }, - new PerfTestUtils.RunTest(FM) { - @Override - public Double call() throws Exception { - return FastMath.sinh(x); - } - }); - - PerfTestUtils.timeAndReport("tanh", - maxWidth, - numCall, - numStat, - false, - new PerfTestUtils.RunTest(SM) { - @Override - public Double call() throws Exception { - return StrictMath.tanh(x); - } - }, - new PerfTestUtils.RunTest(M) { - @Override - public Double call() throws Exception { - return Math.tanh(x); - } - }, - new PerfTestUtils.RunTest(FM) { - @Override - public Double call() throws Exception { - return FastMath.tanh(x); - } - }); - - PerfTestUtils.timeAndReport("expm1", - maxWidth, - numCall, - numStat, - false, - new PerfTestUtils.RunTest(SM) { - @Override - public Double call() throws Exception { - return StrictMath.expm1(x); - } - }, - new PerfTestUtils.RunTest(M) { - @Override - public Double call() throws Exception { - return Math.expm1(x); - } - }, - new PerfTestUtils.RunTest(FM) { - @Override - public Double call() throws Exception { - return FastMath.expm1(x); - } - }); - - PerfTestUtils.timeAndReport("abs", - maxWidth, - numCall, - numStat, - false, - new PerfTestUtils.RunTest(SM) { - @Override - public Double call() throws Exception { - return StrictMath.abs(x); - } - }, - new PerfTestUtils.RunTest(M) { - @Override - public Double call() throws Exception { - return Math.abs(x); - } - }, - new PerfTestUtils.RunTest(FM) { - @Override - public Double call() throws Exception { - return FastMath.abs(x); - } - }); - } -} http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/a960a5ca/src/userguide/java/org/apache/commons/math4/userguide/IntegerDistributionComparison.java ---------------------------------------------------------------------- diff --git a/src/userguide/java/org/apache/commons/math4/userguide/IntegerDistributionComparison.java b/src/userguide/java/org/apache/commons/math4/userguide/IntegerDistributionComparison.java deleted file mode 100644 index 9e3c423..0000000 --- a/src/userguide/java/org/apache/commons/math4/userguide/IntegerDistributionComparison.java +++ /dev/null @@ -1,239 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.complex.userguide; - -import java.awt.BasicStroke; -import java.awt.Color; -import java.awt.Component; -import java.awt.Font; -import java.awt.GridBagConstraints; -import java.awt.GridBagLayout; -import java.awt.Insets; -import java.util.ArrayList; -import java.util.List; - -import javax.swing.BorderFactory; -import javax.swing.BoxLayout; -import javax.swing.JComponent; -import javax.swing.JLabel; -import javax.swing.JPanel; -import javax.swing.JScrollPane; - -import org.apache.commons.complex.distribution.BinomialDistribution; -import org.apache.commons.complex.distribution.GeometricDistribution; -import org.apache.commons.complex.distribution.HypergeometricDistribution; -import org.apache.commons.complex.distribution.IntegerDistribution; -import org.apache.commons.complex.distribution.PascalDistribution; -import org.apache.commons.complex.distribution.PoissonDistribution; -import org.apache.commons.complex.distribution.UniformIntegerDistribution; -import org.apache.commons.complex.distribution.ZipfDistribution; -import org.apache.commons.complex.userguide.ExampleUtils.ExampleFrame; - -import com.xeiam.xchart.Chart; -import com.xeiam.xchart.ChartBuilder; -import com.xeiam.xchart.Series; -import com.xeiam.xchart.SeriesMarker; -import com.xeiam.xchart.StyleManager.ChartType; -import com.xeiam.xchart.StyleManager.LegendPosition; -import com.xeiam.xchart.XChartPanel; - -/** - * Displays pdf/cdf for integer distributions. - */ -public class IntegerDistributionComparison { - - public static void addPDFSeries(Chart chart, IntegerDistribution distribution, String desc, int lowerBound, int upperBound) { - // generates Log data - List<Number> xData = new ArrayList<Number>(); - List<Number> yData = new ArrayList<Number>(); - for (int x = lowerBound; x <= upperBound; x += 1) { - try { - double probability = distribution.probability(x); - if (! Double.isInfinite(probability) && ! Double.isNaN(probability)) { - xData.add(x); - yData.add(probability); - } - } catch (Exception e) { - // ignore - // some distributions may reject certain values depending on the parameter settings - } - } - - Series series = chart.addSeries(desc, xData, yData); - series.setMarker(SeriesMarker.NONE); - series.setLineStyle(new BasicStroke(1.2f)); - } - - public static void addCDFSeries(Chart chart, IntegerDistribution distribution, String desc, - int lowerBound, int upperBound) { - // generates Log data - List<Number> xData = new ArrayList<Number>(); - List<Number> yData = new ArrayList<Number>(); - for (int x = lowerBound; x <= upperBound; x += 1) { - double density = distribution.cumulativeProbability(x); - if (! Double.isInfinite(density) && ! Double.isNaN(density)) { - xData.add(x); - yData.add(density); - } - } - - Series series = chart.addSeries(desc, xData, yData); - series.setMarker(SeriesMarker.NONE); - series.setLineStyle(new BasicStroke(1.2f)); - } - - public static Chart createChart(String title, int minX, int maxX, LegendPosition position) { - Chart chart = new ChartBuilder().width(235).height(200).build(); - - // Customize Chart - chart.setChartTitle(title); - chart.getStyleManager().setChartTitleVisible(true); - chart.getStyleManager().setChartTitleFont(new Font("Arial", Font.PLAIN, 10)); - chart.getStyleManager().setLegendPosition(position); - chart.getStyleManager().setLegendVisible(true); - chart.getStyleManager().setLegendFont(new Font("Arial", Font.PLAIN, 10)); - chart.getStyleManager().setLegendPadding(6); - chart.getStyleManager().setLegendSeriesLineLength(6); - chart.getStyleManager().setAxisTickLabelsFont(new Font("Arial", Font.PLAIN, 9)); - - chart.getStyleManager().setXAxisMin(minX); - chart.getStyleManager().setXAxisMax(maxX); - chart.getStyleManager().setChartBackgroundColor(Color.white); - chart.getStyleManager().setChartPadding(4); - - chart.getStyleManager().setChartType(ChartType.Line); - return chart; - } - - public static JComponent createComponent(String distributionName, int minX, int maxX, String[] seriesText, - IntegerDistribution... series) { - JComponent container = new JPanel(); - container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS)); - - container.add(new JLabel(distributionName)); - - Chart chart = createChart("PDF", minX, maxX, LegendPosition.InsideNE); - int i = 0; - for (IntegerDistribution 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) { - addCDFSeries(chart, d, seriesText[i++], minX, maxX); - } - container.add(new XChartPanel(chart)); - - container.setBorder(BorderFactory.createLineBorder(Color.black, 1)); - return container; - } - - @SuppressWarnings("serial") - public static class Display extends ExampleFrame { - - private JComponent container; - - public Display() { - setTitle("Commons-Math: Integer distributions overview"); - setSize(1320, 920); - - container = new JPanel(); - container.setLayout(new GridBagLayout()); - - GridBagConstraints c = new GridBagConstraints(); - c.fill = GridBagConstraints.VERTICAL; - c.gridx = 0; - c.gridy = 0; - c.insets = new Insets(2, 2, 2, 2); - - JComponent comp = null; - - comp = createComponent("Binomial", 0, 40, - new String[] { "p=0.5,n=20", "p=0.7,n=20", "p=0.5,n=40" }, - new BinomialDistribution(20, 0.5), - new BinomialDistribution(20, 0.7), - new BinomialDistribution(40, 0.5)); - container.add(comp, c); - - c.gridx++; - comp = createComponent("Geometric", 0, 10, - new String[] { "p=0.2", "p=0.5", "p=0.8" }, - new GeometricDistribution(0.2), - new GeometricDistribution(0.5), - new GeometricDistribution(0.8)); - container.add(comp, c); - - c.gridx++; - comp = createComponent("Hypergeometric", 0, 10, - new String[] { "p=0.3", "p=0.5", "p=0.75" }, - new HypergeometricDistribution(100, 6, 20), - new HypergeometricDistribution(100, 10, 20), - new HypergeometricDistribution(100, 15, 20)); - container.add(comp, c); - - c.gridx++; - comp = createComponent("Pascal", 0, 50, - new String[] { "p=0.3", "p=0.5", "p=0.7" }, - new PascalDistribution(10, 0.3), - new PascalDistribution(10, 0.5), - new PascalDistribution(10, 0.7)); - container.add(comp, c); - - c.gridy++; - c.gridx = 0; - comp = createComponent("Poisson", 0, 20, - new String[] { "λ=1", "λ=4", "λ=10" }, - new PoissonDistribution(1), - new PoissonDistribution(4), - new PoissonDistribution(10)); - container.add(comp, c); - - 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)); - container.add(comp, c); - - c.gridx++; - comp = createComponent("Zipf", 0, 15, - new String[] { "n=10,e=0.5", "n=10,e=1", "n=10,e=2", "n=10,e=5" }, - new ZipfDistribution(10, 0.5), - new ZipfDistribution(10, 1), - new ZipfDistribution(10, 2), - new ZipfDistribution(10, 5)); - container.add(comp, c); - - JScrollPane scrollPane = new JScrollPane(container); - add(scrollPane); - - } - - @Override - public Component getMainPanel() { - return container; - } - - } - - public static void main(String[] args) { - ExampleUtils.showExampleFrame(new Display()); - } -}
