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.

  • $\begingroup$ 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. $\endgroup$ – Oleksandr R. May 29 '15 at 3:19
  • $\begingroup$ @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. $\endgroup$ – Kattern May 29 '15 at 3:30
  • 1
    $\begingroup$ Well, not entirely. "rewrite trigonometric functions as complex exponentials" is covered by TrigToExp[], and "combinatorial functions as gamma functions" is done by FunctionExpand[]. $\endgroup$ – J. M.'s discontentment May 29 '15 at 3:32
  • $\begingroup$ @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. $\endgroup$ – Kattern May 29 '15 at 4:01
  • $\begingroup$ Well, it's tricky with FullSimplify[]; not so much with FunctionExpand[]. $\endgroup$ – J. M.'s discontentment May 29 '15 at 4:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.