public Vector columnSums() throws MatricksException { Vector sums = new DenseDoubleVector(numCols()); MatrixCursor cursor = cursor(); while (cursor.next()) { sums.set(cursor.col(), cursor.val() + sums.get(cursor.col())); } return sums; }
/** * a <- max(a,a') * * @param a * @param b */ public void setToMaxTranspose() throws MatricksException { // throw new RuntimeException("not implemented"); // TODO: bail if not square MatrixCursor cursor = cursor(); while (cursor.next()) { double u = cursor.val(); double v = get(cursor.col(), cursor.row()); if (u > v) { set(cursor.col(), cursor.row(), u); } else if (v > u) { cursor.set(v); } } }
/* * simple generic implementation, can take advantage of sparsity structor * of this matrix, but not the given one. * @see org.genemania.engine.matricks.Matrix#elementMultiplySum(org.genemania.engine.matricks.Matrix) */ public double elementMultiplySum(Matrix m) { double sum = 0d; MatrixCursor cursor = this.cursor(); while (cursor.next()) { sum += cursor.val() * m.get(cursor.row(), cursor.col()); } return sum; }
public void add(double a, Matrix B) throws MatricksException { if (B.numRows() != this.numRows() && B.numCols() != this.numCols()) { throw new MatricksException("incompatible size for addition"); } MatrixCursor cursor = B.cursor(); while (cursor.next()) { final int row = cursor.row(); final int col = cursor.col(); double v = a * cursor.val() + this.get(row, col); this.set(row, col, v); } }
public void mmwrite(String filename) throws IOException { BufferedWriter writer = new BufferedWriter(new FileWriter(filename)); try { writer.write(MM_COORDINATE_MATRIX_HEADER + "\n"); int nnz = nnz(); writer.write(String.format("%d %d %d\n", numRows(), numCols(), nnz)); MatrixCursor cursor = cursor(); int total = 0; while (cursor.next()) { total += 1; writer.write(String.format("%d %d %f\n", cursor.row() + 1, cursor.col() + 1, cursor.val())); } // safety check for internal data consistency if (total != nnz) { throw new MatricksException( String.format("expected %d elements, only found %d", nnz, total)); } } finally { writer.close(); } }
public void setAll(final double a) throws MatricksException { MatrixCursor cursor = this.cursor(); while (cursor.next()) { this.set(cursor.row(), cursor.col(), a); } }
/* * A = A ./ (x*x') */ public void dotDivOuterProd(Vector x) { MatrixCursor mCursor = cursor(); while (mCursor.next()) { mCursor.set(mCursor.val() / (x.get(mCursor.row()) * x.get(mCursor.col()))); } }