1 Introduction

In integral equation methods, one of the long-standing challenges is the evaluation of layer potentials for targets points close to the source geometry. The integrals by which these layer potentials are computed are referred to as nearly singular, since the integrand is evaluated close to a singularity. This near singularity reduces the smoothness of the integrand, thereby severely reducing the accuracy of common quadrature rules such as Gauss–Legendre and the trapezoidal rule. To tackle this, many specialized quadrature methods have been proposed for the case of the domain being a one-dimensional line. For panel-based Gauss–Legendre quadrature in the plane, the high-order method of [1] (often referred to as the Helsing–Ojala method) is efficient and well-established. For the trapezoidal rule, which is exponentially convergent on a closed curve, several fixed-order methods have been proposed that reduce the impact of the near singularity by means of a local correction, see e.g. [2,3,4]. In addition, exponentially convergent near evaluation is available through the “globally compensated” quadrature method introduced in [1] and extended in [5].

Recently, the method of singularity swap quadrature (SSQ) was proposed in [6]. The method evaluates nearly singular line quadratures in two and three dimensions by first “swapping” the singularity from the target point in the plane to the preimage of the target point in the curve’s parametrization. After the swap, the integral can be evaluated using interpolatory quadrature with a monomial basis, following the method of [1]. This was shown to improve the accuracy for curved panels compared to [1], and allowed the method to be extended to line integrals in three dimensions.

The SSQ method was in [6] described for curves discretized using panel-based Gauss–Legendre quadrature. In this brief follow-up, we show how the method can be extended to closed curves in the plane that have been discretized using the trapezoidal rule, resulting in a global correction with exponential convergence. In doing so, we assume that the reader has at least some familiarity with the original method.

An essential component of our method is the use of trigonometric interpolation. For the logarithmic kernel, it can therefore be viewed as an extension of the product quadrature of Kress [7] to the nearly singular case. This was also noted by Bao et al. [8], who have proposed an extension of SSQ for the trapezoidal rule, in parallel with and independent of this work. Their approach starts with a Laurent expansion of the kernel on the unit circle, whereas the present work uses a Fourier expansion after singularity swapping, but they arrive at a method that appears equivalent to the present work for the logarithmic and Cauchy kernels. This corresponds to integrals \(I_L\) and \(I_1\) below; they do not consider higher order singularities. In addition, they study application of the method on non-periodic integrals and the Helmholtz equation.

2 Method

Since we are considering the problem in two dimensions, let us identify \(\mathbb {R}^2\) with \(\mathbb {C}\), and let \(\varGamma \) be a closed curve parametrized by a smooth function \(\gamma (t) = g_1(t) + i g_2(t)\), with \(t \in [0, 2\pi )\). Furthermore, let \(\sigma \) be a smooth function defined on \(\varGamma \), which we will refer to as the density. With some abuse of notation, we will denote \(\sigma (t)=\sigma (\gamma (t))\). For a point \(z\in \mathbb {C}\), typically lying close to \(\varGamma \), our integrals of interest are here

$$\begin{aligned} I_L&= I_L(z) := \oint _\varGamma \sigma (\tau ) \log \left| {\tau -z}\right| \left| {\textrm{d}\tau }\right| , \end{aligned}$$
(1)
$$\begin{aligned} I_m&= I_m(z) := \oint _\varGamma \frac{\sigma (\tau ) \textrm{d}\tau }{(\tau -z)^m} , \qquad m=1,2,\dots . \end{aligned}$$
(2)

Being periodic, these integrals are suitable for discretization using an N-point trapezoidal rule, due to its exponential convergence [9]. The quadrature nodes are then \(\gamma (t_j)\), \(j=0,\dots ,N-1\), with \(t_j = 2\pi j/N\) and the quadrature weights \(w_j=2\pi /N\). We will now outline how these discretized integrals can be evaluated using SSQ. Throughout, we will assume that we only have access to the discrete values of the functions \(\sigma \), \(\gamma \) and \(\gamma '\) at the nodes, and not to their analytic definitions.

2.1 The Cauchy integral

We begin with the simplest (and perhaps most common) case, the Cauchy integral,

$$\begin{aligned} I_1(z)&= \oint _\varGamma \frac{\sigma (\tau ) \textrm{d}\tau }{\tau - z} = \int _0^{2\pi } \frac{\sigma (t)\gamma '(t)\textrm{d}t}{\gamma (t)-z} . \end{aligned}$$
(3)

Let us now assume that z is close to \(\varGamma \), which corresponds to the integrand in (3) having a singularity close to the real line at the preimage \(t^\star \in \mathbb {C}\) such that

$$\begin{aligned} \gamma (t^\star ) = z, \end{aligned}$$
(4)

under the complexification of \(\gamma \). In Klinteberg et al. [10] and Barnett [11], it is shown that this significantly reduces the convergence rate of the trapezoidal rule, with the error being proportional to \(e^{-N |{\text {Im}}{t^\star }|}\). In order to reduce the error for small \({\text {Im}}t^\star \), we will now show how to evaluate \(I_1(z)\) using SSQ.

Step one is to find the preimage \(t^\star \) that satisfies (4). Geometrically, the complexification of \(\gamma \) corresponds to following curved lines that extend from \(\varGamma \) in an approximately normal direction (see e.g. [10, fig. 4]). There are many different methods available for finding \(t^\star \). Noteworthy examples include the local and global approximations used together with Newton’s method in [10], and the novel quadrature-based method introduced in [8]. Studying the merits of the various approaches is beyond the scope of this work. However, we note that the computationally most efficient method probably is to use a local expansion, such as a truncated Taylor expansion of \(\gamma \) at each grid point.

For simplicity we will here use a global approximation for finding \(t^\star \). This corresponds to Newton’s method applied to a Fourier expansion of \(\gamma \). We apply a Fast Fourier Transform (FFT) to the node values \(\gamma (t_0),\dots ,\gamma (t_{N-1})\) to get the truncated Fourier series

$$\begin{aligned} \gamma (t)&\approx \sum _{k=-K}^{K} \hat{\gamma }_k e^{ikt}, \end{aligned}$$
(5)
$$\begin{aligned} \gamma '(t)&\approx \sum _{k=-K}^K ik\hat{\gamma }_k e^{ikt}, \end{aligned}$$
(6)

where \(K=\frac{N-1}{2}\), assuming N odd, and \(\hat{\gamma }_k\) are the Fourier coefficients of \(\gamma \). This has a one-time cost of \(\mathcal {O}(N\log N)\), for a given discretization. Substituting this expansion into (4), we can find \(t^\star \) at an \(\mathcal {O}(N)\) cost using Newton’s method (assuming \(\mathcal {O}(1)\) iterations to convergence). A good initial guess for \(t^\star \) is the corresponding t-value of the grid point on \(\varGamma \) that is closest to z,

$$\begin{aligned} t^\star \approx \underset{t_j}{{\text {arg}}\,{\text {min}}} \left| {\gamma (t_j) - z}\right| . \end{aligned}$$
(7)

We assume that \(\varGamma \) is parametrized in a counter-clockwise direction, so that \({\text {Im}}t^\star > 0\) implies z interior to \(\varGamma \), and vice versa.

Step two is to swap out the singularity using a function that regularizes the integrand, and leads to an integrable singularity. Contrary to the Gauss–Legendre panel case, we can not regularize the denominator \(\left( \gamma (t)-\gamma (t^\star )\right) ^{-1}\) using the factor \((t-t^\star )\), as the latter is not periodic. Instead, we use \((e^{it}-e^{it^\star })\), which corresponds to swapping the singularity to a point close to the unit circle (this will be evident shortly). Then,

$$\begin{aligned} I_1(z)&= \int _0^{2\pi } \underbrace{ \frac{\sigma (t)\gamma '(t) \left( e^{it}-e^{it^\star } \right) }{\gamma (t)-\gamma (t^\star )} }_{f(t,t^\star )} \frac{ \textrm{d}t}{e^{it}-e^{it^\star }} . \end{aligned}$$
(8)

The function f is now the original integrand with the singularity at \(t^\star \) removed,

$$\begin{aligned} \lim _{t\rightarrow t^\star }f(t,t^\star )\left( \gamma (t)-\gamma (t^\star ) \right) =0, \end{aligned}$$
(9)

and we expect f to be smooth. Since its values are known at the equidistant nodes \(t_0, \dots , t_{N-1}\), we use an FFT to compute its truncated Fourier series (at an \(\mathcal {O}(N\log N)\) cost), such that

$$\begin{aligned} I_1(z)&\approx \sum _{k=-K}^K \hat{f}_k(t^\star ) \underbrace{ \int _0^{2\pi } \frac{ e^{ikt} \textrm{d}t}{e^{it}-e^{it^\star }} }_{p_k(t^\star )} = {\varvec{\hat{f}}}\cdot {\varvec{p}} . \end{aligned}$$
(10)

This can be interpreted as our integral being approximated by an interpolatory quadrature on the unit circle, which we can evaluate to high accuracy as long as the coefficients \(\hat{f}_k\) decay sufficiently fast. The integrals \(p_k\) can be evaluated analytically (at an \(\mathcal {O}(N)\) cost) by rewriting them as contour integrals on the unit circle S. Let

$$\begin{aligned} \xi (t)&= e^{it}, \end{aligned}$$
(11)
$$\begin{aligned} \xi '(t)&= ie^{it}, \end{aligned}$$
(12)
$$\begin{aligned} \zeta&= e^{it^\star } . \end{aligned}$$
(13)

Then \(e^{ikt} = \xi (t)^k\) and

$$\begin{aligned} p_k(t^\star )&= \frac{1}{i} \int _0^{2\pi } \frac{ e^{i(k-1)t} i e^{it} \textrm{d}t}{e^{it}-e^{it^\star }} \end{aligned}$$
(14)
$$\begin{aligned}&= \frac{1}{i} \int _0^{2\pi } \frac{\xi (t)^{k-1} \xi '(t) \textrm{d}t}{\xi (t) - \zeta } \end{aligned}$$
(15)
$$\begin{aligned}&= \frac{1}{i} \oint _S \frac{\xi ^{k-1} \textrm{d}\xi }{\xi -\zeta } . \end{aligned}$$
(16)

We can evaluate this integral using the residue theorem. If \(|\zeta |<1\) (or equivalently \({\text {Im}}t^\star >0\), corresponding to z being an interior point), then the integrand has a simple pole at \(\zeta \), with residue

$$\begin{aligned} {\text {Res}}\left[ \frac{\xi ^{k-1}}{\xi -\zeta }, \zeta \right] = \zeta ^{k-1}, \quad \text{ if } |\zeta |<1. \end{aligned}$$
(17)

In addition, if \(k \le 0\), then the integrand has a pole of order \(1-k\) at the origin, with residue

$$\begin{aligned} {\text {Res}}\left[ \frac{\xi ^{k-1}}{\xi -\zeta }, 0\right] = -\zeta ^{k-1}, \quad \text{ if } k \le 0. \end{aligned}$$
(18)

The two residues cancel for \(k \le 0\) and \(|\zeta |<1\) (i.e. \({\text {Im}}t^\star >0\)), so we get

$$\begin{aligned} p_k(t^\star ) = 2\pi e^{i(k-1)t^\star } {\left\{ \begin{array}{ll} 1, &{} \text {if } {\text {Im}}t^\star >0 \text { and } k \ge 1,\\ -1, &{} \text {if } {\text {Im}}t^\star <0 \text { and } k \le 0,\\ 0, &{} \text {otherwise}. \end{array}\right. } \end{aligned}$$
(19)

This completes the method, which has a total cost of \(\mathcal {O}(N + N\log N)\) per target point.

The method can perhaps be understood in terms of Fourier coefficients. The accuracy of the trapezoidal rule depends on the regularity of the integrand, which in term is reflected in the decay of the Fourier coefficients of the integrand. As seen in Fig. 1, these appear to decay as \(e^{-|k{\text {Im}}t^\star |}\). In contrast, the coefficients \(\hat{f}_k(t^\star )\) decay rapidly and with a rate that stays nearly constant as \(z\rightarrow \varGamma \).

Fig. 1
figure 1

Example results for the Cauchy integral on the starfish geometry and density from Sect. 3.2, for two different \(t^\star \). The Fourier coefficients of the integrand in (3) decay approximately as \(\rho ^{-|k|}\), where \(\rho =e^{|{\text {Im}}t_0|}\), meaning that the decay rate worsens as \(z\rightarrow \varGamma \). On the other hand, the coefficients of the regularized function f in (10) decay rapidly, with a rate that stays approximately constant as \(z\rightarrow \varGamma \). Note that F in the legend denotes Fourier coefficients, i.e. \(F[f](k)=\hat{f}_k\)

A weakness of the method is that in order to find \(t^\star \), one must evaluate the analytic continuation of \(\gamma \) using a truncated Fourier series, which naturally is most accurate close to \(\varGamma \). For z far away from \(\varGamma \) the rootfinding process will struggle, and the accuracy of SSQ can in some cases deteriorate. However, this typically happens at ranges where the base trapezoidal rule is sufficiently accurate by a wide margin, and can be avoided by a coarse filter that only tries to find \(t^\star \) for points that are within a cutoff distance from \(\varGamma \) based on the grid spacing h.

Remark 1

The reader may note that one could in fact apply a variant of the Helsing–Ojala quadrature directly to (3). First solve the interpolation problem

$$\begin{aligned} \sum _{k=-K}^K c_k \tau _i^k = \sigma _i, \quad i=0,\dots ,N-1, \end{aligned}$$
(20)

where \(\tau _i=\gamma (t_i)\) and \(\sigma _i=\sigma (t_i)\). Then evaluate the layer potential as

$$\begin{aligned} I_1(z) = \sum _{k=-K}^K c_k \oint _\varGamma \frac{\tau ^k \textrm{d}\tau }{\tau - z}, \end{aligned}$$
(21)

where the integrals can be exactly evaluated using residue calculus, just as is done above for the unit circle. This will in fact work perfectly fine for some geometries (the unit circle in particular), but the interpolation problem can be severely ill-conditioned in a way that is hard to control, since it depends on the geometry itself. This could potentially be investigated further, but that is deemed beyond the scope of this work.

2.2 Higher order integrals

For higher order integrals (\(m > 1\)), we have

$$\begin{aligned} I_m(z)&= \oint _\varGamma \frac{\sigma (\tau ) \textrm{d}\tau }{\left( \tau - z \right) ^m} = \int _0^{2\pi } \frac{\sigma (t)\gamma '(t)\textrm{d}t}{(\gamma (t)-z)^m} , \quad m \in \mathbb {N}^*. \end{aligned}$$
(22)

Evaluating this integral with SSQ is completely analogous to the case when \(m=1\), with the difference that we instead regularize with \((e^{it}-e^{it^\star })^m\), such that

$$\begin{aligned} f(t, t^\star ) = \sigma (t)\gamma '(t) \left( \frac{e^{it}-e^{it^\star }}{\gamma (t)-\gamma (t^\star )} \right) ^m . \end{aligned}$$
(23)

It is worth noting that the Fourier coefficients \(\hat{f}_k\) of this definition of f decay like those in Fig. 1, with a rate that is independent of m. Once we have them, we next need expressions for the integrals

$$\begin{aligned} p_k^m(t^\star ) = \frac{1}{i} \oint _S \frac{\xi ^{k-1} \textrm{d}\xi }{\left( \xi -\zeta \right) ^m} . \end{aligned}$$
(24)

Evaluating the residues in the same way as for the \(m=1\) case, we get the general expression for integer \(m \ge 1\),

$$\begin{aligned} p_k^m(t^\star ) = 2\pi \frac{\prod _{j=1}^{m-1} (k-j)}{(m-1)!} e^{i(k-m)t^\star } {\left\{ \begin{array}{ll} 1, &{} \text {if } {\text {Im}}t^\star >0 \text { and } k \ge m,\\ -1, &{} \text {if } {\text {Im}}t^\star <0 \text { and } k \le 0,\\ 0, &{} \text {otherwise}. \end{array}\right. } \end{aligned}$$
(25)

2.3 Log kernel

Consider now the log integral, also known as the Laplace single-layer potential, for a real-valued density \(\sigma \),

$$\begin{aligned} I_L(z)&= \oint _\varGamma \sigma (\tau ) \log |\tau -z|\,|\textrm{d}\tau | \end{aligned}$$
(26)
$$\begin{aligned}&= \int _0^{2\pi } \underbrace{\sigma (t) |\gamma '(t)|}_{f(t)} \log |\gamma (t)-z|\textrm{d}t, \end{aligned}$$
(27)

where f is assumed to be a smooth function. Proceeding in a similar fashion as before, we find \(t^\star \) and separate the integral as

$$\begin{aligned} I_L(z) = \int _0^{2\pi } f(t) \left( \log |\gamma (t)-z| - \log |e^{it}-e^{it^\star }| \right) \textrm{d}t + \int _0^{2\pi } f(t)\log |e^{it}-e^{it^\star }| \textrm{d}t . \end{aligned}$$
(28)

The first integral is now regularized, and can be evaluated using the trapezoidal rule. The second integral we rewrite using a truncated Fourier series and the relation \(\log |r|={\text {Re}}\log r\), such that we can evaluate it using SSQ,

$$\begin{aligned} \int _0^{2\pi } f(t)\log |e^{it}-e^{it^\star }| \textrm{d}t \approx {\text {Re}}\sum _{k=-K}^{K} \hat{f}_k \underbrace{ \int _0^{2\pi } e^{ikt}\log (e^{it}-e^{it^\star })\textrm{d}t }_{q_k(t^\star )} = {\varvec{\hat{f}}} \cdot {\varvec{q}}. \end{aligned}$$
(29)

Just as with \(p_k\), the integrals \(q_k\) can be transformed into integrals on the unit circle,

$$\begin{aligned} q_k(t^\star ) = \frac{1}{i} \int _S \xi ^{k-1}\log (\xi -\zeta )\textrm{d}\xi . \end{aligned}$$
(30)

Note that this is not necessarily a closed contour integral, as there is a branch cut in the complex logarithm that needs to be taken into account. We will now show to evaluate \(q_k(t^\star )\) depending on the sign of \({\text {Im}}t^\star \).

2.3.1 \({\text {Im}}t^\star <0\)

Beginning with the case \(|\zeta |>1\) (or \({\text {Im}}t^\star <0\)), we note that \(\xi -\zeta \ne 0\) on the entire unit disc. We can therefore choose the branch cut of the complex logarithm such that it never intersects the unit circle. This choice allows us to evaluate the integral in (30) as a contour integral on the unit circle, with a possible pole of order \(1-k\) at the origin,

$$\begin{aligned} \frac{1}{2\pi i} \oint _S \xi ^{k-1}\log (\xi -\zeta )\textrm{d}\xi = {\text {Res}}\left[ \xi ^{k-1}\log (\xi -\zeta ), 0\right] = {\left\{ \begin{array}{ll} \zeta ^{k} / k &{} \text { if } k < 0, \\ \log (-\zeta ) &{} \text { if } k=0, \\ 0 &{} \text { if } k>0. \end{array}\right. } \end{aligned}$$
(31)

Since \(\hat{f}_0 \in \mathbb {R}\), we only need the real part of \(q_k\) at \(k=0\). We then have that \({\varvec{q}}\) is for \({\text {Im}}t^\star <0\) computed as

$$\begin{aligned} q_k(t^\star ) = 2\pi {\left\{ \begin{array}{ll} \frac{1}{k} e^{i k t^\star } &{} \text { if } k<0, \\ -{\text {Im}}t^\star &{} \text { if } k=0, \\ 0 &{} \text { if } k>0 . \end{array}\right. } \end{aligned}$$
(32)

2.3.2 \({\text {Im}}t^\star >0\)

We are now left with the case \(|\zeta |<1\) (or \({\text {Im}}t^\star >0\)). Here, \(\xi -\zeta =0\) at a point on the unit disk, so the integral of \(\log (\xi -\zeta )\) on the unit circle must intersect the branch cut in the \(\xi \) plane going from \(\zeta \) to infinity. We choose the branch cut such that it passes through the point \(\xi =1\). We define \(1^+\) and \(1^-\) to be the limits approaching 1 from above and below in the plane, such that

$$\begin{aligned} \log (1^--\zeta )=\log (1^+-\zeta )+2\pi i. \end{aligned}$$
(33)

Then the integral on the unit circle S is,

$$\begin{aligned} q_k(t^\star ) = \frac{1}{i} \int _{1^+}^{1^-} \xi ^{k-1}\log (\xi -\zeta )\textrm{d}\xi . \end{aligned}$$
(34)

In order to evaluate this, we let C be the closed contour following the unit circle from \(1^+\) to \(1^-\), then going from \(1^-\) to \(\zeta \) on the negative side of the branch cut, and then going from \(\zeta \) to \(1^+\) on the positive side of the branch cut,

$$\begin{aligned} \oint _C = \int _{1^+}^{1^-} + \int _{1^-}^{\zeta } + \int _{\zeta }^{1^+} . \end{aligned}$$
(35)

The integrand is analytic inside C, except for a pole of order \(1-k\) at the origin when \(k \le 0\), with residue given by (31). The integrals along the branch cut are

$$\begin{aligned} \int _{1^-}^{\zeta } + \int _{\zeta }^{1^+}&= \int _{1}^{\zeta } \xi ^{k-1} \left( \log (\xi -\zeta ) + 2\pi i \right) \textrm{d}\xi + \int _{\zeta }^{1} \xi ^{k-1}\log (\xi -\zeta )\textrm{d}\xi \end{aligned}$$
(36)
$$\begin{aligned}&=2\pi i{\left\{ \begin{array}{ll} \log (\zeta ) &{} \text { if } k=0,\\ (\zeta ^k - 1) / k &{} \text { otherwise}. \end{array}\right. } \end{aligned}$$
(37)

Combining the results, and just as before keeping only the real part of the \(k=0\) integral, we arrive at the final results for \({\text {Im}}t^\star >0\),

$$\begin{aligned} q_k(t^\star ) = \frac{2\pi }{k} {\left\{ \begin{array}{ll} 1 &{} \text { if } k<0, \\ 0 &{} \text { if } k=0, \\ (1 - e^{i k t^\star }) &{} \text { if } k>0 . \end{array}\right. } \end{aligned}$$
(38)

2.4 Computing weights at linear cost

We sometimes wish to evaluate our target integrals (1) and (2) multiple times for the same density \(\sigma \). Instead of computing the Fourier components \(\hat{f}_k(t^\star )\) every time, we can then compute the corresponding quadrature weights \(w_j^m(t^\star )\) once and reuse them later. For the power-law kernel (2), this corresponds to

$$\begin{aligned} I_m(z) \approx \sum _{k=-K}^K \hat{f}_k(t^\star ) p_k^m(t^\star ) = \sum _{j=0}^{N-1} f(t_j, t^\star ) w_j^m(t^\star ) . \end{aligned}$$
(39)

We expand the discrete Fourier transform, where \(\omega = e^{-2\pi i/N}\),

$$\begin{aligned} \sum _{j=0}^{N-1} f(t_j, t^\star ) w_j^m(t^\star ) = \frac{1}{N} \sum _{k=-K}^K \sum _{j=0}^{N-1} f(t_j, t^\star ) \omega ^{jk} p_k^m(t^\star ) . \end{aligned}$$
(40)

Rearranging, we can identify the weights as

$$\begin{aligned} w_j^m(t^\star ) = \frac{1}{N} \sum _{k=-K}^K p_k^m(t^\star ) \omega ^{jk}, \end{aligned}$$
(41)

which can be computed using an FFT. The process is completely analogous for the log kernel (1). However, for the power-law kernel it is possible express the weights in closed form. Substituting \(p_k^m\) using (25) and inserting \(\omega ^{jk}=e^{-ikt_j}\),

$$\begin{aligned} w_j^m(t^\star ) = \frac{2\pi }{N} \frac{e^{-imt^\star }}{(m-1)!} \sum _{k=-K}^K \prod _{j=1}^{m-1} (k-j) e^{ik(t^\star -t_j)} {\left\{ \begin{array}{ll} 1, &{} \text {if } {\text {Im}}t^\star >0 \text { and } k \ge m,\\ -1, &{} \text {if } {\text {Im}}t^\star <0 \text { and } k \le 0,\\ 0, &{} \text {otherwise}. \end{array}\right. } \end{aligned}$$
(42)

We can write this more compactly as

$$\begin{aligned} w_j^m(t^\star ) = \frac{2\pi }{N} \frac{e^{-imt^\star }}{(m-1)!} S_m\left( e^{i(t^\star -t_j)}\right) , \end{aligned}$$
(43)

where \(S_m\) is defined as follows: Letting \(r= e^{i(t^\star -t_j)}\), for \({\text {Im}}t^\star >0\)

$$\begin{aligned} S_m(r) = \sum _{k=m}^K \prod _{s=1}^{m-1} (k-s) r^k&= \sum _{k=m}^K r^m\frac{\textrm{d}^{m-1}}{\textrm{d}r^{m-1}} r^{k-1} \end{aligned}$$
(44)
$$\begin{aligned}&= r^m \frac{\textrm{d}^{m-1}}{\textrm{d}r^{m-1}} \sum _{k=m}^K r^{k-1} \end{aligned}$$
(45)
$$\begin{aligned}&= r^m \frac{\textrm{d}^{m-1}}{\textrm{d}r^{m-1}} \frac{r^K - r^{m-1}}{r-1} , \end{aligned}$$
(46)

where the last step is given by the closed form of a geometric sum. Similarly, for \({\text {Im}}t^\star <0\) we have that

$$\begin{aligned} S_m(r) = -\sum _{k=-K}^0 \prod _{s=1}^{m-1} (k-s) r^k&= r^m \frac{\textrm{d}^{m-1}}{\textrm{d}r^{m-1}} \frac{r^{-(K+1)} - 1}{r-1} . \end{aligned}$$
(47)

With (43) together with (46) and (47), we thus have compact closed-form expressions that allow us to compute \(w_j^m\) and evaluate (2) at an \(\mathcal {O}(N)\) cost. They can be evaluated and implemented for any m, preferably using a symbolic toolbox (see e.g. the accompanying code [12]). An equivalent closed-form expression was also derived in [8] for the case \(m=1\).

The above simplification to a closed form expression is unfortunately not possible for the log kernel, where the summation terms are of the form \(r^k/k\). Instead we still have to rely on using the FFT.

3 Experiments

In order to demonstrate the method outlined above, we here report experimental results for the by now well-known “starfish” geometry \(\gamma (t) = (1 + 0.3 \cos 5t) e^{it}\), \(\gamma \in [0, 2\pi )\). The method has been implemented in Julia [13], and is available online as open source on GitHub together with the scripts that produce the numerical in this manuscript [12].

3.1 Laplace double-layer potential

As a first demonstration, we reuse the Laplace test problem from [6]. That is, we evaluate the solution to the Laplace equation \(\varDelta u=0\) in the domain bounded by \(\varGamma \), with boundary condition \(u=u_e(z)=\log \left| {3+3i-z}\right| \). The solution to this problem can be represented using the Laplace double-layer potential (DLP) \(u(z)={\text {Im}}I_1(z)\), which leads to a second-kind integral equation in the real-valued density \(\sigma \). Once this integral equation is solved, the layer potential u(z) can be evaluated in the domain using quadrature, and compared to the exact solution \(u_e(z)\).

Fig. 2
figure 2

Error in the Laplace double-layer potential on a domain with \(N=400\) points on the boundary, comparison between the trapezoidal rule and SSQ

Fig. 3
figure 3

Error in the Laplace double-layer potential on a domain with \(N=150\) points on the boundary, comparison between the trapezoidal rule and SSQ

In Fig 2 we show results for the Laplace DLP when \(\varGamma \) is discretized using \(N=400\) points that are equidistant in the parametrization t. This results in a density that is resolved to machine precision, as measured by the decay of its Fourier coefficients. The layer potential is evaluated using the trapezoidal rule on a \(400 \times 400\) grid inside the domain, but for points close to the boundary we also evaluate using SSQ and compare the results. As expected, evaluation using the trapezoidal rule leads to errors on the level of machine precision in the interior of the domain and \(\mathcal {O}(1)\) errors near the boundary, while SSQ is accurate at all points where it is used. However, full machine precision accuracy is not recovered; the maximum error in SSQ close to the boundary is \(\mathcal {O}(10^{-13})\). The reason for this loss of precision is not known, but it is in line with the results when applying SSQ to Gauss–Legendre panels [6].

In Fig. 3, we repeat the above experiment with \(N=150\) points, which results in the density \(\sigma \) not being fully resolved. Here the far-field error is \(\mathcal {O}(10^{-10})\), but SSQ is not able to recover more than five digits of accuracy near the convex regions with high curvature. This is also a phenomenon whose exact cause is not known, but which is in line with the original SSQ algorithm.

3.2 Convergence

Fig. 4
figure 4

Convergence of the trapezoidal rule and SSQ for the log kernel and \(m=1,2,3\), measured as the maximum error on a set of target points such that \({\text {Im}}t^\star =d\). Left column shows interior points, right column shows exterior points

For a more systematic investigation of the method, we create the following setup. Let \(\varGamma \) be the starfish domain from the previous section, and discretize it using N points. We then create 100 evaluation points as \(z=\gamma (t^\star )\), where \({\text {Re}}t^\star \in [0, 2\pi )\) and \({\text {Im}}t^\star =d\). In order to test points both interior and exterior to \(\varGamma \), we use \(d = \{\pm 0.01, \pm 0.02, \pm 0.04\}\). For a range of N, we then compute the integrals \(I_L, I_1, I_2, I_3\) using both trapezoidal and singularity swap quadrature, and for each N save the maximum error across all z. The density \(\sigma \) and reference solution used are:

  • For \(I_L\) we use the \(\sigma (t) = g_1(t)g_2(t)\) and compute the reference solution using adaptive Gauss-Kronrod quadrature (as implemented in the Julia package QuadGK [14]).

  • For \(I_m\) and interior points (\({\text {Im}}t^\star >0\)) we use \(\sigma (\tau )=\tau ^3 + \tau \), with reference solution \(I_m(z)=2 \pi i \sigma ^{(m)}(z)/(m-1)!\).

  • For \(I_m\) and exterior points (\({\text {Im}}t^\star <0\)) we use \(\sigma (\tau )=\tau ^{-1}\), with reference solution \(I_m(z)=2\pi i z^{-m}\).

The density \(\sigma \) is a smooth function in all of the above cases. For \(I_L\) and for \(I_m\) with interior points it is fully resolved when N is very small. For \(I_m\) with exterior points it is fully resolved when \(N\approx 200\).

The results of this investigation are shown in Fig. 4. As expected, the convergence rate of the original trapezoidal rule depends strongly on the distance to the boundary. In contrast, the convergence rate for SSQ should not depend on the distance to the boundary if the singularity is perfectly swapped. Here, this is indeed the case for the exterior points, where the rate of convergence reflects the resolution of the smooth integrand. There appears to be a weak dependence between the rate and the distance for interior points, but that could be an effect of the chosen test problem. More concerning is that lowest attainable error appears to increase for smaller d when \(m>1\). It is unclear what causes this effect, although in experiments there appears to be an upper bound to the error growth.

4 Conclusions

We have extended the singularity swap quadrature (SSQ, [6]) method to closed 2D curves discretized using the trapezoidal rule. This extension builds on the simple observation that interpolatory quadrature can be used on a periodic integrand if the problem is first swapped to the unit circle. The method relies on representing quantities as Fourier expansions, and relies on the fast Fourier transform (FFT) for precomputation on the geometry and, in case of the logarithmic kernel, for evaluation. For the power-law kernel, closed form expressions are derived such that the FFT is not needed at evaluation time.

The method is accurate, exponentially convergent, and relatively simple to implement (and we provide source code). The fact that the cost is \(\mathcal {O}(N)\) (for power-law) or \(\mathcal {O}(N \log N)\) (for log) per target point makes the method unfeasible for large problems, but that is a consequence of the underlying trapezoidal rule being global (and exponentially convergent). For maximum efficiency, composite Gauss–Legendre quadrature with the original SSQ likely remains the better option.

In the original SSQ paper, it was shown that the method can be applied to line integrals in 3D by finding the singularity preimage through analytic continuation of the squared-distance function. The same technique can be applied to the present method, for closed curves in three dimensions. What remains is analytical evaluation of the integrals of the interpolatory quadrature. This is straightforward for the logarithmic kernel, but the integrals corresponding to power-law kernels are more challenging. Deriving expressions for these is currently work in progress.