MathGroup Archive 1999

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

Search the Archive

Re: Integrate with If and Which

There is obviously no "logical" principle involved here.  Since I have no 
access to the Mathematica code I can only speculate about the reasons, but
still I can think of one point which throws some light on this (at least on
the "why If but not Which?" issue). Note that Integrate works only with the
simplest If construction. Thus:

Integrate[If[x < 1, 1, 2], {x, 0, 2}]

Integrate[If[x < 1, 1, If[x < 2, 2, 3]], {x, 0, 3}]
Integrate[If[x < 1, 1, If[x < 2, 2, 3]], {x, 0, 3}]

Both of these can be written in terms of Which, but the first statement
corresponds to a rather trivial form of Which, namely
Which[x<1,1,True,2]. A general Which corresponds to a nested sequence of Ifs
and as we have seen for such a sequence Integrate also does not work. One
could certainly make Integrate work with  simple Which statements, e.g. in
the following way:

Integrate /: Integrate[Which[a_, b_, True, d_], {l__}] :=
  Integrate[If[a, b, d], {l}]

Now indeed we have

Integrate[Which[x < 1, 1, True, 2], {x, 0, 2}]

but you can't say this is really much progress, since anyway Which is not
intended for such simple single clause statements. So really your question
should  be not "Why If but not Which?" but "Why no nested If's?". If we
could Integrate nested Ifs we could extend this to Which.

The answer to this question I really do not know, although I have made a
brief attempt to find a recursive way to integrate nested Ifs and I can say
at least that it is seems hard to do in reasonable generality.
Andrzej Kozlowski
Toyama International University

>From: "L. Dwynn Lafleur" <lafleur at>
To: mathgroup at
>To: mathgroup at
>Subject: [mg20203] [mg20167] Integrate with If and Which
>Date: Mon, Oct 4, 1999, 10:07

> It has been pointed out before in this newsgroup that Mathematica integrates
> some conditional functions but not others.  For example, consider the
> following text translation of a notebook from version 4:
> In[1]:= f[u_] := If[u < 0, u, u^2];
>         g[u_] := Which[u < 0, u, u >= 0, u^2];
> In[3]:= Integrate[f[u], {u, -1, 1}]
> Out[3]= -(1/6)
> In[4]:= Integrate[g[u], {u, -1, 1}]
> Out[4]= Integrate[Which[u < 0, u, u >= 0, u^2], {u, -1, 1}]
> Functions f[u] and g[u] are mathematically identical integrands, but
> Mathematica integrates only the former.  You can force numerical evaluation
> of the latter by wrapping it in N[].
> My question is, "What is the fundamental difference between If and Which
> that makes Mathematica treat them differently?"  As I said above, this
> Mathematica "feature" has been pointed out before and ways to avoid it have
> been described, but I don't recall a post giving the reason for the
> behavior.  I guess I am just curious to know if there is a logical principle
> involved.
> Dwynn
> --
> ==========================================
> L. Dwynn Lafleur
> Professor of Physics
> University of Louisiana at Lafayette
> lafleur at
> ==========================================

  • Prev by Date: Re: Drawing Rectangular Grid
  • Next by Date: Save as HTML
  • Previous by thread: Integrate with If and Which
  • Next by thread: NonlinearRegress and numerical function