[Date Index]
[Thread Index]
[Author Index]
Re: Piecewise vs. /; ?
*To*: mathgroup at smc.vnet.net
*Subject*: [mg104081] Re: Piecewise vs. /; ?
*From*: Szabolcs HorvÃt <szhorvat at gmail.com>
*Date*: Sun, 18 Oct 2009 05:28:22 -0400 (EDT)
*References*: <hashpc$qu8$1@smc.vnet.net> <hav19a$4gr$1@smc.vnet.net>
2009/10/17 Dan Dubin <ddubin at ucsd.edu>:
> Hi -- I want to thank everyone for their thoughtful replies. Even after
> using Mathematica for many years I still have to ask really stupid questions
> sometimes, and I appreciate your patience.
> I appreciate that the use of a conditional statement might be more
> elegant in a given context than the application of a piecewise function. One
> remaining issue for me is that I don't understand the difference between
> "pure mathematics" and what you refer to as "programming constructs". I see
> any program as a function that takes one set of (binary) numbers and
> transforms it into another set. I think of programming control statements
> such as which, if, etc, or pattern matching functions, as piecewise
> functions of their arguments. Am I wrong in thinking this way?
Yes. The conditions used in these might have nothing to do with a
function's argument. Don't always think in terms of "function
definitions". To be precise these are really the definitions of
transformation rules that are used during the evaluation process.
They're neither functions in the mathematical sense, nor in the sense
used in programming languages like C or Fortran.
When one types in Integrate[f[x], {x,-1,1}], first f[x] is evaluated,
and Integrate sees only the result.
>
> So, for example, when someone writes
>
> f[x_] := 1 /; x >= 0
> f[x_] := 0 /; x < 0
f[x] evaluates to f[x] here because neither x >= 0 nor x < 0 evaluate
to True (x is simply treated as a symbol during evaluation).
Integrate will see an "unspecified function". It won't dig into the
transformation rules associated with the symbol f because
transformation rules were not meant (and thus are very impractical) to
represent mathematical concepts. Integrate was designed to work with
"static" Mathematica expressions (i.e. ones that do not evaluate to
anything) that represent a mathematical expression in the integration
variable.
>
> or
>
> f[x_] := Which[x>=1,1,True,0]
>
Here f[x] evaluates to Which[x>=1,1,True,0]. Which[] will not
evaluate further because x >= 1 does not evaluate to either True or
False, so in theory Integrate[] could dig into it, and try to
interpret it as a mathematical expression. However, Which[] can
contain all kinds of nasty conditions, e.g. Which[x >= 1, 1,
$VersionNumber > 5, 0]. Since Which[] is not designed to represent a
mathematical expression (and there are many small details to its
behaviour which would make such a use very impractical or impossible),
Integrate[] can't handle it.
> or
>
> f[x_] :=
> Piecewise[{{1, x >= 0}, {0,
> True}}]
It is Piecewise[] that was designed for this purpose, so use /it/.
The f[x_]:= bit isn't even necessary.
>
> or simply
>
> f[x_] = UnitStep[x]
Yes, this works too. Now you can ask what use was it to introduce
HeavisideTheta[] if we already had UnitStep[] ?! ;-)
>
> I don't understand why Integrate[f[x],{x,-1,1}] should not yield the same
> result in each case. This is an example of what I was trying to get at in
> my previous message . Each function provides exactly the same result for
> given real inputs. The integrand in Integrate need only be evaluated for
> real inputs (although what Integrate actually does is unknown to me).
> Therefore, I would think it would be beneficial if Mathematica would yield
> the same results in this case.
So, in summary, Mathematica has "things" that do something, and
"things" that represent maths stuff. The two often can't be mixed
(but sometimes they can, which causes confusion). There are "maths
things" that appear to *do* something, e.g. Sin[Pi] is magically
changed to 0, or 1+1 is changed to 2, but what happens in fact is that
these mathematical expressions are automatically transformed into
equivalent mathematical objects (in an attempt to bring things to
canonical forms when this is practical to do). The truth is that
Mathematica is messy and not completely consistent. But I don't mind
it at all because otherwise it couldn't be nearly as practical as it
is now :)
To be honest, what I said above is a gross oversimplification and not
completely true. To really understand how the system works, read
about evaluation and pattern matching. Start at
"tutorial/PrinciplesOfEvaluation", then open the Virtual Book, and
follow the table of contents there. If you do this, it'll become
obvious to you why /; and Piecewise are far from interchangeable.
Prev by Date:
**Re: Piecewise vs. /; ?**
Next by Date:
**Re: Piecewise vs. /; ?**
Previous by thread:
**Re: Piecewise vs. /; ?**
Next by thread:
**Re: Piecewise vs. /; ?**
| |