2

Why does log(xy) = log(x) + log(y) not work in SymPy?

I tried this:

from sympy import *
var('x y')
print(simplify(log(x*y)))
print(expand(log(x*y)))
print(collect(log(x*y),x))
print(solve(log(x*y),x))
# log(x*y)
# log(x*y)
# log(x*y)
# [1/y]

1 Answer 1

7

log(xy) = log(x)+log(y) does not always hold. More specifically, this may to problems if both x and y are negative or in the complex domain. The Wolfram Alpha link you gave also states “Alternate form assuming x and y are positive”.

To see this relation in SymPy, you have to mark the symbols x and y as positive, e.g. like this:

from sympy import symbols,log
x,y = symbols("x,y",positive=True)
expr = log(x*y)
expr.expand()

Alternatively (as hinted at by user6655984) you can use the force hint to let SymPy assume that everything is maximally benign:

from sympy import log
from sympy.abc import x,y
expr = log(x*y)
expr.expand(force=True)
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.