From: Dan Dubin on 15 Oct 2009 07:17 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(a)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 --
From: Daniel Lichtblau on 16 Oct 2009 07:01 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! > [...] The documentation makes quite clear that Condition is a construct meant for, and understood by, pattern matching functions. In this capacity it may be used for programmatic constructs, e.g. DownValues that only evaluate under certain conditions. Piecewise would be a terrible emulator of this functionality. By way of contrast, functions like NIntegrate understand mathematical constructs such as Piecewise. As some examples earlier in the thread indicated, evaluation of "functions" defined in terms of Condition might leave one with an unintended integrand. Let me comment on some specifics of your note. "[A]s far as I can see the difference between them is merely a Mathematica-specific matter of the mechanics of how the expressions are evaluated..." The life blood of Mathematica semantics is in the mechanics of evaluation. While this might well be deemed Mathematica-specific, it determines whether and how the language functions. "with little (or no?) utility,..." The differences in supported functionality, several presented in this thread, argue otherwise. But I concede this is a matter of opinion. "that causes considerable confusion among new users." I think some amount of confusion is not new, but rather of the following form. One defines a function using multiple DownValues with Conditional, then tries to use it in Reduce or Integrate, and this has an unwanted outcome. Question might be: "Why does Piecewise work and not my definition?" A few versions ago, prior to introduction of Piecewise to Mathematica*, the question was simply "Why does this not work?" Point being this: Piecewise has extended the range of Mathematica, to do things that Condition was never designed to do, and for which evaluation semantics do not (and never did) support use of Condition. Bottom line: The intended and supported usage of each tend to be quite different, though there may be some amount of overlap at least in terms of attempted usage. Suffice it to say, neither functionality will be getting supplanted by the other. Daniel Lichtblau Wolfram Research *Around 2003 we had a design meeting where the R&D Director said "I plan to introduce some new functionality to Mathematica, piecewise". Naively, I said "Okay. What's the functionality." True story.
From: Szabolcs Horvát on 16 Oct 2009 07:03 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.
From: Leonid Shifrin on 16 Oct 2009 07:03 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(a)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(a)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 > > > -- > > >
From: Szabolcs Horvát on 18 Oct 2009 05:22 2009/10/17 Dan Dubin <ddubin(a)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.
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: undocumented feature: TableView Next: Import from web addresses fails |