A robot sensor calibration routine can reduce a coupled measurement problem to a triangular system. The solve is quick, and a wrong diagonal setting can still produce plausible coefficients. In SciPy, solve_triangular uses the supplied diagonal by default. Setting unit_diagonal=True tells the solver to treat every diagonal entry as one. An explicit calibration gain on the diagonal then disappears from the system being solved.
Build a fixture with unequal gains
Consider two channels where the second measurement includes a contribution from the first unknown. The first equation has a gain of 2. The second has a coupling coefficient of 3 and its own gain of 4. With measurements of 2 and 11, forward substitution gives unknowns of 1 and 2. In the unit-diagonal interpretation, the first unknown becomes 2 and the second becomes 5. Both calculations return finite values.
import numpy as np
from scipy.linalg import solve_triangular
lower = np.array([[2., 0.], [3., 4.]])
measured = np.array([2., 11.])
explicit = solve_triangular(lower, measured, lower=True)
unit = solve_triangular(
lower, measured, lower=True, unit_diagonal=True
)
np.testing.assert_allclose(explicit, [1., 2.])
np.testing.assert_allclose(unit, [2., 5.])
np.testing.assert_allclose(lower @ explicit, measured)
Running the fixture reproduced both expected solutions. Multiplying the unit-diagonal result by the original matrix reconstructs measurements of 4 and 26. The residual against the original measurements is therefore 2 and 15. A test that only checks the output shape or whether the coefficients are finite would miss that error.
Read the factor's storage contract
A factor produced by another routine may have a mathematically unit diagonal, with the diagonal entries left implicit in storage. In that case, the unit-diagonal flag expresses the factor's actual meaning. LAPACK's DTRTRS arguments distinguish unit and non-unit triangular matrices, and the SciPy option expresses the same distinction for its Python caller. Establish which contract applies when reading the factor, especially when two factors share storage or come from different producers.
For a small calibration regression test, reconstruct the measured vector using the intended factor and compare the residual in measurement units. Give the example distinct non-unit diagonal entries so the flag choice changes the result. If the production solve represents only one stage in a factorization, validate the full recovered solution against the original calibration equations too.
Sources
https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.solve_triangular.html
https://netlib.org/lapack/double/dtrtrs.f