Affine-weighted hat mass matrix (source code)

= Affine-weighted hat mass matrix
{title2=$W_{ij}=\int w(x)\phi_i(x)\phi_j(x)\,dx$}

On a one-dimensional uniform <finite element mesh> of spacing $h$, let $w$ be an <affine function> and $\phi_i$ the interior <piecewise-linear hat functions>. The weighted <mass matrix> has entries
$$
W_{ii}=\frac{2h}{3}w(x_i),\qquad
W_{i,i+1}=W_{i+1,i}=\frac h{12}\bigl(w(x_i)+w(x_{i+1})\bigr),
$$
and zero entries for nonadjacent indices. The diagonal product $\phi_i^2$ is symmetric about $x_i$ and integrates to $2h/3$; the neighbouring product is symmetric about $(x_i+x_{i+1})/2$ and integrates to $h/6$. The odd part of an <affine function> integrates to zero around each centre, giving the formulas. This evaluates a variable reaction term exactly without substituting a constant coefficient or a quadrature approximation.