问题
There seems to be no equivalent for cosd
, sind
in sympy (ie cosine and sine for arguments in degrees). Is there any simple way to implement those functions ?
For numpy, I did :
numpy.cosd = lambda x : numpy.cos( numpy.deg2rad(x) )
Something like :
sympy.cosd = lambda x : sympy.cos( sympy.pi/180*x )
works for evaluation, but the expression is printed as :
cos(pi*x/180)
which is not great for readability (I have complicated expression due to 3D coordinates changes). Is there any way to create a sympy.cosd
function which evaluates cos(pi/180 * x)
but prints cosd(x)
?
回答1:
sympy
does have a radian converter, in the mpmath
module.
sympy.cosd = lambda x : sympy.cos( sympy.mpmath.radians(x) )
回答2:
To keep the symbols in tact while using degrees, I did something like:
import sympy as sp
def tand(x):
return sp.tan(x * sp.pi / 180)
def sind(x):
return sp.sin(x * sp.pi / 180)
def cosd(x):
return sp.cos(x * sp.pi / 180)
Example Results
In [5]: tand(60)
Out[5]: sqrt(3)
来源:https://stackoverflow.com/questions/31072815/cosd-and-sind-with-sympy