简体   繁体   English

计算Sympy中的三角表达式

[英]Evaluating trigonometric expressions in sympy

Using python 2.7 with PyCharm Community Edition 2016.2.3 + Anaconda distribution. 在PyCharm Community Edition 2016.2.3 + Anaconda发行版中使用python 2.7。

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) , . 输出为cos(25) Is there a way to evaluate trigonometric identities such as sin/cos, at a certain angle ? 有没有办法在特定角度评估三角身份,例如sin / cos? I've tried cos(degrees(x)) , but nothing differs. 我已经尝试过cos(degrees(x)) ,但是没有什么不同。 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 : 使用函数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: 要以度为单位进行计算,请使用mpmath.radians将角度转换为弧度,从而对弧度值执行计算:

>>> 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. 想象一下,如果同样进行from math import *会发生什么,那么来自这两个模块的cos函数之一将毫无用处。

See the PEP 8 guideline on imports . 参见PEP 8进口指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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