Expand sin(acot(…)) in sympy?

Question:

Is there a way to expand the trigonometric function of an inverse trigonometric function? I have a long-expression f that contains many such subexpressions, e.g.:

  • sin(0.5 acot(x))**2
  • cos(0.5 acot(x))**2
  • sin(acot(x))

These expressions can be rewritten without trigonometric functions, e.g.:

  • 1/2 - 1/2 * x / sp.sqrt(x**2 + 1)

I’ve tried expand_trig and trigsimp to no avail. Also, I can’t find a way to directly substitute the analytical expressions in.

Any suggestions?

Asked By: Xander

||

Answers:

There are various ways to do this. Some examples:

In [1]: sin(acot(x))
Out[1]: 
       1       
───────────────
       ________
      ╱     1  
x⋅   ╱  1 + ── 
    ╱        2 
  ╲╱        x  

In [2]: sin(acot(x)/2)**2
Out[2]: 
   2⎛acot(x)⎞
sin ⎜───────⎟
    ⎝   2   ⎠

In [3]: e = sin(acot(x)/2)**2

In [4]: e.rewrite(log)
Out[4]: 
    ⎛  ⎛   ⎛    ⅈ⎞      ⎛    ⅈ⎞⎞⎞
    ⎜ⅈ⋅⎜log⎜1 - ─⎟ - log⎜1 + ─⎟⎟⎟
   2⎜  ⎝   ⎝    x⎠      ⎝    x⎠⎠⎟
sin ⎜───────────────────────────⎟
    ⎝             4             ⎠

In [5]: e.rewrite(log).rewrite(exp)
Out[5]: 
      ⎛   ⎛      _______       _______⎞ ⎞
      ⎜   ⎜     ╱     ⅈ       ╱     ⅈ ⎟ ⎟
      ⎜   ⎜  4 ╱  1 - ─    4 ╱  1 + ─ ⎟ ⎟
      ⎜   ⎜  ╲╱       x    ╲╱       x ⎟ ⎟
      ⎜-ⅈ⋅⎜- ─────────── + ───────────⎟ ⎟
      ⎜   ⎜      _______       _______⎟ ⎟
      ⎜   ⎜     ╱     ⅈ       ╱     ⅈ ⎟ ⎟
      ⎜   ⎜  4 ╱  1 + ─    4 ╱  1 - ─ ⎟ ⎟
      ⎜   ⎝  ╲╱       x    ╲╱       x ⎠ ⎟
 2⋅log⎜─────────────────────────────────⎟
      ⎝                2                ⎠
ℯ                                        

In [6]: e.rewrite(log).rewrite(exp).expand()
Out[6]: 
       _______             _______ 
      ╱     ⅈ             ╱     ⅈ  
     ╱  1 - ─            ╱  1 + ─  
   ╲╱       x     1    ╲╱       x  
- ───────────── + ─ - ─────────────
        _______   2         _______
       ╱     ⅈ             ╱     ⅈ 
  4⋅  ╱  1 + ─        4⋅  ╱  1 - ─ 
    ╲╱       x          ╲╱       x 

In [7]: simplify(_)
Out[7]: 
1               1            
─ - ─────────────────────────
2         _______     _______
         ╱     ⅈ     ╱     ⅈ 
    2⋅  ╱  1 - ─ ⋅  ╱  1 + ─ 
      ╲╱       x  ╲╱       x 

Some cases will work differently if you declare x as real or positive e.g. x = symbols('x', real=True).

Answered By: Oscar Benjamin
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.