问题描述
我注意到,没有像 sech(x) 和 csch(x) 这样的函数.
I noticed, that there is no functions like sech(x) and csch(x).
有什么办法可以快速的分别定义为 1/cosh(x) 和 1/sinh(x) 吗?
Is there any way to quickly define them as 1/cosh(x) and 1/sinh(x) respectively?
另外,我怎样才能使 sympy 将 arccos 视为 acos?
Also, how can I make sympy to treat arccos as acos?
我正在使用解析器,因此正确解析了cos(pi/2)".但我希望以类似的方式解析arccos(pi/2)".
I'm using parser, so 'cos(pi/2)' is correctly parsed. But I want 'arccos(pi/2)' to be parsed in a similar manner.
推荐答案
您可以定义实现所需行为的函数.
You can define functions that accomplish your desired behaviour.
import sympy
def sech(x):
return sympy.cosh(x)**(-1)
# sympy.sech = sech
def csch(x):
return sympy.sinh(x)**(-1)
# sympy.csch = csch
arccos = sympy.acos
最初我展示了将它们附加到您在导入 sympy 时使用的符号的行(现在已注释掉).这是一种叫做猴子补丁的坏习惯,会干扰sympy的任何更新code> 可能会使用那些名称或其他可能在代码中的其他地方修补
sympy
的人.它还会让阅读您代码的人认为这些属性是原始 sympy
包的一部分.
Originally I showed lines (commented out now) that attach them to the symbol you use when you import sympy. This is a bad habit called monkey patching and will interfere with any updates to sympy
that may use those names or other people who may have monkey patched sympy
elsewhere in the code. It will also make people reading your code think that those attributes were part of the original sympy
package.
y = sympy.symbols('y')
print sech(y)
print csch(y)
print arccos(y)
这篇关于Sympy 自定义函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!