Added necessary import of S from sympy.core