很奇怪的整合行为

2024-10-02 10:25:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我在sympy工作了几天,我已经遇到了一些问题,这些问题不是最佳的或者不起作用,例如limit函数似乎对超过两个部分的Piecewise表达式不起作用,或者有时打印不起作用(这是下面示例中表达式q的示例),因为“TypeError: '>' not supported between instances of 'complex' and 'int'”,即使我将所有符号指定为real。但我一直认为一个包裹可以这样做。即使它是一个bug,它看起来是合理的/有可能发生的。但现在我面临着一个问题,我无法向自己解释为什么sympy会这样做:

在编写我想要实现的目标时,我使用了用于测试的示例函数和值,但我编写了一些漂亮的代码,其中只包含问题和一些解释:

代码如下:

from sympy import *
import sympy
print("sympy version:", sympy.__version__)

x = symbols('x', real = True)
t = symbols('t', real = True)

l1 = 1/Float(3.5)
l2 = 1/Float(1)

#I create two exp-Distributions pdf's
f1 = Piecewise((l1*exp(-l1*x), x>=0), (0, True))
f2 = Piecewise((l2*exp(-l2*x), x>=0), (0, True))

#and calculate their convolution. I cannot plot the result, but it seems to be correct
q = integrate(f1.subs(x, t)*f2.subs(x, x-t), (t, -oo, oo))


#now I want to shift this new distribution q to the left...
r = Float(1.5)
q = q.subs(x, x+r)


#...and cut the part in the negative range, but keep a delta-function instead,
# to conserve the normed (=1) integral
Q0 = integrate(q, (x, -oo, 0))
print("Q0:", Q0)
print()

q_cut = Piecewise((q, x>=0), (0, true))
q_cut2 = Piecewise((q, x>0), (0, true))
q_cut3 = q*Heaviside(x)

myDelta = Q0*DiracDelta(x)
a = integrate(myDelta, (x, -oo, oo))
print("Integral delta-function: ", a)
print("Integral delta-function 2: ", integrate(myDelta, (x, -1, 1)))
print()
b = integrate(q_cut, (x, -oo, oo))
print("Integral cut function: ", b)
print()
print("All correct until now, they also sum up to 1:", a+b)
print()
print("But when I sum them before the integration, the Delta-Function-Part is missing:")
myDist = myDelta+q_cut
c = integrate(myDist, (x, -oo, oo))
print(c, "(I expect this integral to be 1)")
print("no matter which of the above definitions of q_cut I use:")
myDist = myDelta+q_cut2
print(integrate(myDist, (x, -oo, oo)))
myDist = myDelta+q_cut3
print(integrate(myDist, (x, -oo, oo)))
print()
print("If I do the same with a simpler function, it works:")
testFkt1 = exp(-x**2)
print("Integral test-function only:", integrate(testFkt1, (x, -oo, oo)))
testFkt2 = testFkt1 + myDelta
print("Integral with delta:", integrate(testFkt2, (x, -oo, oo)))
testFkt3 = x**2
print("Integral test-function only:", integrate(testFkt3, (x, -1, 1)))
testFkt4 = testFkt3 + myDelta
print("Integral with delta:", integrate(testFkt4, (x, -1, 1)))

下面是输出:

sympy version: 1.3
Q0: 0.177237383515894

Integral delta-function:  0.177237383515894
Integral delta-function 2:  0.177237383515894

Integral cut function:  0.822762616484106

All correct until now, they also sum up to 1: 1.00000000000000

But when I sum them before the integration, the Delta-Function-Part is missing:
0.822762616484106 (I expect this integral to be 1)
no matter which of the above definitions of q_cut I use:
0.822762616484106
0.822762616484106

If I do the same with a simpler function, it works:
Integral test-function only: sqrt(pi)
Integral with delta: 0.177237383515894 + sqrt(pi)
Integral test-function only: 2/3
Integral with delta: 0.843904050182561

我真的在寻找一个解释,为什么会发生这种情况或如何可以避免。 如果我把它应用到未来的问题中,我不知道它是否考虑了delta函数对积分的贡献。先谢谢你。你知道吗


Tags: ofthetowithfunctionoodeltaintegrate

热门问题