import sympy


def dummy_var_matrix(A):
    """Substitute non-zero elements with symbols
    
    Returns the new matrix and a dictionary that can be used
    with the subs method to go back to the original matrix.
    
    This can improve performance considerably of the derminant function in
    Sympy.
    """

    A = sympy.Matrix(A)
    subst_dict = {}
    def elem(i,j):
        if A[i,j] != 0:
            sym = sympy.Symbol('a%d%d'%(i,j))
            subst_dict[sym] = A[i,j]
            return sym
        else:
            return 0
    Aprime = sympy.Matrix(A.rows, A.cols, elem)
    return Aprime, subst_dict