Algorithm Search⌘K

Algorithmadvanced

Empirical interpolation method

Approximates a parametrized nonlinear function by interpolation at greedily chosen points in a basis of its snapshots, so online cost is mesh independent.

Time
O(m n M^2 + m M^3) offlineO(M^2) online
Space
O(m n + M n) extra
Where
M = the number of terms; m = the number of training parameters; n = the number of points of the grid that stands for the domain
Properties
deterministicnot exactinterpolatoryiterative
Origin
Maxime Barrault, Yvon Maday, Ngoc Cuong Nguyen, Anthony T. Patera, 2004

The empirical interpolation method (EIM) replaces a function g(x;μ)g(x; \mu) of a space variable and a parameter by a sum ∑j=1Mcj(μ) qj(x)\sum_{j=1}^{M} c_j(\mu)\, q_j(x) with fixed basis functions qjq_j. The coefficients come from interpolation: they make the sum equal to gg at MM points x1,…,xMx_1, \ldots, x_M, the magic points. A greedy loop over a training set of parameters chooses the basis and the points together. At each step it finds the parameter whose function is worst interpolated, takes its interpolation error as the next basis function, and takes the point where that error is largest as the next point. The online stage then needs only the MM values g(xi;μ)g(x_i; \mu) and a triangular solve of size MM. In a reduced basis method, this turns a coefficient that is not affine in μ\mu into an affine sum with MM terms, so the online cost no longer depends on the mesh. The discrete empirical interpolation method is the variant for vectors: it takes a POD basis that is computed first and then chooses only the points, with the same greedy rule. EIM chooses the basis and the points in the same loop, from the functions themselves.

In plain words

A coefficient such as 1/(x+μ)1/(x + \mu) changes shape with μ\mu. If you store it on the mesh for each new μ\mu, every reduced solve has to touch the whole mesh again. EIM finds a few fixed shapes and a few special points. For a new μ\mu you evaluate the function at those few points only, and the shapes, weighted to match those values, give the function everywhere. The points are chosen where the current approximation is worst, and the shapes are the errors that the earlier shapes left. So each new shape fixes the largest remaining error, and the points stay well spread.

The problem

Let Ωh={x(1),…,x(n)}\Omega_h = \{x^{(1)}, \ldots, x^{(n)}\} be a grid of points that stands for the domain, for example the quadrature points of a finite element mesh, and let ∥v∥∞=max⁡x∈Ωh∣v(x)∣\|v\|_\infty = \max_{x \in \Omega_h} |v(x)|. A function g(x;μ)g(x; \mu) can be evaluated for x∈Ωhx \in \Omega_h and μ\mu in a parameter set P\mathcal{P}. A training set Ξ⊂P\Xi \subset \mathcal{P} has mm points. The input is gg, Ωh\Omega_h, Ξ\Xi, a tolerance εtol\varepsilon_{tol} and a largest size MmaxM_{max}. The output is MM parameters μj\mu_j, points xj∈Ωhx_j \in \Omega_h and functions qjq_j on Ωh\Omega_h, such that the interpolant

IMg(⋅;μ)=∑j=1Mcj(μ) qj,∑j=1Mqj(xi) cj(μ)=g(xi;μ)(i=1,…,M),I_M g(\cdot; \mu) = \sum_{j=1}^{M} c_j(\mu)\, q_j, \qquad \sum_{j=1}^{M} q_j(x_i)\, c_j(\mu) = g(x_i; \mu) \quad (i = 1, \ldots, M),

has max⁡μ∈Ξ∥g(⋅;μ)−IMg(⋅;μ)∥∞≤εtol\max_{\mu \in \Xi} \|g(\cdot; \mu) - I_M g(\cdot; \mu)\|_\infty \le \varepsilon_{tol}, or M=MmaxM = M_{max}. The matrix Bij=qj(xi)B_{ij} = q_j(x_i) of the interpolation system must be invertible, and the method makes it lower triangular with a unit diagonal.

The algorithm

  1. EIM(g, Ω_h, Ξ, ε_tol, M_max)
  2. 1M ← 0; r_μ ← g(·; μ) on Ω_h for each μ in Ξ ▷ I_0 g = 0
  3. 2e(μ) ← max over Ω_h of |r_μ| for each μ in Ξ
  4. 3μ* ← the μ in Ξ with the largest e(μ)
  5. 4if e(μ*) ≤ ε_tol or M = M_max then
  6. 5return μ_1..μ_M, x_1..x_M, q_1..q_M, B
  7. 6M ← M + 1; μ_M ← μ*; r ← r_μ* ▷ the worst residual
  8. 7x_M ← the point of Ω_h with the largest |r(x)|
  9. 8q_M ← r / r(x_M)
  10. 9B_Mj ← q_j(x_M) for j = 1..M; B_iM ← 0 for i < M ▷ B_MM = 1
  11. 10r_μ ← g(·; μ) - I_M g(·; μ) for each μ in Ξ, with B c = (g(x_i; μ))_i; go to L2

Line L1 starts with the empty interpolant, so the first residuals are the functions. Lines L2 and L3 find the worst interpolated training function in the max norm. Line L6 takes its residual r=g(⋅;μM)−IM−1g(⋅;μM)r = g(\cdot; \mu_M) - I_{M-1} g(\cdot; \mu_M). Line L7 chooses the new point where this residual is largest, and line L8 scales it to the value 1 there. The residual is 0 at the earlier points x1,…,xM−1x_1, \ldots, x_{M-1}, because IM−1I_{M-1} interpolates there. So qM(xi)=0q_M(x_i) = 0 for i<Mi < M, and line L9 only adds a row to BB. Line L10 computes the new interpolants by forward substitution. The online stage for a new μ\mu is the last part of line L10 for one function: evaluate g(xi;μ)g(x_i; \mu) for i≤Mi \le M, solve Bc=(g(xi;μ))iB c = (g(x_i; \mu))_i in O(M2)O(M^2), and use c(μ)c(\mu) as the affine coefficients.

Example

By hand. Take the grid {0,1/2,1}\{0, 1/2, 1\}, g(x;μ)=1+μx2g(x; \mu) = 1 + \mu x^2 and Ξ={1,3}\Xi = \{1, 3\}. The functions are (1,1.25,2)(1, 1.25, 2) and (1,1.75,4)(1, 1.75, 4). Their max norms are 2 and 4, so μ1=3\mu_1 = 3, x1=1x_1 = 1 and q1=(0.25,0.4375,1)q_1 = (0.25, 0.4375, 1). The interpolant of g(⋅;1)g(\cdot; 1) is 2q1=(0.5,0.875,2)2 q_1 = (0.5, 0.875, 2), so the residual is (0.5,0.375,0)(0.5, 0.375, 0), with its largest value 0.50.5 at x=0x = 0. Then μ2=1\mu_2 = 1, x2=0x_2 = 0, q2=(1,0.75,0)q_2 = (1, 0.75, 0) and

B=(q1(1)q2(1)q1(0)q2(0))=(100.251).B = \begin{pmatrix} q_1(1) & q_2(1) \\ q_1(0) & q_2(0) \end{pmatrix} = \begin{pmatrix} 1 & 0 \\ 0.25 & 1 \end{pmatrix} .

The span of q1q_1 and q2q_2 is the span of 11 and x2x^2, so the residuals are now 0 and the loop stops with M=2M = 2. For the new parameter μ=2\mu = 2 the online stage uses the values g(1;2)=3g(1; 2) = 3 and g(0;2)=1g(0; 2) = 1: c1=3c_1 = 3 and c2=1−0.25⋅3=0.25c_2 = 1 - 0.25 \cdot 3 = 0.25, and 3q1+0.25q2=(1,1.5,3)=1+2x23 q_1 + 0.25 q_2 = (1, 1.5, 3) = 1 + 2x^2 on the grid.

A singular family. Take g(x;μ)=1/(x+μ)g(x; \mu) = 1/(x + \mu) on 201201 equispaced points of [0,1][0, 1], with the 5151 training values μ=0.05+0.019i\mu = 0.05 + 0.019 i, i=0,…,50i = 0, \ldots, 50, and εtol=10−8\varepsilon_{tol} = 10^{-8}. The table is a measurement from the test file. The column εM\varepsilon_M is the largest max-norm error over Ξ\Xi after MM terms.

MM μM\mu_M xMx_M εM\varepsilon_M
0 20.0
1 0.050 0.000 1.802
2 0.126 0.080 0.3554
3 0.544 0.505 9.890×10−29.890 \times 10^{-2}
5 1.000 1.000 5.700×10−35.700 \times 10^{-3}
8 0.354 0.315 6.565×10−66.565 \times 10^{-6}
11 0.107 0.125 6.958×10−96.958 \times 10^{-9}

The loop stops at M=11M = 11. The first parameter is the one with the largest function, μ=0.05\mu = 0.05, and its peak at x=0x = 0 is the first point. The points gather near x=0x = 0, where the functions change fastest: 0, 0.005, 0.02, 0.045, 0.08 and 0.125 are among the eleven. The second row of BB is q1(x2)=(1/0.13)/(1/0.05)=0.3846q_1(x_2) = (1/0.13)/(1/0.05) = 0.3846. The Lebesgue constant is 1.00, 1.12, 1.63, 2.68, 3.87 and 3.53 for M=1,2,3,5,8,11M = 1, 2, 3, 5, 8, 11, far below the bound 2M−12^M - 1, which is 2047 for M=11M = 11. On a 101, 401 or 801 point grid the loop also stops at M=11M = 11. On 400 test parameters between the training points, the largest error is 3.6×10−53.6 \times 10^{-5} for M=8M = 8 and 5.7×10−75.7 \times 10^{-7} for M=11M = 11, so the training set is too coarse near μ=0.05\mu = 0.05; see the pitfalls.

Correctness

The proof file shows, in exact arithmetic, that the loop is well defined and stops, and that the interpolant has the bounds of a good interpolation scheme.

  1. Each chosen residual is not 0, so line L8 does not divide by 0, and qM(xi)=0q_M(x_i) = 0 for i<Mi < M and qM(xM)=1q_M(x_M) = 1. So BB is lower triangular with a unit diagonal and is invertible. Also ∥qj∥∞=1\|q_j\|_\infty = 1.
  2. IMI_M interpolates: IMg(xi;μ)=g(xi;μ)I_M g(x_i; \mu) = g(x_i; \mu). It reproduces the span: IMv=vI_M v = v for v∈span⁡{q1,…,qM}v \in \operatorname{span}\{q_1, \ldots, q_M\}, and this span is the span of g(⋅;μ1),…,g(⋅;μM)g(\cdot; \mu_1), \ldots, g(\cdot; \mu_M).
  3. With the Lagrange functions ℓj\ell_j of the points (ℓj(xi)=δij\ell_j(x_i) = \delta_{ij}, ℓj\ell_j in the span) and the Lebesgue constant ΛM=max⁡Ωh∑j∣ℓj(x)∣\Lambda_M = \max_{\Omega_h} \sum_j |\ell_j(x)|, every gg satisfies
∥g−IMg∥∞≤(1+ΛM)min⁡v∈span⁡{qj}∥g−v∥∞,ΛM≤2M−1.\|g - I_M g\|_\infty \le (1 + \Lambda_M) \min_{v \in \operatorname{span}\{q_j\}} \|g - v\|_\infty, \qquad \Lambda_M \le 2^M - 1 .
  1. On the finite set Ξ\Xi, the loop stops after at most mm steps, and if it stops by the tolerance, the error is at most εtol\varepsilon_{tol} at every μ∈Ξ\mu \in \Xi.

The bound 2M−12^M - 1 is a worst case. In practice the Lebesgue constant grows slowly, as in the example. Compare it with the Lebesgue constant of Lagrange interpolation at fixed nodes: there the space is fixed and only the points are chosen; here both are adapted to the family.

Proof: The EIM matrix is unit lower triangular, the interpolant is quasi-optimal with a Lebesgue constant at most 2^M - 1, and the loop stopsfull

We assume exact arithmetic. Functions live on the finite grid Ωh\Omega_h, ∥v∥∞=max⁡x∈Ωh∣v(x)∣\|v\|_\infty = \max_{x \in \Omega_h} |v(x)|, and WM=span⁡{q1,…,qM}W_M = \operatorname{span}\{q_1, \ldots, q_M\}. We write IMgI_M g for the interpolant of line L10 when BB is invertible, and rμM=g(⋅;μ)−IMg(⋅;μ)r^{M}_\mu = g(\cdot; \mu) - I_M g(\cdot; \mu), with I0g=0I_0 g = 0.

Lemma 1 (one step). Suppose that after M−1M - 1 steps the matrix BB of size M−1M - 1 is lower triangular with a unit diagonal, that IM−1I_{M-1} interpolates at x1,…,xM−1x_1, \ldots, x_{M-1}, and that line L4 does not stop. Then the residual r=rμ∗M−1r = r^{M-1}_{\mu^\ast} of line L6 is not 0, r(xM)≠0r(x_M) \ne 0, and the new function qM=r/r(xM)q_M = r / r(x_M) satisfies qM(xi)=0q_M(x_i) = 0 for i<Mi < M, qM(xM)=1q_M(x_M) = 1 and ∥qM∥∞=1\|q_M\|_\infty = 1. The new BB is lower triangular with a unit diagonal.

Proof. Line L4 did not stop, so ∥r∥∞=e(μ∗)>εtol≥0\|r\|_\infty = e(\mu^\ast) > \varepsilon_{tol} \ge 0. Line L7 takes a point where ∣r∣|r| reaches ∥r∥∞\|r\|_\infty, so r(xM)≠0r(x_M) \ne 0 and ∣qM(x)∣=∣r(x)∣/∣r(xM)∣≤1|q_M(x)| = |r(x)| / |r(x_M)| \le 1, with equality at xMx_M. For i<Mi < M, r(xi)=g(xi;μ∗)−IM−1g(xi;μ∗)=0r(x_i) = g(x_i; \mu^\ast) - I_{M-1} g(x_i; \mu^\ast) = 0 by interpolation, so qM(xi)=0q_M(x_i) = 0. So the new column of BB has the entries BiM=qM(xi)=0B_{iM} = q_M(x_i) = 0 above the diagonal, which line L9 stores, and BMM=1B_{MM} = 1. The earlier rows do not change. In particular xMx_M differs from x1,…,xM−1x_1, \ldots, x_{M-1}, where rr is 0. □\square

Lemma 2 (interpolation and reproduction). If BB is lower triangular with a unit diagonal, then for every gg the system Bc=(g(xi))iB c = (g(x_i))_i has one solution, IMg=∑jcjqjI_M g = \sum_j c_j q_j satisfies IMg(xi)=g(xi)I_M g(x_i) = g(x_i) for i≤Mi \le M, and IMv=vI_M v = v for every v∈WMv \in W_M. Also WM=span⁡{g(⋅;μ1),…,g(⋅;μM)}W_M = \operatorname{span}\{g(\cdot; \mu_1), \ldots, g(\cdot; \mu_M)\}.

Proof. det⁡B=1\det B = 1, so cc exists and is unique, and (IMg)(xi)=∑jBijcj=g(xi)(I_M g)(x_i) = \sum_j B_{ij} c_j = g(x_i). For v=∑jajqjv = \sum_j a_j q_j, the vector aa solves Ba=(v(xi))iB a = (v(x_i))_i, so c=ac = a by uniqueness and IMv=vI_M v = v. For the span, qMq_M is a nonzero multiple of g(⋅;μM)−IM−1g(⋅;μM)g(\cdot; \mu_M) - I_{M-1} g(\cdot; \mu_M), where IM−1g∈WM−1I_{M-1} g \in W_{M-1}. By induction WM⊆span⁡{g(⋅;μj)}j≤MW_M \subseteq \operatorname{span}\{g(\cdot; \mu_j)\}_{j \le M}, and g(⋅;μM)=r(xM)qM+IM−1g(⋅;μM)∈WMg(\cdot; \mu_M) = r(x_M) q_M + I_{M-1} g(\cdot; \mu_M) \in W_M gives the other inclusion. □\square

By induction on MM with Lemmas 1 and 2, every step of the loop is well defined, BB stays lower triangular with a unit diagonal, and ∥qj∥∞=1\|q_j\|_\infty = 1 for all jj.

Lemma 3 (Lagrange functions). Let ℓjM∈WM\ell^M_j \in W_M with ℓjM(xi)=δij\ell^M_j(x_i) = \delta_{ij} for i,j≤Mi, j \le M. These exist and are unique, IMg=∑jg(xj) ℓjMI_M g = \sum_j g(x_j)\, \ell^M_j, and for j<Mj < M

ℓMM=qM,ℓjM=ℓjM−1−ℓjM−1(xM) qM.\ell^M_M = q_M, \qquad \ell^M_j = \ell^{M-1}_j - \ell^{M-1}_j(x_M)\, q_M .

Proof. Existence and uniqueness: ℓjM=IM\ell^M_j = I_M applied to the grid function with values δij\delta_{ij} at xix_i, by Lemma 2, and the formula for IMgI_M g follows by linearity of the system. The recursion: qM∈WMq_M \in W_M has the values δiM\delta_{iM} by Lemma 1. For j<Mj < M, the right side is in WMW_M. At xix_i with i<Mi < M it equals ℓjM−1(xi)−ℓjM−1(xM)⋅0=δij\ell^{M-1}_j(x_i) - \ell^{M-1}_j(x_M) \cdot 0 = \delta_{ij}, and at xMx_M it equals ℓjM−1(xM)−ℓjM−1(xM)⋅1=0\ell^{M-1}_j(x_M) - \ell^{M-1}_j(x_M) \cdot 1 = 0. Uniqueness gives the identity. □\square

Theorem 1 (Lebesgue constant). ΛM=max⁡x∈Ωh∑j=1M∣ℓjM(x)∣≤2M−1\Lambda_M = \max_{x \in \Omega_h} \sum_{j=1}^{M} |\ell^M_j(x)| \le 2^M - 1.

Proof. Λ1=∥q1∥∞=1\Lambda_1 = \|q_1\|_\infty = 1. For M≥2M \ge 2, Lemma 3 and ∥qM∥∞=1\|q_M\|_\infty = 1 give, at each xx,

∑j=1M∣ℓjM(x)∣≤∑j<M∣ℓjM−1(x)∣+∣qM(x)∣∑j<M∣ℓjM−1(xM)∣+∣qM(x)∣≤ΛM−1+ΛM−1+1.\sum_{j=1}^{M} |\ell^M_j(x)| \le \sum_{j<M} |\ell^{M-1}_j(x)| + |q_M(x)| \sum_{j<M} |\ell^{M-1}_j(x_M)| + |q_M(x)| \le \Lambda_{M-1} + \Lambda_{M-1} + 1 .

So ΛM+1≤2(ΛM−1+1)\Lambda_M + 1 \le 2(\Lambda_{M-1} + 1) and ΛM+1≤2M−1(Λ1+1)=2M\Lambda_M + 1 \le 2^{M-1}(\Lambda_1 + 1) = 2^M. □\square

Theorem 2 (quasi-optimality). For every grid function gg, ∥g−IMg∥∞≤(1+ΛM)min⁡v∈WM∥g−v∥∞\|g - I_M g\|_\infty \le (1 + \Lambda_M) \min_{v \in W_M} \|g - v\|_\infty.

Proof. For v∈WMv \in W_M, IMv=vI_M v = v by Lemma 2, so g−IMg=(g−v)−IM(g−v)g - I_M g = (g - v) - I_M(g - v). By Lemma 3, ∣IMw(x)∣=∣∑jw(xj)ℓjM(x)∣≤∥w∥∞ΛM|I_M w(x)| = |\sum_j w(x_j) \ell^M_j(x)| \le \|w\|_\infty \Lambda_M. So ∥g−IMg∥∞≤(1+ΛM)∥g−v∥∞\|g - I_M g\|_\infty \le (1 + \Lambda_M) \|g - v\|_\infty. The minimum exists because WMW_M has finite dimension. □\square

Theorem 3 (termination and the stopping error). The loop stops after at most min⁡(m,Mmax)\min(m, M_{max}) steps. If it stops by the tolerance, then ∥g(⋅;μ)−IMg(⋅;μ)∥∞≤εtol\|g(\cdot; \mu) - I_M g(\cdot; \mu)\|_\infty \le \varepsilon_{tol} for every μ∈Ξ\mu \in \Xi.

Proof. If step MM chooses μ∗\mu^\ast, its residual is not 0 by Lemma 1, so g(⋅;μ∗)∉WM−1g(\cdot; \mu^\ast) \notin W_{M-1} by Lemma 2. Each chosen g(⋅;μj)g(\cdot; \mu_j) lies in WMW_M by Lemma 2, so a chosen parameter has residual 0 at all later steps and is never chosen again. So at most mm parameters are chosen. After mm steps every training function is in WmW_m and all residuals are 0, so line L4 stops; it also stops at M=MmaxM = M_{max}. The second claim is the test of line L4 with the maximum of line L3. ■\blacksquare

Complexity

In the real RAM model, line L1 costs mnm n evaluations of gg. In step MM, line L10 solves a triangular system of size MM for each training function, O(mM2)O(m M^2), and forms the interpolants on the grid, O(mnM)O(m n M). Lines L2, L3 and L7 cost O(mn)O(m n). The sum over MM steps is O(mnM2+mM3)O(m n M^2 + m M^3). Since IMg=IM−1g+r(xM) qMI_M g = I_{M-1} g + r(x_M)\, q_M for the residual rr of gg, an implementation can update each residual by one term, rμ←rμ−rμ(xM) qMr_\mu \leftarrow r_\mu - r_\mu(x_M)\, q_M, at a cost of O(mn)O(m n) per step and O(mnM)O(m n M) in total. The implementation here solves again for simplicity. The online stage costs MM evaluations of gg and O(M2)O(M^2) for the solve, with no dependence on nn. Memory is O(mn)O(m n) for the training functions and O(Mn)O(M n) for the basis.

Properties

  • Interpolatory. The approximation is exact at the magic points, and exact for the training functions that were chosen.
  • Triangular. BB is lower triangular with a unit diagonal, so the online solve is a forward substitution and is always possible.
  • Online cost independent of the mesh. Only MM values of gg are needed.
  • Adapted points. The points move to where the family changes fastest, without any knowledge of the formula of gg.
  • Quasi-optimal up to 1+ΛM1 + \Lambda_M. The error is within the factor 1+ΛM1 + \Lambda_M of the best max-norm approximation in the same space.
  • No certified bound. The error away from the training set is not bounded by the method.

Variants and improvements

  • DEIM. The discrete empirical interpolation method takes the basis from the POD of snapshots and chooses only the points by the same rule. It is the usual choice for the nonlinear term of a POD-Galerkin model.
  • Generalized EIM. Replace the point values by linear functionals, such as local averages or sensor responses, so the method works for functions in L2L^2 and for measured data.
  • Error estimate with one more point. Use ∣g(xM+1;μ)−IMg(xM+1;μ)∣|g(x_{M+1}; \mu) - I_M g(x_{M+1}; \mu)| as an estimate of the error. It is cheap, but it is a bound only under an assumption on the next term.
  • Discrete versions on the quadrature points. In a finite element code the grid is the set of quadrature points, and the online assembly evaluates gg only in the elements that contain the magic points.
  • Nonlinear operators. The same construction applies to g(x;u(μ))g(x; u(\mu)), where gg depends on the solution, inside a Newton loop.

When to use it

Use EIM inside a reduced basis or POD model when a coefficient, a source term or a nonlinear term depends on the parameter in a way that is not affine, and when the family of functions is smooth in the parameter, so that a few terms are enough. It needs point evaluations of gg and nothing else. Do not use it when the family has a slowly decaying Kolmogorov width, for example a moving front or a moving peak, because then MM becomes large. When the basis must come from data that is given as vectors, use DEIM.

Pitfalls

  • A coarse training set. The error is controlled only at the training parameters. In the example, the error between the training points is 80 times the training error at M=11M = 11. Refine Ξ\Xi where gg changes fast, or check on a test set.
  • The stopping tolerance near rounding. When the residual is near the rounding level, line L8 divides by a tiny number and the new basis function is noise. Stop with a tolerance well above εmachmax⁡∥g∥∞\varepsilon_{mach} \max \|g\|_\infty.
  • Using the Euclidean norm in line L2 but the max in line L7. Both are valid, but the bound with ΛM\Lambda_M is for the max norm. Keep the norms consistent with the claims.
  • Forgetting that the error adds to the model error. In a reduced basis method with EIM, the error bound of the reduced model must account for the interpolation error, or it is no longer a bound.
  • Evaluating gg on the whole mesh online. The gain is lost if the online stage forms gg on all of Ωh\Omega_h and then samples it. Only the MM values are needed.

Implementations

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

empirical_interpolation_method.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/empirical-interpolation-method/. The authors are listed in prov/003ET5.jsonl.
"""The empirical interpolation method (EIM) of Barrault, Maday, Nguyen and Patera.

A parametrized function g(x; mu) is sampled on a grid of points x (the grid stands for the
domain, and the sup norm is the max over the grid). The offline stage chooses parameters
mu_1..mu_M, magic points x_1..x_M and basis functions q_1..q_M by a greedy loop. The online
stage needs the M values g(x_i; mu) only, and solves a lower triangular system.
"""

from typing import Callable

Vector = list[float]


def sup_norm(v: Vector) -> float:
    return max(abs(t) for t in v)


def argmax_abs(v: Vector) -> int:
    """Index of the largest |v_i|; ties go to the first index."""
    best = 0
    for i in range(1, len(v)):
        if abs(v[i]) > abs(v[best]):
            best = i
    return best


def forward_solve(b: list[Vector], rhs: Vector) -> Vector:
    """Solve the lower triangular system b c = rhs (b has a unit diagonal)."""
    c: Vector = []
    for i in range(len(rhs)):
        c.append((rhs[i] - sum(b[i][j] * c[j] for j in range(i))) / b[i][i])
    return c


class EIM:
    """The output of the offline stage: parameters, magic point indices, basis and the matrix B."""

    def __init__(self) -> None:
        self.mus: list[float] = []
        self.pts: list[int] = []  # indices of the magic points in the grid
        self.q: list[Vector] = []  # basis functions on the grid
        self.b: list[Vector] = []  # b[i][j] = q_j(x_i), lower triangular with a unit diagonal

    def coefficients(self, values: Vector) -> Vector:
        """Coefficients of the interpolant from the values at the magic points."""
        return forward_solve(self.b, values)

    def interpolate(self, gvec: Vector) -> Vector:
        """I_M g on the grid, for a function given on the grid (only its magic point values are used)."""
        c = self.coefficients([gvec[i] for i in self.pts])
        return [sum(cj * qj[k] for cj, qj in zip(c, self.q)) for k in range(len(gvec))]

    def lebesgue_constant(self) -> float:
        """max over the grid of sum_j |l_j(x)|, with the Lagrange functions l_j(x_i) = delta_ij."""
        m = len(self.q)
        lag = []
        for j in range(m):
            e = [1.0 if i == j else 0.0 for i in range(m)]
            c = forward_solve(self.b, e)
            lag.append([sum(ck * qk[x] for ck, qk in zip(c, self.q)) for x in range(len(self.q[0]))])
        return max(sum(abs(l[x]) for l in lag) for x in range(len(self.q[0])))


def eim_offline(g: Callable[[float, float], float], xs: Vector, train: list[float], tol: float, m_max: int) -> tuple[EIM, list[float]]:
    """Run the greedy loop. Return the EIM data and the list err_0, err_1, ... of the largest
    sup norm error over train after M = 0, 1, ... terms (err_0 = max ||g(.; mu)||)."""
    snaps = [[g(x, mu) for x in xs] for mu in train]
    eim = EIM()
    errs: list[float] = []
    res = [list(s) for s in snaps]  # @line L1: residuals g - I_M g on the grid; I_0 g = 0
    while True:
        norms = [sup_norm(r) for r in res]  # @line L2: the error of every training function
        k = max(range(len(train)), key=lambda i: norms[i])  # @line L3
        errs.append(norms[k])
        if norms[k] <= tol or len(eim.q) >= m_max:  # @line L4
            return eim, errs  # @line L5
        r = res[k]  # @line L6: the residual at the chosen parameter
        p = argmax_abs(r)  # @line L7: the new magic point
        qn = [t / r[p] for t in r]  # @line L8
        eim.mus.append(train[k])
        for i in range(len(eim.b)):  # @line L9: extend B by a row; the new column is 0 above the diagonal
            eim.b[i].append(0.0)
        eim.b.append([qj[p] for qj in eim.q] + [1.0])
        eim.pts.append(p)
        eim.q.append(qn)
        # @line L10: residuals of all training functions for the new interpolant
        res =[[a - b for a, b in zip(s, eim.interpolate(s))] for s in snaps]
test_empirical_interpolation_method.py 120 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/empirical-interpolation-method/. The authors are listed in prov/003ET5.jsonl.
import random

import pytest

from empirical_interpolation_method import EIM, eim_offline, sup_norm

XS = [i / 200 for i in range(201)]
TRAIN = [0.05 + 0.019 * i for i in range(51)]


def g_inv(x, mu):
    return 1.0 / (x + mu)


def dense_solve(a, b):
    """Reference: Gaussian elimination with partial pivoting."""
    n = len(b)
    m = [list(row) + [bi] for row, bi in zip(a, b)]
    for k in range(n):
        p = max(range(k, n), key=lambda i: abs(m[i][k]))
        m[k], m[p] = m[p], m[k]
        for i in range(k + 1, n):
            f = m[i][k] / m[k][k]
            for j in range(k, n + 1):
                m[i][j] -= f * m[k][j]
    x = [0.0] * n
    for i in range(n - 1, -1, -1):
        x[i] = (m[i][n] - sum(m[i][j] * x[j] for j in range(i + 1, n))) / m[i][i]
    return x


def table_function(vectors):
    """g(x, mu) for a table: mu is an index into the list of grid vectors, x a grid index."""
    return lambda x, mu: vectors[mu][x]


def test_hand_example():
    xs = [0.0, 0.5, 1.0]
    eim, errs = eim_offline(lambda x, mu: 1.0 + mu * x * x, xs, [1.0, 3.0], 1e-12, 5)
    assert eim.mus == [3.0, 1.0]
    assert [xs[p] for p in eim.pts] == [1.0, 0.0]
    assert eim.q[0] == pytest.approx([0.25, 0.4375, 1.0])
    assert eim.q[1] == pytest.approx([1.0, 0.75, 0.0])
    assert eim.b == [[1.0, 0.0], [0.25, 1.0]]
    assert errs[:2] == [4.0, 0.5] and errs[2] < 1e-15
    # the function is affine in mu, so two terms reproduce every mu
    assert eim.coefficients([3.0, 1.0]) == pytest.approx([3.0, 0.25])
    assert eim.interpolate([1.0, 1.5, 3.0]) == pytest.approx([1.0, 1.5, 3.0])


def test_structure_on_random_tables():
    rng = random.Random(1)
    for _ in range(20):
        npts, nf = rng.randint(6, 12), rng.randint(3, 6)
        vecs = [[rng.gauss(0, 1) for _ in range(npts)] for _ in range(nf)]
        eim, errs = eim_offline(table_function(vecs), list(range(npts)), list(range(nf)), 1e-9, nf)
        m = len(eim.q)
        assert len(set(eim.pts)) == m and len(set(eim.mus)) == m
        for i in range(m):
            assert eim.b[i][i] == 1.0
            for j in range(m):
                if j <= i:
                    assert eim.b[i][j] == eim.q[j][eim.pts[i]]
                else:
                    # q_j vanishes at the earlier points up to rounding; B stores an exact 0
                    assert eim.b[i][j] == 0.0 and abs(eim.q[j][eim.pts[i]]) < 1e-12
        assert all(sup_norm(q) <= 1.0 + 1e-12 for q in eim.q)
        # interpolation against a dense reference solve, and the interpolation property
        for v in vecs:
            ref = dense_solve(eim.b, [v[p] for p in eim.pts])
            assert eim.coefficients([v[p] for p in eim.pts]) == pytest.approx(ref, rel=1e-9, abs=1e-9)
            iv = eim.interpolate(v)
            assert [iv[p] for p in eim.pts] == pytest.approx([v[p] for p in eim.pts], abs=1e-9)


def test_termination_and_exactness_on_a_finite_set():
    rng = random.Random(2)
    for _ in range(10):
        npts, nf = 10, rng.randint(2, 5)
        vecs = [[rng.gauss(0, 1) for _ in range(npts)] for _ in range(nf)]
        eim, errs = eim_offline(table_function(vecs), list(range(npts)), list(range(nf)), 1e-9, 100)
        assert len(eim.q) <= nf and errs[-1] <= 1e-9


def test_lebesgue_constant_bound_and_inequality():
    rng = random.Random(3)
    eim, errs = eim_offline(g_inv, XS, TRAIN, 1e-8, 40)
    for m in range(1, len(eim.q) + 1):
        sub = EIM()
        sub.pts, sub.q, sub.b = eim.pts[:m], eim.q[:m], [row[:m] for row in eim.b[:m]]
        lam = sub.lebesgue_constant()
        assert 1.0 - 1e-12 <= lam <= 2 ** m - 1 + 1e-9
        # ||g - I g|| <= (1 + Lambda) ||g - v|| for every v in the span: try perturbed interpolants
        for mu in rng.sample(TRAIN, 5):
            gv = [g_inv(x, mu) for x in XS]
            err = sup_norm([a - b for a, b in zip(gv, sub.interpolate(gv))])
            c = sub.coefficients([gv[p] for p in sub.pts])
            for _ in range(5):
                a = [cj + rng.gauss(0, 1e-3 * (1 + abs(cj))) for cj in c]
                v = [sum(aj * qj[k] for aj, qj in zip(a, sub.q)) for k in range(len(XS))]
                assert err <= (1 + lam) * sup_norm([s - t for s, t in zip(gv, v)]) * (1 + 1e-9) + 1e-12


def test_example_run():
    eim, errs = eim_offline(g_inv, XS, TRAIN, 1e-8, 40)
    assert len(eim.q) == 11
    assert eim.mus[:3] == pytest.approx([0.05, 0.126, 0.544])
    assert [XS[p] for p in eim.pts[:3]] == pytest.approx([0.0, 0.08, 0.505])
    assert errs[0] == pytest.approx(20.0) and errs[1] == pytest.approx(1.802, rel=1e-3)
    assert errs[-1] <= 1e-8 < errs[-2]
    assert eim.lebesgue_constant() == pytest.approx(3.528, rel=1e-3)


def test_online_stage_uses_only_the_magic_points():
    eim, errs = eim_offline(g_inv, XS, TRAIN, 1e-8, 40)
    gv = [g_inv(x, 0.3) for x in XS]
    other = [t if k in eim.pts else 1e6 for k, t in enumerate(gv)]
    assert eim.interpolate(other) == eim.interpolate(gv)

History

Barrault, Maday, Nguyen and Patera introduced the empirical interpolation method in 2004, to keep the offline-online split of reduced basis methods for coefficients that are not affine in the parameter. Their note gives the greedy construction and the bound 2M−12^M - 1 of the Lebesgue constant. Maday, Nguyen, Patera and Pau presented the construction in 2009 as a general interpolation procedure, with the name magic points. Chaturantabut and Sorensen gave the discrete version DEIM in 2010.

References

  1. Maxime Barrault, Yvon Maday, Ngoc Cuong Nguyen and Anthony T. Patera, An empirical interpolation method, application to efficient reduced-basis discretization of partial differential equations, Comptes Rendus Mathematique 339(9), 2004, 667-672, the greedy construction of the basis and the interpolation points, and the bound 2^M - 1 of the Lebesgue constant
  2. Yvon Maday, Ngoc Cuong Nguyen, Anthony T. Patera and George S. H. Pau, A general multipurpose interpolation procedure, the magic points, Communications on Pure and Applied Analysis 8(1), 2009, 383-404, the magic points for a general family of functions, and the Lebesgue constant of the construction
  3. Jan S. Hesthaven, Gianluigi Rozza and Benjamin Stamm, Certified Reduced Basis Methods for Parametrized Partial Differential Equations, SpringerBriefs in Mathematics, Springer, 2016, Chapter 5, Section 5.2 (the empirical interpolation method)

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.offline
  • a full proof of time.online
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a scene preset for the example