问题
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