Re: Piecewise vs. /; ?
- To: mathgroup at smc.vnet.net
- Subject: [mg104035] Re: Piecewise vs. /; ?
- From: Szabolcs Horvát <szhorvat at gmail.com>
- Date: Fri, 16 Oct 2009 07:20:25 -0400 (EDT)
- References: <hashpc$qu8$1@smc.vnet.net> <hav19a$4gr$1@smc.vnet.net> <hb70d0$8uj$1@smc.vnet.net>
On 2009.10.15. 13:17, Dan Dubin wrote:
> Folks --
>
> I would respectfully disagree with the idea that /; is more general
> than Piecewise. The example given, a function that returns a
> different value in the morning than the evening, is simply a
> time-dependent function that could be constructed using Piecewise, as
> in
>
> f[x_,h_] = Piecewise[{{x, h< 12}, {-x, h>= 12}}];
>
> and called via
>
> f[x,DateList[[4]]]
>
>
> In fact, it seems to me that any difference in the way that
> Mathematica interprets Piecewise and conditionals should be removed
> from later editions of Mathematica, since as far as I can see the
> difference between them is merely a Mathematica-specific matter of
> the mechanics of how the expressions are evaluated with little (or
> no?) utility, that causes considerable confusion among new users. I
> don't see any benefit in this distinction, but feel free to convince
> me otherwise!
>
Piecewise[] is explicitly designed to be used to represent a
mathematical concept, piecewise functions. Mathematical operations,
like D[], can be applied to it.
For example,
D[Piecewise[{{1, x > 0}}, 0], x]
/; can be used with patterns (or, as a more special use, with :=
definitions or Module). It cannot be used to compose a standalone
mathematical expression like Piecewise can.
Actually Piecewise[] is much closer to Which[] than to /;. But again,
Which[] is a programming construct, not something that is meant to be
used as a mathematical expression. The following does not work as
expected from a mathematical point of view:
D[Which[x > 0, 1, True, 0], x]
There are many differences between how these three types of expressions
are (or are not) evaluated, making Piecewise[] suitable for mathematical
use, but unwieldy for programming/pattern matching use.
The overlap between programming and mathematical use of some constructs
(for example, "functions") can already cause confusion in Mathematica.
I can see how a programming use of Piecewise could be forced... but I
think that it was a good decision to try to separate these two kinds of
uses in this case.