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(); } }
/** * {@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 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} */ public double[][] toDenseArray() { double[][] m = new double[rows()][columns()]; for (int r = 0; r < rows(); ++r) { m[r] = getRowVector(r).toArray(); } return m; }
/** * {@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; if (m.rows() == rows() && m.columns() == columns()) { for (int row = 0; row < rows(); ++row) { if (!getRowVector(row).equals(m.getRowVector(row))) return false; } } } return false; }
/** * 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; }