```
DenseMatrixHandle matrix_convert::to_dense(const MatrixHandle& mh)
{
  auto dense = matrix_cast::as_dense(mh);
  if (dense)
    return dense;

  auto col = matrix_cast::as_column(mh);
  if (col)
    return boost::make_shared<DenseMatrix>(*col);

  auto sparse = matrix_cast::as_sparse(mh);
  if (sparse)
  {
    DenseMatrixHandle dense_matrix(new DenseMatrix(sparse->nrows(),sparse->ncols(),0));

    for (index_type row = 0; row < sparse->outerSize(); row++)
    {
      for (SparseRowMatrix::InnerIterator it(*sparse,row); it; ++it)
        (*dense_matrix)(row, it.index()) = it.value();   
    }
    return dense_matrix;
  }

  return DenseMatrixHandle();
}
```