/** * {@inheritDoc} */ public double[][] toDenseArray() { double[][] m = new double[rows()][columns()]; for (int r = 0; r < rows(); ++r) { m[r] = getRowVector(r).toArray(); } return m; }
public String toString() { int rows = rows(); int columns = columns(); StringBuilder sb = new StringBuilder(rows * columns * 2); for (int r = 0; r < rows; ++r) { sb.append('['); for (int c = 0; c < columns; ++c) { sb.append(get(r, c)); if (c + 1 < columns) sb.append(", "); } sb.append("]\n"); } return sb.toString(); } }
/** * Returns the sum of all rows hash codes. */ public int hashCode() { int hash = 0; for (int i = 0; i < rows(); ++i) hash += getRowVector(i).hashCode(); return hash; }
/** * {@inheritDoc} * * @throws IllegalArgumentException if the length of {@code columns} is not * equal to the number of columns */ public void setRow(int row, DoubleVector values) { if (values.length() != columns()) throw new IllegalArgumentException( "Number of values is not equal the number of rows"); for (int c = 0; c < values.length(); ++c) set(row, c, values.get(c)); }
/** * {@inheritDoc} * * @throws IllegalArgumentException if the length of {@code values} is not * equal to the number of rows */ public void setColumn(int column, DoubleVector values) { if (values.length() != rows()) throw new IllegalArgumentException( "Number of values is not equal the number of rows"); for (int r = 0; r < values.length(); ++r) set(r, column, values.get(r)); }
/** * {@inheritDoc} */ public double[] getRow(int row) { return getRowVector(row).toArray(); }
/** * {@inheritDoc} * * @throws IllegalArgumentException if the length of {@code values} is not * equal to the number of rows */ public void setColumn(int column, double[] values) { setColumn(column, Vectors.asVector(values)); }
/** * {@inheritDoc} * * @throws IllegalArgumentException if the length of {@code columns} is not * equal to the number of columns */ public void setRow(int row, double[] columns) { setRow(row, Vectors.asVector(columns)); }
/** * {@inheritDoc} */ public double[] getColumn(int column) { return getColumnVector(column).toArray(); }
/** * Returns the sum of all rows hash codes. */ public int hashCode() { int hash = 0; for (int i = 0; i < rows(); ++i) hash += getRowVector(i).hashCode(); return hash; }
/** * {@inheritDoc} * * @throws IllegalArgumentException if the length of {@code columns} is not * equal to the number of columns */ public void setRow(int row, DoubleVector values) { if (values.length() != columns()) throw new IllegalArgumentException( "Number of values is not equal the number of rows"); for (int c = 0; c < values.length(); ++c) set(row, c, values.get(c)); }
/** * {@inheritDoc} * * @throws IllegalArgumentException if the length of {@code values} is not * equal to the number of rows */ public void setColumn(int column, DoubleVector values) { if (values.length() != rows()) throw new IllegalArgumentException( "Number of values is not equal the number of rows"); for (int r = 0; r < values.length(); ++r) set(r, column, values.get(r)); }
/** * {@inheritDoc} */ public double get(int row, int col) { return getRowVector(row).get(col); }
/** * {@inheritDoc} * * @throws IllegalArgumentException if the length of {@code values} is not * equal to the number of rows */ public void setColumn(int column, double[] values) { setColumn(column, Vectors.asVector(values)); }
/** * {@inheritDoc} * * @throws IllegalArgumentException if the length of {@code columns} is not * equal to the number of columns */ public void setRow(int row, double[] columns) { setRow(row, Vectors.asVector(columns)); }
/** * {@inheritDoc} */ public double[] getColumn(int column) { return getColumnVector(column).toArray(); }
/** * {@inheritDoc} */ public double[][] toDenseArray() { double[][] m = new double[rows()][columns()]; for (int r = 0; r < rows(); ++r) { m[r] = getRowVector(r).toArray(); } return m; } }
/** * Returns {@code} if the object is a {@link Matrix} with the same * dimensions as whose values are equivalent. Value equivalence is computed * using {@link Vector} equality on the row vectors of each matrix. */ public boolean equals(Object o) { if (o instanceof Matrix) { Matrix m = (Matrix)o; int rows = rows(); if (m.rows() != rows) return false; int cols = columns(); if (m.columns() != cols) return false; // REMINDER: this could potentially be optimized for sparse // matrices. for (int r = 0; r < rows; ++r) { for (int c = 0; c < cols; ++c) { if (m.get(r, c) != get(r, c)) return false; } } return true; } return false; }
/** * {@inheritDoc} */ public DoubleVector getColumnVector(int column) { DoubleVector col = new DenseVector(rows()); for (int r = 0; r < rows(); ++r) col.set(r, getRowVector(r).get(column)); return col; }