Testing Numerical and Scientific Code
Choose analytical cases, invariants, independent references and justified tolerances for numerical regression tests.
Use this skill when ordinary exact-equality tests cannot distinguish numerical roundoff from a scientific regression. Identify the quantity, units, valid input range, expected accuracy, precision and algorithmic assumptions. A tolerance is part of the scientific claim, not a setting to increase until tests turn green. ## Key Points - [Python floating-point closeness](https://docs.python.org/3/library/math.html#math.isclose) - [NumPy assert_allclose](https://numpy.org/doc/stable/reference/generated/numpy.testing.assert_allclose.html)
skilldb get mathematics-skills/testing-numerical-codeFull skill: 38 linesTesting Numerical and Scientific Code
Use this skill when ordinary exact-equality tests cannot distinguish numerical roundoff from a scientific regression. Identify the quantity, units, valid input range, expected accuracy, precision and algorithmic assumptions. A tolerance is part of the scientific claim, not a setting to increase until tests turn green.
Choose useful evidence
Begin with analytical examples: zero input where valid, an identity transform, a known integral, a small system with a hand-computed solution. Add degenerate and ill-conditioned cases separately. State when a case lies outside the algorithm's supported domain.
Test invariants such as conservation, symmetry, monotonicity, positive definiteness or normalization only when the mathematics actually guarantees them. Use metamorphic relations when a trustworthy exact answer is unavailable: scaling or reordering inputs may have a predictable effect. Do not impose permutation invariance on an order-sensitive algorithm.
Cross-check with an independently derived implementation or higher precision. Two functions calling the same underlying routine are not independent evidence. Check convergence behavior across step sizes, mesh resolutions or iteration limits where applicable.
Specify comparisons
Reject unexpected NaN and infinity before computing errors. Some assertion libraries consider paired NaNs equal by default; configure that intentionally. Verify shapes and dtypes separately so broadcasting does not conceal a wrong-dimensional answer.
For scalar symmetric closeness, a common criterion is absolute error bounded by the greater of absolute tolerance and relative tolerance times the larger magnitude. Libraries differ: NumPy's allclose-style rule uses an additive absolute and relative bound referenced to one argument. Read the exact comparison used by the test.
Derive absolute tolerance in the output's units, especially near zero. Relate relative tolerance to conditioning, floating-point precision and the required use of the result. Check both aggregate error and worst-case components when an average could hide an important failure.
Make regressions reproducible
Save random seeds, generator versions, parameters and a small failing fixture. Separate deterministic unit tests from statistical tests with stated false-alarm rates. Record compiler, library, hardware and parallelism settings for cross-platform differences.
Do not bless a new baseline merely because code changed. Explain the numerical difference, compare independent evidence and obtain the project's scientific review where needed. Preserve the old baseline and rationale.
Deliver test cases, tolerance derivations, observed error and limitations. A passing synthetic example validates that example; it does not establish the model's scientific validity across an untested population.
References
Install this skill directly: skilldb add mathematics-skills
Related Skills
Topology
Triggers when users need help with topology. Activate for questions about topological spaces, open sets, closed sets, compactness, connectedness, Hausdorff spaces, continuous maps, homeomorphisms, homotopy, fundamental group, covering spaces, manifolds, point-set topology, algebraic topology, quotient topology, product topology, topological data analysis, and persistent homology.
Abstract Algebra
Triggers when users need help with abstract algebra. Activate for questions about groups, rings, fields, homomorphisms, isomorphisms, quotient groups, quotient rings, Sylow theorems, normal subgroups, cosets, Lagrange's theorem, polynomial rings, ideals, field extensions, Galois theory, group actions, and applications in cryptography and coding theory.
Calculus
Triggers when users need help with calculus concepts and problems. Activate for questions about limits, continuity, derivatives, integrals, series, Taylor expansions, Fourier series, vector calculus, gradient, divergence, curl, Stokes' theorem, Green's theorem, multivariable calculus, partial derivatives, chain rule, implicit differentiation, integration by parts, substitution, improper integrals, and applications of calculus in physics and engineering.
Complex Analysis
Triggers when users need help with complex analysis. Activate for questions about complex functions, analytic functions, holomorphic functions, Cauchy-Riemann equations, contour integration, Cauchy's integral theorem, Cauchy's integral formula, Laurent series, residue theorem, poles, singularities, conformal mappings, Mobius transformations, and applications in fluid dynamics and electrical engineering.
Differential Equations
Triggers when users need help with differential equations. Activate for questions about ODEs, PDEs, separable equations, exact equations, linear ODEs, Bernoulli equations, systems of ODEs, heat equation, wave equation, Laplace equation, Fourier series, Fourier transforms, Laplace transforms, numerical methods for ODEs, Euler method, Runge-Kutta, stability analysis, phase portraits, and boundary value problems.
Discrete Mathematics
Triggers when users need help with discrete mathematics. Activate for questions about combinatorics, permutations, combinations, pigeonhole principle, inclusion-exclusion, graph theory, paths, trees, graph coloring, planar graphs, network flow, recurrence relations, generating functions, Boolean algebra, lattices, partial orders, and applications of discrete math in computer science.