Nihl
Nihl

Reputation: 587

cosd and sind with sympy

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

Upvotes: 1

Views: 14657

Answers (2)

lukecampbell
lukecampbell

Reputation: 15256

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)

Upvotes: 0

tmdavison
tmdavison

Reputation: 69193

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

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

Upvotes: 1

Related Questions