Example usage for org.apache.commons.math3.util MathArrays distance

List of usage examples for org.apache.commons.math3.util MathArrays distance

Introduction

In this page you can find the example usage for org.apache.commons.math3.util MathArrays distance.

Prototype

public static double distance(int[] p1, int[] p2) 

Source Link

Document

Calculates the L2 (Euclidean) distance between two points.

Usage

From source file:conceptor.chaos.Lyapunov.java

/**
   Given a DynamicalSystem, returns the maximul lyapunov exponent
   using the method outline by J.C. Sprott see:
   http://sprott.physics.wisc.edu/chaos/lyapexp.htm
 *///from w ww  . ja  va2  s. co m
public static double computeLargestExponent(DynamicalSystem system) {

    double d0 = 10e-8;

    // First, evolve the system such that it's very likely to be on
    // the attractor (1000 iterations is a decent guess)
    evolveSystem(system, 10000);

    // Next, pick a test point a distance d0 away
    double[] testPoint = getTestPoint(system, d0);
    double[] x = system.getState();

    double d1 = 0.0;
    double l = 0.0;

    // Evolve both the test point and the original
    for (int i = 0; i < 64100; i++) {

        testPoint = system.evolveOther(testPoint);
        x = system.evolve();

        // Test for unbounded orbits
        if (isUnbounded(x)) {
            return Double.NaN;
        }

        // Compute distance
        d1 = MathArrays.distance(testPoint, x);

        // Compute lyapunov exponent this round
        if (i > 100) {
            l = l + Math.log(d1 / d0);
        }

        // Renormalize test point
        for (int j = 0; j < x.length; j++) {
            testPoint[j] = x[j] + (d0 / d1) * (testPoint[j] - x[j]);
        }
    }
    return l / (63999 * system.getStepSize());
}

From source file:it.unibo.alchemist.model.implementations.positions.ContinuousGenericEuclidean.java

@Override
public double getDistanceTo(final Position p) {
    final double[] coord = p.getCartesianCoordinates();
    if (c.length == coord.length) {
        return MathArrays.distance(c, coord);
    } else {/*from   w w w.j  a  va  2s .  c o  m*/
        throw new UncomparableDistancesException(this, p);
    }
}

From source file:iDynoOptimizer.MOEAFramework26.src.org.moeaframework.util.ReferenceSetMerger.java

/**
 * Adds the population from the specified source.
 * //from   www.j  av a 2s  .c o m
 * @param source the source of the population
 * @param population the population
 * @throws IllegalArgumentException if a population has been added
 *         previously with the specified source
 */
public void add(String source, Population population) {
    if (populations.containsKey(source)) {
        throw new IllegalArgumentException("source already exists");
    }

    populations.put(source, population);

    for (Solution solution : population) {
        solution.setAttribute(SOURCE_ATTRIBUTE, source);

        //print warning if duplicate solutions found
        for (Solution s : combinedPopulation) {
            if (MathArrays.distance(s.getObjectives(), solution.getObjectives()) < Settings.EPS) {
                System.err.println("duplicate solution found");
            }
        }

        combinedPopulation.add(solution);
    }
}

From source file:iDynoOptimizer.MOEAFramework26.src.org.moeaframework.util.weights.RandomGenerator.java

/**
 * Returns the weights for problems of arbitrary dimension.
 * //from w  w w .  jav  a  2 s . c o m
 * @return the weights
 */
private List<double[]> initializeWeightsND() {
    int N = 50;
    List<double[]> candidates = new ArrayList<double[]>(numberOfPoints * N);

    // create random weights
    for (int i = 0; i < numberOfPoints * N; i++) {
        double[] weight = new double[numberOfObjectives];

        for (int j = 0; j < numberOfObjectives; j++) {
            weight[j] = PRNG.nextDouble();
        }

        double sum = StatUtils.sum(weight);

        for (int j = 0; j < numberOfObjectives; j++) {
            weight[j] /= sum;
        }

        candidates.add(weight);
    }

    List<double[]> weights = new ArrayList<double[]>(numberOfPoints * N);

    // add boundary weights (1,0,...,0), (0,1,...,0), ..., (0,...,0,1)
    for (int i = 0; i < numberOfObjectives; i++) {
        double[] weight = new double[numberOfObjectives];
        weight[i] = 1.0;
        weights.add(weight);
    }

    // fill in remaining weights with the weight vector with the largest
    // distance from the assigned weights
    while (weights.size() < numberOfPoints) {
        double[] weight = null;
        double distance = Double.NEGATIVE_INFINITY;

        for (int i = 0; i < candidates.size(); i++) {
            double d = Double.POSITIVE_INFINITY;

            for (int j = 0; j < weights.size(); j++) {
                d = Math.min(d, MathArrays.distance(candidates.get(i), weights.get(j)));
            }

            if (d > distance) {
                weight = candidates.get(i);
                distance = d;
            }
        }

        weights.add(weight);
        candidates.remove(weight);
    }

    return weights;
}

From source file:iDynoOptimizer.MOEAFramework26.src.org.moeaframework.core.indicator.Contribution.java

@Override
public double evaluate(NondominatedPopulation approximationSet) {
    int count = 0;

    for (Solution solution1 : referenceSet) {
        boolean match = false;

        for (Solution solution2 : approximationSet) {
            if (comparator == null) {
                double distance = MathArrays.distance(solution1.getObjectives(), solution2.getObjectives());

                if (distance < Settings.EPS) {
                    match = true;//from  ww  w  . j a  v a  2 s .c  o  m
                    break;
                }
            } else {
                comparator.compare(solution1, solution2);

                if (comparator.isSameBox()) {
                    match = true;
                    break;
                }
            }
        }

        if (match) {
            count++;
        }
    }

    return count / (double) referenceSet.size();
}

From source file:it.unibo.alchemist.model.implementations.utils.RectObstacle2D.java

@Override
@SuppressFBWarnings("FE_FLOATING_POINT_EQUALITY")
public double[] nearestIntersection(final double startx, final double starty, final double endx,
        final double endy) {
    final double nearx = closestTo(startx, maxX, minX);
    final double neary = closestTo(starty, maxY, minY);
    final double farx = nearx == maxX ? minX : maxX;
    final double fary = neary == maxY ? minY : maxY;
    final double[] intersectionSide1 = intersection(startx, starty, endx, endy, nearx, neary, nearx, fary);
    final double[] intersectionSide2 = intersection(startx, starty, endx, endy, nearx, neary, farx, neary);
    final double d1 = MathArrays.distance(intersectionSide1, new double[] { startx, starty });
    final double d2 = MathArrays.distance(intersectionSide2, new double[] { startx, starty });
    if (d1 < d2) {
        return intersectionSide1;
    }//  www  . j  a v  a2 s .  c  o m
    return intersectionSide2;
}

From source file:br.fapesp.myutils.MyUtils.java

/**
 * Compute the Euclidean dissimilarity matrix on data
 * /*www.j a  v a2s .c  om*/
 * @param N data points in R^n
 * @return an NxN matrix containing the Euclidean distance among points.
 */
public static double[][] getEuclideanMatrix(double[][] data) {
    int N = data.length;
    double[][] dist = new double[N][N];
    double d;

    for (int i = 0; i < N - 1; i++) {
        for (int j = i + 1; j < N; j++) {
            d = MathArrays.distance(data[i], data[j]);
            dist[i][j] = d;
            dist[j][i] = d;
        }
    }

    return (dist);
}

From source file:org.bonej.wrapperPlugins.AnalyseSkeletonWrapper.java

private void showAdditionalResults(final SkeletonResult results) {
    if (!verbose) {
        return;/*ww w . j a  v a2s  . c  om*/
    }
    final DefaultGenericTable table = new DefaultGenericTable();
    final List<PrimitiveColumn<?, ?>> columns = Arrays.asList(new IntColumn("# Skeleton"),
            new IntColumn("# Branch"), new DoubleColumn("Branch length"), new IntColumn("V1 x"),
            new IntColumn("V1 y"), new IntColumn("V1 z"), new IntColumn("V2 x"), new IntColumn("V2 y"),
            new IntColumn("V2 z"), new DoubleColumn("Euclidean distance"),
            new DoubleColumn("running average length"), new DoubleColumn("average intensity (inner 3rd)"),
            new DoubleColumn("average intensity"));
    final Graph[] graphs = results.getGraph();
    for (int i = 0; i < graphs.length; i++) {
        final ArrayList<Edge> edges = graphs[i].getEdges();
        // Sort into descending order by length
        edges.sort((a, b) -> -Double.compare(a.getLength(), b.getLength()));
        for (int j = 0; j < edges.size(); j++) {
            final Edge edge = edges.get(j);
            ((IntColumn) columns.get(0)).add((i + 1));
            ((IntColumn) columns.get(1)).add((j + 1));
            ((DoubleColumn) columns.get(2)).add((edge.getLength()));
            final Point point = edge.getV1().getPoints().get(0);
            ((IntColumn) columns.get(3)).add((point.x));
            ((IntColumn) columns.get(4)).add((point.y));
            ((IntColumn) columns.get(5)).add((point.z));
            final Point point2 = edge.getV2().getPoints().get(0);
            ((IntColumn) columns.get(6)).add((point2.x));
            ((IntColumn) columns.get(7)).add((point2.y));
            ((IntColumn) columns.get(8)).add((point2.z));
            final double distance = MathArrays.distance(new double[] { point.x, point.y, point.z },
                    new double[] { point2.x, point2.y, point2.z });
            ((DoubleColumn) columns.get(9)).add((distance));
            ((DoubleColumn) columns.get(10)).add((edge.getLength_ra()));
            ((DoubleColumn) columns.get(11)).add((edge.getColor3rd()));
            ((DoubleColumn) columns.get(12)).add((edge.getColor()));
        }
    }
    table.addAll(columns);
    if (!table.isEmpty()) {
        verboseTable = table;
    }
}

From source file:org.moeaframework.problem.RotatedProblemsTest.java

private void assertNotEquals(Problem problemA, Problem problemB) {
    Initialization initialization = new RandomInitialization(problemA, 1);

    for (int i = 0; i < TestThresholds.SAMPLES; i++) {
        Solution solutionA = initialization.initialize()[0];
        Solution solutionB = solutionA.copy();

        problemA.evaluate(solutionA);//from  w  w  w  . ja  v a  2s.  com
        problemB.evaluate(solutionB);

        Assert.assertTrue(
                MathArrays.distance(solutionA.getObjectives(), solutionB.getObjectives()) > Settings.EPS);
    }
}

From source file:org.um.feri.ears.util.RandomGenerator.java

/**
 * Returns the weights for problems of arbitrary dimension.
 * /*w w  w.ja va2s.c  om*/
 * @return the weights
 */
private double[][] initializeWeightsND() {
    int N = 50;
    List<double[]> candidates = new ArrayList<double[]>(numberOfPoints * N);

    // create random weights
    for (int i = 0; i < numberOfPoints * N; i++) {
        double[] weight = new double[numberOfObjectives];

        for (int j = 0; j < numberOfObjectives; j++) {
            weight[j] = Util.nextDouble();
        }

        double sum = StatUtils.sum(weight);

        for (int j = 0; j < numberOfObjectives; j++) {
            weight[j] /= sum;
        }

        candidates.add(weight);
    }

    List<double[]> weights = new ArrayList<double[]>(numberOfPoints * N);

    // add boundary weights (1,0,...,0), (0,1,...,0), ..., (0,...,0,1)
    for (int i = 0; i < numberOfObjectives; i++) {
        double[] weight = new double[numberOfObjectives];
        weight[i] = 1.0;
        weights.add(weight);
    }

    // fill in remaining weights with the weight vector with the largest
    // distance from the assigned weights
    while (weights.size() < numberOfPoints) {
        double[] weight = null;
        double distance = Double.NEGATIVE_INFINITY;

        for (int i = 0; i < candidates.size(); i++) {
            double d = Double.POSITIVE_INFINITY;

            for (int j = 0; j < weights.size(); j++) {
                d = Math.min(d, MathArrays.distance(candidates.get(i), weights.get(j)));
            }

            if (d > distance) {
                weight = candidates.get(i);
                distance = d;
            }
        }

        weights.add(weight);
        candidates.remove(weight);
    }

    double[][] a = new double[numberOfPoints][numberOfObjectives];

    for (int i = 0; i < weights.size(); i++) {
        a[i] = weights.get(i);
    }

    return a;
}