import os
from numpy.polynomial.hermite import hermgauss
from numpy.polynomial.laguerre import laggauss
from numpy.polynomial.legendre import leggauss
MAX_ORDER = 30
OUT_DIR = os.path.join(os.path.dirname(__file__), "..", "src", "gaussian_tables")
FAMILIES = {
"legendre": ("LEGENDRE", leggauss),
"hermite": ("HERMITE", hermgauss),
"laguerre": ("LAGUERRE", laggauss),
}
def render(name, gauss):
out = [
"// Auto-generated by scripts/build_gaussian_integration_tables.py",
"// Do not edit manually.",
"",
]
for order in range(1, MAX_ORDER + 1):
abscissae, weights = gauss(order)
out.append("pub static {}_{}: [(f64, f64); {}] = [".format(name, order, order))
for w, x in zip(weights, abscissae):
out.append(" ({!r}, {!r}),".format(float(w), float(x)))
out.append("];")
out.append("")
out.append("pub static {}: [&[(f64, f64)]; {}] = [".format(name, MAX_ORDER + 1))
out.append(" &[],")
for i in range(1, MAX_ORDER + 1):
out.append(" &{}_{},".format(name, i))
out.append("];")
out.append("")
return "\n".join(out)
def main():
os.makedirs(OUT_DIR, exist_ok=True)
for stem, (name, gauss) in FAMILIES.items():
path = os.path.join(OUT_DIR, stem + ".rs")
with open(path, "w") as f:
f.write(render(name, gauss))
print("wrote", path)
if __name__ == "__main__":
main()