diff works with polynomials but diff(sin(x),x) gives out the error message. When I removed "from math import *" from the code, diff(sin(x),x) worked. Why is it? What of "math" crashes with symbolic differentiation of trig function?
-
3make sure that the arguments of Sympy functions such as `diff` are Sympy objects. In particular, `sin` should be the *Sympy version* of sin, i.e., the correct usage is `sympy.diff(sympy.sin(x))`. To avoid such issues, it is good practice not to import modules using `import *` – Stelios May 31 '17 at 12:24
-
Thanks! yet to understand the difference between sympy version of sin and math version of sin. but knowing that there is difference helps to understand this crash. – J. S May 31 '17 at 14:13
1 Answers
SymPy functions only work with SymPy functions.
The functions in the math
module are numeric functions. They only know how to work with numeric arguments (floats or ints). If you give them a symbolic expression like x
they won't know what to do. This is because only SymPy functions know how to stay unevaluated.
It is recommend to not use import *
unless you are working interactively, and even then, do it from at most one module. SymPy functions and expressions do not mix with numeric functions from modules like math
or numpy
.
Instead, you can run
import sympy as sym
and use sym.sin
and so on.
Also, in general, it should not be necessary to use the math
module at all when using SymPy, as everything implemented in the math
module is also in implemented in SymPy.

- 86,894
- 26
- 169
- 240