Search code examples
sympylogarithm

How to expand a logarithm with multiple variables in sympy?


I am trying to do the following expansion of the log but other vars disappear for some reason.

Is there something extra about the a, b vars that needs to be included?

sympy log expand


Solution

  • You probably want to use the series method rather than Taylor term:

    In [15]: from sympy.abc import a, b, c                                                                                                         
    
    In [16]: e = log(1 + a * (b - c**2) / (1 - 1/c))                                                                                               
    
    In [17]: e                                                                                                                                     
    Out[17]: 
       ⎛  ⎛     2⎞    ⎞
       ⎜a⋅⎝b - c ⎠    ⎟
    log⎜────────── + 1⎟
       ⎜      1       ⎟
       ⎜  1 - ─       ⎟
       ⎝      c       ⎠
    
    In [18]: e.series(c)                                                                                                                           
    Out[18]: 
       ⎛   2  2      ⎞      ⎛   3  3                  ⎞      ⎛   4  4              2  2                 ⎞      ⎛   5  5                          
     2 ⎜  a ⋅b       ⎟    3 ⎜  a ⋅b     2  2          ⎟    4 ⎜  a ⋅b     3  3   3⋅a ⋅b     2            ⎟    5 ⎜  a ⋅b     4  4      3  3    3  2
    c ⋅⎜- ───── - a⋅b⎟ + c ⋅⎜- ───── - a ⋅b  - a⋅b + a⎟ + c ⋅⎜- ───── - a ⋅b  - ─────── + a ⋅b - a⋅b + a⎟ + c ⋅⎜- ───── - a ⋅b  - 2⋅a ⋅b  + a ⋅b 
       ⎝    2        ⎠      ⎝    3                    ⎠      ⎝    4                2                    ⎠      ⎝    5                            
    
                                 ⎞                
          2  2      2            ⎟            ⎛ 6⎞
     - 2⋅a ⋅b  + 2⋅a ⋅b - a⋅b + a⎟ - a⋅b⋅c + O⎝c ⎠
                                 ⎠