Example usage for org.apache.commons.math.linear RealVector getDistance

List of usage examples for org.apache.commons.math.linear RealVector getDistance

Introduction

In this page you can find the example usage for org.apache.commons.math.linear RealVector getDistance.

Prototype

double getDistance(double[] v);

Source Link

Document

Distance between two vectors.

Usage

From source file:datafu.pig.hash.lsh.metric.L2.java

public static double distance(RealVector v1, RealVector v2) {
    return v1.getDistance(v2);
}

From source file:it.incipict.tool.profiles.util.ProfilesToolUtils.java

public static Map<ProfileType, Double> getEuclideanDistances(Survey survey, List<Profile> profilesList) {
    Map<ProfileType, Double> DistancesMap = new HashMap<ProfileType, Double>();
    int size = survey.getAnswersRealVector().size();
    Double[] answers, ranks;/*from w  w w . j  a  v  a 2 s .  com*/
    answers = survey.getAnswersRealVector().toArray(new Double[size]);

    RealVector answersRealVector = new ArrayRealVector(answers);

    for (Profile profile : profilesList) {
        size = profile.getRanks().size();
        ranks = profile.getRanks().toArray(new Double[size]);

        RealVector ranksRealVector = new ArrayRealVector(ranks);

        // Calculate Euclidea Distance
        double distance = answersRealVector.getDistance(ranksRealVector);
        // add "distance" to Distance's Map
        DistancesMap.put(profile.getProfileType(), distance);
    }
    return DistancesMap;
}

From source file:net.ostis.scpdev.scg.geometry.GraphLayout.java

private void calculateForces() {
    int n = nodes.size();

    RealVector[] forces = new RealVector[n];
    for (int i = 0; i < n; ++i)
        forces[i] = new ArrayRealVector(new double[] { 0, 0 });

    Map<IFigure, Integer> obj_f = new HashMap<IFigure, Integer>();
    Point[] o_pos = new Point[n];

    ///*from ww  w. j av  a2  s  .co  m*/
    // Calculation repulsion forces.
    //
    for (int idx = 0; idx < n; ++idx) {
        obj_f.put(nodes.get(idx), idx);

        Point p1 = nodes.get(idx).getBounds().getLocation();

        RealVector p1v = new ArrayRealVector(2);
        p1v.setEntry(0, p1.x);
        p1v.setEntry(0, p1.y);

        double l = nullVector.getDistance(p1v);

        RealVector f = p1v.mapMultiply(gravity * (l - 3.0));

        forces[idx].subtract(f);

        for (int jdx = idx + 1; jdx < n; ++jdx) {
            Point p2 = nodes.get(idx).getBounds().getLocation();

            RealVector p2v = new ArrayRealVector(2);
            p2v.setEntry(0, p2.x);
            p2v.setEntry(0, p2.y);

            l = p1v.getDistance(p2v);

            if (l > max_rep_length)
                continue;

            if (l > 0.5) {
                f = p1v.subtract(p2v).mapMultiply(repulsion / l / l);
            } else {
                f = new ArrayRealVector(new double[] { Math.cos(0.17 * idx) * length * 7,
                        Math.sin(0.17 * (idx + 1)) * length * 7 });
            }

            forces[idx].add(f);
            forces[jdx].subtract(f);
        }
    }

    for (int idx = 0; idx < n; ++idx) {
        RealVector f = forces[idx];
        f.mapMultiply(stepSize);
        nodes.get(idx).setLocation(new Point(f.getEntry(0), f.getEntry(1)));
    }
}

From source file:net.ostis.scpdev.scg.geometry.LayoutRunnable.java

private void calculateForces() {
    int n = nodes.size();

    RealVector[] forces = new RealVector[n];
    for (int i = 0; i < n; ++i)
        forces[i] = new ArrayRealVector(nullVector);

    Map<IFigure, Integer> obj_f = new HashMap<IFigure, Integer>();

    //// w ww . ja  v  a2s. co  m
    // Calculation repulsion forces.
    //
    for (int idx = 0; idx < n; ++idx) {
        SCgNodeShape obj = nodes.get(idx);

        obj_f.put(obj, idx);

        Point p1 = translateToCenter(obj.getBounds().getLocation());

        RealVector p1v = new ArrayRealVector(2);
        p1v.setEntry(0, p1.x);
        p1v.setEntry(1, p1.y);

        double l = nullVector.getDistance(p1v);

        RealVector f = p1v.mapMultiply(gravity * (l - 3.0));

        forces[idx] = forces[idx].subtract(f);

        for (int jdx = idx + 1; jdx < n; ++jdx) {
            Point p2 = translateToCenter(nodes.get(jdx).getBounds().getLocation());

            RealVector p2v = new ArrayRealVector(2);
            p2v.setEntry(0, p2.x);
            p2v.setEntry(1, p2.y);

            l = p1v.getDistance(p2v) / 50;

            if (l > max_rep_length)
                continue;

            if (l > 0.5) {
                f = p1v.subtract(p2v).mapMultiply(repulsion / l / l);
            } else {
                f = new ArrayRealVector(new double[] { Math.cos(0.17 * idx) * length * 7,
                        Math.sin(0.17 * (idx + 1)) * length * 7 });
            }

            forces[idx] = forces[idx].add(f);
            forces[jdx] = forces[jdx].subtract(f);
        }
    }

    //
    // Calculation springs.
    //
    for (SCgPairConnection line : lines) {
        SCgPair pair = line.getModel();

        SCgObject begin = pair.getBegin();
        SCgObject end = pair.getEnd();

        if (begin == null || end == null)
            continue;

        IFigure beginFigure = obj2figure.get(begin);
        IFigure endFigure = obj2figure.get(end);

        Point p1 = translateToCenter(beginFigure.getBounds().getLocation());
        Point p2 = translateToCenter(endFigure.getBounds().getLocation());

        RealVector p1v = new ArrayRealVector(2);
        p1v.setEntry(0, p1.x);
        p1v.setEntry(1, p1.y);

        RealVector p2v = new ArrayRealVector(2);
        p2v.setEntry(0, p2.x);
        p2v.setEntry(1, p2.y);

        double l = p1v.getDistance(p2v) / 50;

        RealVector f = null;

        if (l > 0) {
            RealVector pv = p2v.subtract(p1v);
            pv.unitize();

            int cnt = begin.getInputCount() + end.getOutputCount();
            f = pv.mapMultiply(rigidity * (l - Math.max(length, cnt / 3.0)) / l);

            if (nullVector.getDistance(f) > 10)
                f = pv.mapMultiply(rigidity / l);
        } else {
            f = new ArrayRealVector(nullVector);
        }

        if (obj_f.containsKey(beginFigure)) {
            int index = obj_f.get(beginFigure);
            forces[index] = forces[index].add(f);
        }

        if (obj_f.containsKey(endFigure)) {
            int index = obj_f.get(endFigure);
            forces[index] = forces[index].subtract(f);
        }
    }

    double maxf = 0.0;

    for (int idx = 0; idx < n; ++idx) {
        RealVector f = forces[idx];
        f.mapMultiplyToSelf(stepSize);

        maxf = Math.max(maxf, nullVector.getDistance(f));

        IFigure node = nodes.get(idx);
        Point location = translateToCenter(node.getBounds().getLocation());
        location.x += f.getEntry(0);
        location.y += f.getEntry(1);
        node.setLocation(translateFromCenter(location));
    }

    if (maxf > maxForce) {
        stepSize = stepMaxSize;
    } else {
        stepSize *= 0.97;
    }

    needLayout = stepSize > stepMinSize;
}

From source file:it.univaq.incipict.profilemanager.common.utility.Utility.java

public static HashMap<Profile, Double> getEuclideanDistances(List<Profile> profilesList, User user) {
    Map<Profile, Double> result = new HashMap<Profile, Double>();
    Set<ProfileInformation> profileInformationSet;
    Set<Information> userInformationSet;

    // Retrieve user information set
    userInformationSet = user.getInformationSet();
    if (userInformationSet.isEmpty()) {
        return (HashMap<Profile, Double>) result;
    }/*from ww w . j a va  2 s .co  m*/

    // For each Profile
    for (Profile profile : profilesList) {
        profileInformationSet = profile.getProfileInformationSet();
        int vectorsLenght = Math.max(profileInformationSet.size(), userInformationSet.size());
        RealVector ranksRealVector = new ArrayRealVector(new double[] {});
        RealVector userInformationVector = new ArrayRealVector(new double[] {});

        // Loop userInformationSet and
        // profileInformationSet (i.e. one specific column vector in the
        // knowledge base representation)
        for (Information information : userInformationSet) {
            Long x = information.getId();
            for (ProfileInformation profileInformation : profileInformationSet) {
                Long y = profileInformation.getInformation().getId();
                // User selected information was stored in a RealVector at same
                // position of relative ranksVector
                // This permit to calculate Euclidean distance right.
                if (x == y) {
                    userInformationVector = userInformationVector.append(1d); // Associated:1, Else:0
                    ranksRealVector = ranksRealVector.append(profileInformation.getRank());

                    profileInformationSet.remove(profileInformation);
                    break;
                }
            }
        }
        // At this point we aren't interested to elements position
        // because every other information worth zero.
        // Euclidean distance are not influenced from position of 0-elements in
        // a "sub-vector".
        // if they are all zeros.
        // => Append the zeros until completion of the length of the vectors
        userInformationVector = userInformationVector
                .append(new double[vectorsLenght - userInformationSet.size()]);

        for (ProfileInformation profileInformation : profileInformationSet) {
            // Append the remaining elements of this set (profileInformationSet)
            ranksRealVector = ranksRealVector.append(profileInformation.getRank());
        }

        // Calculate Euclidean Distance
        double distance = userInformationVector.getDistance(ranksRealVector);
        // add the distance to Distance's Map
        result.put(profile, distance);

    } // END, goto Next Profile

    // return the HashMap sorted by value (ASC)
    return (HashMap<Profile, Double>) MapUtil.sortByValueASC(result);
}