Example usage for org.apache.commons.math.linear ArrayRealVector ArrayRealVector

List of usage examples for org.apache.commons.math.linear ArrayRealVector ArrayRealVector

Introduction

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

Prototype

public ArrayRealVector(ArrayRealVector v) 

Source Link

Document

Construct a vector from another vector, using a deep copy.

Usage

From source file:com.zinnia.nectar.util.math.MatrixSolver.java

public static double[] solveMatrix(double[][] coefficientMatrix, double[] rhsVector) {
    RealVector x = null;/* www .j  av a 2 s  . com*/
    try {
        RealMatrix a = new Array2DRowRealMatrix(coefficientMatrix);
        RealVector b = new ArrayRealVector(rhsVector);
        DecompositionSolver solver = new LUDecompositionImpl(a).getSolver();
        x = solver.solve(b);
    } catch (Exception e) {
        e.printStackTrace();
    }

    double[] result;
    result = x.toArray();

    return result;
}

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  .  ja v  a  2 s . 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);
}

From source file:fi.smaa.libror.PerformanceMatrixTest.java

@Test
public void testGetLevels() {
    RealVector[] lvls = matrix.getLevels();
    assertEquals(new ArrayRealVector(new double[] { 37.0, 38.0, 39.0, 40.0, 41.0, 42.0, 44.0, 45.0, 47.0, 50.0,
            51.0, 59.0, 74.0, 82.0 }), lvls[0]);
    assertEquals(new ArrayRealVector(new double[] { 21.0, 26.0, 28.0, 34.0, 36.0, 37.0, 40.0, 41.0, 43.0, 50.0,
            53.0, 57.0, 59.0, 61.0, 73.0, 77.0, 91.0, 94.0 }), lvls[1]);
}

From source file:fi.smaa.libror.PerformanceMatrix.java

private void initializeLevels(RealMatrix perfMatrix) {
    levels = new RealVector[perfMatrix.getColumnDimension()];
    for (int i = 0; i < levels.length; i++) {
        Set<Double> levelsSet = new TreeSet<Double>();
        for (double d : perfMatrix.getColumn(i)) {
            levelsSet.add(d);/*from   w  w  w.j ava2s  . c  om*/
        }
        RealVector lvl = new ArrayRealVector(levelsSet.toArray(new Double[0]));
        levels[i] = lvl;
    }
}

From source file:datafu.pig.hash.lsh.RepeatingLSH.java

public RepeatingLSH(List<LSH> lshList) throws MathException {
    super(lshList.get(0).getDim(), lshList.get(0).getRandomGenerator());
    this.lshList = lshList;
    RandomGenerator rg = lshList.get(0).getRandomGenerator();
    RandomData rd = new RandomDataImpl(rg);
    /*// w  ww.  ja  v  a2 s.  com
     * Compute a random vector of lshList.size() with each component taken from U(0,10)
     */
    randomVec = new ArrayRealVector(lshList.size());
    for (int i = 0; i < randomVec.getDimension(); ++i) {
        randomVec.setEntry(i, rd.nextUniform(0, 10.0));
    }
}

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];

    ////  w ww  .j a  v a2  s.c  o 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:datafu.pig.hash.lsh.util.DataTypeUtil.java

private RealVector convertTuple(Tuple t, int dim) throws PigException {
    double[] values = new double[dim];
    for (int i = 0; i < t.size(); ++i) {
        values[i] = ((Number) t.get(i)).doubleValue();
    }/* w w  w  .ja  va 2  s.c om*/
    return new ArrayRealVector(values);
}

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>();

    ///*from   w w w . j  a v  a 2s.  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:com.opengamma.analytics.math.util.wrapper.CommonsMathWrapper.java

/**
 * @param x An OG vector of doubles, not null
 * @return A Commons vector/*from  w  ww .  java  2 s  . c  o  m*/
 */
public static RealVector wrap(final DoubleMatrix1D x) {
    Validate.notNull(x);
    return new ArrayRealVector(x.getData());
}

From source file:datafu.pig.hash.lsh.cosine.HyperplaneLSH.java

/**
 * Locality sensitive hash that maps vectors onto 0,1 in such a way that colliding
 * vectors are "near" one another according to cosine similarity with high probability.  
 * /*from   w  w  w .j av  a 2  s.c  om*/
 * <p>
 * Generally, multiple LSH are combined via repetition to increase the range of the hash function to the full set of longs.
 * This repetition is accomplished by wrapping instances of the LSH in a LSHFamily, which does the combination.
 * 
 * The size of the hash family corresponds to the number of independent hashes you want to apply to the data.
 * In a k-near neighbors style of searching, this corresponds to the number of neighbors you want to find
 * (i.e. the number of vectors within a distance according to cosine similarity).
 */
public HyperplaneLSH(int dim, RandomGenerator rg) {
    super(dim, rg);

    UnitSphereRandomVectorGenerator generator = new UnitSphereRandomVectorGenerator(dim, rg);
    //compute our vector representing a hyperplane of dimension dim by taking a random vector
    //located on the unit sphere
    double[] normalVector = generator.nextVector();
    r = new ArrayRealVector(normalVector);
}