code: public Matrix transform(Matrix matrix, Matrix transformed) {
          // Reject any transforms on matrices that do not have the same
          // sizes.
          if (matrix.rows() != transformed.rows() ||
              matrix.columns() != transformed.columns())
              throw new IllegalArgumentException(
                      "Dimensions of the transformed matrix must match the " +
                      "input matrix");
  
          transform = getTransform(matrix);
  
          if (matrix instanceof SparseMatrix) {
              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
              // and having it transformed.
              for (int row = 0; row < matrix.rows(); ++row) {
                  for (int col = 0; col < matrix.columns(); ++col) {
                      double oldValue = matrix.get(row, col);
                      if (oldValue != 0d) {
                          double newValue = 
                              transform.transform(row, col, oldValue);
                          transformed.set(row, col, newValue);
                      }
                  }
              }
          }
  
          return transformed;
      }
docs: {@inheritDoc}