如何使我的分段函数在 python 中提供的间隔之外为零

How to make my Piecewise function zero outside the provided interval in python

这是我的代码:

    In [61]: import sympy as sp

    In [62]: x = sp.Symbol('x')

    In [63]: phi_1 = sp.Piecewise( ( (1.3-x)/0.3, 1<=x <=1.3 ))

    In [64]: phi_1.subs(x,1.2)
    Out[64]: 0.333333333333334

    In [65]: phi_1.subs(x,1.4)
    Out[65]: Piecewise()

更具体地说,我想得到零作为输入编号的答案。 65,因为 1.4 在区间 [1, 1.3].

之外

您需要告诉 Piecewise 您希望函数在超出范围时求值为零,例如:

import sympy as sp

x = sp.Symbol('x')

phi_1 = sp.Piecewise(
    (0, x < 1),
    (0, x > 1.3),
    ( (1.3-x)/0.3, True )
)

print(phi_1.subs(x,1.2)) # 0.333333333333334

print(phi_1.subs(x,1.4)) # 0

请注意,此语法在 0.7.1 和 0.7.6 中有效——您的代码在 0.7.6 中使用 "compound conditional" 1 <=x <=1.3

引发了 TypeError