Python:分段函数积分错误: "TypeError: cannot determine truth value of ..."

标签 python sympy integrate piecewise

这段代码运行正确:

import sympy as sp

def xon (ton, t):
    return (t-ton)/5

xonInt = sp.integrate (xon(ton, t),t)

print xonInt

但是当函数变得分段时,例如:

import sympy as sp

def xon (ton, t):
    if ton <= t:
        return (t-ton)/5
    else:
        return 0

xonInt = sp.integrate (xon(ton, t),t)

print xonInt

我收到以下错误:

File "//anaconda/lib/python2.7/site-packages/sympy/core/relational.py", line > 103, in nonzero raise TypeError("cannot determine truth value of\n%s" % self)

TypeError: cannot determine truth value of ton <= t

据我了解,该错误是由于 tont 都可以是正数和负数。这是对的吗?如果我为 t 设置正积分限制,错误不会消失。如何计算给定分段函数的积分?

更新:该功能的更新版本,有效:

import sympy as sp

t = sp.symbols('t')   
ton = sp.symbols('ton')
xon = sp.Piecewise((((t-ton)/5), t <= ton), (0, True))

xonInt = sp.integrate (xon,t)
print xonInt

最佳答案

分段类

您需要使用sympy Piecewise class .

正如评论中所建议的:

Piecewise(((t - ton)/5, ton <= t), (0, True))

关于Python:分段函数积分错误: "TypeError: cannot determine truth value of ...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35971417/

相关文章:

python - 如何在 python 中返回函数参数 args、kwargs

python - 如何在 python 中使用 ctypes.windll.Wininet.InternetQueryOptionW

python - IndexedBase 的替代方案

latex - 为物理值 "hbar"定义一个 sympy 符号,它将用 latex 打印

python - Sympy 无法计算涉及 gamma 函数的无限和

css - 如何将 javascript 控件集成到现有的 css 站点中?

python - 如何增加plotly.express直方图中的x刻度

python - mod_wsgi 报告 Pyramid 模块中的语法错误

r - 积分一个非常峰值的函数

java - 将 C++ 库转换为 .so/.dll 共享库