import numberdb.sage as numberdb
from sage.arith.misc import factorial
from sage.matrix.constructor import matrix
from sage.modules.free_module_element import vector
from sage.rings.rational_field import QQ
def central_difference_coefficients(m, r):
offsets = list(range(-r, r + 1))
rows = [[QQ(j) ** q for j in offsets] for q in range(2 * r + 1)]
rhs = [QQ(factorial(m)) if q == m else QQ(0) for q in range(2 * r + 1)]
return dict(zip(offsets, matrix(QQ, rows).solve_right(vector(QQ, rhs))))
print(central_difference_coefficients(2, 2))The generator builds the moment equations over $\mathbb{Q}$ on the offsets $-r,-r+1,\ldots,r$ and solves them exactly. Each completed stencil is checked against the defining moments, the parity symmetry, the first nonzero error term and the quoted central-difference rows on Wikipedia.