这是我的代码:
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()
更具体地说,我希望得到零作为输入no的答案。 65,因为1.4超出了区间[1,1.3]。
答案 0 :(得分:7)
您需要告诉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 - 您的代码使用“复合条件”TypeError
1 <=x <=1.3