multicalc 0.6.0

Rust scientific computing for single and multi-variable calculus
Documentation
"""Generate Gaussian quadrature node/weight tables for orders 1..=MAX_ORDER.

Requires numpy. Emits one module file per family into src/gaussian_tables/,
each holding `(weight, abscissa)` arrays plus a flat index table used by
`gaussian_tables::nodes`.

Run from anywhere:  python3 scripts/build_gaussian_integration_tables.py
"""

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()