/** * Returns a {@link UnaryOperator} that transposes the matrix. * * Example {@code transpose(true).apply(m);} * * @param parallel * Whether to perform the transpose concurrently. */ public static UnaryOperator<ArrayMatrix> transpose(boolean parallel) { return (m) -> { double[][] data = m.getData(); IntStream stream = range(0, m.getColumnDimension()); stream = parallel ? stream.parallel() : stream; double[][] transpose = stream.mapToObj( column -> range(0, data.length).mapToDouble(row -> data[row][column]).toArray()) .toArray(double[][]::new); return new ArrayMatrix(transpose); }; }
/** * Loads the {@link SemanticSpace} from the binary formatted file, adding * its words to {@link #termToIndex} and returning the {@code Matrix} * containing the space's vectors. * * @param sspaceFile a file in {@link SSpaceFormat#BINARY binary} format */ private Matrix loadBinary(InputStream fileStream) throws IOException { DataInputStream dis = new DataInputStream(fileStream); int rows = dis.readInt(); int cols = dis.readInt(); // create a dense matrix Matrix m = new ArrayMatrix(rows, cols); double[] d = new double[cols]; for (int row = 0; row < rows; ++row) { String word = dis.readUTF(); termToIndex.put(word, row); for (int col = 0; col < cols; ++col) { d[col] = dis.readDouble(); } m.setRow(row, d); } return m; }
/** * Loads the {@link SemanticSpace} from the binary formatted file, adding * its words to {@link #termToIndex} and returning the {@code Matrix} * containing the space's vectors. * * @param sspaceFile a file in {@link SSpaceFormat#BINARY binary} format */ private Matrix loadBinary(InputStream fileStream) throws IOException { DataInputStream dis = new DataInputStream(fileStream); int rows = dis.readInt(); int cols = dis.readInt(); // create a dense matrix Matrix m = new ArrayMatrix(rows, cols); double[] d = new double[cols]; for (int row = 0; row < rows; ++row) { String word = dis.readUTF(); termToIndex.put(word, row); for (int col = 0; col < cols; ++col) { d[col] = dis.readDouble(); } m.setRow(row, d); } return m; }
} else { LOGGER.finer("creating new (in memory) ArrayMatrix"); return new ArrayMatrix(rows, cols);
} else { LOGGER.finer("creating new (in memory) ArrayMatrix"); return new ArrayMatrix(rows, cols);
int rows = result.Ut.rows; Matrix U = new ArrayMatrix(cols, rows); Matrix Vt = new ArrayMatrix(result.Vt.rows, result.Vt.cols); for (int row = 0; row < rows; ++row) { for (int col = 0; col < cols; ++col) {
/** * Creates a new {@code Matrix} based on the provided type, with the * provided dimensions * * @param matrixType the type of matrix to create * @param rows the number of rows in the matrix * @param cols the number of columns in the matrix */ public static Matrix create(int rows, int cols, Type matrixType) { switch (matrixType) { case SPARSE_IN_MEMORY: return new YaleSparseMatrix(rows, cols); case DENSE_IN_MEMORY: return new ArrayMatrix(rows, cols); case DIAGONAL_IN_MEMORY: return new DiagonalMatrix(rows); case SPARSE_ON_DISK: //return new SparseOnDiskMatrix(rows, cols); // REMDINER: implement me return new OnDiskMatrix(rows, cols); case DENSE_ON_DISK: return new OnDiskMatrix(rows, cols); } throw new IllegalArgumentException( "Unknown matrix type: " + matrixType); }
/** * Creates a new {@code Matrix} based on the provided type, with the * provided dimensions * * @param matrixType the type of matrix to create * @param rows the number of rows in the matrix * @param cols the number of columns in the matrix */ public static Matrix create(int rows, int cols, Type matrixType) { switch (matrixType) { case SPARSE_IN_MEMORY: return new YaleSparseMatrix(rows, cols); case DENSE_IN_MEMORY: return new ArrayMatrix(rows, cols); case DIAGONAL_IN_MEMORY: return new DiagonalMatrix(rows); case SPARSE_ON_DISK: //return new SparseOnDiskMatrix(rows, cols); // REMDINER: implement me return new OnDiskMatrix(rows, cols); case DENSE_ON_DISK: return new OnDiskMatrix(rows, cols); } throw new IllegalArgumentException( "Unknown matrix type: " + matrixType); }
dataClasses = new ArrayMatrix(1, n, eigenValues); classFeatures = new ArrayMatrix(numDimensions, n, eigenVectors);
/** * {@inheritDoc} */ public void factorize(SparseMatrix m, int numDimensions) { if (numDimensions >= m.columns() || numDimensions >= m.rows()) throw new IllegalArgumentException( "Cannot factorize with more dimensions than there are " + "rows or columns"); this.numDimensions = numDimensions; A = new ArrayMatrix(m.rows(), numDimensions); initialize(A); X = new ArrayMatrix(numDimensions, m.columns()); initialize(X); for (int i = 0; i < numIterations; ++i) { updateX(computeGofX(m), computeLearningRateX()); updateA(computeGofA(m), computeLearningRateA()); } }
/** * {@inheritDoc} */ public void factorize(SparseMatrix m, int numDimensions) { if (numDimensions >= m.columns() || numDimensions >= m.rows()) throw new IllegalArgumentException( "Cannot factorize with more dimensions than there are " + "rows or columns"); this.numDimensions = numDimensions; A = new ArrayMatrix(m.rows(), numDimensions); initialize(A); X = new ArrayMatrix(numDimensions, m.columns()); initialize(X); for (int i = 0; i < numIterations; ++i) { updateX(computeGofX(m), computeLearningRateX()); updateA(computeGofA(m), computeLearningRateA()); } }
/** * {@inheritDoc} */ public Matrix classFeatures() { if (!scaledClassFeatures) { scaledClassFeatures = true; classFeatures = new ArrayMatrix(V.rows(), V.columns()); // Weight the values in the document space by the singular // values. // // REMINDER: when the RowScaledMatrix class is merged in with // the trunk, this code should be replaced. for (int r = 0; r < classFeatures.rows(); ++r) for (int c = 0; c < classFeatures.columns(); ++c) classFeatures.set(r, c, V.get(r, c) * singularValues[r]); } return classFeatures; }
/** * {@inheritDoc} */ public Matrix dataClasses() { if (!scaledDataClasses) { scaledDataClasses = true; dataClasses = new ArrayMatrix(U.rows(), U.columns()); // Weight the values in the data point space by the singular // values. // // REMINDER: when the RowScaledMatrix class is merged in with // the trunk, this code should be replaced. for (int r = 0; r < dataClasses.rows(); ++r) for (int c = 0; c < dataClasses.columns(); ++c) dataClasses.set(r, c, U.get(r, c) * singularValues[c]); } return dataClasses; }
/** * {@inheritDoc} */ public double compare(Partition p1, Partition p2) { // Create the contingency matrix. Matrix contingency = new ArrayMatrix(p1.numClusters(), p2.numClusters()); for (int i = 0; i < p1.numPoints(); ++i) { int x = p1.assignments()[i]; int y = p2.assignments()[i]; if (x >= 0 && y >= 0) contingency.add(x, y, 1d); } // Aggregate the data. return aggregator.aggregate(contingency); } }
averageMatrix = new ArrayMatrix(1, 1); average = average / (double) (m.rows() * m.columns()); averageMatrix.set(1, 1, average); } else if (dim == Dimension.ROW) { averageMatrix = new ArrayMatrix(m.rows(), 1); for (int i = 0; i < m.rows(); ++i) { double average = 0; averageMatrix = new ArrayMatrix(1, m.columns()); for (int i = 0; i < m.rows(); ++i) { for (int j = 0; j < m.columns(); ++j) {
Matrix G = new ArrayMatrix(A.rows(), numDimensions);
Matrix G = new ArrayMatrix(A.rows(), numDimensions);
Matrix G = new ArrayMatrix(numDimensions, X.columns());
Matrix G = new ArrayMatrix(numDimensions, X.columns());
Matrix newAdjacency = new ArrayMatrix( baseAssignments.numClusters(), baseAssignments.numClusters()); for (int row = 0; row < adjacency.rows(); ++row) {