如何在python中提供的间隔之外使我的分段函数为零 [英] How to make my Piecewise function zero outside the provided interval in python
本文介绍了如何在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] 之外.
More specifically, I want to get zero as an answer to the input no. 65, since 1.4 is outside the interval [1, 1.3].
推荐答案
您需要告诉 Piecewise
您希望函数在超出范围时计算为零,例如:
You need to tell Piecewise
that you want the function to evaluate to zero when outside the bounds, for example:
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 中使用复合条件"1 <=x
引发
TypeError
=1.3
Note that this syntax works in 0.7.1 and 0.7.6 -- your code raises an TypeError
in 0.7.6 with the "compound conditional" 1 <=x <=1.3
这篇关于如何在python中提供的间隔之外使我的分段函数为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文