Search code examples
pythonnumpysympylogarithm

Python Logarithm Function


I'm looking for an example of operations with logarithms in Python. I've tried with sympy and numpy and I still can't do what I want. For example, for an input like this:

log(x+1)+log(4-x)=log(100) # it's just an example 

the output should give me the x value. I need to do this with any other functions like log(x+1)=4 or log(x)-log(x+1)=log(x).

Is there some method or somewhere (documentation or similar) where can I find how to do this?


Solution

  • I may be misunderstanding what you need to do because you said you tried sympy already. However, it looks like you just want to solve for x in an algebraic equation.

    Solving for x in the equation

    log(x+1)+log(4-x)=log(100)
    

    using sympy would be

    >>> from sympy import Symbol, solve, log
    >>> x = Symbol('x')
    >>> solve(log(x+1) + log(4-x) - log(100), x)
    [3/2 - 5*sqrt(15)*I/2, 3/2 + 5*sqrt(15)*I/2]
    

    If you want, you can check that these two solutions are correct with numpy.

    >>> import numpy as np
    >>> a = 3/2 - 5*np.sqrt(15)*1j/2
    >>> b = 3/2 + 5*np.sqrt(15)*1j/2
    >>> np.log(a + 1) + np.log(4-a)
    (4.6051701859880918+0j)
    >>> np.log(b + 1) + np.log(4-b)
    (4.6051701859880918+0j)
    >>> np.log(100)
    4.6051701859880918
    

    Is that not what you are looking for?