简体   繁体   中英

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM