MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: piecewise vs which


If you examine the Help for Piecewise you will see that it is possible to
include, as a last argument, a return value if none of the conditions are
True, and that the default value for this return value is 0. So the routine
was behaving exactly as advertised.

You could define it this way.

f[x_] := Piecewise[{{x^2, x < 2}, {3x, x > 2}}, Indeterminate]


In fact, this is probably a good example for students in computer algebra.
One has to define things completely and if this is not done one is at the
mercy of others.

The Limit question is another good Mathematica question. Mathematica already
has thousands of commands. Most of them are fairly well designed but some of
them aren't and Limit seems to be one of these because people are always
asking and complaining about the Direction option. In any case, students and
teachers will often have to write routines to specialize to their case, or
to implement a more favorable usage. It would be just as well that students
learn this fact, and get used to writing definitions. So here are routines
you could use. (Students should learn how to write usage messages also.)

ForwardLimit::usage =
    "ForwardLimit[expr, point, xvar:x] is the same as Limit[expr, xvar -> \
point, Direction -> 1]";
ForwardLimit[expr_, point_, xvar_:x] :=
  Limit[expr, xvar -> point, Direction -> 1]

BackwardLimit::usage =
    "BackwardLimit[expr, point, xvar:x] is the same as Limit[expr, xvar -> \
point, Direction -> -1]";
BackwardLimit[expr_, point_, xvar_:x] :=
  Limit[expr, xvar -> point, Direction -> -1]

{ForwardLimit[f[x], 2], BackwardLimit[f[x], 2]}
{4, 6}

David Park
djmp at

From: Bradley Stoll [mailto:BradleyS at]
To: mathgroup at

Consider defining a function in Mathematica (v. 5.2) in two different
ways:  f[x_]=Piecewise[{{x^2,x<2},{3x,x>2}}] and
g[x_]=Which[x<2,x^2,x>2,3x].  Notice that 2 is not in the domain of
either function.  However, if I ask for f[2], Mathematica returns 0 and if I
for g[2] Mathematica (correctly) returns nothing.  Is this a bug with
Mathematica (that Mathematica returns 0 for f[2]), since 2 is not in the
While I have eyes, there is another question regarding limits.  Is it
the case that Limit[f[x],x->2] defaulted as
Limit[f[x],x->2,Direction->-1] (a right hand limit)?  Both return 6 in
the above example.  I'm using Mathematica in my calculus class and would
like to explain why Mathematica does certain things.  It doesn't seem
that it would've been too difficult to just have two subroutines (a
right and left hand limit) to determine whether a 'full' limit actually


  • Prev by Date: Re: piecewise vs which
  • Next by Date: Re: Does ContourPlot behave correctly?
  • Previous by thread: Re: piecewise vs which
  • Next by thread: Re: Re: piecewise vs which