Fix issue sympy/sympy#6618.