Package edu.ucla.sspace.vector

Examples of edu.ucla.sspace.vector.DoubleVector


    /**
     * {@inheritDoc}
     */
    public synchronized int addColumn(Vector col) {
        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));
            }
        }
        else {
            for (int r = 0; r < column.length(); ++r) {
                double d = column.get(r);
                if (d != 0d) {
                    // NOTE: Matlab indices start at 1, not 0, so update all
                    // the row and column values to be Matlab formatted.
                    addEntry(r + 1, curColumn + 1, d);
                }
View Full Code Here


            if (!focusWord.equals(IteratorFactory.EMPTY_TOKEN)) {
                // Incorporate the context into the semantic vector for the
                // focus word.  If the focus word has no semantic vector yet,
                // create a new one, as determined by the index builder.
                DoubleVector meaning = termHolographs.get(focusWord);
                if (meaning == null) {
                    meaning = new DenseVector(indexVectorSize);
                    documentVectors.put(focusWord, meaning);
                }
                updateMeaning(meaning, prevWords, nextWords);
            }

            prevWords.offer(focusWord);
            if (prevWords.size() > 1)
                prevWords.remove();
        }

        // Add the local cached semantics to the global term semantics.
        for (Map.Entry<String, DoubleVector> entry :
                documentVectors.entrySet()) {
            synchronized (entry.getKey()) {
                // Get the global semantic representation of each word.  If it
                // does not currently exist, then just put the local copies
                // representation, otherwise add the local copy to the global
                // version.
                DoubleVector existingVector =
                    termHolographs.get(entry.getKey());
                if (existingVector == null)
                    termHolographs.put(entry.getKey(), entry.getValue());
                else
                    VectorMath.add(existingVector, entry.getValue());
View Full Code Here

                               Queue<String> nextWords) {
        // Generate the semantics of the context using summation of index
        // vectors.
        if (semanticType == SemanticType.COMPOSITE ||
            semanticType == SemanticType.CONTEXT) {
            DoubleVector context = new DenseVector(indexVectorSize);

            // Sum the words prior to the focus word, skipping filtered tokens.
            for (String term: prevWords) {
                if (term.equals(IteratorFactory.EMPTY_TOKEN))
                    continue;
                VectorMath.add(context, vectorMap.get(term));
            }

            // Sum the words after the focus word, skipping filtered tokens.
            for (String term: nextWords) {
                if (term.equals(IteratorFactory.EMPTY_TOKEN))
                    continue;
                VectorMath.add(context, vectorMap.get(term));
            }

            // Normalize the context vector and add it to the meaning.
            normalize(context);
            VectorMath.add(meaning, context);
        }

        // Generate the semantics of the ordering using circular convolution of
        // n-grams.
        if (semanticType == SemanticType.COMPOSITE ||
            semanticType == SemanticType.ORDERING) {
            DoubleVector order = groupConvolution(prevWords, nextWords);

            // Normalize the order vector and add it to the meaning.
            normalize(order);
            VectorMath.add(meaning, order);
        }
View Full Code Here

     * @return The semantic vector generated from the circular convolution.
     */
    private DoubleVector groupConvolution(Queue<String> prevWords,
                                          Queue<String> nextWords) {
        // Generate an empty DoubleVector to hold the convolution.
        DoubleVector result = new DenseVector(indexVectorSize);

        // Do the convolutions starting at index 0.
        String prevWord = prevWords.peek();
        DoubleVector tempConvolution;
        if (!prevWord.equals(IteratorFactory.EMPTY_TOKEN)) {
            tempConvolution =
                convolute(vectorMap.get(prevWords.peek()), placeHolder);
            VectorMath.add(result, tempConvolution);
        } else
View Full Code Here

        // Use the Fast Fourier Transform on each vector.
        FastFourierTransform.transform(left);
        FastFourierTransform.transform(right);

        // Multiply the two together.
        DoubleVector result = VectorMath.multiply(left, right);

        // The inverse transform completes the convolution.
        FastFourierTransform.backtransform(result);
        return result;
    }
View Full Code Here

     * @param orderVector The ordering of values to be used.
     *
     * @return The shuffled version of {@code data}.
     */
    private DoubleVector changeVector(DoubleVector data, int[] orderVector) {
        DoubleVector result = new DenseVector(indexVectorSize);
        for (int i = 0; i < indexVectorSize; i++)
            result.set(i, data.get(orderVector[i]));
        return result;
    }
View Full Code Here

            PrintWriter pw = new PrintWriter(output);
            // Count the number of non-zero values in the matrix
            int nonZero = 0;
            int rows = matrix.rows();
            for (int i = 0; i < rows; ++i) {
                DoubleVector v = matrix.getRowVector(i);
                if (v instanceof SparseVector)
                    nonZero += ((SparseVector)v).getNonZeroIndices().length;
                else {
                    for (int col = 0; col < v.length(); ++col) {
                        if (v.get(col) != 0)
                            nonZero++;
                    }
                }
            }
            // Write the header: rows cols non-zero
            pw.println(matrix.rows() + " " + matrix.columns() + " " + nonZero);
            for (int row = 0; row < rows; ++row) {
                StringBuilder sb = new StringBuilder(nonZero / rows);
                // NOTE: the columns in CLUTO start at 1, not 0, so increment
                // one to each of the columns
                DoubleVector v = matrix.getRowVector(row);
                if (v instanceof SparseVector) {
                    int[] nzIndices = ((SparseVector)v).getNonZeroIndices();
                    for (int nz : nzIndices) {
                        sb.append(nz + 1).append(" ").
                            append(v.get(nz)).append(" ");
                    }
                }
                else {
                    for (int col = 0; col < v.length(); ++col) {
                        double d = v.get(col);
                        if (d != 0)
                            sb.append(col+1).append(" ").append(d).append(" ");
                    }
                }
                pw.println(sb.toString());
View Full Code Here

                docVec.add(dim, 1d);
        }       
       
        // Transform the vector according to this instance's transform's state,
        // which should normalize the vector as the original vectors were.
        DoubleVector transformed = transform.transform(docVec);

        // Represent the document as a 1-column matrix       
        Matrix queryAsMatrix = new ArrayMatrix(1, numDims);
        for (int nz : docVec.getNonZeroIndices())
            queryAsMatrix.set(0, nz, transformed.get(nz));
       
        // Project the new document vector, d, by using
        //
        //   d * U_k * Sigma_k^-1
        //
        // where k is the dimensionality of the LSA space
       
        Matrix UtimesSigmaInv = null;
           
        // We cache the reuslts of the U_k * Sigma_k^-1 multiplication since
        // this will be the same for all projections.
        while (UtimesSigmaInv == null) {
            if (UtimesSigmaInvRef != null
                    && ((UtimesSigmaInv = UtimesSigmaInvRef.get()) != null))
                break;
           
            int rows = sigma.rows();
            double[] sigmaInv = new double[rows];
            for (int i = 0; i < rows; ++i)
                sigmaInv[i] = 1d / sigma.get(i, i);
            DiagonalMatrix sigmaInvMatrix = new DiagonalMatrix(sigmaInv);

            UtimesSigmaInv =
                Matrices.multiply(U, sigmaInvMatrix);
            // Update the field with the new reference to the precomputed matrix
            UtimesSigmaInvRef = new WeakReference<Matrix>(UtimesSigmaInv);
        }

        // Compute the resulting projected vector as a matrix
        Matrix result = Matrices.multiply(queryAsMatrix, UtimesSigmaInv);

        // Copy out the vector itself so that we don't retain a reference to the
        // matrix as a result of its getRowVector call, which isn't guaranteed
        // to return a copy.
        int cols = result.columns();
        DoubleVector projected = new DenseVector(result.columns());
        for (int i = 0; i < cols; ++i)
            projected.set(i, result.get(0, i));
        return projected;
    }
View Full Code Here

    public SortedMultiMap<Double,String> getMostSimilar(
             Set<String> terms, int numberOfSimilarWords) {
        if (terms.isEmpty())
            return null;
        // Compute the mean vector for all the terms
        DoubleVector mean = new DenseVector(sspace.getVectorLength());
        int found = 0;
        for (String term : terms) {
            Vector v = sspace.getVector(term);
            if (v == null)
                info(LOGGER, "No vector for term " + term);
View Full Code Here

            return Matrices.asSparseMatrix(scaledVectors);
        } else {
            List<DoubleVector> scaledVectors =
                new ArrayList<DoubleVector>(matrix.rows());
            for (int r = 0; r < matrix.rows(); ++r) {
                DoubleVector v = matrix.getRowVector(r);
                scaledVectors.add(new ScaledDoubleVector(v, 1/v.magnitude()));
            }
            return Matrices.asMatrix(scaledVectors);
        }
    }
View Full Code Here

TOP

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

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.