1 Introduction

Standard numerical quadrature methods can usually evaluate real-valued definite integrals to machine precision accuracy using only a modest number of function evaluations when the integrand has some desirable regularity properties. A particularly well-behaved class of integrands are those which are complex-analytic on a neighborhood of the integration domain—preferably a large neighborhood, and preferably without rapid growth away from the real line. In this setting, standard procedures include Gauss–Legendre and Clenshaw–Curtis quadratures for aperiodic problems and the trapezoid rule for periodic problems. These converge geometrically—that is, the logarithm of the error decreases linearly with the number of quadrature nodes. Unhappily, these standard techniques do not work well for the class of problems known as nearly singular integrals, wherein the integrand fails to be analytic somewhere near but not on the interval of integration in the complex plane. The trapezoid, Clenshaw–Curtis, and Gauss–Legendre rules still provide geometric convergence, but with an unacceptably small slope. Standard theorems relate this slope to the domain of analyticity of the integrand. In this paper we survey some existing methods of accelerating the convergence of these standard methods and we introduce several new ones, assuming that the location of the nearby singularity is known, but without using any additional information on the nature of the singularity.Footnote 1

While our motivation comes from the quadrature problem in the context of solving integral equations, some similar issues also arise in the context of interpolation from samples of a nearly singular function. This is an important component of spectral methods for differential equations when the desired solution has abrupt fronts or peaks. In fact, some of the same acceleration techniques have been independently discovered by researchers in the two communities. For example, Johnston and Elliot’s hyperbolic sine transformation [10] was also discovered by Tee and Trefethen [16], while Jafari’s transformation [9] is very similar to the repeated sinh method of [6]. One of our aims is to assemble the results from these two communities in one place.

We focus on two families of acceleration strategies. The first group of methods split the domain into carefully chosen subintervals and then solve subproblems on each of them. The other strategy that we consider is to change variables with a complex-analytic transformation so that the singularity lies farther away.

An early example of a splitting method for aperiodic problems was given by Ma and Kamiya [11], who subdivide at the real part of the singularity, assuming that this lies within the integration interval and the problem is aperiodic (in fact they subsequently use an exponential change of variables for each of the new subproblems, thereby combining both of the principal strategies considered here). Subsequently, Driscoll and Weideman [5] gave a formula for the optimal splitting location in terms of the location of the singularity, again for the aperiodic case. Their formula is especially useful in cases where the singularity is near the endpoint of the interval in the complex plane or on the real line outside the interval, where simply using the real part of the singularity is ineffective or impossible. We develop an analogous method for periodic problems, replacing the trapezoid rule for a full period with individual Gauss–Legendre integrations on two subintervals of different sizes.

Splitting methods are extremely simple and, as we demonstrate, can yield dramatic improvements in the convergence rate. However, we can find even better convergence rates using methods that avoid dividing the available quadrature nodes among two or more subproblems. There are a vast array of possibilities for conformal mappings that distort the neighborhood of a real interval while fixing the endpoints and remaining real-valued and monotone within the interval. The Jacobi elliptic functions are a powerful tool for designing transformations that use all of the analyticity we have assumed; we list existing methods for periodic and aperiodic problems and give a new version for the aperiodic case when the singularity lies on the real line outside the integration interval. This is a small extension of results by Trefethen, Tee and Hale [7, 8, 15, 16]. However, the methods employing elliptic functions are less robust than some elementary alternatives when the integrand has other challenging features like additional distant singularities or rapid growth away from the real line. We therefore list or introduce some good elementary alternatives such as the sinh transformation for aperiodic problems [10, 16], our iterated sine map for periodic problems, and our quadratic transformation for aperiodic problems with real singularity.

The organization of the paper is as follows. We state and comment on the standard theorems describing the convergence of the trapezoid and Gauss–Legendre rules in Sect. 2 and we briefly introduce the Jacobi elliptic functions in Sect. 3. We consider periodic problems in Sect. 4. For aperiodic problems we first consider singularities off the real line in Sect. 5 and then singularities that occur on the real line (but outside the interval of integration) in Sect. 6. We test all of the methods on a suite of integrands with various properties in Sect. 7. As an application, we then compute some nearly singular surface integrals arising in three-dimensional Stokes flow in Sect. 8, followed by concluding remarks.

2 Discussion of the standard theorems

For a periodic integrand, the convergence rate of the trapezoid rule depends on the distance from the singularity to the real line.

Theorem 1

(Geometric convergence of trapezoid rule [22]) Suppose that a \(2\pi \)-periodic function f is analytic and satisfies \(|f(z)|<M\) within the strip \(|\Im (z)|<\lambda \). Then the difference between the integral \(I = \int _{0}^{2\pi } f(x)\,dx \) and its n-point trapezoid rule approximation \(I_n =(2\pi /n)\sum _{j=1}^{n} f\left( 2\pi j / n\right) \) satisfies

$$\begin{aligned} \left|I-I_n \right|\le \frac{4\pi M}{\exp (\lambda n)-1}. \end{aligned}$$
(1)

A similar theorem holds for Gauss–Legendre quadrature, with an ellipse replacing the infinite strip.

Theorem 2

(Geometric convergence of Gauss–Legendre quadrature [21]) Suppose that f is analytic with \(|f(z)|< M\) on the interior of the Bernstein ellipse \(E_\rho \), whose foci are \(\pm 1\) and whose semimajor and semiminor axis lengths sum to \(\rho >1\). Let C be the constant \(C=64\rho ^2/({15(1-\rho ^{-2})})\), let \(I = \int _{-1}^1 f(x)\,dx\) be the exact value of the integral, and let \(I_n\) be its n-point Gauss–Legendre rule approximation. Then

$$\begin{aligned} |I-I_n |\le \frac{CM}{\rho ^{2n}}. \end{aligned}$$
(2)

To employ this result in practice, we will often need to find the value of the ellipse parameter \(\rho \) so that the ellipse passes through a given point \(z\in {\mathbb {C}} \setminus [-1,1]\). We quote af Klinteberg and Barnett’s useful formula [1],

$$\begin{aligned} \rho = \rho (z) = |z \pm \sqrt{z^2-1}|, \quad \text {with sign chosen so that }\rho >1. \end{aligned}$$
(3)

The performance of the Clenshaw–Curtis method is generally similar to that of Gauss–Legendre quadrature, a phenomenon discussed in detail by Trefethen [20]. In particular, Trefethen writes that “our conclusion is that the Clenshaw–Curtis and Gauss formulas have essentially the same accuracy unless [the integrand] is analytic in a sizable neighborhood of the interval of integration.” In particular, we expect Clenshaw–Curtis and Gauss–Legendre quadratures to behave more and more similarly as the nearby singularity approaches \([-1,1]\). This prediction holds for the numerical experiments that we carry out in Sect. 7. We therefore focus mainly on Gauss–Legendre and trapezoid integration (and improvements to them) in the remainder of this paper.

When we apply the preceding theorems, the twin goals of maximizing \(\lambda \) or \(\rho \) while minimizing M are in tension. If f is entire or has only distant singularities and n is fixed, this leads to an optimization problem for the value of \(\lambda \) or \(\rho \) that will produce the strongest statement from the theorem. Of course, the relative importance of M decreases as n grows. In the nearly singular case, where \(\lambda \approx 0\) or \(\rho \approx 1\), our priority is to improve the convergence rate even if this results in a large increase in M. In this paper we are assuming knowledge about the locations of the singularities of the integrand, so \(\lambda \) and \(\rho \) are known. However, we make no assumptions about the nature of those singularities or about the growth of \(|f(z)|\) away from the integration interval, so M is unavailable. Therefore we present a range of options instead of searching for an optimal strategy. The relatively cautious methods we consider do not make use of all of the analyticity we have assumed for f(z), and are therefore less vulnerable to the danger of fast growth in \(|f(z) |\) away from the integration interval. In contrast, the more aggressive methods make use of all of the analyticity we have assumed. The aggressive methods can boast of better theoretical convergence rates, but the errors may reach machine precision before they actually decrease at the advertised rate. In contrast, the more cautious methods have (slightly) smaller convergence rates but are more likely to actually achieve these rates for challenging integrands.

3 Definitions of Jacobi elliptic functions

Here we give the definitions of some special functions that will be useful later, and we comment on the implementations that we employ in numerical work. For \(\phi \in {\mathbb {R}}\) and \(0<m<1\), the incomplete elliptic integral of the first kind is

$$\begin{aligned} F(\phi ,m) = \int _0^\phi \frac{d\theta }{\sqrt{1-m\sin ^2\theta }}. \end{aligned}$$
(4)

The quarter period K(m) is defined by \(K(m) = F(\pi /2,m)\). For fixed m, the mapping \(\phi \mapsto F(\phi ,m)\) is smooth and strictly increasing on \({\mathbb {R}}\). Therefore this mapping has a smooth inverse, which is called the Jacobi amplitude function: \({{\,\textrm{am}\,}}(t,m)\) satisfies \({{\,\textrm{am}\,}}(F(\phi ,m),m)=\phi \) and \(F({{\,\textrm{am}\,}}(t,m),m)=t\). Note that \(F(\phi ,m)\) increases by 4K(m) whenever \(\phi \) increases by \(2\pi \), and consequently \({{\,\textrm{am}\,}}(t,m)\) increases by \(2\pi \) whenever t increases by 4K(m). We then define the elliptic sine and cosine and the delta amplitude functions:

$$\begin{aligned} {{\,\textrm{sn}\,}}(t,m)&=\sin ({{\,\textrm{am}\,}}(t,m))\nonumber \\ {{\,\textrm{cn}\,}}(t,m)&=\cos ({{\,\textrm{am}\,}}(t,m))\nonumber \\ {{\,\textrm{dn}\,}}(t,m)&=\frac{d}{dt}{{\,\textrm{am}\,}}(t,m) = \sqrt{1 - m \sin ^2\big ({{\,\textrm{am}\,}}(t,m)\big )}. \end{aligned}$$
(5)

These are all periodic in t with period 4K(m), hence the term ‘quarter period’ for K(m) (in fact \({{\,\textrm{dn}\,}}\) is also 2K(m)-periodic). Note that \(y(t) = {{\,\textrm{am}\,}}(t,m)\) satisfies the differential equation \(y' = \sqrt{1-m\sin ^2(y)}\).

To construct the quadratures considered in this paper, we need to evaluate these functions for real t only, and we use the ellipfun function from the Python library mpmath [17] (this gives the three functions defined in (5), and then we obtain \({{\,\textrm{am}\,}}\) by taking the two-argument arctangent of \({{\,\textrm{sn}\,}}\) and \({{\,\textrm{cn}\,}}\)). To create Figs. 1, 2, and 3 below, which use complex t, we use the Mathematica function JacobiAmplitude, which has branch cuts on vertical segments connecting the points \(2sK(m)+(2t+1)K(1-m)i\) for \(s,t\in {\mathbb {Z}}\).

4 Methods for periodic problems

We begin with the case where the integrand is \(2\pi \)-periodic. We suppose that f is analytic except at the points \(x=2\pi k \pm Bi\) for \(k\in {\mathbb {Z}}\), or along branch cuts extending vertically from these points to infinity. The ordinary trapezoid rule will be an effective choice if B is large, since Theorem 1 predicts errors of size \(e^{-Bn}\) with n function evaluations. We therefore assume that B is small but nonzero, so the integral is nearly singular, and we consider several methods to use our knowledge of the domain of analyticity of f in order to improve on the performance of the trapezoid rule. We begin with a decomposition method and then continue with several conformal mappings. Numerical examples demonstrating these techniques appear in Sect. 7.

4.1 Subdivision

One way to integrate over a full period of f is to choose an appropriately small \(\delta >0\) and then split the integral into subproblems on \([-\delta , \delta ]\) and \([\delta , 2\pi -\delta ]\). The subproblems are not periodic, so we apply Gauss–Legendre integration for each of them. Rescaling the subintegrals linearly to \([-1,1]\), we have

$$\begin{aligned} \int _{-\pi }^\pi f(x)\,dx&=\delta \int _{-1}^1 f(\delta t)\,dt + (\pi - \delta ) \int _{-1}^1 f\left( \pi + (\pi -\delta )w\right) \,dw. \end{aligned}$$
(6)

The two subintegrals are singular at \(t=Bi/\delta \) and at \(w=\frac{B i \pm \pi }{\pi -\delta }\), respectively. If we want the overall integration procedure to converge as quickly as possible and we assume n is large, we should choose \(\delta \) so that both subproblems have the same asymptotic convergence rate. Equivalently, both \(Bi/\delta \) and \(\frac{B i \pm \pi }{\pi -\delta }\) should lie on the same ellipse with foci \(\pm 1\) in the complex plane. Letting m denote the semiminor axis length, we have the equations

$$\begin{aligned} \frac{0^2}{1+m^2} + \frac{(B/\delta )^2}{m^2} = 1, \qquad \frac{(\pi /(\pi -\delta ))^2}{1+m^2} +\frac{(B/(\pi -\delta ))^2}{m^2} = 1. \end{aligned}$$

After some simplification we arrive at the cubic equation \(0=2\delta ^3 + 2B^2 \delta - B^2 \pi .\) This has a unique real solution which we write in terms of hyperbolic sines as follows:

$$\begin{aligned} \delta = \frac{2B}{\sqrt{3}}\sinh \left( \frac{1}{3} {{\,\textrm{arcsinh}\,}}\left( \frac{3\pi \sqrt{3}}{4B}\right) \right) . \end{aligned}$$
(7)

We expect the overall error for the splitting method to decay like \(\rho ^{-n}\) where \(\rho = B/\delta + \sqrt{1+B^2/\delta ^2}\). This is an improvement over the trapezoid rule on the original integral if \(\rho >e^B\), which occurs for \(B<0.95\).

For singularities lying very close to the real line, it is possible to get even faster convergence via further (recursive) subdivision using the methods of Sect. 5. However, we found that even the recursive splitting does not lead to quadratures that are competitive with the conformal mapping strategies that we discuss next, so we do not consider this idea further.

4.2 Conformal maps for periodic problems

A more powerful method for periodic problems is to compute the integral using the n-point trapezoid rule following a transformation x(t) that preserves the interval \([-\pi ,\pi ]\):

$$\begin{aligned} \int _{-\pi }^\pi f(x)\,dx = \int _{-\pi }^\pi f(x(t))x'(t)\,dt \approx \frac{2\pi }{n}\sum _{j=1}^n f\left( x\left( t_j\right) \right) x'\left( t_j\right) \end{aligned}$$
(8)

where \(t_j=-\pi + {2\pi j}/{n}\). In view of Theorem 1, the error will decay like \(e^{-\lambda n}\) as long as the new integrand \(f(x(t))x'(t)\) is periodic and analytic on the strip \(S_\lambda = \{t\in {\mathbb {C}}:|\Im (t) |<\lambda \}\). We therefore seek a transformation x(t) which carries a wide strip surrounding the real line in the complex t-plane into the domain of analyticity of f in the complex x-plane, avoiding the branch cuts; another consideration is that the derivative \(x'(t)\) should not have any singularities for \(t\in S_\lambda \).

Two recent works have presented transformations with the general properties we seek: Tee constructed one using the Jacobi elliptic functions [15], while Berrut and Elefante gave an elementary formula based on a Möbius transformation [3]. After discussing these two methods, we propose a third, which we call the iterated sine map. We illustrate the conformal maps in Fig. 1 with \(B=0.3\) along with the resulting predicted convergence rates. We then give numerical examples comparing these three mappings, as well as the decomposition method from Sect. 4.1 and the ordinary trapezoid rule, in Fig. 4.

4.2.1 The Jacobi amplitude map (JAM)

Tee created a conformal map that carries a strip onto the doubly slit region of analyticity of f [15]. Here we present a new derivation of the same formula using differential equations rather than complex analysis. Our approach is based on the intuition that the factor \(x'(t)\) should be small when x is close to the singularity. To respect periodicity, we wrap the real line onto the unit circle and imagine the singularity as a nearby point (1, 0, B). We then assume that \(x'(t)\) is linearly proportional to the distance from \((\cos (x),\sin (x),0)\) to the singularity, leading to the boundary value problem

$$\begin{aligned} \frac{dx}{dt} = k \sqrt{(1-\cos ( x))^2 + \sin ^2( x) + B^2}, \quad x(-\pi )=-\pi ,\;\; x(\pi )=\pi \end{aligned}$$
(9)

where the value of k is determined as part of the solution. The exact solution of this BVP is

$$\begin{aligned} x(t) = -\pi + 2 {{\,\textrm{am}\,}}\left( \frac{\pi + t}{\pi }K \left( \frac{4}{4+B^2}\right) ,\frac{4}{4+B^2}\right) \end{aligned}$$
(10)

where the Jacobi amplitude \({{\,\textrm{am}\,}}\) and the quarter-period K were defined in Sect. 3. In practice, we suggest computing the derivative dx/dt using the delta amplitude \({{\,\textrm{dn}\,}}(t,m)\) rather than the differential equation (9):

$$\begin{aligned} x'(t) = \frac{2}{\pi }K\left( \frac{4}{4+B^2}\right) {{{\,\textrm{dn}\,}}\left( \frac{\pi + t}{\pi }K\left( \frac{4}{4+B^2}\right) , \frac{4}{4+B^2}\right) }. \end{aligned}$$
(11)

This leads to an improved convergence rate of

$$\begin{aligned} \lambda = \pi \frac{K\left( B^2/(4+B^2)\right) }{K\left( 4/(4+B^2)\right) }\;{ \approx \frac{-\pi /2}{\log (B/8)} -\frac{\pi B^2 }{32 \log (B/8)^2}.} \end{aligned}$$
(12)

The transformation (10) is the sum of the identity map and a \(2\pi \)-periodic function, and it carries the rectangle \(\{x+iy: |x|\le \pi , |y |< \lambda \}\) onto the doubly slit region \(\{x+iy:|x |\le \pi \text { and }|y |<p_y \text { if }x=0\}\), thereby using all of the analyticity that we have assumed for f. This is the most aggressive option for the periodic problem.

4.2.2 The boundary correspondence map (BCM)

Berrut and Elefante recently suggested an alternative mapping that avoids the use of elliptic functions [3]. Adapting their method slightly, we get the formulas

$$\begin{aligned} a&= \exp (B) - \sqrt{-1+\exp (2B)} \end{aligned}$$
(13)
$$\begin{aligned} x(t)&= -i \log \left( \frac{\exp (it)+a}{1+a\exp (it)}\right) \end{aligned}$$
(14)
$$\begin{aligned} x'(t)&= \frac{1-a^2}{a^2+2 a \cos (t)+1}\text { for } t\in {\mathbb {R}} \end{aligned}$$
(15)
$$\begin{aligned} \lambda&= -\log (a)\; {\approx \sqrt{2B} + \frac{B^{3/2}}{3\sqrt{2}} } \end{aligned}$$
(16)

The formula (14) is quoted directly from [3], but the value of a that we list in (13) is new. To derive it, note that x(t) is analytic for \(|\Im (t)|<\log (a)\), but has singularities at \(t = \pm \pi \mp i\log a\). We therefore set \(B = \Im (x(0+i\log (a))) =\log (2a/(1+a^2))\) and solve for a to obtain (13). We also comment that the logarithm in (14) should be the usual branch with imaginary part in \((-\pi ,\pi ]\), unlike in [3], since our integral is posed on \([-\pi ,\pi ]\) with singularity near 0.

If \(B\ll 1\), the resulting quadrature is much better than the ordinary trapezoid rule. However, it does not perform nearly as well as the Jacobi amplitude map, as one can see in Fig. 4. The conformal image of the strip (top right in Fig. 1) is unbounded and does not closely approach the sides of the vertical branch cuts.

4.2.3 The iterated sine map (ISM)

We searched for another conformal mapping with the goal of reproducing the good convergence rate of the Jacobi amplitude map with a simpler transformation. An early candidate was the mapping \(\phi (t) = t - a \sin (t)\) for \(a\in [0,1)\). This function increases monotonically, it carries \([-\pi ,\pi ]\) to itself, and it has a small derivative when \(0=t=x(t)\) if \(a\approx 1\). These properties also hold for the compositions \(\phi \circ \phi \) and \(\phi \circ \phi \circ \phi \). The mapping \(x(t) = \phi (t)\) is not competitive with the Jacobi amplitude transformation, but with a suitable choice of parameter a, the mapping \(x(t)=\phi (\phi (t))\) is.Footnote 2 We call this the iterated sine map:

$$\begin{aligned} x(t)&= t - a\sin (t) - a\sin (t-a\sin (t)) \end{aligned}$$
(17)
$$\begin{aligned} x'(t)&= (1-a\cos t)\left( 1-a\cos (t-a\sin t)\right) . \end{aligned}$$
(18)

The optimal value of the parameter a depends on the location of the singularity \(0\pm Bi\). To find it, we study the imaginary part of \(x(0+i\lambda )\) as a function of \(\lambda \). This increases from 0 when \(\lambda =0\) to a local maximum when \(\lambda ={{\,\textrm{arccosh}\,}}(1/a)\). Setting the value of this local maximum equal to B, we have the equation

$$\begin{aligned} -\sqrt{1-a^2}+a \sinh \left( \sqrt{1-a^2} -{{\,\textrm{arcsech}\,}}(a)\right) +{{\,\textrm{arcsech}\,}}(a)=B. \end{aligned}$$
(19)

This is difficult or impossible to solve analytically for a. In searching for a good initial guess for Newton’s iteration, we found that the optimal value of a was very close to \(1 + B/5 - B^{2/5}\), especially for \(B \approx 0\). The approximation is good enough that we forego Newton iteration entirely and use \(a = 1+B/5-B^{2/5}\), with the caveat that for \(B>1.5\), one should abandon the iterated sine map and use the ordinary trapezoid rule without any change of variable. Therefore, the improved convergence rate is

$$\begin{aligned} \lambda = {{\,\textrm{arccosh}\,}}\left( \frac{1}{1+B/5 - B^{2/5}}\right) \approx \sqrt{2}B^{1/5} + \frac{5B^{3/5}}{6\sqrt{2}} -\frac{B^{4/5}}{5\sqrt{2}} + \frac{43B}{80\sqrt{2}}. \end{aligned}$$
(20)

This mapping is illustrated at bottom left in Fig. 1; it is a ‘cautious’ method because it carries the strip into a relatively small bounded region, but its convergence rate is nearly as good as the Jacobi amplitude map. For \(B>0.03\) the image of the strip overlaps the branch cut slightly, possibly impairing the convergence rate, but this problem disappears for \(B<0.03\).

Fig. 1
figure 1

Comparison of three conformal mappings for periodic problems when the integrand has a branch cut extending vertically from \(0+0.3i\) (red lines). The three grids are the conformal images of three rectangles \(\{x+iy: |x |\le \pi , 0\le y\le \lambda \}\). The Jacobi amplitude mapping carries a wide strip \((\lambda = 1.5)\) onto the full, unbounded domain of analyticity we have assumed for the integrand. The boundary correspondance map carries a thinner strip (\(\lambda = 0.81\)) into a subset of the region of analyticity, avoiding the sides of the branch cut but extending to infinity for \(|x |>\pi /2\). The iterated sine map carries a wide strip \((\lambda = 1.46\)) onto a bounded region which slightly overlaps the branch cut, an issue that disappears when the height B of the singularity is less than 0.03. At bottom right, we compare the improved convergence rates for these conformal maps as well as the splitting method and the ordinary trapezoid rule for \(B\in [10^{-4},1]\); larger values of \(\lambda \) correspond to better convergence rates (color figure online)

5 Methods for aperiodic problems with singularity off the real line

We now consider the problem of integrating an aperiodic function f(x) on \([-1,1]\), assuming that f is analytic except for singularities at \(A\pm Bi\) with \(B>0\), or along branch cuts extending vertically from these points to infinity. Our goal is to use this information about the integrand to improve on standard Gauss–Legendre quadrature. As before, we describe a decomposition method and also several methods based on conformal maps. Here we do not propose any novel mappings, but we give some new results on the convergence properties of those previously described [6, 9, 10, 16].

5.1 Decomposition

A simple strategy for integration on \([-1,1]\) is to carry out separate Gauss–Legendre integrations on \([-1,\delta ]\) and \([\delta ,1]\), where \(\delta \) depends on \(A\pm Bi\). Rescaling both subproblems back to \([-1,1]\), we obtain

$$\begin{aligned} \int _{-1}^1 f(x)\,dx= & {} \frac{\delta +1}{2}\int _{-1}^1 f \left( \frac{\delta -1}{2} + \frac{\delta +1}{2}t \right) \,dt \nonumber \\ {}{} & {} + \frac{1-\delta }{2}\int _{-1}^1 f\left( \frac{1+\delta }{2} + \frac{1-\delta }{2}w \right) \,dw. \end{aligned}$$
(21)

Now the integrands on the right side of (21) have singularities at the points \(t^* = (2A-\delta +1+ 2Bi)/(1+\delta )\) and \(w^* = ({2A-\delta -1+2Bi})/({1-\delta })\), respectively. To optimize the convergence rate, we must choose \(\delta \) so that the Bernstein ellipses passing through \(t^*\) and \(w^*\) coincide. In particular, their semimajor axis lengths must be equal. Letting a denote this common length, we have the equations

$$\begin{aligned} \frac{(2A-\delta +1)^2}{a^2(\delta +1)^2} +\frac{(2B)^2}{(a^2-1)(\delta + 1)^2} = 1 =\frac{(2A-\delta -1)^2}{a^2(\delta +1)^2} + \frac{(2B)^2}{(a^2-1)(1-\delta )^2} . \end{aligned}$$
(22)

By eliminating a we arrive at the quartic polynomial equation

$$\begin{aligned} 2\delta (2A - \delta +1)^2 (A-\delta ) + 4\delta B^2 (2A-\delta ) = 2(\delta +1)^2(2A-\delta )(A-\delta ). \end{aligned}$$
(23)

The real solution with \(|\delta |<1\) is given by

$$\begin{aligned} \delta ={{\,\textrm{sgn}\,}}(A)\left( |A |- \frac{\sqrt{2}}{2} \sqrt{A^2-1 - B^2 + \sqrt{-4A^2 + \left( 1+A^2+B^2\right) ^2}}\right) . \end{aligned}$$
(24)

This formula agrees with Driscoll and Weideman’s result (equation 2.7 in [5]), and gives a simple method for evaluating \(\int _{-1}^1 f(x)\,dx\), given the location of the nearest singularity \(x^* = A+Bi\): define \(\delta \) using (24), then evaluate the right-hand side of (21) using n/2-point Gauss–Legendre quadrature on each subintegral. Letting \(\rho _{x^*}\) and \(\rho _{t^*}=\rho _{w^*}\) be the Bernstein ellipse parameters for the original and decomposed problems, we see that the error decay rates of Theorem 2 are \(\rho _{x^*}^{-2n}\) and \(\rho _{t^*}^n\), respectively. Therefore the splitting procedure will be worthwhile if \(\sqrt{\rho _{t^*}} > \rho _{x^*}\).

5.2 Conformal maps

We now turn to strategies based on conformal mapping. Writing \(t_j\) and \(w_j\) for the nodes and weights of Gauss–Legendre quadrature on \([-1,1]\), we have

$$\begin{aligned} \int _{-1}^1 f(x)\,dx = \int _{-1}^1 f(x(t))x'(t)\,dt \approx \sum _{j=1}^n f(x(t_j))x'(t_j)w_j \end{aligned}$$
(25)

The new integrand \(f(x(t))x'(t)\) should be analytic on a large Bernstein ellipse in the complex t-plane. In particular, the image of the ellipse should lie within the domain of analyticity of f in the complex x-plane, and the derivative \(x'(t)\) should not itself be singular, at least for t within the Bernstein ellipse. We also require that \(x(t)\in [-1,1]\) for \(t\in [-1,1]\), and \(x(\pm 1)=\pm 1\). See Fig. 2 for illustrations of the four transformations when \(A\pm Bi = 2/3 \pm i/3\) as well as a plot of the convergence rates for other values of B, and Fig. 5 for numerical results.

5.2.1 The hyperbolic sine map (Sinh)

We give a new derivation of the sinh transformation [10, 15], using a differential equation. Motivated by the desire to keep the derivative \(x'(t)\) small when x is near the singularity \(A+Bi\), we consider the BVP

$$\begin{aligned} x'(t) = k\sqrt{(x-A)^2 + B^2},\quad x(-1)=-1,\;\;x(1)=1 \end{aligned}$$
(26)

where the proportionality constant k is to be found as part of the solution. This can be solved by hand, yielding the transformation

$$\begin{aligned} x(t) = A + B \sinh \left( \frac{1-t}{2}{{\,\textrm{arcsinh}\,}}\left( \frac{-1-A}{B}\right) + \frac{1+t}{2}{{\,\textrm{arcsinh}\,}}\left( \frac{1-A}{B}\right) \right) . \end{aligned}$$
(27)

The derivative \(x'(t)\) is an entire function, so the convergence rate will be limited by the analyticity of f(x(t)). The value of t for which \(x(t)=A+Bi\) is

$$\begin{aligned} t^* = 1 + \frac{i \pi - 2 {{\,\textrm{arcsinh}\,}}((1-A)/B)}{{{\,\textrm{arcsinh}\,}}((1-A)/B) +{{\,\textrm{arcsinh}\,}}((1+A)/B)} \end{aligned}$$
(28)

and we can use this in (3) to obtain the improved value of the ellipse parameter \(\rho \). If the singularity is on the imaginary axis, we have the formula

$$\begin{aligned} A = 0 \implies \rho = \frac{\pi + \sqrt{\pi ^2 + 4 {{\,\textrm{arcsinh}\,}}(1/B)^2}}{2{{\,\textrm{arcsinh}\,}}(1/B)}. \end{aligned}$$
(29)

5.2.2 The elliptic sine map (Tee)

By conformally mapping a Bernstein ellipse onto the doubly slit plane, Tee and Hale [7] obtained the transformation x(t) given by the equations

$$\begin{aligned} c&= \frac{{{\,\textrm{sgn}\,}}(A)}{\sqrt{2}} \sqrt{A^2+B^2+1 -\sqrt{(A^2+B^2+1)^2-4A^2}}\end{aligned}$$
(30)
$$\begin{aligned} m&= \frac{\left( -B + \sqrt{B^2 + 1-c^2}\right) ^4}{(1-c^2)^2}\end{aligned}$$
(31)
$$\begin{aligned} h(t)&= m^{1/4} {{\,\textrm{sn}\,}}\left( \frac{2K(m)}{\pi }\arcsin (t), m\right) \end{aligned}$$
(32)
$$\begin{aligned} x(t)&= \frac{c}{m^{1/4}} - \frac{1-m^{1/2}}{2m^{1/4}} \left( \frac{1-c}{ h(t) - 1} + \frac{1+c}{h(t)+1}\right) . \end{aligned}$$
(33)

Note that we have condensed their notation. The intermediate conformal map h(t), carrying an ellipse to the unit circle, is due to Schwarz [13, 14]. The enlarged Bernstein ellipse has parameter given by [7]

$$\begin{aligned} \rho = \exp \left( \frac{\pi K(1-m)}{4 K(m)}\right) . \end{aligned}$$
(34)

For convenience in computing the derivative \(x'(t)\), we note that

$$\begin{aligned} h'(t) = \frac{2m^{1/4}K(m)}{\pi \sqrt{1-t^2}} {{\,\textrm{cn}\,}}\left( \frac{2K(m)}{\pi }\arcsin (t), m\right) {{\,\textrm{dn}\,}}\left( \frac{2K(m)}{\pi }\arcsin (t), m\right) . \end{aligned}$$
(35)

This aggressive strategy takes full advantage of the assumed analyticity of the integrand and has a large convergence rate.

5.2.3 Jafari–Varzaneh and Hosseini’s mapping (JVH)

Jafari-Varzaneh and Hosseini used the composition of the hyperbolic sine mapping and another similar function to obtain a new mapping [9]. With \(\alpha = \frac{1}{2} {{\,\textrm{arcsinh}\,}}\left( \frac{1-A}{B}\right) + \frac{1}{2} {{\,\textrm{arcsinh}\,}}\left( \frac{1+A}{B}\right) \), their formulas can be condensed to

$$\begin{aligned} x(t) {=} A {+} B \sinh \left( {{\,\textrm{arcsinh}\,}}\left( \frac{1-A}{B}\right) {-}\alpha + \frac{2\alpha L {+} \pi }{2} \tan \left( t\, \arctan \left( \frac{2\alpha }{2\alpha L {+} \pi }\right) \right) \right) \end{aligned}$$
(36)

where L is a tunable parameter between 0.2 and 0.9. In light of their statement that “experiments show that different values of this parameter approximately give the same results,” we choose to take \(L=0.5\) in all cases. We solved for the value of \(t^*\) satisfying \(x(t^*) = A+Bi\) and obtained

$$\begin{aligned} t^* = \arctan \left( \frac{2\alpha - 2{{\,\textrm{arcsinh}\,}}\left( \frac{1-A}{B}\right) +i\pi }{2\alpha L+\pi }\right) /\arctan \left( \frac{2\alpha }{2\alpha L+\pi }\right) , \end{aligned}$$
(37)

which can be used in (3) to find a prediction of the new convergence rate. If the singularity is on the imaginary axis, we find the formula

$$\begin{aligned} A = 0 \implies \rho = \frac{{{\,\textrm{arctanh}\,}}(c_1) +\sqrt{\arctan (c_2)^2 + {{\,\textrm{arctanh}\,}}(c_1)^2}}{\arctan (c_2)} \end{aligned}$$
(38)

where \(c_1 = \pi /(\pi + {{\,\textrm{arcsinh}\,}}(1/B))\) and \(c_2 =2{{\,\textrm{arcsinh}\,}}(1/B)/(\pi + {{\,\textrm{arcsinh}\,}}(1/B))\). This strategy is more cautious than Tee’s elliptic sine mapping but more aggressive than the hyperbolic sine tranformation.

5.2.4 The iterated sinh map (2Sinh)

For some problems, Elliot and Johnston suggested applying the sinh mapping twice in succession [6]. To construct the mapping, we can use (28) to obtain the singularity location after one sinh transformation and then apply another sinh transformation accordingly. For convenience, we simplify to obtain the formula \(x(t) = A+B \sinh \left( \frac{\pi }{2} \sinh \left( \ell (t)\right) \right) \), where \(\ell (t)\) is the linear function

$$\begin{aligned} \ell (t) {=} \frac{t+1}{2} {{\,\textrm{arcsinh}\,}}\left( \frac{2}{\pi } {{\,\textrm{arcsinh}\,}}\left( \frac{1-A}{B}\right) \right) {+}\frac{t-1}{2} {{\,\textrm{arcsinh}\,}}\left( \frac{2}{\pi } {{\,\textrm{arcsinh}\,}}\left( \frac{A+1}{B}\right) \right) . \end{aligned}$$
(39)

The new singularity lies at

$$\begin{aligned} t^* = 1+\frac{i \pi -2 {{\,\textrm{arcsinh}\,}}\left( \frac{2}{\pi } {{\,\textrm{arcsinh}\,}}((1-A)/B)\right) }{{{\,\textrm{arcsinh}\,}}\left( \frac{2}{\pi } {{\,\textrm{arcsinh}\,}}((1-A)/B)\right) +{{\,\textrm{arcsinh}\,}}\left( \frac{2}{\pi } {{\,\textrm{arcsinh}\,}}((1+A)/B)\right) } \end{aligned}$$
(40)

instead of \(x^*=A\pm Bi\). This can be used with (3) to find the ellipse parameter; in particular, when \(A=0\) this gives the formula

$$\begin{aligned} A=0\implies \rho = \frac{\pi + \sqrt{\pi ^2 + 4{{\,\textrm{arcsinh}\,}}\left( \frac{2}{\pi }{{\,\textrm{arcsinh}\,}}(1/B)\right) ^2}}{2{{\,\textrm{arcsinh}\,}}\left( \frac{2}{\pi }{{\,\textrm{arcsinh}\,}}(1/B)\right) }. \end{aligned}$$
(41)

This map tends to wrap the Bernstein ellipse onto a relatively large region surrounding the singularity (overlapping the branch cut if there is one). Elliot and Johnston observed that the iteration gives better results for what they term nearly strongly singular problems, meaning integrals that become divergent when the singularity reaches the real line. In contrast, they present experiments showing that a single sinh transformation is better for nearly weakly singular integrals (which become convergent improper integrals when the singularity reaches the integration interval). This distinction is outside our scope because it relies on information about the nature of the singularity in addition to its location.

Fig. 2
figure 2

Comparison of four conformal mappings designed to avoid a singularity at \(2/3 + i/3\). We plot the image of four Bernstein ellipses with different values of \(\rho \). The mapping introduced by Tee, in terms of the Jacobi elliptic sine, permits a large value of \(\rho \) and uses all of the assumed analyticity. The \(\rho \)-value for the map of Jafari-Varzaneh and Hosseini is nearly as large, and the image is bounded. The hyperbolic sine mapping has the smallest \(\rho \)-value of the four, and it uses far less of the assumed domain of analyticity. The iterated or double sinh transformation achieves a large value of \(\rho \), but the image of the ellipse extends far from the original interval and, while avoiding the singularity itself, wraps around it and significantly overlaps any branch cut extending from the singularity, so this value overestimates the convergence rate. The lower panel gives the ellipse parameter \(\rho \) for the four conformal mappings as well as the splitting and Gauss–Legendre schemes for a singularity at \(2/3 + Bi\) with \(B\in (0,1)\)

6 Methods for aperiodic problems with singularity on the real line

We now consider the case where the integrand f(x) is analytic except for an isolated singularity at some real A with \(|A |>1\), or possibly with a horizontal branch cut from A to infinity. This situation is less common than the fully complex case, both for quadrature problems and for rational barycentric interpolation and Chebyshev spectral methods, and accordingly has received less attention. We describe several strategies for using the analyticity of f in order to improve on Gauss–Legendre integration with n nodes; to our knowledge these are all new.

6.1 Subdivision

One simple way to take advantage of information on the location of the singularity is to carry out separate Gauss–Legendre integrations, each using n/2 nodes, on \([-1,\delta ]\) and \([\delta ,1]\), where \(\delta \) is given by (24). With \(B=0\), that formula simplifies to

$$\begin{aligned} \delta = {{\,\textrm{sgn}\,}}(A)\left( |A |- \sqrt{A^2-1}\right) . \end{aligned}$$
(42)

As above, the improved convergence rate comes from putting the rescaled singularity \((2A-\delta +1)/(1+\delta )\) into (3).

6.2 Conformal maps

We again follow (25), applying Gauss–Legendre integration following a change of variable. The mapping x(t) must preserve the interval \([-1,1]\) and fix its endpoints, and ideally should carry a large Bernstein ellipse in the t-plane into the domain of analyticity of f. We introduce three possibilities and illustrate them in Fig. 3; numerical examples appear in Fig. 6.

6.2.1 Quadratic transformation (Quad)

There is a unique second-degree polynomial x(t) which satisfies the boundary conditions \(x(-1)=-1\) and \(x(1)=1\), satisfies \(x'(t)=0\) when \(x=A\), and is strictly increasing for \(-1\le t\le 1\). It is given by

$$\begin{aligned} x(t) = \frac{-1}{2} {{\,\textrm{sgn}\,}}(A) \left( |A |-\sqrt{A^2-1}\right) (t^2-1) + t. \end{aligned}$$
(43)

The unique value of t satisfying \(x(t)=A\) is the semimajor axis length of the enlarged ellipse; this leads to the improved ellipse parameter

$$\begin{aligned} \rho = |A |+ \sqrt{A^2-1} +\sqrt{\left( |A |+ \sqrt{A^2-1}\right) ^2-1}. \end{aligned}$$
(44)

This relatively cautious strategy gave the best results for most of our test problems.

6.2.2 Exponential transformation (Exp)

By solving the BVP

$$\begin{aligned} \frac{dx}{dt} = k|x-A|,\quad x(-1)=-1,\;x(1)=1, \end{aligned}$$
(45)

where the value of k is determined as part of the problem, we obtain an exponential change of variable:

$$\begin{aligned} x(t) = A + (1-A)\exp \left( \frac{1-t}{2} \log \left( \frac{A+1}{A-1}\right) \right) \end{aligned}$$
(46)

There is no value of t satisfying \(x(t)=A\), so the composition f(x(t)) is entire if f has an isolated singularity. On the other hand, if f has a branch cut from A to infinity, then the composition is analytic only on the Bernstein ellipse with parameter \(\rho = s + \sqrt{1+s^2}\) with \(s = 2\pi /|\log ( (A+1)/(A-1))|.\)

6.2.3 Ellipse-to-slit-plane map (JESN)

We have already seen the Schwarz mapping carrying the interior of an ellipse onto the unit disk. We can compose this map with the transformation \(z\mapsto - (1-z)^2/(1+z)^2\) to obtain a mapping from the ellipse to the slit plane \({\mathbb {C}}\backslash [0,\infty )\). Then, by scaling and translation, we can arrange for \(x(\pm 1) = \pm 1\). The transformation and its derivative are given by

$$\begin{aligned} c_1&= 17-80A^2+64A^4 \end{aligned}$$
(47)
$$\begin{aligned} c_2&= (4A^2-3)|A |\sqrt{A^2-1} \end{aligned}$$
(48)
$$\begin{aligned} m&= c_1+16c_2 - 4 \sqrt{2}\sqrt{(A^2-1)(8+c_1(4A^2-1)) + c_1c_2} \end{aligned}$$
(49)
$$\begin{aligned} h(t)&= m^{1/4} {{\,\textrm{sn}\,}}\left( \frac{2K(m)}{\pi }\arcsin (t), m\right) \end{aligned}$$
(50)
$$\begin{aligned} x(t)&= \frac{h(t)+2\sqrt{m}\left( 1+h(t)+h(t)^2\right) + m\, h(t)}{(1+h(t))^2 \left( 1+\sqrt{m}\right) m^{1/4}} \end{aligned}$$
(51)
$$\begin{aligned} x'(t)&= -h'(t) \frac{(h(t)-1)\left( 1-\sqrt{m}\right) ^2}{(1+h(t))^3 \left( 1+\sqrt{m}\right) m^{1/4}} \end{aligned}$$
(52)

while the new convergence rate is \(\rho = \exp (0.25\pi K(1-m)/K(m))\). This aggressive strategy was more effective than the splitting method but less effective than the other conformal maps in our tests.

Fig. 3
figure 3

Three conformal mappings that avoid a singularity on the real line, at 4/3. The Jacobi elliptic sine mapping (top left) carries a large ellipse (\(\rho = 8.38\)) onto \({\mathbb {C}} \backslash [4/3,\infty )\). The exponential transformation (top right) carries a smaller ellipse (\(\rho = 6.61)\) onto a large but bounded region. The quadratic transformation (bottom left) carries a still smaller ellipse (\(\rho =4.19\)) into a much smaller bounded region. All three methods improve substantially against ordinary Gauss–Legendre integration, which has \(\rho = 2.21\) for \(A=4/3\). At lower right we plot the relationship between \(\rho -1\) and \(A-1\) for these methods as well as the decomposition method of Sect. 6.1

7 Numerical examples

We now assess the performance of all of the strategies described above on a collection of nearly singular integrals of varying difficulty. We plot relative errors by comparing to reference solutions obtained through high-precision computations in Mathematica. We begin with a careful discussion of the results for periodic problems and then treat the aperiodic problems more briefly, since the themes are similar.

7.1 Periodic examples

For periodic problems, we consider integrals of the form \(\int _{-\pi }^\pi f_i(x;\epsilon )\,dx\) for \(\epsilon \in \{10^{-1}\), \(10^{-2}\), \(10^{-3}\}\) where \(f_i\) is one of the following:

$$\begin{aligned} f_1(x;\epsilon )&= \log (\cosh (\epsilon )-\cos (x)) + (\cosh (\epsilon )-\cos (x))^{3/10} \end{aligned}$$
(53)
$$\begin{aligned} f_2(x;\epsilon )&= \frac{1}{\sqrt{\cosh (\epsilon )-\cos (x)}} \end{aligned}$$
(54)
$$\begin{aligned} f_3(x;\epsilon )&= \frac{\cos ^2(6x)}{\sqrt{\cosh (\epsilon )-\cos (x)}} \end{aligned}$$
(55)
$$\begin{aligned} f_4(x;\epsilon )&= \frac{\sqrt{\cosh (1)+\cos (x)}}{\sqrt{\cosh (\epsilon ) -\cos (x)}}. \end{aligned}$$
(56)

Because \(\cosh (\epsilon ) = \cos (\epsilon i)\), all of these integrands have branch cuts extending vertically to infinity from \(x=2\pi k \pm \epsilon i\), for \(k\in {\mathbb {Z}}\). The first three integrands have no singularities other than these branch cuts, while \(f_4\) has additional branch cuts extending to infinity from \(2\pi k + \pi \pm 1i\). The results, displayed in Fig. 4, suggest that the iterated sine mapping method usually reaches machine precision with a similar or smaller number of quadrature nodes n compared to the Jacobi amplitude mapping, and that these two methods give much better results than the other three methods. We now make more detailed comments about each row of the figure.

Fig. 4
figure 4

We test five strategies for integration of periodic, nearly singular integrals on twelve problems of varying difficulty. The relative numerical errors (glyphs) generally decay with the predicted slopes (lines), with exceptions in the last two rows due to particular features of the integrands as discussed in the text. The predicted slopes depend only on the locations of the singularities, \(\pm \epsilon i\), and are identical within columns of the figure. The iterated sine mapping (stars) gives the best results, except in the second row where the Jacobi amplitude transformation (pluses) is better

For the first integrand \(f_1\), a sum of logarithmic and fractional-power singularities, all five methods converge with the predicted slopes (top row of Fig. 4). The Jacobi amplitude mapping has the best slope, but the iterated sine mapping reaches machine precision at the same time or slightly earlier, with about \(n=50\) quadrature nodes.

For the second integrand \(f_2\), which has an inverse root singularity, the Jacobi amplitude mapping gives remarkably good results. This is something of a lucky accident particular to this integrand (if one multiplies \(f_2\) by \(\cos (x)\), the result, not pictured, is similar to the top row of Fig. 4). We also see that the boundary correspondence mapping converges twice as quickly as predicted when n is odd, but converges at the expected rate for even n (the precise n sampled in Fig. 4 are the multiples of 7 up to 147). For the smaller values of B (middle and right column of Fig. 4), this is still much slower than the ISM or JAM results.

The third integrand \(f_3\) is the product of \(f_2\) and the entire function \(\cos ^2(6x)\), which grows rapidly away from the real line. Therefore we expect the constant M in Theorem 1 to play a more influential role in the third row of Fig. 4 than in the second. To be more precise, the rapid growth of \(|f_3|\) away from the real line means that with moderate n, the optimal statement of the theorem comes from setting \(\lambda \) somewhat smaller than the full domain of analyticity would permit. This effect will fade as n grows and eventually the errors should decrease at the expected rate, although this will not be observable if the approximation reaches machine precision before n has grown sufficiently. In fact, for \(\epsilon =1/1000\), both the iterated sine mapping and the Jacobi amplitude mapping converge more slowly than predicted, and the iterated sine mapping significantly outperforms the Jacobi amplitude mapping. We also remark that the splitting method, while not converging as quickly as the best methods, does converge at the predicted rate, which is unsurprising given that it uses analyticity only in two thin ellipses which do not extend far from the real line.

Finally we turn to the fourth integrand, which has a different domain of analyticity. While both the JAM and BCM methods have extensions to the the case of multiple singularities [3, 15], we choose to construct the mappings with reference only to the singularities at \(0\pm \epsilon i\), ignoring the more distant ones at \(\pi \pm i\). This means that the results based on conformal mapping should converge somewhat more slowly than expected, a prediction confirmed by the last row of Fig. 4. However, for small B the iterated sine mapping is much less impaired by this failure of analyticity than the Jacobi amplitude and boundary correspondence mappings. We can explain this difference by examining the illustrations of the three mappings in Fig. 1. Indeed, on the lines \(x=\pm \pi +\xi i\), the iterated sine mapping assumes analyticity only for a bounded range of \(\xi \), while the other mappings assume analyticity for all \(\xi \). This makes the singularity at \(\pm \pi \pm 1i\) more hazardous for the BCM and JAM methods, which rely more completely on the analyticity we have assumed.

7.2 Aperiodic problems with nonreal singularity

For this setting we modify the test problems slightly so that the integration interval is \([-1,1]\) and the singularity lies at \(2/3 \pm \epsilon i\). Specifically, we use

$$\begin{aligned} g_1(x;\epsilon )&= -\log (\cosh (x-2/3)-\cos (\epsilon )) + (\cosh (x-2/3)-\cos (\epsilon ))^{0.3} \end{aligned}$$
(57)
$$\begin{aligned} g_2(x;\epsilon )&= \frac{1}{\sqrt{\cosh (x-2/3)-\cos (\epsilon )}} \end{aligned}$$
(58)
$$\begin{aligned} g_3(x;\epsilon )&= \frac{\cos ^2(6\pi x)}{\sqrt{\cosh (x-2/3) -\cos (\epsilon )}} \end{aligned}$$
(59)
$$\begin{aligned} g_4(x;\epsilon )&= \frac{\sqrt{\cosh (x+2/3)-\cos (1)}}{\sqrt{\cosh (x-2/3)-\cos (\epsilon )}}. \end{aligned}$$
(60)

Because the integration interval is smaller by a factor of \(\pi \), we take \(\epsilon \in \{1/30\), 1/300, \(1/3000\}\) to obtain problems of comparable difficulty to the previous subsection.

As above, the third integrand is the product of the second integrand and an entire function, while the fourth integrand has an additional pair of singularities at \(x=-2/3\pm i\). The results, given in Fig. 5, are very similar to the periodic examples. In particular, the mapping that uses the Jacobi elliptic functions to carry a Bernstein ellipse onto the full domain of analyticity of the integrand does not give the best results, even though its predicted convergence rate is the best; instead, the more cautious hyperbolic sine mapping appears to be the best general choice. For these problems, the iterated sinh map does not converge at the large rate suggested by (40); its performance is similar to Tee’s elliptic mapping.

All of these examples have \(A=2/3\). In other tests, not plotted, we found similar results for integrands with singularities at \(1+\epsilon + \epsilon i\) for \(\epsilon = 1/30\), \(\epsilon =1/300\), and \(\epsilon =1/3000\). In particular, the sinh map again gave the best results overall.

Fig. 5
figure 5

Integration of the functions (57)–(60) by the methods of Sect. 5. Here “Tee” refers to the Jacobi elliptic mapping, “JVH” is the composite mapping introduced by Jafari-Varzaneh and Hosseini, “2Sinh” and “Sinh” are the iterated and ordinary hyperbolic sine mappings, “Split” is the decomposition strategy, “GL” is unmodified Gauss–Legendre quadrature, and “CC” is Clenshaw–Curtis quadrature. Among these, the ordinary sinh method generally gives the best results

7.3 Aperiodic problems with real singularity

We now integrate on \([-1,1]\) with singularity at \(1 + \epsilon \in {\mathbb {R}}\). Specifically, we use

$$\begin{aligned} h_1(x;\epsilon )&= -\log (1+\epsilon -x) + (1+\epsilon -x)^{0.3} \end{aligned}$$
(61)
$$\begin{aligned} h_2(x;\epsilon )&= \frac{1}{\sqrt{1+\epsilon -x}} \end{aligned}$$
(62)
$$\begin{aligned} h_3(x;\epsilon )&= \frac{\cos ^2(6\pi x)}{\sqrt{1+\epsilon -x}} \end{aligned}$$
(63)
$$\begin{aligned} h_4(x;\epsilon )&= \frac{\sqrt{\cosh (x+2/3)-\cos (1)}}{\sqrt{1+\epsilon -x}}. \end{aligned}$$
(64)

We again take \(\epsilon \in \{1/30, 1/300, 1/3000\}\).

The results, in Fig. 6, indicate that the quadratic transformation gives the best results for \(h_2\), \(h_3\), and \(h_4\), while the exponential transformation is better for \(h_1\).

Fig. 6
figure 6

Integration of the functions (61)–(64) via the exponential, quadratic and Jacobi elliptic sine maps along with the splitting method and ordinary Gauss–Legendre and Clenshaw–Curtis quadrature as discussed in Sect. 6. The quadratic transformation is best in the last three rows, while the exponential map is best in the first row

8 Application: evaluation of single-layer potentials in Stokes flow

As an application of the preceding methods, we will evaluate some nearly singular surface integrals that arise in the study of viscous fluid flow. Specifically, we will consider the Stokes single-layer potential defined by

$$\begin{aligned} {\mathcal {S}}(\varvec{x}) = \int _D \left( \frac{\varvec{f}(\varvec{y})}{|\varvec{x}-\varvec{y} |} + \frac{\varvec{x} - \varvec{y}}{|\varvec{x}-\varvec{y} |^3} ((\varvec{x} - \varvec{y})\cdot \varvec{f}(\varvec{y}))\right) \, dS_{\varvec{y}} \end{aligned}$$
(65)

where D is the surface of the slender fiber depicted in the right panel of Fig. 7. The integral has a physical meaning: it is the velocity field that results when point forces of strength \(\varvec{f}\) are distributed over the surface D. The integral is challenging when the observation point \(\varvec{x}\) is near but not on the surface D. Although the surface has an irregular shape, the integral has a known exact solution when the density \(\varvec{f}\) is equal to the outward pointing surface normal: in this case the resulting velocity is zero everywhere, and the surface traction \(\varvec{f}\) is due solely to hydrostatic pressure. This arrangement allows us to test various quadrature strategies in a setting where the geometry is nontrivial but an exact solution (zero) is known. Note that we have omitted the usual factor of \(1/(8\pi )\) in (65).

The problem of computing nearly singular surface integrals arises in many situations when a linear PDE is solved via integral equations. For the Stokes PDE, a number of methods have been developed that take into account the particular form of the Stokes fundamental solutions. Prominent recent examples include singularity subtraction and local expansion [18, 19], quadrature by expansion [2], and singularity swapping [1]. Another relevant method is the hedgehog extrapolation procedure [12], which is based on the observation that the nearly singular integrals in the BIE setting can be regularized by moving a target point farther away from a nearby surface. Here we attempt to address this challenge without using special knowledge about the particular form of the integrands. This test problem involves a toroidal topology, which we break into topologically cylindrical patches so that we can demonstrate both the periodic quadratures of §4 and the aperiodic quadratures of §5. The fact that the fiber has circular cross sections will allow us to find the singularities of the inner integral analytically, on paper; this is convenient because it will allow us to avoid a numerical rootfinding procedure in one of the two directions. For a surface that is instead decomposed into topologically rectangular patches, we would use an outer product of aperiodic rules generated in a similar manner, and our ability to find the singularities analytically will depend on the surface geometry.

Fig. 7
figure 7

To demonstrate our integration strategies we consider the problem of evaluating a Stokes single-layer potential where the target point is near but not on the surface of integration. The surface is a fiber whose centerline follows a closed path on the surface of a torus (left). We then let the target point range over a square domain that is punctured in three places by the fiber (right). The integrals become numerically challenging when the target point approaches the surface. To organize the integration we subdivide the fiber surface into sixteen panels, as depicted in green and orange at right (color figure online)

To describe the fiber surface, we begin by parameterizing the surface of a torus whose centerline has unit radius and whose circular cross sections have radius 0.4:

$$\begin{aligned} \varvec{v}(\theta ,\phi ) = (1+0.4\cos (\phi ))\begin{pmatrix} \cos \theta \\ \sin \theta \\ 0 \end{pmatrix} +0.4\sin \phi \begin{pmatrix} 0\\ 0\\ 1 \end{pmatrix}. \end{aligned}$$
(66)

Then we construct a closed curve on this surface by letting \(\theta \) and \(\phi \) be periodic functions of a (non-arclength) parameter s:

$$\begin{aligned} \varvec{w}(s) = \varvec{v}\Big (s,2 \exp (\cos (s + 1)) \cos (2 s) + 2 s\Big ). \end{aligned}$$
(67)

The path \(\varvec{w}\) appears on the surface of the torus in the left panel of Fig. 7. Let \(\varvec{T}(s)\) denote the unit tangent vector for the curve, \(\varvec{T}(s) = \varvec{w}'(s) / |\varvec{w}'(s)|\). To define the fiber surface we need vectors \(\varvec{N}(s)\) and \(\varvec{B}(s)\) so that \(\{\varvec{T}, \varvec{N},\varvec{B}\}\) is an orthonormal frame. To avoid the derivatives involved in the standard Frenet definition, we plotted \(\varvec{T}(s)\) as a path on the unit sphere and noticed that it always remains far from the line through \(\pm \varvec{p}\), where \(\varvec{p} = \langle 10,3,6\rangle \). Thus we can complete the frame by putting

$$\begin{aligned} \varvec{N}(s) = \frac{\varvec{T}(s) \times \varvec{p}}{|\varvec{T}(s) \times \varvec{p}|},\qquad \varvec{B}(s) = \varvec{T}(s) \times \varvec{N}(s). \end{aligned}$$
(68)

Finally we define the fiber surface D via

$$\begin{aligned} \varvec{y}(s,t) = \varvec{w}(s) + \epsilon \cos (t)\varvec{N}(s) +\epsilon \sin (t)\varvec{B}(s) \end{aligned}$$
(69)

where s and t both range from 0 to \(2\pi \), and the radius \(\epsilon \) is a constant (we take \(\epsilon = 0.05\)). The surface normal vector for the fiber is

$$\begin{aligned} \varvec{f}(s,t) = \varvec{\nu }(s,t) = \cos (t)\varvec{N}(s) + \sin (t)\varvec{B}(s) \end{aligned}$$
(70)

while the Jacobian or surface integration weight is

$$\begin{aligned} J(s,t) = \left|\frac{\partial \varvec{y}}{\partial s} \times \frac{\partial \varvec{y}}{\partial t}\right|= {\epsilon } (|\varvec{w}'(s)|- {\epsilon } \kappa _1 \cos (t) - {\epsilon }\kappa _2\sin (t)) \end{aligned}$$
(71)

where \(\kappa _{1,2}\) are defined by \(\kappa _1 = \varvec{w}'(s)\cdot \varvec{N}(s)\) and \(\kappa _2 = \varvec{w}'(s)\cdot \varvec{B}(s)\).

8.1 Reference solution

We first evaluate the integral using ordinary Gauss–Legendre quadrature and the trapezoid rule. We subdivide the fiber surface into 16 panels using a heuristic that considers the panel lengths and their maximum curvatures; the panel endpoints are at \(s\in \{ 0\), 0.58, 1.21, 1.83, 2.35, 2.76, 3.19, 3.86, 4.26, 4.65, 5.04, 5.24, 5.41, 5.57, 5.75, 6.05, \(2\pi \}\). On each panel, we use the outer product of a Gauss–Legendre grid in s and an equally spaced grid in t, using the same quadrature rule for every target point. The target points range over a square domain \(\{(x,y,0):0<x<1, -5/8<y<3/8\}\) that is punctured in three locations by the fiber surface. The exact velocity is zero, and we depict the norm of the computed velocity at each target point as a contour plot in the first column of Fig. 9. Predictably, we see that this combination of Gauss–Legendre and trapezoid rules is effective only when the target is far from the fiber surface.

8.2 Finding the singularities

In order to improve on the reference solution using the methods described in this paper, we need to find the singularities of the inner (periodic) integrand in the complex t-plane for fixed s, as well as the singularities of the outer integrand in the complex s-plane. Although the outer integral in s is also \(2\pi \)-periodic, we chose to integrate separately on each of the panels, leading to sixteen aperiodic subproblems.

Because the cross sections of the fiber are circles, we can find the singularities for the periodic, inner problem on paper as follows. We begin by writing (65) as a double integral. With \(\varvec{r}(s,t) = \varvec{x} - \varvec{y}(s,t)\) we have

$$\begin{aligned} {\mathcal {S}}(\varvec{x}) = \int _0^{2\pi }\int _0^{2\pi } \left( \frac{\varvec{f}(\varvec{y}(s,t))}{|\varvec{r}(s,t)|} + \frac{\varvec{r}(s,t)}{|\varvec{r}(s,t)|^3}(\varvec{r}(s,t) \cdot \varvec{f}(\varvec{y}(s,t)))\right) J(s,t)\, dt\,ds. \end{aligned}$$
(72)

For fixed s, the vector \(\varvec{r}(s,t)\) traces out a circle at constant speed as t varies. The denominators can therefore be written as trigonometric functions of t and we can solve analytically for the complex value of t where they vanish. To do this we write

$$\begin{aligned} |\varvec{r}(s,t)|^2&= \big |\varvec{x} - \varvec{w}(s) -\epsilon \cos (t)\varvec{N}(s) - \epsilon \sin (t)\varvec{B}(s)\big |^2\\&=|\varvec{x} - \varvec{w}(s)|^2 + \epsilon ^2 -2\epsilon \\&\quad \times \Big (\cos (t) (\varvec{x}-\varvec{w}(s))\cdot \varvec{N}(s) + \sin (t) (\varvec{x}-\varvec{w}(s))\cdot \varvec{B}(s)\Big )\\&= |\varvec{x} - \varvec{w}(s)|^2 + \epsilon ^2\\&\quad -{2\epsilon }{\sqrt{((\varvec{x}-\varvec{w}(s))\cdot \varvec{N}(s))^2 +((\varvec{x}-\varvec{w}(s))\cdot \varvec{B}(s))^2}}\cos (t-\xi ) \end{aligned}$$

where the angle \(\xi \) is defined by

$$\begin{aligned} \cos (\xi )&= \frac{(\varvec{x}-\varvec{w}(s))\cdot \varvec{N}(s)}{\sqrt{((\varvec{x}-\varvec{w}(s))\cdot \varvec{N}(s))^2+((\varvec{x}-\varvec{w}(s)) \cdot \varvec{B}(s))^2}},\;\\ \sin (\xi )&= \frac{(\varvec{x}-\varvec{w}(s))\cdot \varvec{B}(s)}{\sqrt{((\varvec{x}-\varvec{w}(s))\cdot \varvec{N}(s))^2+((\varvec{x} -\varvec{w}(s))\cdot \varvec{B}(s))^2}}. \end{aligned}$$

Therefore, \(\varvec{r}(s,t)\) vanishes when

$$\begin{aligned} \cos (t-\xi ) = \frac{|\varvec{x} - \varvec{w}(s)|^2 + \epsilon ^2}{2\epsilon \sqrt{((\varvec{x}-\varvec{w}(s)) \cdot \varvec{N}(s))^2+((\varvec{x}-\varvec{w}(s))\cdot \varvec{B}(s))^2}} \end{aligned}$$

and we find that the required value of t is

$$\begin{aligned} t^* = \xi + \sqrt{-1}{{\,\textrm{arccosh}\,}}\left( \frac{|\varvec{x} - \varvec{w}(s)|^2 + \epsilon ^2}{2\epsilon \sqrt{((\varvec{x}-\varvec{w}(s))\cdot \varvec{N}(s))^2 +((\varvec{x}-\varvec{w}(s))\cdot \varvec{B}(s))^2}}\right) . \end{aligned}$$
(73)

This allows us to choose a quadrature for the inner integral using knowledge of the integrand’s complex singularity, as in Sect. 4.

We will need some numerical rootfinding to locate the singularities of the outer integral. We note that the inner integral will diverge if the imaginary part of \(t^*\) in (73) vanishes. Therefore, the complex singularities of the outer integrand are the solutions of the equation

$$\begin{aligned} \Big (|\varvec{x} - \varvec{w}(s)|^2 + \epsilon ^2\Big )^2 =4\epsilon ^2\Big (((\varvec{x}-\varvec{w}(s))\cdot \varvec{N}(s))^2 +((\varvec{x}-\varvec{w}(s))\cdot \varvec{B}(s))^2\Big ). \end{aligned}$$
(74)

To obtain these roots we find the eigenvalues of a \(49\times 49\) Chebyshev colleague matrix. This size is chosen to balance the cost of the eigenvalue computation and the accuracy of the computed roots, with inspiration from the Chebfun system [4] wherein rootfinding problems are recursively subdivided to obtain systems of size about 50. For each computed root we then find the corresponding Bernstein ellipse parameter \(\rho \). If all computed \(\rho \)-values are greater than 2, we revert to ordinary Gauss–Legendre quadrature; otherwise we use the root with the smallest \(\rho \)-value to accelerate the quadrature in the outer integral.

8.3 Improvement via decomposition and conformal mapping

Overall, the surface quadrature procedure that we have outlined is laborious: for each target point and within each panel, we find a customized 1D quadrature rule for the aperiodic outer integral. Then, for each of the resulting outer quadrature nodes, we find a customized 1D quadrature rule for the periodic inner integral. An example of the resulting surface quadrature rule appears in Fig. 8. Although our goal is to demonstrate the accuracy of the underlying quadratures rather than to address the fast generation of rules for many target points, we make one adjustment for the sake of efficiency: for each panel, we revert to the reference Gauss–Legendre/trapezoid scheme for all targets whose distance to the panel surface is greater than \(7\epsilon \). This allows us to use the same quadrature rule simultaneously for many distant targets.

Fig. 8
figure 8

A surface quadrature rule with \(24^2\) nodes (blue dots) on one panel, customized for a target point (pink dot) that is near but not on the surface. We generated this rule with the hyperbolic sine transformation in the centerline direction, s, and the iterated sine map in the circumferential direction, t (calculated separately for each value of s) (color figure online)

The second column of Fig. 9 shows the result of combining the periodic decomposition method for the inner integral with the aperiodic decomposition method in the outer integral. This leads to more correct digits than the reference solution when the target point is near the surface. However, the third column, the result of using the iterated sine map for the inner integral and the hyperbolic sine map for the outer integral, is dramatically better. We chose the sinh and ISM methods because of their simplicity and superior performance on the tests of Sect. 7. However, other combinations of the conformal mapping strategies, including the Jafari-Varzaneh map and the various options based on Jacobi elliptic functions, yield similar (but not better) results for this application. Note that we used the Quad map of 6 if the numerical rootfinding procedure found a real-valued singularity outside \([-1,1]\), which did happen occasionally.

Our Fig. 9 should be compared with Figures 2, 3, and 8 of [1], a related work on line integrals, where the authors make use of additional information about the nature of the singularities, not merely their location, and accordingly develop a more powerful but less general method for accelerating the quadrature of the nearly singular integrals. We note that they developed methods for finding and using multiple root pairs, but found in practice that this did not give better results than the methods based on a single root pair.

In addition to the contour plots of Fig. 9, we studied the convergence of the outer product rules in more detail for three specially chosen target points, namely \(\varvec{x} = \varvec{w}(s) + h \epsilon \varvec{B}(s)\) for \(s = 5.37\) and \(h \in \{2,1.1,1.01\}\). Note that with \(h=1\) we would arrive at a point on the fiber surface with surface normal \(\varvec{B}\). Therefore the targets lie near but not on the surface, at distances of \(\epsilon \), \(\epsilon /10\), and \(\epsilon /100\) respectively. The value \(s=5.37\) was chosen so that these targets are approximately on the square domain that we used for the targets of Fig. 9, and the choice of \(\varvec{B}(s)\) and not \(\varvec{N}(s)\) for the normal direction is so that the most distant target point does not approach another section of the fiber. In Fig. 10 we display the convergence curves, again by comparison to the known exact solution (zero). Unsurprisingly, we find that the reference Gauss/Trapezoid method converges more slowly than the splitting/splitting method, which in turn converges more slowly than the conformal mapping methods. Among these, we found little difference between the convergence behavior of a combination of cautious methods (Sinh/ISM) and a combination of aggressive methods (Tee/JAM). It is encouraging to see these conformal mapping techniques yield double precision accuracy with about \(n=60\) nodes in each direction, even for a target that is very close to the surface (within 1% of the fiber radius in the third column of Fig 10).

Fig. 9
figure 9

Contour plot of errors in the single-layer problem as the target point ranges over a square domain, punctured in three places by the slender fiber. The error bar reports \(\log _{10} (|\varvec{u}(\varvec{x})|)\) where \(\varvec{u}(\varvec{x})\) is the single-layer velocity induced at the target point \(\varvec{x}\) by a surface traction which equals the unit vector (\(\varvec{u}=\varvec{0}\) if the integration is done correctly). The columns of the figure show different quadrature strategies, while the rows of the table show differing densities of quadrature nodes; for example, \(n=32\) means that we use \(32\cdot 32\) nodes on each panel (there are always 16 panels)

Fig. 10
figure 10

Convergence plots for three target points located at varying distances d from the fiber surface. We plot the size of the computed single-layer potential integral (72) versus n, where n is the quadrature order in each direction (there are \(n^2\) quadrature nodes on each of the 16 panels). The reference quadrature, namely Gauss–Legendre in s and the trapezoid rule in t, converges slowly compared to the other methods. The splitting strategy, following Sect. 5.1 in s and Sect. 4.1 in t, is better than the reference quadrature but slower than the conformal mapping methods. We tested a combination of cautious methods (Sinh/ISM) and a combination of aggressive methods (Tee/JAM) and found that these had similar convergence rates for this problem, with the aggressive version being slightly faster to reach machine precision. The values of n considered here are the multiples of 8 up to 72

9 Conclusion

We surveyed a number of possible strategies for accelerating the quadrature of nearly singular integrals given knowledge of the location of the nearby singularity. We found that the splitting methods are less effective than the conformal maps. Among the many possible conformal maps, we can make suggestions for general use: we recommend the iterated sine map for periodic problems, the sinh map for aperiodic problems with complex singularity, and the quadratic map for aperiodic problems with real singularity. We demonstrated the effectiveness of the resulting quadratures on some nearly singular integrals from Stokes flow, namely the single-layer potential on the curved, two-dimensional surface of a slender fiber embedded in \({\mathbb {R}}^3\). In this integral equation context, our method was capable of achieving high accuracy with a modest number of function evaluations even for target points lying very close to the surface of integration. On the other hand, each target point currently requires a customized quadrature rule. We have not made a careful attempt to compare the costs of our computational method to other options in the BIE setting; we view the simplicity of our approach to be its principal advantage, and we do not expect our approach in its current form to be competitive with existing ones in terms of computational cost.

A natural priority for future work is to ask what additional improvements are possible when we have information about the nature of the nearest singularity in addition to its location. For example, the integrand \(f(x) = ((x-0.3)^2+\epsilon ^2)^p\) has the same domain of analyticity whether we take \(p=2.5\) or \(p=-2.5\), and accordingly the Gauss–Legendre quadrature errors will eventually decay at the same rate in either case. However, it is also true that Gauss–Legendre integration reaches machine precision much more quickly with the positive value of p. This statement can be made more precise with the aid of the theorems of Sect. 2, but it remains to make use of this information to optimize the choice of conformal map.

A second issue arises when many integrals over the same surface, but with varying singularities, need to be computed. The strategy described here would use customized quadratures for each of the singularities, resulting in a possibly expensive interpolation operation. It would be valuable to partition the complex plane into regions such that any singularity within a region can be integrated to prescribed accuracy with a precomputed reference set of quadrature nodes and weights.