1
votes

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)?

2
Many mathematical formulas look beautiful when working in radians, but sprout pi/180 warts when working in degrees. It's better to work in radians and avoid degrees entirely -- or convert to degrees at the end if you absolutely must. - unutbu
Fair enough, thanks for the link. Then how can the replacement be done ? if i have an expression y=sympy.cos(x), y.subs(sympy.cos, sympy.cosd) does not work with the above definition of sympy.cosd (as it's not a sympy function but just a usual lambda function). y.subs(x, pi/180*x) works here, but if the expression in the cos is complicated (like polynomial expression of various parameters) it might not be convenient to search which parameters must be replace with a pi/180 and which must not. - Nihl
Let x, theta, phi, etc. be Symbols representing quantities in radians. Keep a list of these symbols: angles = [x, theta, phi]. Then, at the very end, use y.subs([(angle, angle*pi/180) for angle in angles]) to change the meaning of the symbols to degrees. - unutbu

2 Answers

1
votes

sympy does have a radian converter, in the mpmath module.

sympy.cosd = lambda x : sympy.cos( sympy.mpmath.radians(x) )
0
votes

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)