Sympy custom function

元气小坏坏 提交于 2019-12-10 18:44:32

问题


I noticed, that there is no functions like sech(x) and csch(x).

Is there any way to quickly define them as 1/cosh(x) and 1/sinh(x) respectively?

Also, how can I make sympy to treat arccos as acos?

I'm using parser, so 'cos(pi/2)' is correctly parsed. But I want 'arccos(pi/2)' to be parsed in a similar manner.


回答1:


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

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)


来源:https://stackoverflow.com/questions/24848051/sympy-custom-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!