/** * {@inheritDoc} */ public synchronized SparseDoubleVector getRowVector(int row) { return m.getRowVector(row); }
/** * {@inheritDoc} */ public synchronized SparseDoubleVector getRowVector(int row) { return m.getRowVector(row); }
/** * {@inheritDoc} */ public SparseDoubleVector getVector(String word) { int index = basis.getDimension(word); return (index < 0) ? null : bigramMatrix.getRowVector(index); }
/** * {@inheritDoc} */ public SparseDoubleVector getRowVector(int row) { return matrix.getRowVector(getRealRow(row)); } }
/** * Returns an array containing the row indices of the neighbors of the * impost node and the row index of the impost node itself. */ private static int[] getImpostNeighbors(SparseMatrix sm, int rowIndex) { int[] impost1edges = sm.getRowVector(rowIndex).getNonZeroIndices(); int[] neighbors = Arrays.copyOf(impost1edges, impost1edges.length + 1); neighbors[neighbors.length - 1] = rowIndex; return neighbors; }
/** * Returns an array containing the row indices of the neighbors of the * impost node and the row index of the impost node itself. */ private static int[] getImpostNeighbors(SparseMatrix sm, int rowIndex) { int[] impost1edges = sm.getRowVector(rowIndex).getNonZeroIndices(); int[] neighbors = Arrays.copyOf(impost1edges, impost1edges.length + 1); neighbors[neighbors.length - 1] = rowIndex; return neighbors; }
/** * {@inheritDoc} */ public SparseDoubleVector getRowVector(int row) { return matrix.getRowVector(getRealRow(row)); } }
/** * {@inheritDoc} */ public SparseDoubleVector getRowVector(int row) { return new ScaledSparseDoubleVector( m.getRowVector(row), scales.get(row)); } }
/** * {@inheritDoc} */ public SparseDoubleVector getRowVector(int row) { return new ScaledSparseDoubleVector( m.getRowVector(row), scales.get(row)); } }
/** * {@inheritDoc} */ public SparseDoubleVector getRowVector(int row) { row = getIndexFromMap(rowMaskMap, row); return new MaskedSparseDoubleVectorView( matrix.getRowVector(row), colMaskMap, reverseColMaskMap); }
/** * {@inheritDoc} */ public SparseDoubleVector getRowVector(int row) { row = getIndexFromMap(rowMaskMap, row); return new MaskedSparseDoubleVectorView( matrix.getRowVector(row), colMaskMap, reverseColMaskMap); }
/** * {@inheritDoc} */ public void processSpace(Properties properties) { SparseMatrix cleanedMatrix = (SparseMatrix) transform.transform( cooccurrenceMatrix); for (String term : basis.keySet()) { int index = basis.getDimension(term); SparseDoubleVector sdv = cleanedMatrix.getRowVector(index); double score = 0; for (int i : sdv.getNonZeroIndices()) score += sdv.get(i); wordScores.put(term, score); } }
/** * Writes the {@link SparseMatrix} to the file using a particular format. */ public void writeMatrix(SparseMatrix m, OutputStream s) { PrintStream p = new PrintStream(s); // Check to see if the last element in the matrix is non zero. If it // has a zero value, print out a single dummy value to bound the total // size of the matrix. if (m.get(m.rows()-1, m.columns()-1) == 0d) p.printf("%d %d %f\n", m.rows(), m.columns(), 0.0); // Print the row, col, value entrie for each element in the matrix. for (int r = 0; r < m.rows(); ++r) { SparseDoubleVector v = m.getRowVector(r); for (int c : v.getNonZeroIndices()) p.printf("%d %d %f\n", r+1, c+1, m.get(r,c)); } p.flush(); p.close(); } }
/** * {@inheritDoc} */ public void processSpace(Properties properties) { SparseMatrix cleanedMatrix = (SparseMatrix) transform.transform( cooccurrenceMatrix); for (String term : basis.keySet()) { int index = basis.getDimension(term); SparseDoubleVector sdv = cleanedMatrix.getRowVector(index); double score = 0; for (int i : sdv.getNonZeroIndices()) score += sdv.get(i); wordScores.put(term, score); } }
/** * {@inheritDoc} */ public void processSpace(Properties properties) { SparseMatrix cleanedMatrix = (SparseMatrix) transform.transform( cooccurrenceMatrix); for (String term : basis.keySet()) { int index = basis.getDimension(term); SparseDoubleVector sdv = cleanedMatrix.getRowVector(index); double score = 0; for (int i : sdv.getNonZeroIndices()) score += sdv.get(i); wordScores.put(term, score); } }
/** * {@inheritDoc} */ public void processSpace(Properties properties) { SparseMatrix cleanedMatrix = (SparseMatrix) transform.transform( cooccurrenceMatrix); for (String term : basis.keySet()) { int index = basis.getDimension(term); SparseDoubleVector sdv = cleanedMatrix.getRowVector(index); double score = 0; for (int i : sdv.getNonZeroIndices()) score += sdv.get(i); wordScores.put(term, score); } }
/** * {@inheritDoc} */ public double[] rank(SparseMatrix adj, double[] initialRanks) { double[] ranks = initialRanks; double[] columnSums = TransformStatistics.extractStatistics(adj).columnSums; for (int i = 0; i < numIterations; i++) { double[] newRanks = new double[adj.rows()]; for (int r = 0; r < adj.rows(); ++r) for (int c : adj.getRowVector(r).getNonZeroIndices()) newRanks[r] += adj.get(r, c) / columnSums[c] * ranks[r]; for (int r = 0; r < adj.rows(); ++r) newRanks[r] = alpha * newRanks[r] + beta * initialRanks[r]; ranks = newRanks; } return ranks; }
/** * {@inheritDoc} */ public Vector getVector(String word) { Integer index = termToIndex.getDimension(word); if (index == null) return null; // If the matrix hasn't had columns dropped then the returned vector // will be the combination of the word's row and column else if (reduced == null) { // NOTE: the matrix could be asymmetric if the a word has only // appeared on one side of a context (its row or column vector would // never have been set). Therefore, check the index with the matrix // size first. SparseDoubleVector rowVec = (index < cooccurrenceMatrix.rows()) ? cooccurrenceMatrix.getRowVectorUnsafe(index) : new CompactSparseVector(termToIndex.numDimensions()); SparseDoubleVector colVec = (index < cooccurrenceMatrix.columns()) ? cooccurrenceMatrix.getColumnVectorUnsafe(index) : new CompactSparseVector(termToIndex.numDimensions()); return new ConcatenatedSparseDoubleVector(rowVec, colVec); } // The co-occurrence matrix has had columns dropped so the vector is // just the word's row return reduced.getRowVector(index); }
/** * Returns the dot product between the transpose of a given matrix and a * given vector. This method has special casing for a {@code SparseMatrix}. * This method also assumes that {@code matrix} is row based and iterates * over each of the values in the row before iterating over another row. */ protected static DoubleVector computeMatrixTransposeV(Matrix matrix, DoubleVector v) { DoubleVector newV = new DenseVector(matrix.columns()); if (matrix instanceof SparseMatrix) { SparseMatrix smatrix = (SparseMatrix) matrix; for (int r = 0; r < smatrix.rows(); ++r) { SparseDoubleVector row = smatrix.getRowVector(r); int[] nonZeros = row.getNonZeroIndices(); for (int c : nonZeros) newV.add(c, row.get(c) * v.get(r)); } } else { for (int r = 0; r < matrix.rows(); ++r) for (int c = 0; c < matrix.columns(); ++c) newV.add(c, matrix.get(r, c) * v.get(r)); } return newV; }
/** * Returns the dot product between the transpose of a given matrix and a * given vector. This method has special casing for a {@code SparseMatrix}. * This method also assumes that {@code matrix} is row based and iterates * over each of the values in the row before iterating over another row. */ protected static DoubleVector computeMatrixTransposeV(Matrix matrix, DoubleVector v) { DoubleVector newV = new DenseVector(matrix.columns()); if (matrix instanceof SparseMatrix) { SparseMatrix smatrix = (SparseMatrix) matrix; for (int r = 0; r < smatrix.rows(); ++r) { SparseDoubleVector row = smatrix.getRowVector(r); int[] nonZeros = row.getNonZeroIndices(); for (int c : nonZeros) newV.add(c, row.get(c) * v.get(r)); } } else { for (int r = 0; r < matrix.rows(); ++r) for (int c = 0; c < matrix.columns(); ++c) newV.add(c, matrix.get(r, c) * v.get(r)); } return newV; }