Re: If and Piecewise don't quite do what I need

*To*: mathgroup at smc.vnet.net*Subject*: [mg113432] Re: If and Piecewise don't quite do what I need*From*: Bill Rowe <readnews at sbcglobal.net>*Date*: Thu, 28 Oct 2010 04:27:56 -0400 (EDT)

On 10/27/10 at 5:15 AM, sam.takoy at yahoo.com (Sam Takoy) wrote: >I have found that If doesn't do winside it like I would want it to >do. Piecewise does do it, but then it doesn't work the way I would >want it. Is there a solution that does both? >Example: >c[n_] := Piecewise[{{1, {n, 0}}}, >Integrate[a^2 Cos[n a], {a, -Pi, Pi}]] >c[n] (* Calculates the Integral *) >Sum[c[n], {n, 0, Infinity}] (* But fails to Sum *) >d[n_] := If[n == 0, 1, Integrate[a^2 Cos[n a], {a, -Pi, Pi}]] >(* Fails to calculate the integral *) >d[n] (* But does sum OK *) >Sum[d[n], {n, 0, Infinity}] >I want it to evaluate the integral and to sum properly. Instead of using either Piecewise or If why not do: In[5]:= d[0] = 1; d[n_] = Integrate[a^2 Cos[n a], {a, -Pi, Pi}]; In[7]:= Sum[d[n], {n, 0, \[Infinity]}] Out[7]= Pi^3/3 Note, I use Set (=) not SetDelayed (:=) when defining d. This avoids any need to re-compute the integral when doing the summation.