Pre-requisite Definition
Stieltjes constants ($\gamma_n$) are the constants that occur in the Laurent series expansion of the Riemann zeta function. $${\displaystyle \zeta (s)={\frac {1}{s-1}}+\sum _{n=0}^{\infty }{\frac {(-1)^{n}}{n!}}\gamma _{n}(s-1)^{n}.}$$
They have a finite integral representation as follows, $${\displaystyle \gamma _{n}={\frac {(-1)^{n}n!}{2\pi }}\int _{0}^{2\pi }e^{-nix}\zeta \left(e^{ix}+1\right)dx.}$$
Code
I have writen the following simple Monte Carlo algorithm for computing $\gamma_n$.Note: I'm aware of MonteCarlo method in NIntegrate
. The purpose of this code is more to understand/motivate the algorithm involved in crude MC Integration and I want to define it myself.
sint[n_, x_] := Exp[-n I x] Zeta[Exp[I x] + 1]
lowerlim = 0; upperlim = 2 Pi;
ParallelRepeatedStieltjesIntegral[n_, points_, repeat_] :=
((-1)^n n!)/(2 Pi) ParallelTable[(upperlim - lowerlim)/points Total[sint[n, RandomReal[{lowerlim, upperlim}, {points}]]], {repeat}]
My question is as follows, How can I speed up this code?
Extra Info
Time taken to execute this code on my PC
ParallelRepeatedStieltjesIntegral[1, 10^3, 10^5]
took 40 minutes!ParallelRepeatedStieltjesIntegral[2, 10^3, 10^5]
took 32 minutes!
Also at the end of all this the values are quite poor (2 digit accuracy). I would prefer to run this code with larger points values.
I also want to repeat it, for the purpose of displaying the histogram (as this nicely demonstrates the law of large numbers).
PS: A not very relevant side question, how do you generate quasirandom numbers in Mathematica, instead of pseudorandom. (To demonstrate quasi-Monte Carlo integration in similar code as above.)
Parallel*
functions but rather use packed arrays and vectorized operations (which are automatically and very efficiently parallelized). Search for “[performance-tuning] packed” and sort by votes. The top two hits make good introductory reading. (Sorry on an iOS device w/o Mathematica at present and cannot show you. Several others on site know how, though.) $\endgroup$"MKL"
generator implements the Sobol' and Niederreiter sequences, which may be of interest. $\endgroup$