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

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

Use the proper syntax for Piecewise and use Set rather than SetDelayed so that the integral is evaluated just once c[n_] = Piecewise[{{1, n == 0}}, Integrate[a^2 Cos[n a], {a, -Pi, Pi}]]; c[n] Piecewise[{{1, n == 0}}, (4*n*Pi*Cos[n*Pi] + 2*(-2 + n^2*Pi^2)* Sin[n*Pi])/n^3] Sum[c[n], {n, 0, Infinity}] (1/3)*(3 - Pi^3) For If, use Evaluate and Set d[n_] = If[n == 0, 1, Evaluate[Integrate[a^2 Cos[n a], {a, -Pi, Pi}]]]; d[n] If[n == 0, 1, (4*n*Pi*Cos[n*Pi] + 2*(-2 + n^2*Pi^2)*Sin[n*Pi])/n^3] Sum[c[n], {n, 0, Infinity}] (1/3)*(3 - Pi^3) Bob Hanlon ---- Sam Takoy <sam.takoy at yahoo.com> wrote: ============= Hi, 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. Thanks, Sam PS: Slightly offtopic, I found that the Limit trick for n = 0 with Fourier coefficients often doesn't work resulting in a 0*Infinity indetermination.