Re: Piecewise vs. /; ?
- To: mathgroup at smc.vnet.net
- Subject: [mg104082] Re: Piecewise vs. /; ?
- From: Szabolcs HorvÃt <szhorvat at gmail.com>
- Date: Sun, 18 Oct 2009 05:28:34 -0400 (EDT)
- References: <hashpc$qu8$1@smc.vnet.net> <hav19a$4gr$1@smc.vnet.net>
I think I messed up the text of my previous message, so it might be a bit difficult to follow. But this got over-explained anyway. In short, theoretically Integrate[] could be made to work with your first two f[x] examples. However, the properties of the Mathematica language are such that it would be very difficult and impractical to write an Integrate[] that could do this. Why is this? Imagine that we are preparing to write a program that does *symbolic* integration. You have to choose in what format to provide the input to the program. The two choices are: the input will be a Fortran subroutine that can compute the value of the integrand. Or the input will be specified in a declarative language designed specifically to represent integrands (and nothing else). Which one would you choose? 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? > > So, for example, when someone writes > > f[x_] := 1 /; x >= 0 > f[x_] := 0 /; x < 0 > > or > > f[x_] := Which[x>=1,1,True,0] > > or > > f[x_] := > Piecewise[{{1, x >= 0}, {0, > True}}] > > or simply > > f[x_] = UnitStep[x] > > 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. > -- Dan > > > > > >> Hi Dan, >> >> The way I understand the difference between /; and Piecewise is the >> following (I am in the camp of David and Szabolcs): Condition is a >> programming construct, not necessarily used only in a mathematical >> setting. >> For internals of Mathematica (such as pattern-matcher and evaluator), >> Condition is more fundamental - if you wish, it is a component of the >> pattern-matcher. It is a part of the programming language, which can be >> used >> for lots of other purposes outside its applications to pure mathematical >> problems. >> >> Pattern-matching is very powerful, but in its pure form completely >> syntax-based. >> Condition (along with PatternTest) allows pattern-matcher to call >> evaluator >> on parts of the expression where the fact of the match can not be >> determined >> by pure syntax (head, structure checks), and this greatly expands the >> class >> of useful patterns that can be constructed. I think that anyone interested >> in software development aspects of Mathematica programming language (for >> example, to exploit new/interesting programming techniques) will probably >> be >> hard pressed to see how many of the things possible due to existence of >> Condition would find simple substitutes should you remove it from the >> language. This is amplified by the fact that a lot of existing Mathematica >> functionality has been developed in Mathematica language itself. >> >> It seems to me that Piecewise was introduced for exactly the purpose of >> better integration of piecewise functions with the other mathematical >> functionality. Since it is less general than Condition (in the above >> sense), >> and furthermore completely separated from the evaluation process >> (pattern-matching), it must be easier to formulate rules or identities >> involving Piecewise and be sure that they will always hold in (and only >> in) >> situations when they should (this is my guess). >> >> As far as Mathematica novices are concerned, I think that a clean >> separation >> of >> programming and mathematical layers of Mathematica language is one of the >> most inportant things to be achieved by an introductory presentation of >> Mathematica, to save them (novices) from the confusion you mentioned. This >> is only my opinion anyway. >> >> Regards, >> Leonid >> >> >> >> >> >> On Thu, Oct 15, 2009 at 3:19 PM, Dan Dubin <ddubin at ucsd.edu> 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! >>> >>> >>> >Szabolcs Horv=E1t wrote: >>> >> In short, /; is a programming construct that affects evaluation. >>> >> Piecewise[] is used to represent a mathematical concept: piecewise >>> >> functions. >>> >> >>> >> If you want to do mathematical operations like integration, then use >>> >> Piecewise[]. Condition[] (/;) is a lot more general than >>> Piecewise[], >>> >> for example one could write a "function" that returns a different >>> value >>> >> in the evening than in the morning, which is obviously not a >>> function >>> in >>> >> the mathematical sense, and thus can't be integrated. >>> > >>> >Got it! Thanks to you and David for responding. >>> > >>> >-- >>> >Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/ >>> > San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype >>> erikmaxfrancis >>> > God is love, but get it in writing. >>> > -- Gypsy Rose Lee >>> >>> >>> -- >>> >>> >>> > > > -- > Professor Dan Dubin > Dept. of Physics, UCSD > La Jolla CA > 92093-0319 > ddubin at ucsd.edu > (858)-534-4174 > fax: (858)-534-0173 >