Re: How to convert a HeavisideTheta to a PieceWise function
- To: mathgroup at smc.vnet.net
- Subject: [mg93799] Re: How to convert a HeavisideTheta to a PieceWise function
- From: Szabolcs Horvát <szhorvat at gmail.com>
- Date: Wed, 26 Nov 2008 05:12:45 -0500 (EST)
- Organization: University of Bergen
- References: <gggqf5$317$1@smc.vnet.net>
wxziwyz at 126.com wrote: > For example: > we have f(x) = (x + (-2 + x) HeavisideTheta[-2 + x] - 2 (-1 + x) > HeavisideTheta[-1 + x]) HeavisideTheta[x] > > apparently, this is a piecewise function. > > My problem is, How to transfer it into a Piecewise expression, i.e. > > f(x) = Piecewise[{{0, x < 0}, {x, 0 < x <= 1}, {2- x, 1 < x <= 2}, {0, > x > 2}}] > > Is there any function in Mathematica can do this? > This is the first solution that jumps to mind: In[1]:= expr = (x + (-2 + x) HeavisideTheta[-2 + x] - 2 (-1 + x) HeavisideTheta[-1 + x]) HeavisideTheta[x] Out[1]= (x + (-2 + x) HeavisideTheta[-2 + x] - 2 (-1 + x) HeavisideTheta[-1 + x]) HeavisideTheta[x] In[2]:= expr /. HeavisideTheta[e_] :> Piecewise[{{1, e > 0}}] Out[2]= (x + (-2 + x)*Piecewise[{{1, -2 + x > 0}}] - 2*(-1 + x)*Piecewise[{{1, -1 + x > 0}}])* Piecewise[{{1, x > 0}}] In[3]:= PiecewiseExpand[%] Out[3]= \[Piecewise] { {2 - x, 1 < x <= 2}, {x, 0 < x <= 1} } Note that the default value of Piecewise is 0. Also note the difference between HeavisideTheta and Piecewise/UnitStep: HeavisideTheta is not defined for 0, and is meant to be used with functions like D, Integrate, etc. So UnitStep is more suitable for what you want to do, and it automatically works with PiecewiseExpand: In[4]:= expr /. HeavisideTheta -> UnitStep // PiecewiseExpand Out[4]= \[Piecewise] { {2 - x, 1 <= x < 2}, {x, 0 <= x < 1} } You might find it useful to know that FunctionExpand can transform products of HeavisideTheta/UnitStep: In[5]:= expr // FunctionExpand Out[5]= -2 HeavisideTheta[-2 + x] + x HeavisideTheta[-2 + x] + 2 HeavisideTheta[-1 + x] - 2 x HeavisideTheta[-1 + x] + x HeavisideTheta[x]