'Converts MutableDenseNDimArray to Matrix. Can convert only 2-dim array, else will raise error.
Examples
>>> from sympy import MutableSparseNDimArray
>>> a = MutableSparseNDimArray([1 for i in range(9)], (3, 3))
>>> b = a.tomatrix()
>>> b
Matrix([
[1, 1, 1],
[1, 1, 1],
[1, 1, 1]])'

def tomatrix(self):

    from sympy.matrices import SparseMatrix
    if (self.rank() != 2):
        raise ValueError('Dimensions    must    be    of    size    of    2')
    mat_sparse = {}
    for (key, value) in self._sparse_array.items():
        mat_sparse[self._get_tuple_index(key)] = value
    return SparseMatrix(self.shape[0], self.shape[1], mat_sparse)
