step or unit-interval)step rows use spacing $h=1$. On an interval with nodes $a,a+h,\ldots,a+nh$, the quadrature rule is $\int_a^{a+nh}f(x)\,\mathrm{d}x\approx h\sum_{j=0}^n w_{n,j}f(a+jh)$. The unit-interval rows divide the same weights by $n$, giving $\int_0^1 f(x)\,\mathrm{d}x\approx\sum_{j=0}^n C_{n,j}f(j/n)$.import numberdb.sage as numberdb
from sage.rings.rational_field import QQ
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
R = PolynomialRing(QQ, 'x')
x = R.gen()
def newton_cotes_weights(n):
weights = []
for j in range(n + 1):
ell = R(1)
for k in range(n + 1):
if k != j:
ell *= (x - k) * (QQ(1) / QQ(j - k))
anti = ell.integral()
weights.append(anti(n) - anti(0))
return weights
newton_cotes_weights(4) # [14/45, 64/45, 8/15, 64/45, 14/45]The generator builds the Lagrange moment equations over $\mathbb{Q}$ and solves them exactly.
Each completed rule is checked against the moments through degree $n$, and through degree $n+1$ when $n$ is even, against the symmetry and the two normalisations, against the quoted rows on Wikipedia and MathWorld for the named small rules, and against OEIS A093735 and A093736 where those b-files give the same rows.