# Is there a function equivalent to rewrite function in Sympy

Sympy provide rewrite function to rewrite expression in terms of other functions.

Rewrites expression containing applications of functions
of one kind in terms of functions of different kind. For
example you can rewrite trigonometric functions as complex
exponentials or combinatorial functions as gamma function.


This function can do something like following:

In [8]: atan(x).rewrite(asin)
Out[8]: (-asin(1/sqrt(x**2 + 1)) + pi/2)*sqrt(x**2)/x

In [9]: atan(x).rewrite(asec)
Out[9]: sqrt(x**2)*asec(sqrt(x**2 + 1))/x


For trigonometric function, here is a related question: How to express trigonometric equation in terms of of given trigonometric function?

P.S.: Sympy actually writing these rewrite rules into the definition of the function.

• You can try Simplify or FullSimplify with a customized ComplexityFunction. These surely know the rules you are interested in, but applying them in the desired fashion can be a bit tricky at times. FunctionExpand, ExpToTrig/TrigToExp, ComplexExpand can be useful in this to get a reasonable starting point offering many possibilities for simplification in the desired way. – Oleksandr R. May 29 '15 at 3:19
• @OleksandrR. generally, I think the answer for this question may be no. I think it is unlikely by using Simplify with ComplexityFunction can do this kind of job automatically. For example FullSimplify[TrigToExp@ArcTan[x], ComplexityFunction -> (LeafCount[#] + 100 Count[#, _ArcTan, Infinity] &)] returns ArcTan[x] as well. – Kattern May 29 '15 at 3:30
• Well, not entirely. "rewrite trigonometric functions as complex exponentials" is covered by TrigToExp[], and "combinatorial functions as gamma functions" is done by FunctionExpand[]. – J. M.'s discontentment May 29 '15 at 3:32
• @Guesswhoitis. rewriting Gamma is also tricky. Assuming[n \[Element] Integers && n > 1, FullSimplify[Gamma[n], ComplexityFunction -> (LeafCount[#] + 100 Count[#, _Gamma, Infinity] &)]] returns Gamma[n]. Same code for Gamma[n + 1] returns n!. Here is a related question. – Kattern May 29 '15 at 4:01
• Well, it's tricky with FullSimplify[]; not so much with FunctionExpand[]. – J. M.'s discontentment May 29 '15 at 4:27