SparseMatrix smatrix = (SparseMatrix) matrix;
// Transform a sparse matrix by only iterating over the non zero
// values in each row.
for (int row = 0; row < matrix.rows(); ++row) {
SparseDoubleVector rowVec = smatrix.getRowVector(row);
for (int col : rowVec.getNonZeroIndices()) {
double newValue =
transform.transform(row, col, rowVec.get(col));
transformed.set(row, col, newValue);
}
}
} else {
// Transform dense matrices by inspecting each value in the matrix