{'fraction_non_alphanumeric': 0.04081272084805654, 'fraction_numerical': 0.013825088339222615, 'mean_word_length': 4.859472049689441, 'pattern_counts': {'":': 0, '<': 1, '<?xml version=': 0, '>': 1, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
High-order difference and pseudospectral methods for discontinuous problems

High order finite-difference or spectral methods are typically problematic in approximating a function with a jump discontinuity. Some common remedies come with a cost in accuracy near discontinuities, or in computational cost, or in complexity of implementation. However, for certain classes of problems involving piecewise analytic functions, the jump in the function and its derivatives are known or easy to compute. We show that high-order or spectral accuracy can then be recovered by simply adding to the Lagrange interpolation formula a linear combination of the jumps. Discretizations developed for smooth problems are thus easily extended to nonsmooth problems. Furthermore, in the context of one-dimensional finite-difference or pseudospectral discretizations, numerical integration and differentiation amount to matrix multiplication. We construct the matrices for such operations, in the presence of known discontinuities, by operating on the corrected Lagrange formula. In a method-of-lines framework, this provides a simple and efficient way to obtain solutions with moving discontinuities to evolution partial differential equations.


I. INTRODUCTION
Finite-difference and spectral methods are commonly used for numerically solving ordinary or partial differential equations. Such methods are highly efficient and accurate when the solution is smooth. When discontinuities are present, however, oscillations due to Gibbs phenomena arise that contaminate the solution and cause loss of convergence. Several workarounds exist and are commonly used to suppress or avoid these phenomena. However, they typically come at a cost. For piecewise analytic solutions, domain decomposition avoids the point of discontinuity, but can make implementation complicated and computation expensive, especially in time-dependent problems with moving discontinuities, as remapping of coordinates or regridding is required at each time step. For finite-difference methods, lowering the order of approximation globally or near the discontinuity reduces oscillations but sacrifices accuracy. For spectral methods, applying filtering techniques can improve the convergence properties of the solution away from discontinuities, but the accuracy near discontinuities remains poor [11]. Post-processing of the resulting oscillatory data using direct [26][27][28][29][30][31][32] and inverse [36] Gegenbauer reconstruction can recover spectrally convergent non-oscillatory solutions. This approach has been successfully used in several contexts, such as magnetic resonance imaging [2][3][4] and evolving nonlinear hyperbolic systems [49]. Nevertheless, reconstruction techniques are also computationally expensive and subject to complications. For instance, the Gegenbauer transformation matrices become ill-conditioned for high-degree polynomials [36]. In addition, the reconstruction must be constrained to use a sufficiently small ratio of order to truncation, which can impair its effectiveness in certain applications [12].
In many problems, however, the jumps of the function and its derivatives at the discontinuity are known, or easily computed. One may thus construct scheme that exploit this information. Such ideas have been introduced by Krylov [40], Lanczos [41] and Eckhoff [19][20][21][22][23]43] in the context of Fourier methods (see also [1,7,8,37,44,45]) and by Lipman and Levin [42] in the context of finitedifference methods. In what follows, we consider nodal methods based on Lagrange interpolation. We show that adding a linear combination of the jumps to the Lagrange interpolation formula can account for the presence of the discontinuity and restore the original accuracy. Finite-difference and pseudospectral scheme developed for smooth problems, outlined in §2, can thus be very easily extended to discontinuous or nonsmooth problems, outlined in §3. The merits and possible extensions of this method are summarized in §4.

II. SMOOTH FUNCTIONS
As a primer for the discontinuous methods that follow in §3, we first review the Lagrange interpolation method for sufficiently differentiable functions and its role in numerical integration and differentiation.
A. Interpolation The collocation polynomial of degree N : that interpolates the given N + 1 nodes, is unique and determined by solving the linear system of algebraic con- for the coefficients c j . The solution may be written in the Lagrange form are the Lagrange basis polynomials. By construction, the basis polynomials satisfy the conditions π j (x i ) = δ ij , with δ ij denoting the Kronecker symbol, so that the polynomial (2.3) satisfies the conditions (2.2). The nodes have so far been left unspecified. For polynomials of low degree, equidistant nodes are commonly used. For polynomials of high degree, however, this choice leads to oscillations due to Runge's phenomenon and the interpolation process diverges. The oscillations can be minimized by using nodes that are distributed more densely towards the edges of the interval. For instance, interpolation on Chebyshev-Gauss-Lobatto nodes converges uniformly for every absolutely continuous function [9]. These nodes are the extrema of a Chebyshev polynomial of degree N . If all the function derivatives are bounded on the interval x ∈ [a, b], Chebyshev interpolation always has the property of infinite order (i.e. exponential) convergence. Spectral methods are often introduced and implemented as expansions in terms of orthogonal basis functions. However, as the use of spectral expansions for most practical purposes requires the introduction of a finite grid, one may question the necessity of special basis functions. Indeed, the construction of a spectral expansion, with coefficients determined via collocation at the nodes x i , can be replaced by a global interpolating polynomial if the nodes are the abscissas of a Gaussian quadrature (typically the simple roots or extrema of a Jacobi polynomial, of which Chebyshev is a special case) associated with the basis functions [9,11]. This pseudospectral approach allows numerical operations to be performed in physical, rather than spectral, space without compromise in convergence order. This property can be exploited in numerical codes, offering the flexibility of switching between finite-difference and pseudospectral methods by simply shifting the location of the nodes (e.g. changing from Eq. (2.5) to (2.6)). We shall make use of this flexibility in §3.

B. Differentiation
The n-th derivative f (n) = d n f /dx n of f may be approximated by differentiating the Lagrange interpolating polynomial (2.3) n times. This operation may be written as matrix multiplication: is the n th derivative matrix, constructed from the n th derivative of the basis polynomials (2.4) with respect to x = x i . Note that, for n = 0, the above expression gives the identity matrix, D ij = δ ij , as expected. For fixed grids, the above matrices can be pre-computed and stored in memory. The floating-point precision of Eq. (2.7) can be improved using a "negative sum trick" [5] which ensures that the derivative matrices vanish upon multiplying a constant vector. Fast methods for computing the matrices have been developed [9,24,25,47,50] and implemented in computational libraries and software. For instance, the Mathematica command uses Fornberg's algorithm to compute the n-th derivative matrix Dn, given the list of grid-points X={x0,x1,x2,...,xN}, at the desired order of approximation m. The latter is an integer satisfying n ≤ m ≤ N that yields a finite-difference error O(∆x m ), where ∆x is the maximum local grid spacing.
A choice m < N yields a composite derivative matrix of size N + 1 constructed from centred (m + 1)-point stencils. For example, the choice n = 1 and m = 2 computes 1 st derivatives of 2 nd order polynomials (given by Eq. (2.3) for N = 2) to construct the centred finitedifference approximation The entire domain [a, b] is covered in a composite fashion (that is, the above formula is applied repeatedly at each node x i using only nearest neighbour points). This gives the centred-derivative matrix for all i = 1, . . . , N − 1, while one-sided finite differences are used at the end-points i = 0, N . A single-domain pseudospectral derivative matrix can be obtained by choosing the maximal value m = N and selecting nodes x i that coincide with the abscissas of a Gaussian quadrature, such as the Chebyshev-Gauss-Lobatto nodes (2.6). It is interesting to note that the pseudospectral derivative matrices obtained from this (single-domain) application of Eq. (2.8) are the infiniteorder limit of the finite-difference derivative matrices obtained by (composite) application of Eq. (2.8) [9]. This adds further support to the view of pseudospectral methods as a special case of finite-difference methods.

C. Integration
Quadrature, or numerical integration, amounts to approximating the definite integral of a given function or, equivalently, integrating an approximation to the function. Integrating the approximation (2.3) gives a quadrature rule in the form of a weighted sum of function evaluations: are the relevant weights of integration. Various wellknown quadrature rules stem from the above formulae. For instance, using interpolation of 2 or 3 equidistant nodes yields the composite trapezoidal or Simpson rule respectively. For the Chebyshev-Gauss-Lobatto nodes (2.6), the above procedure yields the homonymous quadrature formula, which is exponentialy convergent for smooth integrands.

III. NON-SMOOTH FUNCTIONS
In general, a jump discontinuity in a function or its derivatives cannot be accurately approximated by a single polynomial. As mentioned earlier, a common workaround is to divide the computational domain so that any discontinuities are located at the boundary between domains. In what follows, we explore a simpler approach that does not require domain decomposition but uses the location and magnitude of the discontinuity if known. We will illustrate that incorporating discontinuities into the approximating function instead of avoiding them offers flexibility and ease of programming without loss of accuracy.
A. Interpolation Lagrange interpolation method outlined in §2 requires sufficient differentiability, that is, k ≥ N + 1. However, non-differentiable solutions appear in multiple physical contexts, and their approximation necessitates modification of the above methods. If f is a piecewise-C N +1 function and its jump discontinuities are known, then a simple generalization of the Lagrange method to any k ≥ −1 may be obtained as follows.
Let the discontinuity be located at some ξ ∈ (a, b) and the (non-infinite) jumps in f and its derivatives be given by: where ξ − and ξ + denote the left and right limits to ξ.
One may then approximate f by a piecewise polynomial function that (i) interpolates the given N + 1 nodes, and (ii) has the same derivative jumps at the discontinuity. Eq. (2.1) is thus replaced by the ansatz denotes the Heaviside step function and denote the left and right interpolating polynomials. Note that the polynomials p − , p + and the piecewise polynomial function p depend implicitly on the location ξ of the discontinuity.
Half of the (2N + 2) polynomial coefficients c ± j are to be determined by the collocation conditions (2.2) which, given the ansatz (3.2), translate to (3.5) The remaining coefficients could be determined by the first N + 1 of the jump conditions (3.1). However, as information on high order jumps may not be beneficial (as discussed below and illustrated in Figs. 1 and 2) or easily accessible, we shall allow the number of jumps enforced to be user-specifiable, by dropping jumps in derivatives higher than order M ≤ N , where M is some chosen integer between −1 and N . The remaining N + 1 coefficients are then determined by the jump conditions p (m) It should be noted that the ansatz (3.4) should not be viewed merely as two independent interpolating polynomials matched at the discontinuity. If that were the case, the polynomial degrees would differ left and right of the discontinuity, depending on the number of grid points on each side, and our method would amount to domain decomposition. Instead, our ansatz should be regarded as a single "polynomial" of degree N that covers the whole domain [a, b] and has piecewise constant coefficients with a jump at the discontinuity ξ. The left and right coefficients encode information from all nodes and are correlated via the jump conditions (3.6).
As mentioned above, the "no jump" choice M = −1 corresponds to Lagrange interpolation, which suffices for smooth functions. A choice of M at least as high as k + 1 = 0, 1, 2 . . . is required to approximate C k functions with accuracy better than Lagrange, as this reproduces the first nonvanishing jump, J k+1 , in the (k + 1) th derivative. (For instance, a C 0 function can be approximated by specifying at least the first derivative jump f ′ (ξ + ) − f ′ (ξ − ) = J 1 .) Incorporating higher derivative jumps, that is, using M > k + 1, increases the approximation accuracy of the function as the jumps J k+2 , J k+3 , . . . , J M in its (k + 2) th , (k + 3) th , . . . , M th derivative are also reproduced. 1 Nevertheless, the accuracy degrades for values of M too close to N due to the appearance of spurious oscillations analogous to Runge's phenomenon. Numerical experiments suggest that intermediate choices of M relative to N (typically M/N ≈ 1/2 depending on the function) are optimal and consistently yield highly accurate results.
As detailed above, the linear system of algebraic conditions (3.5)-(3.6) can be uniquely solved by substituting the polynomials (3.4) and determining their unknown coefficients. This procedure is simplified if the polynomials are first converted to a Lagrange basis, Substitution in the algebraic conditions (3.5)-(3.6) yields are weights computed from the jump conditions at the discontinuity ξ given the nodes x j . Substituting Eqs.
(3.7)-(3.9) into the ansatz (3.2) yields the concise solution which generalizes the Lagrange formula (2.3) to a piecewise polynomial interpolating function. This interpolating function depends implicitly on the location ξ of the discontinuity through the weight functions (3.11) When J m = 0, the above quantities vanish and Lagrange interpolation is recovered. When evaluated at a node x = x i , the above expression simplifies to Since the prefactor in s j (x i ; ξ) is antisymmetric in its two indices and π j (x i ) = δ ij is symmetric, the correction s j (x; ξ)π j (x) in the interpolating formula (3.10) vanishes at each node x = x i . This ensures that the collocation conditions (2.2) or, equivalently, (3.5), are satisfied by construction.
As an example, let us consider the piecewise smooth solutions to the inhomogeneous Legendre differential equation with a singular (Dirac delta function) source, where P l (x) and Q l (x) denote the Legendre functions of the first and second kind respectively. Such solutions arise in the context of self-forces acting on static scalar and electric test charges in the spacetime of a Schwarzschild black hole [13]. In this case, the derivative jumps are given by (3.14) In general, although such solutions have finite differentiability at x = ξ, their derivative jumps can be obtained analytically from the field equation they satisfy without knowledge of the actual solution. This knowledge of the jumps can be used to inform the method outlined above and obtain numerical solutions to the radial part of the field equations. The accuracy of the numerical solutions depends on how well the underlying interpolating function (used for numerical operations such as integration or differentiation) approximates the actual solution.
A comparison of the analytic solution (3.13) to interpolating functions based on the Lagrange formula (2.3) and its generalization, Eq. (3.10), is depicted in Fig. 1 for equidistant and Fig. 2 for pseudospectral nodes. Although both formulas exhibit Runge oscillations near the boundaries for equidistant nodes, only the corrected formula faithfully reproduces the discontinuity given sufficient resolution. Pseudospectral nodes eliminate the Runge oscillations near the boundaries, but the corrected formula manages to also eliminate the Gibbs oscillations of the Lagrange formula near the discontinuity. Evidently, the accuracy lost when applying the Lagrange formula on nonsmooth functions is recovered by simply adding the correction terms (3.11).
A comparison of convergence rates using pseudospectral nodes and different numbers of derivative jumps is depicted in Fig. 3. As mentioned earlier, the convergence estimates favour the use of an intermediate number of derivative jumps relative to a given number of nodes (M ≈ N/2). With pseudospectral nodes, the formula (3.10) is at least M th -order convergent if derivatives of f higher than M have finite jumps, such as the example given above. For functions whose derivatives higher than order M are continuous, the formula (3.10) is exponentially convergent.

B. Differentiation
A finite-difference or pseudospectral approximation to the n-th derivative of f can be obtained by differentiating the piecewise polynomial (3.10). When evaluated at a node, x = x i , this yields with the derivative matrices D (n) ij given again by Eq. (2.8). Again, high-order or exponential convergence can be attained thanks to the correction terms (3.12).
As an example, let us consider a function with jumps J 0 , J 1 and J 2 in its 0 th , 1 st and 2 nd derivative at the J th node, x J = ξ. Then, for 3-point finite-difference stencils, Eq. (3.15) gives the left and right derivatives: with f J given by Eq. (2.9) for i = J. Notice that the above formula satisfies the jump condition f ′ J + −f ′ J − = J 1 as it ought to.
Higher order or pseudospectral formulas can be obtained in a similar fashion. For instance, using the nodes given by Eq. (2.6), the formula (3.15) yields a Gauss-Chebyshev-Lobatto scheme that is at least of M th -order convergence if derivatives of f higher than M have finite jumps, or of infinite-order convergence if derivatives higher than M are continuous.

C. Integration
Numerical integration formulas for nonsmooth integrands can be obtained, as in §2.3, by integrating the interpolating formula (3.10). Then, the standard quadrature formula (2.11) generalizes to [w j f j + q j (ξ)] (3.17) where the weights w j are given by Eq. (2.12), and the corrections account for the discontinuity and vanish when J m = 0. Eq. (3.17) can be used to obtain discontinuous generalizations of standard composite or Gaussian quadrature formulas using, for example, the nodes (2.5) or (2.6) respectively. This is very useful but relatively straightforward and thus not detailed here.  Having dropped the (nonzero in this case) jumps in derivatives higher than M , we observe convergence of order slightly above M . For functions with continuity in derivatives higher than M , infiniteorder (exponential) convergence is attained.

IV. CONCLUDING REMARKS
We have shown that, if the derivative jumps across a discontinuity are known, the simple modification (3.10) to the Lagrange formula (2.3) can fully account for the presence of a discontinuity and completely avoid Gibbs phenomena. This can be exploited in finite-difference or pseudospectral scheme to recover their original accuracy. The main advantage offered by the present method is that it is efficient and simple to implement, even when the discontinuity moves freely inside the domain, as no requirements for domain decomposition, regridding, or reconstruction arise.
Accurate numerical differentiation of piecewise smooth functions is crucial to many problems of physical interest. A common scenario consists of evolving a timedependent partial differential equation via the method of lines [38,48]. Spatial operators are then computed via finite-difference or pseudospectral methods described above, and time integration is performed using standard numerical methods for coupled ordinary differential equations, such as Runge-Kutta. The standard methods of spatial discretization in §2 cease to apply when discontinuities are present; this is when Eq. (3.15) becomes useful. For a moving discontinuity, the weights (3.12) must by evaluated at each time step; if the jumps J m at ξ are known, this evaluation is not expensive computationally as it requires a few subtractions and multiplications but no extra function evaluations. The cost and accuracy of obtaining the jumps and their location depends on the problem at hand; in certain problems this information is available "for free". For instance, in calculations of radiation-reaction forces in electrodynamics or gravitational self-force corrections to geodesic motion of small bodies in general relativity [6,46], the source of the dimensionally reduced field equations is singular, making the solution or its derivative discontinuous across the particle location [14][15][16]35]. However, the jumps (3.1) occur at the known particle location and can be computed analytically from the radial part of the field equations. The present method is ideally suited to such problems where information on the jumps is available.
Lipman and Levin [42] have developed a closely related interpolation method and a moving least squares technique for detecting the location and magnitude of an unknown discontinuity or singularity, with accuracy of higher-order than subcell resolution methods [33]. This technique would allow an extension of the present method to problems with time-dependent discontinuities that are not known a priori.
For pseudospectral discretizations, a mapping devised by Kosloff and Tal-Ezer to improve stability of the Chebyshev collocation scheme [39] and the accuracy of higher derivatives [17,18], can also be employed in conjunction with Eq. (3.15) in method-of-lines implementations to evolve discontinuous solutions.
In problems where the grid values of the function derivatives are available, in addition to their jumps at the discontinuity, Eqs. (3.5) can be complemented by conditions on the derivatives at the same nodes. Together with Eq. (3.6), this will yield generalizations of Hermite [34] and Birkhoff [10] interpolation to discontinuous problems.
As a final remark, we note that extension of the present technique to higher dimension is technically possible. However, for separable multi-dimensional problems discretized on tensor-product grids, the present method can be applied directly.