Package edu.ucla.sspace.vector

Examples of edu.ucla.sspace.vector.SparseVector


            " constructor).";

        // Special case for sparse Vectors, for which we already know the
        // non-zero indices for the column
        if (row instanceof SparseVector) {
            SparseVector s = (SparseVector)row;
            int[] nonZero = s.getNonZeroIndices();
            nonZeroValues += nonZero.length;
            StringBuilder sb = new StringBuilder();
            for (int i : nonZero) {
                sb.append(i+1).append(" ");
                sb.append(row.getValue(i).doubleValue()).append(" ");
View Full Code Here


        }

        // Check whether both vectors are sparse.  If so, use only the non-zero
        // indices to speed up the computation by avoiding zero multiplications
        else if (a instanceof SparseVector && b instanceof SparseVector) {
            SparseVector svA = (SparseVector)a;
            SparseVector svB = (SparseVector)b;
            int[] nzA = svA.getNonZeroIndices();
            int[] nzB = svB.getNonZeroIndices();
            // Choose the smaller of the two to use in computing the dot
            // product.  Because it would be more expensive to compute the
            // intersection of the two sets, we assume that any potential
            // misses would be less of a performance hit.
            if (nzA.length < nzB.length) {
View Full Code Here

     */
    public static double euclideanDistance(DoubleVector a, DoubleVector b) {
        check(a, b);
       
        if (a instanceof SparseVector && b instanceof SparseVector) {
            SparseVector svA = (SparseVector)a;
            SparseVector svB = (SparseVector)b;

            int[] aNonZero = svA.getNonZeroIndices();
            int[] bNonZero = svB.getNonZeroIndices();
            TIntSet union = new TIntHashSet(aNonZero);
            union.addAll(bNonZero);
           
            double sum = 0;
            int[] nzIndices = union.toArray();
            for (int nz : nzIndices) {
                double x = a.get(nz);
                double y = b.get(nz);
                double diff = x - y;
                sum += diff * diff;
            }
            return Math.sqrt(sum);

        } else if (b instanceof SparseVector) {
            // If b is sparse, use a special case where we use the cached
            // magnitude of a and the sparsity of b to avoid most of the
            // computations.
            SparseVector sb = (SparseVector) b;
            int[] bNonZero = sb.getNonZeroIndices();
            double sum = 0;

            // Get the magnitude for a.  This value will often only be computed
            // once for the first vector once since the DenseVector caches the
            // magnitude, thus saving a large amount of computation.
View Full Code Here

     */
    public static double euclideanDistance(IntegerVector a, IntegerVector b) {
        check(a, b);
        
        if (a instanceof SparseVector && b instanceof SparseVector) {
            SparseVector svA = (SparseVector)a;
            SparseVector svB = (SparseVector)b;

            int[] aNonZero = svA.getNonZeroIndices();
            int[] bNonZero = svB.getNonZeroIndices();
            HashSet<Integer> sparseIndicesA = new HashSet<Integer>(
                    aNonZero.length);
            double sum = 0;
            for (int nonZero : aNonZero) {
                sum += Math.pow((a.get(nonZero) - b.get(nonZero)), 2);
View Full Code Here

        // Special case when both vectors are sparse vectors.
        if (a instanceof SparseVector &&
            b instanceof SparseVector) {
            // Convert both vectors to sparse vectors.
            SparseVector sa = (SparseVector) a;
            int[] aNonZeros = sa.getNonZeroIndices();

            SparseVector sb = (SparseVector) b;
            int[] bNonZeros = sb.getNonZeroIndices();

            // If b is the smaller vector swap it with a.  This allows the dot
            // product to work over the vector with the smallest number of non
            // zero values.
            if (bNonZeros.length < aNonZeros.length) {
                SparseVector temp = sa;
                int[] tempNonZeros = aNonZeros;

                sa = sb;
                aNonZeros = bNonZeros;
View Full Code Here

        // Special case when both vectors are sparse vectors.
        if (a instanceof SparseVector &&
            b instanceof SparseVector) {
            // Convert both vectors to sparse vectors.
            SparseVector sa = (SparseVector) a;
            int[] aNonZeros = sa.getNonZeroIndices();

            SparseVector sb = (SparseVector) b;
            int[] bNonZeros = sb.getNonZeroIndices();

            // If b is the smaller vector swap it with a.  This allows the dot
            // product to work over the vector with the smallest number of non
            // zero values.
            if (bNonZeros.length < aNonZeros.length) {
                SparseVector temp = sa;
                int[] tempNonZeros = aNonZeros;

                sa = sb;
                aNonZeros = bNonZeros;
View Full Code Here

        // Special case when both vectors are sparse vectors.
        if (a instanceof SparseVector &&
            b instanceof SparseVector) {
            // Convert both vectors to sparse vectors.
            SparseVector sa = (SparseVector) a;
            int[] aNonZeros = sa.getNonZeroIndices();

            SparseVector sb = (SparseVector) b;
            int[] bNonZeros = sb.getNonZeroIndices();

            // If b is the smaller vector swap it with a.  This allows the dot
            // product to work over the vector with the smallest number of non
            // zero values.
            if (bNonZeros.length < aNonZeros.length) {
                SparseVector temp = sa;
                int[] tempNonZeros = aNonZeros;
                sa = sb;
                aNonZeros = bNonZeros;
                sb = temp;
                bNonZeros = tempNonZeros;
View Full Code Here

        double divergence = 0;

        // Iterate over just the non zero values of a if it is a sparse vector.
        if (a instanceof SparseVector) {
            SparseVector sa = (SparseVector) a;
            int[] aNonZeros = sa.getNonZeroIndices();

            for (int index : aNonZeros) {
                double aValue = a.get(index);
                double bValue = b.get(index);
View Full Code Here

        double divergence = 0;

        // Iterate over just the non zero values of a if it is a sparse vector.
        if (a instanceof SparseVector) {
            SparseVector sa = (SparseVector) a;
            int[] aNonZeros = sa.getNonZeroIndices();

            for (int index : aNonZeros) {
                double aValue = a.get(index);
                double bValue = b.get(index);
View Full Code Here

        double divergence = 0;

        // Iterate over just the non zero values of a if it is a sparse vector.
        if (a instanceof SparseVector) {
            SparseVector sa = (SparseVector) a;
            int[] aNonZeros = sa.getNonZeroIndices();

            for (int index : aNonZeros) {
                double aValue = a.getValue(index).doubleValue();
                double bValue = b.getValue(index).doubleValue();
View Full Code Here

TOP

Related Classes of edu.ucla.sspace.vector.SparseVector

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.