问题
I am trying to rewrite some exponential functions in an expression to cosh and sinh. The rewrite() function works to get from a hyperbolic function to its exponential representation. But it does not work to get back.
>>> import sympy
>>> x=sympy.Symbol('x')
>>> sympy.cosh(x).rewrite(sympy.exp)
exp(x)/2 + exp(-x)/2
>>> sympy.cosh(x).rewrite(sympy.exp).rewrite(sympy.cosh)
exp(x)/2 + exp(-x)/2
I would expect the result of the last command to be 'cosh(x)'. Can someone explain to me why it is not? I tried to find some documentation on the rewrite() function but the only bit I found was the short section in http://docs.sympy.org/latest/tutorial/simplification.html that is not really helpful.
回答1:
Applying .rewrite(sympy.cos) returns cosh(x) as you wanted. Apparently, the hyperbolic cosine is treated by rewrite as a variant of the normal one.
Here is a reference on rewrite method.
Alternatively, simplify(expr) also transforms exp(x)/2 + exp(-x)/2 into cosh(x).
来源:https://stackoverflow.com/questions/44157724/simplifying-exponential-representation-of-hyperbolic-functions-in-sympy