MTL5 -- Matrix Template Library C++ API
mtl:: -- C++20 header-only linear algebra for mixed-precision algorithm design
Loading...
Searching...
No Matches
mtl::detail::norms_adl Namespace Reference

The default two_norm and frobenius_norm sum the squares unscaled, which overflows for elements above about sqrt(max) and loses everything below about sqrt(min) (#690). More...

Concepts

concept  rescalable_sum_squares

Functions

template<typename Mag, typename ForEachAbs>
Mag rescaled_two_norm (ForEachAbs &&for_each_abs)
 sqrt(sum a^2) over the magnitudes that for_each_abs(f) passes to f, computed with a power-of-two scale so that neither the squares nor their sum overflow or underflow.

Detailed Description

The default two_norm and frobenius_norm sum the squares unscaled, which overflows for elements above about sqrt(max) and loses everything below about sqrt(min) (#690).

Rather than scale every element, as LAPACK's dnrm2 does, they keep the plain sum and call this afterwards: when the sum has reached max() (an infinity, or a saturated posit), or is too close to the underflow threshold for the small squares to have been accumulated accurately, it is recomputed in a second pass scaled by a power of two near max|a|. An ordinary input never rescans, so its result is bit-identical to the unscaled sum.

The rescan needs frexp, ldexp and sqrt, found by ADL so that a number library's own (lns, fixpnt, posit, takum, ...) are used, with the std versions brought in by using-declarations for the built-in types; and a std::numeric_limits specialization for max(), min() and epsilon(). A type without them, and any integer type, keeps the plain sum, so integers keep their wrapping semantics.

Function Documentation

◆ rescaled_two_norm()

template<typename Mag, typename ForEachAbs>
Mag mtl::detail::norms_adl::rescaled_two_norm ( ForEachAbs && for_each_abs)

sqrt(sum a^2) over the magnitudes that for_each_abs(f) passes to f, computed with a power-of-two scale so that neither the squares nor their sum overflow or underflow.

In a binary floating-point type the scaling is exact; in a tapered type it also keeps the squares near 1, where the type is most precise.