当对实数值对数表达式进行积分时,为什么sympy积分会引入虚数常量?

问题描述 投票:1回答:1

我在研究问题时找到了解决方案,但我仍然在此处发布问题/答案。我在网上搜索时找不到其他资源,因此希望本文对以后的人有所帮助。

您好,我正在使用sympy计算分段定义函数的积分。但是,积分会引入虚数常量。

最小示例

from sympy import *
f = interpolating_spline(1, Symbol('p'), [0,0.1,1], [0,10,1000])
r = (ln(20000-f)).simplify()
s = integrate(r)
print('r='+latex(r))
print('s='+latex(s)) 

哪个给予

r=\log{\left(\begin{cases} 20000 - 100.0 p & \text{for}: p \geq 0 \wedge p \leq 0.1 \20100.0 - 1100.0 p & \text{for}: p \geq 0.1 \wedge p \leq 1 \end{cases} \right)}s=\begin{cases} \text{NaN} & \text{for}: p < 0 \1.0 p \log{\left(20000 - 100.0 p \right)} - 1.0 p - 200.0 \log{\left(100.0 p - 20000.0 \right)} + 1980.69751050723 + 200.0 i \pi & \text{for}: p \leq 0.1 \1.0 p \log{\left(20100.0 - 1100.0 p \right)} - 1.0 p - 18.2727272727273 \log{\left(1100.0 p - 20100.0 \right)} + 181.054613456189 + 18.2727272727273 i \pi & \text{for}: p \leq 1 \\text{NaN} & \text{otherwise} \end{cases}

为什么积分中存在虚数常数?我隐约记得一些复杂分析中关于分支切割的事情,所以它可能与此有关?

问题表述的原点是Kelly criterion,适用于超额损失曲线。

预期结果

如果仅对第一个表达式进行积分,则会得到不带常量的实值积分:

In [71]: integrate(ln(20000-100*p))
Out[71]: p*log(20000 - 100*p) - p - 200*log(p - 200)

但是这个结果也很奇怪,因为p=0.1将为负,因此将不会为p-200定义结果。非常奇怪。

python sympy
1个回答
1
投票

将集成策略更改为manual

使用manual=True积分策略时,常数消失。

In [84]: print('r='+latex(integrate(r,manual=True)))

r=\begin{cases} \text{NaN} & \text{for}: p < 0 \- 1.0 p - 0.01 \left(20000 - 100.0 p\right) \log{\left(20000 - 100.0 p \right)} + 200.0 \log{\left(20000 \right)} & \text{for}: p \leq 0.1 \- 1.0 p - 0.000909090909090909 \left(20100.0 - 1100.0 p\right) \log{\left(20100.0 - 1100.0 p \right)} - 1799.64289705104 + 200.0 \log{\left(20000 \right)} & \text{for}: p \leq 1 \\text{NaN} & \text{otherwise} \end{cases}

我在https://docs.sympy.org/latest/modules/integrals/integrals.html#sympy.integrals.integrals.integrate的文档中找到此选项

© www.soinside.com 2019 - 2024. All rights reserved.