.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/verification/polynomial_degree.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_verification_polynomial_degree.py: Polynomial degree of every method ================================= The *degree* of a quadrature method is the highest polynomial degree it integrates exactly. It is the main accuracy property of a method: a method of degree d makes an error on one subinterval of width h that falls like h^(d + 2) for a smooth integrand, so a higher degree means fewer subintervals for the same accuracy. This script measures the degree of every method in padaquad from its stored nodes and weights, and checks it against the degree the method claims (``method.degree``). It also checks each method's *error estimate*. padaquad estimates the error on a subinterval as the difference between the method and a lower-degree method that shares its nodes; the solver assumes that this lower-degree method has degree ``error_exponent - 2``, and the script checks that too. Run it with:: python examples/verification/polynomial_degree.py It prints one row per method and exits with status 1 if any check fails. The same checks run in the test suite (``tests/test_exactness.py``). .. GENERATED FROM PYTHON SOURCE LINES 28-41 Part 1: measure every method ---------------------------- ``measure_degree`` integrates the Legendre polynomials P_0, P_1, P_2, ... over one subinterval and compares each result with the exact value. The degree is the last polynomial integrated without error (to 1e-12). "Miss" is the error on the first polynomial the method gets wrong; it is far above rounding (about 1e-16), so the measurement is unambiguous. ``interpolatory3_variable`` computes its weights from the actual node positions. With the middle node exactly at the center it becomes Simpson's rule, which has one degree more, so it is measured with the middle node at 0.3, which shows its general degree. .. GENERATED FROM PYTHON SOURCE LINES 41-76 .. code-block:: Python from __future__ import annotations import torch from padaquad import UNIFORM_METHODS, VARIABLE_METHODS GENERAL_POSITION = { "interpolatory3_variable": torch.tensor([0.0, 0.3, 1.0], dtype=torch.float64) } methods = dict(UNIFORM_METHODS) methods.update({f"{name} (variable)": m for name, m in VARIABLE_METHODS.items()}) failures = 0 print( f"{'method':34s} {'nodes':>5s} {'claimed':>7s} {'measured':>8s} {'miss':>9s} ", end="", ) print(f"{'estimate: expected':>18s} {'measured':>8s}") for label, method in methods.items(): nodes = GENERAL_POSITION.get(label.removesuffix(" (variable)")) measured = method.measure_degree(nodes) miss = float(measured.errors[measured.degree + 1]) ok = ( measured.degree == method.degree and measured.lower_degree == method.error_exponent - 2 ) failures += not ok print( f"{label:34s} {method.n_nodes:5d} {method.degree:7d} {measured.degree:8d} " f"{miss:9.1e} {method.error_exponent - 2:18d} {measured.lower_degree:8d} " f"[{'OK ' if ok else 'FAIL'}]" ) .. rst-class:: sphx-glr-script-out .. code-block:: none method nodes claimed measured miss estimate: expected measured adaptive_heun 2 1 1 1.0e+00 0 0 [OK ] fehlberg2 3 1 1 -4.9e-01 0 0 [OK ] bosh3 4 2 2 -4.2e-01 1 1 [OK ] dopri5 7 4 4 -4.7e-02 3 3 [OK ] gk7 9 11 11 9.3e-02 5 5 [OK ] gk15 17 23 23 5.5e-03 13 13 [OK ] gk21 23 31 31 9.4e-04 19 19 [OK ] gk31 33 47 47 5.9e-04 29 29 [OK ] cc5 5 5 5 -1.4e-01 3 3 [OK ] cc9 9 9 9 -6.5e-03 5 5 [OK ] cc17 17 17 17 -5.4e-04 9 9 [OK ] cc33 33 33 33 -4.8e-05 17 17 [OK ] cc65 65 65 65 -4.2e-06 33 33 [OK ] adaptive_heun (variable) 2 1 1 1.0e+00 0 0 [OK ] interpolatory3_variable (variable) 3 2 2 6.7e-01 1 1 [OK ] .. GENERATED FROM PYTHON SOURCE LINES 77-96 Part 2: why the test uses Legendre polynomials and not powers ------------------------------------------------------------- A simpler test would integrate the powers x^0, x^1, x^2, ... of x = 2t - 1 (the subinterval [0, 1] mapped to [-1, 1]) instead. That test fails for the high-degree methods, and this part shows why. A high power of x looks almost exactly like a combination of lower powers, which the method integrates perfectly. Only a small fraction of x^k is new at degree k: the fraction (k!)^2 2^k / (2k)!, about 4e-14 for k = 48. So the method's error on x^(degree + 1) is its Legendre miss times that fraction. (Powers of t itself are worse: there the fraction is (k!)^2 / (2k)!.) When that product is below float64 rounding (about 1e-16), the power test cannot see the miss and reports a degree that is too high. The columns: the true degree; the degree a power test reports (tolerance 1e-12, searching up to 2 x nodes, the most any method can reach); the Legendre miss; and the power miss it implies, which is what the power test would have to detect. .. GENERATED FROM PYTHON SOURCE LINES 96-128 .. code-block:: Python print() print( f"{'method':14s} {'degree':>6s} {'power test says':>15s} {'Legendre miss':>13s} ", end="", ) print(f"{'power miss':>11s}") for name, method in UNIFORM_METHODS.items(): c = method.node_fractions() b = method.weights(c[None, :, None])[0][0, :, 0] x = 2 * c - 1 power_degree = None for k in range(2 * len(c) + 1): exact = 1.0 / (k + 1) if k % 2 == 0 else 0.0 # integral of x^k over [0, 1] if abs(float(b @ x**k) - exact) > 1e-12: power_degree = k - 1 break measured = method.measure_degree() k = measured.degree + 1 share = torch.exp( # (k!)^2 2^k / (2k)!, in logs to avoid overflow 2 * torch.lgamma(torch.tensor(k + 1.0)) + k * torch.log(torch.tensor(2.0)) - torch.lgamma(torch.tensor(2 * k + 1.0)) ) legendre_miss = float(measured.errors[k]) says = f">= {2 * len(c)}" if power_degree is None else str(power_degree) flag = "" if says == str(measured.degree) else " <- wrong" print( f"{name:14s} {measured.degree:6d} {says:>15s} {legendre_miss:13.1e} " f"{float(share) * legendre_miss:11.1e}{flag}" ) .. rst-class:: sphx-glr-script-out .. code-block:: none method degree power test says Legendre miss power miss adaptive_heun 1 1 1.0e+00 6.7e-01 fehlberg2 1 1 -4.9e-01 -3.3e-01 bosh3 2 2 -4.2e-01 -1.7e-01 dopri5 4 4 -4.7e-02 -5.9e-03 gk7 11 11 9.3e-02 1.4e-04 gk15 23 23 5.5e-03 2.9e-09 gk21 31 31 9.4e-04 2.2e-12 gk31 47 63 5.9e-04 2.6e-17 <- wrong cc5 5 5 -1.4e-01 -9.5e-03 cc9 9 9 -6.5e-03 -3.6e-05 cc17 17 17 -5.4e-04 -1.6e-08 cc33 33 37 -4.8e-05 -2.9e-14 <- wrong cc65 65 >= 130 -4.2e-06 -8.3e-25 <- wrong .. GENERATED FROM PYTHON SOURCE LINES 129-131 Result ------ .. GENERATED FROM PYTHON SOURCE LINES 131-138 .. code-block:: Python print() if failures: print(f"[FAIL] {failures} method(s) do not have the degree they claim.") raise SystemExit(1) print("All methods have the degree they claim, and every error estimate has the") print("strength the solver assumes.") .. rst-class:: sphx-glr-script-out .. code-block:: none All methods have the degree they claim, and every error estimate has the strength the solver assumes. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.014 seconds) .. _sphx_glr_download_auto_examples_verification_polynomial_degree.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: polynomial_degree.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: polynomial_degree.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: polynomial_degree.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_