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

*To*: mathgroup at smc.vnet.net*Subject*: [mg113457] Re: If and Piecewise don't quite do what I need*From*: Bob Hanlon <hanlonr at cox.net>*Date*: Fri, 29 Oct 2010 06:29:35 -0400 (EDT)

You get a wrong result that way because the special case of (n = 0) is not handled by Sum Clear[d] d[0] = 1; d[n_] = Integrate[a^2 Cos[n a], {a, -Pi, Pi}]; Sum[d[n], {n, 0, Infinity}] Pi^3/3 % // N 10.3354 Handling the first term separately, 1 + Sum[d[n], {n, 1, Infinity}] 1 - Pi^3/3 % // N -9.33543 Bob Hanlon ---- Bill Rowe <readnews at sbcglobal.net> wrote: ============= 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.