Ticket #2452 (assigned enhancement)

Opened 10 months ago

Last modified 3 months ago

heaviside step function needed

Reported by: gfurnish Assigned to: gfurnish (accepted)
Priority: critical Milestone: sage-3.4
Component: calculus Keywords:
Cc:

Description

Symbolic heaviside step function is needed for ease of plotting. Right now you must

sage: def u(x):
    if(x<0):
        return 0
    else:
        return 1*cos(x)
sage: plot(u,-5,5)

instead of

 plot(heaviside(t)*cos(t),t,-5,5)

Change History

03/10/2008 04:05:35 AM changed by wdj

You can make a Heaviside function with Piecewise. sage: f0 = lambda x: 0; f1 = lambda x:x^0; Heaviside = Piecewise([[(-infinity,0),f0],[(0,infinity),f1]]) sage: Heaviside(3) 1 sage: Heaviside(-23) 0 However, plotting is broken. You have to make a truncated function to plot: sage: f0 = lambda x: 0; f1 = lambda x:x^0; Heaviside = Piecewise([[(-10,0),f0],[(0,10),f1]]) sage: Heaviside.plot() works. Of course, improvements to Piecewise would be great...

03/16/2008 01:12:08 PM changed by gfurnish

  • owner changed from was to gfurnish.
  • status changed from new to assigned.

10/21/2008 09:59:45 AM changed by jason

What is the status of this? I noticed something else when trying to do a diffeq lesson:

sage: def unit_step(c):
....:         return piecewise( [( (-oo,c), 0), ((c, oo), 1)] )
....:
sage: unit_step(2)
Piecewise defined function with 2 parts, [((-Infinity, 2), 0), ((2, +Infinity), 1)]
sage: unit_step(2)*unit_step(3)
Piecewise defined function with 3 parts, [[(2, 3), 0], [(3, -Infinity), 0], [(-Infinity, +Infinity), 1]]

That last multiplication just plain does not make sense. Even the intervals are all messed up.

10/21/2008 10:28:23 AM changed by wdj

This might explain the problem:

sage: def unit_step(c):
    return piecewise( [( (-Infinity,c), 0), ((c, Infinity), 1)] )
....:
sage: unit_step(1)
Piecewise defined function with 2 parts, [((-Infinity, 1), 0), ((1, +Infinity), 1)]
sage: unit_step(1)(1/2)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/home/wdj/sagefiles/sage-3.2.alpha0/<ipython console> in <module>()

/home/wdj/sagefiles/sage-3.2.alpha0/local/lib/python2.5/site-packages/sage/functions/piecewise.pyc in __call__(self, x0)
    591         for i in range(n):
    592             if endpts[i] < x0 < endpts[i+1]:
--> 593                 return self.functions()[i](x0)
    594         raise ValueError,"Value not defined outside of domain."
    595

TypeError: 'sage.rings.integer.Integer' object is not callable