def U(a, side): '''The unit function: 0 when x<a and 1 when x>a. The value at x=a is determined by the 'side' argument. side = 'left' -> 0 at x=a side = 'right' -> 1 at x=a ''' # Using the definition of Heaviside where it's 1 at x=0 # uncomment these if you want it when it's 0 at x=0 # H0 = Heaviside(x, 0) # heaviside, 0 at x=0 # H1 = 1-H0.subs(x,-x) # heaviside, 1 at x=0 H1 = Heaviside(x, 1) H0 = 1 - H1.subs(x, -x) if side == 'left': return H0.subs(x, x - a) elif side == 'right': return H1.subs(x, x - a) else: raise ValueError("side was not left or right (%s)" % (side))
if x is S.Zero: return S.One elif x is S.Infinity: return S.Zero def _eval_is_real(self): returnself.args[0].is_real h(0) h(oo) # %% plot(x*x, (x, 0, 6)) # %% H = Heaviside(x - 2) H.subs(x, 1) plot(H) # %% y = Symbol('y') f = y**2 g = log(y) p = Piecewise((99, y < -1), (f, y <= 1), (g, True)) [p.subs(y, i) for i in range(-20, 20)]