matrix = Matrices.create(rows, columns, false); while ((line = br.readLine()) != null) { String[] termVectorPair = line.split("\\|");
matrix = Matrices.create(rows, columns, false); while ((line = br.readLine()) != null) { String[] termVectorPair = line.split("\\|");
/** * Returns a copied version of a given matrix. The returned matrix will * have the same dimensionality, values, and sparsity, but it may not have * the same exact sub-type. * * @param matrix the matrix to be copied * * @throws IllegalArgumentException when the dimensionality of matrix and * output do not match * * @return a copied version of matrix */ public static Matrix copy(Matrix matrix) { Matrix copiedMatrix = null; if (matrix instanceof SparseMatrix) copiedMatrix = Matrices.create( matrix.rows(), matrix.columns(), Type.SPARSE_IN_MEMORY); else copiedMatrix = Matrices.create( matrix.rows(), matrix.columns(), Type.DENSE_IN_MEMORY); return copyTo(matrix, copiedMatrix); }
/** * Returns a new {@code Matrix} that has been resized from the original, * truncating values if smaller, or adding zero elements if larger. */ public static Matrix resize(Matrix matrix, int rows, int columns) { // REMDINER: the third argument decides whether the matrix is dense or // not. If new sparse matrices are added, there should be addiional // cases. Ideally, we should put in a package method for determining // whether a given matrix instance is sparse and/or on disk. -jurgens boolean isDense = !(matrix instanceof SparseMatrix || matrix instanceof DiagonalMatrix); Matrix resized = create(rows, columns, isDense); int r = Math.min(rows, matrix.rows()); int c = Math.min(columns, matrix.columns()); for (int row = 0; row < r; ++row) { for (int col = 0; col < c; ++col) { resized.set(row, col, matrix.get(row, col)); } } return resized; }
/** * Returns a new {@code Matrix} that has been resized from the original, * truncating values if smaller, or adding zero elements if larger. */ public static Matrix resize(Matrix matrix, int rows, int columns) { // REMDINER: the third argument decides whether the matrix is dense or // not. If new sparse matrices are added, there should be addiional // cases. Ideally, we should put in a package method for determining // whether a given matrix instance is sparse and/or on disk. -jurgens boolean isDense = !(matrix instanceof SparseMatrix || matrix instanceof DiagonalMatrix); Matrix resized = create(rows, columns, isDense); int r = Math.min(rows, matrix.rows()); int c = Math.min(columns, matrix.columns()); for (int row = 0; row < r; ++row) { for (int col = 0; col < c; ++col) { resized.set(row, col, matrix.get(row, col)); } } return resized; }
/** * Returns a copied version of a given matrix. The returned matrix will * have the same dimensionality, values, and sparsity, but it may not have * the same exact sub-type. * * @param matrix the matrix to be copied * * @throws IllegalArgumentException when the dimensionality of matrix and * output do not match * * @return a copied version of matrix */ public static Matrix copy(Matrix matrix) { Matrix copiedMatrix = null; if (matrix instanceof SparseMatrix) copiedMatrix = Matrices.create( matrix.rows(), matrix.columns(), Type.SPARSE_IN_MEMORY); else copiedMatrix = Matrices.create( matrix.rows(), matrix.columns(), Type.DENSE_IN_MEMORY); return copyTo(matrix, copiedMatrix); }
private static Matrix multiplyLeftDiag(Matrix m1, Matrix m2) { Matrix resultMatrix = create(m1.rows(), m2.columns(), true); for (int r = 0; r < m1.rows(); ++r) { double element = m1.get(r, r); double[] m2Row = m2.getRow(r); for (int c = 0; c < m2.columns(); ++c) resultMatrix.set(r, c, element * m2Row[c]); } return resultMatrix; }
private static Matrix multiplyLeftDiag(Matrix m1, Matrix m2) { Matrix resultMatrix = create(m1.rows(), m2.columns(), true); for (int r = 0; r < m1.rows(); ++r) { double element = m1.get(r, r); double[] m2Row = m2.getRow(r); for (int c = 0; c < m2.columns(); ++c) resultMatrix.set(r, c, element * m2Row[c]); } return resultMatrix; }
private static Matrix multiplyRightDiag(Matrix m1, Matrix m2) { Matrix resultMatrix = create(m1.rows(), m2.columns(), true); for (int r = 0; r < m1.rows(); ++r) { double[] row = m1.getRow(r); for (int c = 0; c < m2.columns(); ++c) { double value = m2.get(c, c); resultMatrix.set(r, c, value * row[c]); } } return resultMatrix; }
private static Matrix multiplyRightDiag(Matrix m1, Matrix m2) { Matrix resultMatrix = create(m1.rows(), m2.columns(), true); for (int r = 0; r < m1.rows(); ++r) { double[] row = m1.getRow(r); for (int c = 0; c < m2.columns(); ++c) { double value = m2.get(c, c); resultMatrix.set(r, c, value * row[c]); } } return resultMatrix; }
/** * Computes and returns the similarity matrix for {@code m} using the * specified similarity function */ private static Matrix computeSimilarityMatrix(Matrix m, SimType similarityFunction) { Matrix similarityMatrix = Matrices.create(m.rows(), m.rows(), Matrix.Type.DENSE_ON_DISK); for (int i = 0; i < m.rows(); ++i) { for (int j = i + 1; j < m.rows(); ++j) { double similarity = Similarity.getSimilarity(similarityFunction, m.getRowVector(i), m.getRowVector(j)); similarityMatrix.set(i, j, similarity); similarityMatrix.set(j, i, similarity); } } return similarityMatrix; }
/** * Computes and returns the similarity matrix for {@code m} using the * specified similarity function */ public static Matrix computeSimilarityMatrix(Matrix m, SimType similarityFunction) { Matrix similarityMatrix = Matrices.create( m.rows(), m.rows(), Matrix.Type.DENSE_ON_DISK); for (int i = 0; i < m.rows(); ++i) { for (int j = i + 1; j < m.rows(); ++j) { double similarity = Similarity.getSimilarity(similarityFunction, m.getRowVector(i), m.getRowVector(j)); similarityMatrix.set(i, j, similarity); similarityMatrix.set(j, i, similarity); } } return similarityMatrix; }
Matrix resultMatrix = create(m1.rows(), m2.columns(), true); for (int r = 0; r < m1.rows(); ++r) { double[] row = m1.getRow(r);
Matrix resultMatrix = create(m1.rows(), m2.columns(), true); for (int r = 0; r < m1.rows(); ++r) { double[] row = m1.getRow(r);