# Efficiently define a function as the numerical result of infinite sums

I want to approximate the solution to the following sum, in such a way that I can plot the function for the variable $$\phi$$, for a fixed value of $$\mu$$.

$$$$f(r(\phi), \mu)=4 e^{-\mu } r^2\sum_{n=0}^{\infty}\sum_{k=0}^{\infty}\frac{ \left(\frac{r^2}{r^2+1}\right)^k \binom{n}{k} \mu ^{k+n} \left(\frac{1}{r^2+1}\right)^{n-k} \left(n-\mu\frac{ \, _1F_1\left(-n;n+2;-r^2 \mu \right)}{\, _1F_1\left(-n;n+1;-r^2 \mu \right)}\right)^2}{\Gamma (k+n+1)}$$$$

with $$r(\phi)=\tan \phi$$

So now I want to represent that expression as a function of $$\phi$$ for fixed $$\mu$$. I define the Function to be summed

F[μ_, Φ_] := 4*((μ^(k + n)*Binomial[n, k]*(Cos[Φ]^2)^(-k + n)*(-n + ((1 + n)*μ*n!
*Hypergeometric1F1[-n, 2 + n, (-μ)*Tan[Φ]^2])/
((1 + n)!*Hypergeometric1F1[-n, 1 + n, (-μ)*Tan[Φ]^2]))^2*(Sin[Φ]^2)^k)/(E^μ*(k + n)!))*Tan[Φ]^2;


Now the following way to perform the summation will take indefinite time to compute even for a given value of $$\phi$$ and $$\mu$$ so I cannot use it to plot:

 (NSum[NSum[N[F[μ,Φ]],{k,0,∞}],{n,0,∞}])/.{μ->1,Φ->Pi/4}


Sometime ago I would just truncate the series in a value that I think it is appropiate for example $$n=10$$ and $$k=10$$:

((NSum[NSum[N[F[μ, Φ]], {k, 0, 10}], {n, 0,
10}]) Tan[Φ]^2) /. {μ -> 1, Φ -> Pi/4}
(*2.23421*)


Note that if you sum all the terms up to $$n=20$$ and $$k=20$$ the result is the same. If you try to sum up to $$n=30$$ and $$k=30$$ the computation start to become so lengthy. It is an option to do the plot using a summation up to 10. But this arbitrary truncation doesn't look very scientific and for some other values of $$\phi$$ the number of terms contributing to the sum could change. Do you have an alternative way to make this plot?

• In your $\LaTeX$ expression, the $\rho$ parameter is unused and $\bar{n}$ is not defined. Apr 29, 2019 at 9:55

You can do the sum over $$k$$ analytically up to infinity, which simplifies things greatly:

G[μ_, Φ_, n_] = Assuming[Element[Φ, Reals],
Sum[F[μ, Φ], {k, 0, Infinity}] // FullSimplify]
(* analytic answer *)


Then do the $$n$$-sum numerically. Also, substitute the numerical values of μ and Φ before doing the numerical sum, not afterwards.

Here's a very efficient way of doing the sum until the result no longer changes (i.e. until machine-precision convergence has been reached):

S[μ_?NumericQ, Φ_?NumericQ] :=
Module[{n = 0}, FixedPoint[# + G[N@μ, N@Φ, n++] &, 0]]


Try it out:

S[1, π/4]


2.23421

# update: if analytic summing is impossible

For other situations, where analytic summation is not possible, we can still rewrite the doubly infinite sum into a singly infinite one:

$$\sum_{n=0}^{\infty}\sum_{k=0}^{\infty}F(n,k) = \sum_{m=0}^{\infty}\left[\sum_{i=0}^m F(i,m-i)\right]$$

This singly-infinite sum can then be done with the same FixedPoint trick as above:

F[μ_, Φ_, n_, k_] = 4*((μ^(k + n)*Binomial[n, k]*(Cos[Φ]^2)^(-k + n)*(-n + ((1 + n)*μ*n!
*Hypergeometric1F1[-n, 2 + n, (-μ)*Tan[Φ]^2])/
((1 + n)!*Hypergeometric1F1[-n, 1 + n, (-μ)*Tan[Φ]^2]))^2*(Sin[Φ]^2)^k)/(E^μ*(k + n)!))*Tan[Φ]^2;

S[μ_?NumericQ, Φ_?NumericQ] := Module[{m = -1},
FixedPoint[(m++; # + Sum[F[N@μ, N@Φ, i, m - i], {i, 0, m}]) &, 0]]


Try it out:

S[1, π/4]


2.23421

• Hello, I'm gonna try this. I have redone the calculations and It turns out I have made a mistake, so the actual function I have to sum is different, but I think I may use most of the things you have written. Thanks Apr 29, 2019 at 16:58