       Re: Integrate with If and Which

• To: mathgroup at smc.vnet.net
• Subject: [mg20203] Re: [mg20167] Integrate with If and Which
• From: "Andrzej Kozlowski" <andrzej at tuins.ac.jp>
• Date: Wed, 6 Oct 1999 21:06:24 -0400
• Sender: owner-wri-mathgroup at wolfram.com

```There is obviously no "logical" principle involved here.  Since I have no
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:

In:=
Integrate[If[x < 1, 1, 2], {x, 0, 2}]
Out=
3
but

In:=
Integrate[If[x < 1, 1, If[x < 2, 2, 3]], {x, 0, 3}]
Out=
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:

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

Now indeed we have

In:=
Integrate[Which[x < 1, 1, True, 2], {x, 0, 2}]
Out=
3

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
JAPAN
http://sigma.tuins.ac.jp
http://eri2.tuins.ac.jp

----------
>From: "L. Dwynn Lafleur" <lafleur at usl.edu>
To: mathgroup at smc.vnet.net
>To: mathgroup at smc.vnet.net
>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:= f[u_] := If[u < 0, u, u^2];
>         g[u_] := Which[u < 0, u, u >= 0, u^2];
>
> In:= Integrate[f[u], {u, -1, 1}]
>
> Out= -(1/6)
>
> In:= Integrate[g[u], {u, -1, 1}]
>
> Out= 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 usl.edu
> ==========================================
>
>
>
>
>
>

```

• 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