1/(1-x) 与 SymPy 的集成在对数内给出了错误的符号

Integration of 1/(1-x) with SymPy gives wrong sign inside the logarithm

我想在 sympy 中集成以下和 easyfunction。

import sympy
x = sympy.symbols('x')
e_a = 1/(1-x)
u_x = sympy.integrate(e_a,x)

print(u_x)
sympy.plot(u_x)

我的微积分记忆表明我的结果是 -log(1-x),而 sympy returns -log(x - 1)。看不懂代码有什么问题...

-log(1-x)-log(x-1) 都是有效答案。这是在 issue tracker 上讨论过的,所以我从那里引用。

-log(x-1)=-(log(1-x)+log(-1)) by log rules, and log(-1)=i*pi (as e**(i*pi)=-1). -log(x-1) is the same as -log(1-x)-i*pi so the expressions actually differ by a constant, which makes no difference to the result when taking the derivative of the expression.

This is indeed correct behavior. SymPy doesn't return log(abs(x)) for integrate(1/x) because it isn't valid for complex numbers. Instead, the answer is correct up to an integration constant (which may be complex). All SymPy operations assume that variables are complex by default.

线程末尾建议了一些解决方法。

但最重要的是,-log(x-1) 是正确的,它是 x 大于 1 时所需的答案形式。SymPy 不知道您的意思是 x 小于 1 还是大于1.

要获得特定的反导数,请从给定的初始点积分。例如,从 0 开始的积分给出当 x=0 时为 0 的反导数。

x, t = sympy.symbols('x t')
e_a = 1/(1-x)
u_x = sympy.integrate(e_a, (x, 0, t)).subs(t, x)    
sympy.plot(u_x)