# How to evaluate sum only when the function is specified explicitly?

How does one instruct Mathematica to perform the following infinite sum only if the function U is specified explicitly?

ωn = 2 π n;
Sum[(ωn^2 + D[U[x], x])/(ωn^2 + k^2 + D[U[x], x]), {n, -Infinity, Infinity}]


Currently the sum evaluates with an error message Sum::div: Sum does not converge."

• Isn't that what is happening already? In other words, in your code Sum returns unevaluated, with a warning. If you don't want the warning, you could use Quiet@Sum[...]. You can still use the return value further. – MarcoB Jul 5 '18 at 20:34
• "...function is specified explicitly"... what do you mean by that? Of course you put in a function. Do you mean instead "...if the sum converges"? – David G. Stork Jul 5 '18 at 20:35
• @DavidG.Stork I think OP meant "only when U[x] has been assigned a specific value". – MarcoB Jul 5 '18 at 20:36
• If you mean when U[x] has a value, then you could do something like sum /; U[x] === Unevaluated[U[x]] := Sum[ ... ], and use sum where you would use Sum[ ... ]. – JungHwan Min Jul 5 '18 at 22:04
• @DavidG.Stork I want to evaluate the sum if and only if the explicit form of the function U[x] is specified. The convergence of the sum depends on the choice of U[x]. – 121 Jul 5 '18 at 23:00

I'm not sure if this accurately tends to the needs of the question but I guess it's a start:

sum[U_?((Head[#]===Symbol//Not)&), var_,k_]:=Module[{dU=D[U,var]},
Sum[((2 π n)^2 + dU)/((2 π n)^2 + k^2 + dU),
{n, -Infinity, Infinity}]
]


In a nutshell, sum evaluates when U is not a symbol.

Obviously this is not a foolproof way to check if there is a function defined with that head. I suspect that a more thorough solution would have to go through the DownValues and perhaps OwnValues (check thoroughly what kind of definitions are associated with symbol U).

• This does it -- thanks! – 121 Jul 6 '18 at 16:13
• Great to know it helps; you're welcome – yosimitsu kodanuri Jul 7 '18 at 6:29