You are on page 1of 7

Computer Physics Communications 270 (2022) 108149

Contents lists available at ScienceDirect

Computer Physics Communications


www.elsevier.com/locate/cpc

Numerical multidimensional integration with PyMikor ✩


Erik Bartoš
Institute of Physics, Slovak Academy of Sciences, Dúbravská cesta 9, 845 11 Bratislava, Slovak Republic

a r t i c l e i n f o a b s t r a c t

Article history: The PyMikor is a package for a numerical evaluation of multidimensional integrals with constant limits of
Received 20 November 2020 integration by the Korobov’s method. The package provides new implementation for Python programming
Accepted 16 August 2021 language. It is coming with predefined optimal coefficients used for the construction of parallelepiped
Available online 31 August 2021
lattice. The built-in integration strategies allow automatic or user defined calculation of the value of
Keywords:
integrals. We provide comparison tests with typical mathematical functions and standard numerical
Numerical integration algorithms Vegas and Divonne. A submitted package is expected to be of benefit to other physicists or
Korobov’s method people interested in numerical integration, typically in particle physics, e.g., for the evaluation of Feynman
Python diagrams, cross sections or creation of sophisticated Monte Carlo generators.

Program summary
Program Title: PyMikor
Licensing provisions: GPLv3
Programming language: Python
Supplementary material: https://github.com/Mezek/pymikor
Nature of problem: Many problems solved in physical and engineering sciences lead to calculation of
multidimensional integrals. Often the nature of integrands do not allow them to find their analytical
expression, and one must rely on numerical calculation methods.
Solution method: In PyMikor, the Korobov’s method for numerical evaluation of multidimensional integrals
with constants limits of integration is implemented as a set of python methods. They allow calculated
the integrands up to 20 dimensions with predefined optimal coefficient with two main modifications of
algorithm.
Additional comments including Restrictions and Unusual features: none
© 2021 Elsevier B.V. All rights reserved.

1. Introduction In the field of physics, the general purpose algorithms for mul-
tidimensional integration have been described in the literature and
they are still increasing for new problems. But it is still rather dif-
The numerical integration methods and optimization are the
integral part of scientific research tools, especially for many ap- ficult to predict which method would be the most efficient and
plications in mathematics, physics and engineering. The numerical one must try different methods in each particular case. Today the
integration in one dimension is an essential part of all numer- vast majority of methods rely on an iterative and adaptive Monte
ical packages for different programming languages with the use Carlo (MC) or quasi-Monte Carlo schemes, e.g. [1,2]. But except MC
of standard interpolation methods (Gaussian quadrature, Newton- methods also a different integration methods are known. One such
Cotes formula, etc.). In recent years the numerical calculation of method is the Korobov’s method [3], which it seems is very helpful
multiple integrals has received considerable attention with the de- for the integration in more dimensions, especially for the functions
velopment of central and graphics processing units (CPU & GPU), whose Fourier series converge fast. In this work we limit ourselves
but it still remains a difficult problem. This fact is obviously caused to illustrating this method by applying it to the new implementa-
by the lack of analytic results and the complexity of integrands tion in PyMikor package. The algorithm used here is improved over
within the regions of integration. the original Fortran implementations [4,5]. This package is writ-
ten in Python programming language, it uses four strategies which
allows simple control of the calculations. The PyMikor package

The review of this paper was arranged by Prof. Z. Was. comes with recalculated optimal parameters [6] for parallelepiped
E-mail address: erik.bartos@savba.sk. lattice. Their values have been computed and they are incorporated

https://doi.org/10.1016/j.cpc.2021.108149
0010-4655/© 2021 Elsevier B.V. All rights reserved.
E. Bartoš Computer Physics Communications 270 (2022) 108149

into the code to speed up the integration for very large values of ϕ (x1 , x2 , · · · , xs )
lattice nodes.
= f [ψσ (x1 ), ψσ (x2 ), · · · , ψσ (xs )]ψσ (x1 )ψσ (x2 ) · · · ψσ (xs ), (8)
The paper is organized as follows, in Section 2, the brief
overview of used Korobov’s method is introduced. The PyMikor   x
2σ − 1
package itself is described in Section 3, with a short example of ψσ (x) = (2σ − 1) [t (1 − t )]σ −1 dt , (9)
its usage. Some technical aspects of testing the integrands for var- σ −1
0
ious methods are discussed in Appendix A.
n
where k
is a binomial coefficient and σ is a periodization param-
2. Overview of Korobov’s method eter.
Now we return to the optimal parameters a. According [3], the
components ai are prime relative to N = p, and can be constructed
The PyMikor is a package for a numerical evaluation of mul-
as follows.
tidimensional integrals with constant limits of integration by the
In the case s = 2, two-dimensional parallelepiped lattice is
Korobov’s method [3], also known in the literature as number-
 
theoretic methods [7]. For a unit s-dimensional cube, the numer- k   k Q N −1 
ical integration of a multidimensional function f (x) is approxi- Mk = , , k = 1, 2, · · · , Q N (10)
QN QN
mated by the sum S
where p = Q N and the numbers Q 0 , Q 1 , · · · , Q N form Fibonacci
   a k 
a1 k 

N sequence
1 s
I= f (x) dx ≈ f ,··· , = S, (1)
N
k =1
N N Q 0 = 1, Q 1 = 1, Q j = Q j −1 + Q j −2 , j = 2, 3, · · · , N . (11)
[0,1]s
For the case s > 2 one can define the function
where a vector x = (x1 , x2 , · · · , xs ) is the real s-vector, with the
  z j −1 2
3s 
p s
elements 0 ≤ xi ≤ 1, i = 1, · · · , s. The formula
H ( z) = 1−2 k . (12)
  a k 
a1 k 
p p
s k =1 j =1
Mk = ,··· , , k = 1, 2, · · · , N (2)
N N If for z = a the function H ( z) reaches a minimum at interval 1 ≤
defines a parallelepiped lattice for a prime number N, when N > s. z < p, the integers
A vector of integer numbers a = (a1 , a2 , · · · , as ), 1 ≤ ai < N, gives
a i = a i −1 , i = 1, · · · , s (13)
a set of optimal parameters, which are defined later. Here the sym-
bol { y } represents fractional part { y } ≡ y − y , for all y, 0 ≤ y < 1. represent the optimal parameters of the parallelpiped lattice (2).
The use of the parallelepiped lattice guarantees the accuracy of The number of operations required to calculate the optimal coeffi-
Eq. (1) not worse than cients can be shortened several times if equations
 lnα s N
H ( p − z) = H ( z), (14)
R = |I − S | = O . (3)
Nα p −1
 
 s  z j −1 2
3s
2
This fact was proved in [3] for all functions f (x), which are pe- H ( z) = 1+2 1−2 k (15)
riodic in each variable xi with the period 1 and whose Fourier p p
k =1 j =1
coefficients
are used. Then it is sufficient to limit z over the values 1 ≤ z ≤
 p −1
.
2
C (m) = f (x)e−2π imx dx, m = (m1 , m2 , · · · , ms ), (4) For the case of very large numbers p, one can use the modi-
[0,1]s fication of the algorithm, using p = p 1 · p 2 , where p 1 and p 2 are

distinct primes, and p 2 is of the order p 1 . One can again define
satisfy the condition the function
s 
p1 p2 s   p z j −1 + p a j −1 2
K 3s
|C (m)| ≤ , |||m||| = max(|mi |, 1) (5) 
H ( z) = 1−2 k
1 2
, (16)
|||m||| α p1 p2 p1 p2
i =1 k =1 j =1

for positive constant K > 0 which does not depend on m and α > a – the same number as in the previous case (12) when p is
1. changed to p 1 . If for z = b the function 
H ( z) reaches a minimum
In the case of nonperiodic functions, when the function f (x) at interval 1 ≤ z < p 2 , the integers
∂ αs f
has a continuous derivative ∂ xα ···xα and α ≥ 2, one can find a peri-
odic function
1
ϕ (x) which obeys the conditions
s
a i = p 1 b i −1 + p 2 a i −1 , i = 1, · · · , s (17)
will be the optimal parameters. The theorem for the generation
ϕ (x1 , x2 , · · · , xi−1 , 1, xi+1 , · · · , xs ) of optimal coefficients for the general case of the product of J
distinct primes p = p 1 · p 2 · · · p J was introduced and proved in [8].
= ϕ (x1 , x2 , · · · , xi −1 , 0, xi +1 , · · · , xs ), (6)
  3. Package description and usage
f (x) dx = ϕ (x) dx. (7)
[0,1]s [0,1]s The PyMikor package was design to be the most simple as pos-
sible. To maintain simplicity while allowing parameters input, the
There are several options to perform the periodization, we chose algorithms called strategies were prepared. The first two strate-
the option used in [5], with the following substitution of variables gies work with predefined optimal coefficients, while two other

2
E. Bartoš Computer Physics Communications 270 (2022) 108149

strategies allow more user’s inputs to be fixed, but at the cost of Table 1
increased computer demands. Main methods of PyMikor class.
Strategy 1. This is a simple and fast numerical evaluation. The Method Description
essential input is a number of nodes N. The algorithm chooses the (fcn, eps, stat) Class instance is called by
closest tabulated prime number p to N and then it performs the __call__ method (line 8 in
calculation. The integration itself is done using the optimal param- Listing 1) with the parameters:
eters (see Eq. (13)). fcn — user defined integrand
function
Strategy 2. As in previous case, the essential input parameter eps — optional value for absolute
is the number of nodes N. This number is the product of two error (eps=1e-5)
tabulated prime numbers p and q. The optimal parameters for par- stat — if set it shows used
allelpiped lattice are calculated according Eq. (17). tabulated values (stat=1)
set_values(parameters, args) Set parameters and arguments for
In the combination of the strategy 1 or 2 with an optional argu-
the integration, they are described
ment eps for the error, the algorithm increases selected number p in Table 2 (see also line 7 in
(or p, q) in the order to achieve required accuracy. If the accuracy Listing 1)
is not achieved, the computation stops at the last provided value show_parameters() Print current values of all
for the optimal coefficients a (or a, b). parameters
tabulated_optimals() Provide last used primes and
Strategy 3. Again, the essential input is the number of nodes
optimal coefficients
N. If N is not prime number, it is converted to the closest prime first_optimal_a(prt) Find first optimal value a, prt=1:
number q to entered N. Then the optional coefficients are calcu- show iteration progress, prt=0: no
lated exactly for this prime number p, where the first optimal a is verbose information
first_optimal_b(first_a) Calculate first optimal value b
received with the help of Eq. (12).
based on the value of first optimal
Strategy 4. If not set the secondary number of nodes N 2 , the first_a
number of nodes is divided between to closest primes p and q,

where q ∼ p. Else the number of nodes N 2 is converted to prime
number q. Then the final number of nodes is equaled to N = p .q
Table 2
and the first optimals a, b are gained with the help of Eq. (16). The mandatory parameters and optional arguments used for the input of PyMikor
The last two strategies are more time consuming, as the opti- class.
mal coefficients are calculated exactly for the entered number of
Parameter Values Description
nods. Their practical use is only in the limited cases: (i) a very
strategy 1, 2, 3, 4 User defined strategy for the evaluation
high number of nodes; (ii) precision tests, etc. For this reason, it is
of integral:
recommended to use tabulated values, i.e., the Strategies 1. and 2. 1: automatic evaluation with predefined
The integration is possible in dimensions 2 through 20. By de- optimal coefficients for nodes p, see
fault the upper and lower bounds in each dimension are set to Eq. (12)
2: automatic evaluation with predefined
0, 1 . But the PyMikor supports also any finite boundaries u , w . optimal coefficients for nodes p and
Then well known one dimension transformation of finite integral secondary nodes q, see Eq. (16)
bounds u , w to unit interval 0, 1 is done by 3: evaluation with entered nodes p, optimal
coefficients are exactly calculated
w 1 4: evaluation with entered nodes p and
secondary nodes q, optimal coefficients
f (x) dx → f u + ( w − u) y ( w − u) d y (18) are exactly calculated
u 0 dimension s = 2, . . . , 20 Dimension of multidimensional integral
nodes N = any prime p Nodes for the lattice, default p = 1009
and its generalization to more dimensions is straightforward. sec_nodes N 2 = any prime Secondary nodes for the lattice, default
A short example of a usage of PyMikor class is demonstrated q q=1
Argument Values Description
in following Listing 1.
sigma 1, 2, 3, . . . degree of periodisation
limits [u , w ] s Vector of upper and lower bounds,
1 from pymikor import ∗ default = [0, 1]s
2 eps 10−n absolute error
3 def fcn ( x ) :
4 return 3 . ∗ x [ 0 ] ∗ x [ 0 ] + 4 . ∗ x [ 1 ] ∗ x [ 2 ]
5
6 i n t e g r a l = PyMikor ( ) this purpose we have fixed strategy = 1 and four values of nodes:
7 i n t e g r a l . s e t _ v a l u e s ( 1 , 4 , 10000 , 1 , sigma =2 , 1259, 10 007, 100 003 and 1 000 003. For the comparison, we have
8 limits =[[0 , 1] , [0 , 1] , [0 , 1] , [0 , 1]]) chosen also two other Python packages on the market, represented
9 r e s u l t = i n t e g r a l ( fcn , eps=1e − 5, s t a t =1) by four variations: VegasA, VegasB, VegasC, PyCuba. They are de-
10 print ( f ’ \ nResult o f i n t e g r a t i o n : { result } ’ ) noted as VegasA and VegasB in the case of vegas package [10],
Listing 1: Short example of usage for PyMikor class. and VegasC and PyCuba in the case of PyCuba package [11]. VegasA
uses the parameters nitn = 10, neval = 103 , while VegasB works
with the parameters nitn = 10, neval = 105 . VegasC uses the Ve-
The main methods as well as the mandatory parameters and
gas algorithm built into the package, while PyCuba uses Divonne
optimal arguments are described in Tables 1, 2. Potential users will
algorithm.
find more examples of practical use of PyMikor class in the pack-
We have performed 100 runs for the dimensions of integration:
age [9].
3, 5, 8, 10, 13 and when possible also 15. In each run, the random
4. Examples values of the parameters a and u were generated in the interval
[0, 1], and the corresponding values of all integrals v a were gained.
Here we illustrate the performance of the code using the nu- They were compared with the analytical values v e of the integra-
merical estimation of six integrand families (see Appendix A). For tion and then the absolute value of relative error was evaluated

3
E. Bartoš Computer Physics Communications 270 (2022) 108149

Table 3
The values of scaling parameters e and h used for different normalization sets of a parameters.

Function Set e h Set e h Set e h


f 1 (x) norm1 1.5 110 norm2 4.0 250 norm3 6.0 150
f 1 (x) norm4 1.2 50 norm5 1.7 90 norm6 2.0 110

f 2 (x) norm1 2.0 600 norm2 5.0 800 norm3 6.0 350
f 2 (x) norm4 1.2 150 norm5 1.7 200 norm6 1.9 300

f 3 (x) norm1 1.5 150 norm2 2.0 200 norm3 2.0 500
f 4 (x) norm1 1.5 150 norm2 3.0 300 norm3 5.0 600
f 5 (x) norm1 1.5 150 norm2 3.0 300 norm3 5.0 450
f 6 (x) norm1 1.5 150 norm2 3.0 200 norm3 4.0 250

Fig. 1. The plot of mean relative error of integration with the testing functions for various dimensions. The vector of parameters a is randomly generated from the interval
[0, 1].

v − v 
εr = 
a e
. (19) functions can be visible for lower dimensions 3 and 5, e.g., oscil-
ve latory and corner peak functions in Figs. 2a, 3a. Sometimes the
results for higher dimensions are on the computational edge, e.g.,
For each 100 runs a mean relative error (MRE) was computed to
see Figs. 3b, 3c.
help judge the estimation accuracy of integration. Subsequently,
the procedure was repeated with different sets of the scaling pa- 5. Conclusion
rameters e and h used for the normalization of a parameters (see
Table 3). In this communication we report PyMikor, a package for a nu-
The received results are presented in Figs. 1–3. The plots if merical evaluation of multidimensional integrals with constant
Figs. 1a–1f show the obvious trend of lowering accuracy of the limits of integration by the Korobov’s method. The package pro-
integration with the increase of the dimension of integration. Also vides new implementation for Python programming language.
the increase of a number of nodes (p = 1000003 in the case of Compared to previous implementations, in particular Fortran, the
PyMikor, neval = 105 for VegasB) leads to lowering of values for package comes with predefined optimal coefficients and buil-in
relative error. But that is true only for lower dimensions 3 − 8, strategies, which help the user to perform the integral evalua-
in the case of higher dimensions 10 − 13, the values of relative tion simple and fast. We have recalculated the optimal coefficients
errors are the similar for all used algorithms and testing func- for all dimensions up to prime number p = 5000011. In addition,
tions. we have tabulated the optimal parameters for the case of very
The use of different normalization sets confirms the trends from large numbers p = p 1 · p 2 up to prime numbers p 1 = 1000003 and
previous plots. However, some difference in values for individual p 2 = 997.

4
E. Bartoš Computer Physics Communications 270 (2022) 108149

Fig. 2. The plot of mean relative error of integration with the testing functions for various dimensions and selected normalization sets norm1 – norm6 (see Table 3).

Fig. 3. The plot of mean relative error of integration with the testing functions for various dimensions and normalization sets: norm1, norm2 and norm3 (see Table 3).

5
E. Bartoš Computer Physics Communications 270 (2022) 108149

As an example of use, we have performed a few compara- Table A.4


tive calculations on typical mathematical functions. The purpose The function families proposed to the testing of multidimensional
integration subroutines.
of them was not to show which method is the best, but that in
different situations one can choose the most suitable method to Integrand Family Attribute
achieve the required accuracy of computation. The final result of 
n

multidimensional integration depends on the nature of the inte- f 1 (x) = cos 2π u 1 + a i xi Oscillatory
i =1
grand and the dimension of integration. The computing cost is n
−1
typically proportional to the combination of several parameters. f 2 (x) = a−
i
2
+ (xi − u i )2 Product Peak
i =1
Further improvements of the presented method are technically 
n
−(n+1)
possible through the programming with the help of Cython. There f 3 (x) = 1 + a i xi Corner Peak
is a place to speed up computation, especially in the loops, for i =1

n
higher dimensions and for very large values of nodes. f 4 (x) = exp − a2i (xi − u i )2 Gaussian
The calculation of Feynman diagrams, the cross sections, the i =1
 n
parameters in theoretical models or the creation of sophisticated f 5 (x) = exp − a i | xi − u i | C 0 Function
Monte Carlo generators requires the reliable methods for their ⎧ i =1

evaluation. We hope the package is expected to be of benefit to ⎪


⎨0 if x1 > u 1 or x2 > u 2
f 6 (x) = 
n
Discontinuous
other physicists, typically in particle physics or people interested ⎪
⎩exp a i xi otherwise
in numerical integration. i =1

Declaration of competing interest

The authors declare that they have no known competing finan- 


(−1)n
cial interests or personal relationships that could have appeared to f 3 (x) dx =
influence the work reported in this paper. n!a1 . . . an
[0,1]n

Acknowledgements  
n  
i n −1
−1 −1
× 1+ ai − 1+ aj
The work was partially supported by Slovak Grant Agency for i =1 i ∈C n,n−1 j =i 1
Science VEGA under the grant No. VEGA 2/0105/21.

 
i n −2 
−1 n
Appendix A. Testing suit of integrand class + 1+ aj − · · · + (−1) , (A.4)
i ∈C n,n−2 j =i 1
The PyMikor package comes also with additional Integrand
class, which contains test suit proposed by Genz [12]. The class
(C n,k denotes combinations without repetition of n indexes in k
serves as handful source of different types of functions, not only
tuples.)
for the testing purposes of multidimensional integration. The re-
gion of the integration for the integrand families in Table A.4 
π  
n
is the unit n-dimensional cube. The u parameters (0 ≤ u i ≤ 1,
f 4 (x) dx = erf ai (1 − u i ) − erf − ai u i , (A.5)
i = 1, . . . , n) are varied randomly and they should not affect 2ai
[0,1]n i =1
the difficulty of the integration. The a parameters are positive
numbers and the difficulty increases with the size of the norm 

1 
n
||a||1 = ni=1 ai . At the creation of Integrand object, the random
f 5 (x) dx = 2. − exp − ai (1 − u i ) − exp − ai u i ,
n-dimensional vectors a and u are computed with their random ai
[0,1]n i =1
components chosen from the interval [0, 1]. Then the vector a can
(A.6)
be scaled (the method normalize_a(e,h)) by a constant c, so
that a new vector a = ca satisfies the scaling given by the condi- 
exp(a1 u 1 ) − 1 exp(a2 u 2 ) − 1
tion f 6 (x) dx = ×
a1 a2
[0,1]n

n
ne ai = h, (A.1) n
i =1 exp(ai ) − 1
× . (A.7)
ai
where e, h are new parameters. i =3
The selection of the integrand families was done in such a way
that their numerical integration can be compared with their ana- Thus the testing of various multiple integration algorithms can
lytical evaluation by the formulae be performed more reliable and accurately.
     The class itself comes with six methods corresponding to
 n 
n
2 ai ai above function in Table A.4: oscillatory_fcn(), prod-
f 1 (x) dx = sin × cos b + , (A.2)
ai 2 2 uct_peak_fcn(), corner_peak_fcn(), gaussian_fcn(),
[0,1]n i =1 i =1
c0_fcn(), discontinuous_fcn(). Moreover, the class con-
 n   tains builtin functions (A.2)–(A.7): exact_oscillatory(),
f 2 (x) dx = ai arctan ai (1 − u i ) − arctan − ai u i , exact_product_peak(), exact_corner_peak(), exact_
i =1 gaussian(), exact_c0(), exact_discontinuous(). A
[0,1]n
(A.3) short example of their usage is presented in Listing 2.

6
E. Bartoš Computer Physics Communications 270 (2022) 108149

1 from pymikor import ∗ [2] T. Hahn, Comput. Phys. Commun. 168 (2) (2005) 78–95, https://doi.org/10.
1016/j.cpc.2005.01.010.
2 from integrand import ∗
[3] N. Korobov, The Number Theory Methods in the Approximation Analysis, Gos.
3 ndim = 5
Izdat. Fiz.-Mat. Lit., Moscow, 1963.
4 i n t e g r a l = PyMikor ( ) [4] A. Saltykov, I. Silin, Mikor, http://wwwinfo.jinr.ru/programs/jinrlib/d121.htm,
5 i n t e g r a l . s e t _ v a l u e s ( 1 , ndim , 10009 , 1 , sigma =2 , 2003.
6 limits =[[0 , 1] , [0 , 1] , [0 , 1] , [0 , 1]] , [0 , 1]]) [5] K. Zakrzewska, J. Dudek, N. Nazarewicz, Comput. Phys. Commun. 14 (3–4)
7 f o f = Integrand ( ’ FCN ’ , ndim ) (1978) 299–309, https://doi.org/10.1016/0010-4655(78)90023-1.
8 f o f . normalize_a ( 1 . 5 , 110) [6] A. Saltykov, USSR Comput. Math. Math. Phys. 3 (1) (1963) 235–242, https://
9 r e s u l t = i n t e g r a l ( f o f . corner_peak_fcn ) doi.org/10.1016/0041-5553(63)90134-4.
10 e x a c t _ r e s u l t = f o f . exact_corner_peak ( ) [7] A.H. Stroud, Approximate Calculation of Multiple Integrals, Prentice-Hall Series
in Automatic Computation, Prentice-Hall, 1971.
11 print ( f ’ \ nResult o f i n t e g r a t i o n : { r e s u l t } ’ )
[8] P. Keast, SIAM J. Numer. Anal. 10 (5) (1973) 831–838, https://doi.org/10.1137/
12 print ( f ’ Exact r e s u l t : { exact_result } ’ )
0710068.
Listing 2: Short example of usage for Integrand class. [9] E. Bartoš, PyMikor, https://github.com/Mezek/pymikor, 2020.
[10] G.P. Lepage, Vegas, https://github.com/gplepage/vegas, 2013.
[11] J. Buchner, PyCuba, https://github.com/JohannesBuchner/PyMultiNest, 2015.
References [12] A. Genz, in: P. Keast, G. Fairweather (Eds.), Numerical Integration: Recent De-
velopments, Software and Applications, Springer Netherlands, Dordrecht, 1987,
[1] G. Peter Lepage, J. Comput. Phys. 27 (2) (1978) 192–203, https://doi.org/10. pp. 337–340.
1016/0021-9991(78)90004-9.

You might also like