/** * {@inheritDoc} */ public void setColumn(int column, double[] values) { checkIndices(values.length - 1, column); for (int row = 0; row < rows; ++row) matrix[getIndex(row,column)] = values[column]; }
/** * {@inheritDoc} */ public double[] getColumn(int column) { checkIndices(0, column); double[] values = new double[rows]; for (int row = 0; row < rows; ++row) values[row] = get(row, column); return values; }
/** * {@inheritDoc} */ public DoubleVector getColumnVector(int column) { return new DenseVector(getColumn(column)); }
/** * 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); }; }
/** * Create a {@code ArrayMatrix} from a two dimensional array. * * @param matrix2d The two dimensional array this {@code ArrayMatrix} will * represent. * @throws IllegalArgumentExceptiona if matrix2d is invalid. */ public ArrayMatrix(double[][] matrix2d) { if (matrix2d == null) throw new IllegalArgumentException("invalid matrix dimensions"); rows = matrix2d.length; if (rows < 1) throw new IllegalArgumentException("invalid matrix dimensions"); cols = matrix2d[0].length; if (cols < 1) throw new IllegalArgumentException("invalid matrix dimensions"); matrix = new double[rows*cols]; for (int i = 0; i < rows; ++i) { if (cols != matrix2d[i].length) throw new IllegalArgumentException("invalid matrix dimensions"); for (int j = 0; j < cols; ++j) set(i, j, matrix2d[i][j]); } }
/** * 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; }
/** * Create a {@code ArrayMatrix} from a two dimensional array. * * @param matrix2d The two dimensional array this {@code ArrayMatrix} will * represent. * @throws IllegalArgumentExceptiona if matrix2d is invalid. */ public ArrayMatrix(double[][] matrix2d) { if (matrix2d == null) throw new IllegalArgumentException("invalid matrix dimensions"); rows = matrix2d.length; if (rows < 1) throw new IllegalArgumentException("invalid matrix dimensions"); cols = matrix2d[0].length; if (cols < 1) throw new IllegalArgumentException("invalid matrix dimensions"); matrix = new double[rows*cols]; for (int i = 0; i < rows; ++i) { if (cols != matrix2d[i].length) throw new IllegalArgumentException("invalid matrix dimensions"); for (int j = 0; j < cols; ++j) set(i, j, matrix2d[i][j]); } }
/** * 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; }
/** * {@inheritDoc} */ public void set(int row, int col, double val) { checkIndices(row, col); int index = getIndex(row, col); matrix[index] = val; }
/** * {@inheritDoc} */ public double[] getColumn(int column) { checkIndices(0, column); double[] values = new double[rows]; for (int row = 0; row < rows; ++row) values[row] = get(row, column); return values; }
/** * {@inheritDoc} */ public DoubleVector getColumnVector(int column) { return new DenseVector(getColumn(column)); }
matrix = new ArrayMatrix(rows, columns);
/** * {@inheritDoc} */ public void setRow(int row, double[] columns) { checkIndices(row, columns.length - 1); for (int col = 0; col < cols; ++col) matrix[getIndex(row,col)] = columns[col]; }
matrix = new ArrayMatrix(rows, columns);
/** * {@inheritDoc} */ public double get(int row, int col) { checkIndices(row, col); int index = getIndex(row, col); return matrix[index]; }
} else { LOGGER.finer("creating new (in memory) ArrayMatrix"); return new ArrayMatrix(rows, cols);
/** * {@inheritDoc} */ public double[] getRow(int row) { checkIndices(row, 0); double[] rowArr = new double[cols]; int index = getIndex(row, 0); for (int i = 0; i < cols; ++i) rowArr[i] = matrix[index++]; return rowArr; }
} else { LOGGER.finer("creating new (in memory) ArrayMatrix"); return new ArrayMatrix(rows, cols);
/** * {@inheritDoc} */ public void setRow(int row, double[] columns) { checkIndices(row, columns.length - 1); for (int col = 0; col < cols; ++col) matrix[getIndex(row,col)] = columns[col]; }