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

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

Introduction

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

Prototype

RealVector append(double[] a);

Source Link

Document

Construct a vector by appending a double array to this vector.

Usage

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;
    }//ww  w  .  j  av a 2s . c o 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);
}