code: public static Matrix copyTo(Matrix matrix, Matrix output) {
          if (matrix.rows() != output.rows() ||
              matrix.columns() != output.columns())
              throw new IllegalArgumentException(
                      "Matrix dimensions must match when copying.");
  
          if (matrix instanceof SparseMatrix) {
              SparseMatrix smatrix = (SparseMatrix) matrix;
  
              // Copy 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())
                      output.set(row, col, rowVec.get(col));
              }
          } else {
              for (int row = 0; row < matrix.rows(); ++row)
                  for (int col = 0; col < matrix.columns(); ++col)
                      output.set(row, col, matrix.get(row, col));
          }
          return output;
      }
docs: Copies values from {@code matrix} to {@code output} and returns {@code
  output}.  {@code output} is assumed to be empty.  This method is useful
  for uses who wish to specify the type of matrix used during a copy
  operation.
  
  @param matrix the matrix to be copied
  @param output the matrix storing the copied values.
  
  @throws IllegalArgumentException when the dimensionality of matrix and
  output do not match
  
  @return {@code output}