basix icon indicating copy to clipboard operation
basix copied to clipboard

Reordered Legendre element returns interpolation coefficients in the reference order

Open vera-rubin opened this issue 1 month ago • 0 comments

A degree-one Legendre element with dof_ordering=[1, 0] returns the constant's interpolation coefficients in the reference order. Evaluating the resulting interpolation at x=1/4 gives -0.8660254037844388.

Versions

Tested on Linux x86-64 with Basix 0.12.0.dev0 built from 4b3796f07b7018cd484e59018d924fa883cebf01 in Release mode. Python 3.12.3, NumPy 2.5.4, SymPy 1.14.0, nanobind 3.1.0, scikit-build-core 1.1.1, GCC/G++ 13.3.0, CMake 3.28.3 and pip 24.0. The system netlib BLAS/LAPACK packages were 3.12.0-3build1.1.

Reproduction

Run in an empty directory on Linux with the compiler, CMake and system BLAS/LAPACK libraries listed above installed:

git clone https://github.com/FEniCS/basix.git basix
git -C basix checkout --detach 4b3796f07b7018cd484e59018d924fa883cebf01
python3 -m venv venv
venv/bin/python -m pip install pip==24.0 numpy==2.5.4 sympy==1.14.0 \
  mpmath==1.3.0 scikit-build-core==1.1.1 nanobind==3.1.0
CMAKE_BUILD_PARALLEL_LEVEL=2 OPENBLAS_NUM_THREADS=1 OMP_NUM_THREADS=1 \
  venv/bin/python -m pip install --no-build-isolation \
  --config-settings=build-dir=build \
  --config-settings=cmake.build-type=Release \
  --config-settings=cmake.define.BLAS_LIBRARIES=/lib/x86_64-linux-gnu/libblas.so.3 \
  --config-settings=cmake.define.LAPACK_LIBRARIES=/lib/x86_64-linux-gnu/liblapack.so.3 \
  -v ./basix

Then run this example:

OPENBLAS_NUM_THREADS=1 OMP_NUM_THREADS=1 venv/bin/python - <<'PY'
import basix
import numpy as np
import sympy as s

e = basix.create_element(
    basix.ElementFamily.P, basix.CellType.interval, 1,
    basix.LagrangeVariant.legendre,
    discontinuous=True, dof_ordering=[1, 0],
)
x = s.Symbol("x")
polys = [s.Integer(1), s.sqrt(3) * (2*x - 1)]
expected = np.array([
    float(s.integrate(p, (x, 0, 1))) for p in polys
])[::-1]
actual = e.interpolation_matrix @ np.ones(e.points.shape[0])
basis = e.tabulate(0, np.array([[0.25]]))[0, 0, :, 0]
value = basis @ actual
print("exact expected coefficients:", expected)
print("observed coefficients:", actual)
print("interpolated constant at x=1/4:", value, "expected:", 1)
assert np.allclose(actual, expected, atol=1e-13, rtol=0)
assert abs(value - 1) < 1e-13
PY

Expected and observed

The DOF ordering contract places reference DOF i at custom position v[i]. The interpolation contract says to evaluate at the element's points and multiply by its interpolation matrix to obtain coefficients.

The Legendre construction uses moments against orthonormal polynomials. On [0,1], the first two are 1 and sqrt(3)*(2*x-1). Exact integration of the constant gives reference moments [1,0]. The swap therefore requires [0,1] and a function value of 1.

Observed output before the first assertion fails:

exact expected coefficients: [0. 1.]
observed coefficients: [1. 0.]
interpolated constant at x=1/4: -0.8660254037844388 expected: 1

Source inspection points to the constructor's ordering block. It reorders entity indices and interpolation points while leaving the interpolation matrix in its original order. This appears to explain the mismatch. A correction needs to keep the matrix rows consistent with the basis order and its columns consistent with the returned sample points.

Scope

Independent verification used a fresh Release build through both Python bindings and native C++. Both returned [1,0]. Default ordering and an explicit identity ordering passed exact constant and linear checks. The swapped tabulated basis also matched the exact polynomials. The linear interpolation check failed too.

This report covers degree-one float64 interval Legendre elements. Other degrees, cells and scalar types were not tested for this failure. The expectation uses exact SymPy mathematics.

Issue 961 concerns a Bernstein point-count assertion. This Legendre construction succeeds and returns the values above. PR 965 removed Bernstein from the ordering test and retained Legendre.

The example above is available under CC0.

Found by ChipLab's AI verification agents and independently reproduced.

ChipLab ref: CLAB-2026-962

vera-rubin avatar Oct 12 '26 15:10 vera-rubin