borgmater
borgmater

Reputation: 706

Evaluating trigonometric expressions in sympy

Using python 2.7 with PyCharm Community Edition 2016.2.3 + Anaconda distribution.

I have an input similar to :

from sympy import *

x = symbols('x')
f = cos(x)
print (f.subs(x, 25))

The output is cos(25), . Is there a way to evaluate trigonometric identities such as sin/cos, at a certain angle ? I've tried cos(degrees(x)), but nothing differs. Am I missing some crucial part of documentation or there really isn't a way to do this ? Ty for your help :)

Upvotes: 4

Views: 11323

Answers (1)

Moses Koledoye
Moses Koledoye

Reputation: 78546

Perform a numerical evaluation using function N:

>>> from sympy import N, symbols, cos
>>> x = symbols('x')
>>> f = cos(x)
>>> f.subs(x, 25)
cos(25)
>>> N(f.subs(x, 25)) # evaluate after substitution 
0.991202811863474

To make the computation in degrees, convert the angle to radians, using mpmath.radians, so the computation is performed on a rad value:

>>> import mpmath
>>> f.subs(x, mpmath.radians(25))
0.906307787036650

Importing with * (wildcard imports) isn't a very good idea. Imagine what happens if you equally did from math import *, then one of the cos functions from both modules will be out in the wild.

See the PEP 8 guideline on imports.

Upvotes: 13

Related Questions