Re: Piecewise vs. /; ?
- To: mathgroup at smc.vnet.net
- Subject: [mg103901] Re: Piecewise vs. /; ?
- From: Szabolcs Horvát <szhorvat at gmail.com>
- Date: Mon, 12 Oct 2009 06:35:38 -0400 (EDT)
- References: <hashpc$qu8$1@smc.vnet.net>
On 2009.10.11. 14:07, Erik Max Francis wrote: > I'm defining a piecewise function: > > mmin = 0.08; > mmax = 100; > > \[Xi][m_] := Piecewise[ > {{0.035 m^-1.3, 0.08<= m<= 0.50}, > {0.019 m^-2.2, 0.50< m<= 1.00}, > {0.019 m^-2.7, 1.00< m<= 100}}]; (* stars per pc^3 *) > > This works fine for plotting (which I won't show) and integrating: > > In[5]:= Integrate[\[Xi][m], {m, mmin, mmax}] > > Out[5]= 0.136978 > > If I try to define \[Xi][m] with /;, it doesn't work. This version: > > \[Xi][m_ /; 0.08<= m<= 0.50] = 0.035 m^-1.3 ; > \[Xi][m_ /; 0.50< m<= 1.00] = 0.019 m^-2.2; > \[Xi][m_ /; 1.00< m<= 100] = 0.019 m^-2.7; > (* stars per pc^3 *) > > plots correctly but integrate refuses to do anything: > > In[26]:= Integrate[\[Xi]2[m], {m, mmin, mmax}] > > Out[26]= \!\( > \*SubsuperscriptBox[\(\[Integral]\), \(0.08`\), \(100\)]\(\[Xi]2[ > m] \[DifferentialD]m\)\) > > This version (putting the /; in a different place): > > \[Xi]3[m_] = 0.035 m^-1.3 /; 0.08<= m<= 0.50; > \[Xi]3[m_] = 0.019 m^-2.2 /; 0.50< m<= 1.00; > \[Xi]3[m_] = 0.019 m^-2.7 /; 1.00< m<= 100; > (* stars per pc^3 *) > > neither plots properly (the plot is blank) nor integrates properly: > > In[27]:= Integrate[\[Xi]3[m], {m, mmin, mmax}] > > Out[27]= \!\( > \*SubsuperscriptBox[\(\[Integral]\), \(0.08`\), \(100\)]\(\(( > \*FractionBox[\(0.035`\), > SuperscriptBox[\(m\), \(1.3`\)]] /; > 0.08`<= m<= 0.5`)\) \[DifferentialD]m\)\) > > I was under the impression that these were equivalent; what am I missing? > 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. The third version of your example doesn't work (can't be plotted) because the syntax is incorrect: when /; is placed at the end, := must be used, not = . This can be easily discovered if one tries to evaluate that function with a specific numerical value.