Copyright (C) 2020 Andreas Kloeckner
Here's the contents of a file containing numerics code:
!pygmentize norms.py
import numpy as np def norm_1(ary): """Computes the 1-norm of vectors or matrices *ary* passed in as numpy arrays.""" if len(ary.shape) == 1: return np.sum(np.abs(ary)) elif len(ary.shape) == 2: return np.max(np.sum(np.abs(ary), axis=1)) else: raise ValueError("ary must be vector or matrix")
Note:
!pygmentize test_norms.py
import numpy as np def test_norm_1(): from norms import norm_1 for i in range(10): A = np.random.randn(20, 20) x = np.random.randn(20) assert norm_1(A@x) <= norm_1(A) * norm_1(x)
!python -m pytest
============================= test session starts ============================== platform linux -- Python 3.7.2+, pytest-4.0.2, py-1.7.0, pluggy-0.8.0 rootdir: /home/andreas, inifile: pytest.ini plugins: celery-4.2.1 collected 1 item test_norms.py . [100%] =========================== 1 passed in 0.13 seconds ===========================
A typical use for these tests would be to run them on every commit to a codebase.
Example: https://github.com/inducer/boxtree (click the "Pipeline" button)