Package edu.ucla.sspace.vector

Examples of edu.ucla.sspace.vector.SparseVector


                // once to save cost.
                int length = sumVector.length();
                double d = 1d / indices.size();
                if (sumVector instanceof SparseVector) {
                    centroid = new SparseHashDoubleVector(length);
                    SparseVector sv = (SparseVector)sumVector;
                    for (int nz : sv.getNonZeroIndices())
                        centroid.set(nz, sumVector.get(nz) * d);
                }
                else {
                    centroid = new DenseVector(length);
                    for (int i = 0; i < length; ++i)
View Full Code Here


    public void setColumn(int column, DoubleVector values) {
        if (values.length() != rows)
            throw new IllegalArgumentException("cannot set a column " +
                "whose dimensions are different than the matrix");
        if (values instanceof SparseVector) {
            SparseVector sv = (SparseVector)values;
            for (int nz : sv.getNonZeroIndices())
                backingMatrix.set(getRealRow(nz), nz, values.get(nz));
        }
        else {
            for (int i = 0; i < rowToReal.length; ++i)
                backingMatrix.set(rowToReal[i], i, values.get(i));
View Full Code Here

        DoubleVector column = Vectors.asDouble(col);
        if (isFinished)
            throw new IllegalStateException(
                "Cannot add columns to a MatrixBuilder that is finished");
        if (column instanceof SparseVector) {
            SparseVector s = (SparseVector)column;
            for (int r : s.getNonZeroIndices()) {
                // NB: Matlab sparse format is in [row col val] format
                //
                // NOTE: Matlab indices start at 1, not 0, so update all the
                // column and column values to be Matlab formatted.
                addEntry(r + 1, curColumn + 1, column.get(r));
View Full Code Here

    public void setRow(int row, DoubleVector values) {
        if (values.length() != columns)
            throw new IllegalArgumentException("cannot set a row " +
                "whose dimensions are different than the matrix");
        if (values instanceof SparseVector) {
            SparseVector sv = (SparseVector)values;
            for (int nz : sv.getNonZeroIndices())
                backingMatrix.set(nz, getRealColumn(nz), values.get(nz));
        }
        else {
            for (int i = 0; i < columnToReal.length; ++i)
                backingMatrix.set(i, columnToReal[i], values.get(i));
View Full Code Here

        if (values.length() != cols)
            throw new IllegalArgumentException(
                "The number of values does not match the number of columns");

        if (values instanceof SparseVector) {
            SparseVector sv = (SparseVector)values;
            for (int i : sv.getNonZeroIndices())
                set(row, i, values.get(i));
        }
        else {
            for (int i = 0; i < values.length(); ++i)
                set(row, i, values.get(i));
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 (a.length() < b.length() ||
                nzA.length < nzB.length) {
                DoubleVector t = a;
                a = b;
                b = t;
            }

            for (int nz : nzB) {
                double aValue = a.get(nz);
                double bValue = b.get(nz);
                dotProduct += aValue * bValue;
            }
        }

        // Check if the second vector is sparse.  If so, use only the non-zero
        // indices of b to speed up the computation by avoiding zero
        // multiplications.
        else if (b instanceof SparseVector) {
            SparseVector svB = (SparseVector)b;
            for (int nz : svB.getNonZeroIndices())
                dotProduct += b.get(nz) * a.get(nz);
        }

        // Check if the first vector is sparse.  If so, use only the non-zero
        // indices of a to speed up the computation by avoiding zero
        // multiplications.
        else if (a instanceof SparseVector) {
            SparseVector svA = (SparseVector)a;
            for (int nz : svA.getNonZeroIndices())
                dotProduct += b.get(nz) * a.get(nz);
        }

        // Otherwise, just assume both are dense and compute the full amount
        else {
View Full Code Here

                    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

                        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

                    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

                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

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.