cosd and sind with sympy

泪湿孤枕 提交于 2021-02-08 08:38:38

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!