Examples of SparseVector


Examples of edu.ucla.sspace.vector.SparseVector

                    for (int i = 1; i < nz.length; ++i)
                        sb.append(",").append(nz[i]).append(",").
                            append(sdv.getValue(nz[i]).doubleValue());
                }
                else {
                    SparseVector sv = (SparseVector)vector;
                    int[] nz = sv.getNonZeroIndices();                   
                    sb = new StringBuilder(nz.length * 4);
                    // special case the first
                    sb.append(nz[0]).append(",")
                        .append(sv.getValue(nz[0]).doubleValue());
                    for (int i = 1; i < nz.length; ++i)
                        sb.append(",").append(nz[i]).append(",").
                            append(sv.getValue(nz[i]).doubleValue());
                }
            }
           
            else {
                boolean first = true;
View Full Code Here

Examples of edu.ucla.sspace.vector.SparseVector

                        dos.writeInt(i);
                        dos.writeDouble(sdv.get(i));
                    }
                }
                else {
                    SparseVector sv = (SparseVector)vector;
                    int[] nz = sv.getNonZeroIndices();
                    dos.writeInt(nz.length);
                    for (int i : nz) {
                        dos.writeInt(i);
                        dos.writeDouble(sv.getValue(i).doubleValue());
                    }
                }
            }
            else {
                // count how many are non-zero
View Full Code Here

Examples of edu.ucla.sspace.vector.SparseVector

                    sb.append(0).append(",").append(sdv.get(0));
                    for (int i = 1; i < nz.length; ++i)
                        sb.append(",").append(i).append(",").append(sdv.get(i));
                }
                else {
                    SparseVector sv = (SparseVector)vector;
                    int[] nz = sv.getNonZeroIndices();                   
                    sb = new StringBuilder(nz.length * 4);
                    // special case the first
                    sb.append(0).append(",")
                        .append(sv.getValue(0).doubleValue());
                    for (int i = 1; i < nz.length; ++i)
                        sb.append(",").append(i).append(",").
                            append(sv.getValue(i).doubleValue());
                }
            }
           
            else {
                boolean first = true;
                sb = new StringBuilder(vectorLength / 2);
                for (int i = 0; i < vector.length(); ++i) {
                    double d = vector.getValue(i).doubleValue();
                    if (d != 0d) {
                        if (first) {
                            sb.append(i).append(",").append(d);
                            first = false;
                        }
                        else {
                            sb.append(",").append(i).append(",").append(d);
                        }
                    }
                }
            }
            pw.println(sb.toString());
            pw.flush();
            break;
        }
           


        case TEXT: {
            PrintWriter pw = new PrintWriter(writer);
            pw.println(word + "|" + VectorIO.toString(vector));
            pw.flush();
            break;
        }

        case BINARY: {
            writer.writeUTF(word);
            for (int i = 0; i < vector.length(); ++i) {
                writer.writeDouble(vector.getValue(i).doubleValue());
            }           
            break;
        }

        case SPARSE_BINARY: {
            writer.writeUTF(word);
            if (vector instanceof SparseVector) {
                if (vector instanceof DoubleVector) {
                    SparseDoubleVector sdv = (SparseDoubleVector)vector;
                    int[] nz = sdv.getNonZeroIndices();
                    writer.writeInt(nz.length);
                    for (int i : nz) {
                        writer.writeInt(i);
                        writer.writeDouble(sdv.get(i));
                    }
                }
                else {
                    SparseVector sv = (SparseVector)vector;
                    int[] nz = sv.getNonZeroIndices();
                    writer.writeInt(nz.length);
                    for (int i : nz) {
                        writer.writeInt(i);
                        writer.writeDouble(sv.getValue(i).doubleValue());
                    }
                }
            }
            else {
                // count how many are non-zero
View Full Code Here

Examples of edu.ucla.sspace.vector.SparseVector

                return 0;

            // Calcuate the term frequencies in this new document
            double colSum = 0;
            if (column instanceof SparseVector) {
                SparseVector sv = (SparseVector)column;
                for (int nz : sv.getNonZeroIndices())
                    colSum += column.get(nz);
            }
            else {
                int length = column.length();
                for (int i = 0; i < length; ++i)
View Full Code Here

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

Examples of edu.ucla.sspace.vector.SparseVector

        }

        // 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

Examples of edu.ucla.sspace.vector.SparseVector

     */
    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

Examples of edu.ucla.sspace.vector.SparseVector

     */
    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

Examples of edu.ucla.sspace.vector.SparseVector

        // 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

Examples of edu.ucla.sspace.vector.SparseVector

        // 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
TOP
Copyright © 2018 www.massapi.com. 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.