batmat 0.0.21
Batched linear algebra routines
Loading...
Searching...
No Matches
norms.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file
4/// Vector reductions.
5/// @ingroup topic-utilities
6
7#include <batmat/config.hpp>
8#include <batmat/simd.hpp>
9#include <cmath>
10
11namespace batmat::linalg {
12
13/// @addtogroup topic-utilities
14/// @{
15
16/// Utilities for computing vector norms.
17/// @tparam T Scalar type.
18/// @tparam simd SIMD type. Void for scalar-only.
19template <class T, class simd = void>
20struct norms;
21
22template <class T>
23struct norms<T>;
24
25template <class T, class simd>
26struct norms : norms<T> {
27 /// Accumulator.
28 using result = typename norms<T>::result;
29 /// Lane-wise accumulators.
30 struct result_simd {
31 simd amax;
32 simd asum;
33 simd sumsq;
34 };
35
36 using norms<T>::operator();
37
38 /// Update the accumulator with a new value.
39 result_simd operator()(result_simd accum, simd t) const {
40 using std::abs;
41 using std::max;
42 auto at = abs(t);
43 return {.amax = max(at, accum.amax), .asum = at + accum.asum, .sumsq = t * t + accum.sumsq};
44 }
45
46 /// Reduce the SIMD accumulator to a scalar result.
48 using batmat::datapar::hmax;
49 return {hmax(accum.amax), reduce(accum.asum), reduce(accum.sumsq)};
50 }
51
52 using norms<T>::zero;
53 static result_simd zero_simd() { return {}; }
54};
55
56template <class T>
57struct norms<T, void> {
58 /// Accumulator.
59 struct result {
60 T amax; ///< Maximum absolute value (ignoring NaNs).
63
64 /// ℓ₁ norm.
65 [[nodiscard]] T norm_1() const { return asum; }
66 /// ℓ₂ norm.
67 [[nodiscard]] T norm_2() const {
68 using std::sqrt;
69 return sqrt(sumsq);
70 }
71 [[nodiscard]] T norm_inf() const {
72 using std::isfinite;
73 return isfinite(asum) ? amax : asum;
74 }
75 };
76
77 /// Update the accumulator with a new value.
78 result operator()(result accum, T t) const {
79 using std::abs;
80 using std::max;
81 auto at = abs(t);
82 return {.amax = max(at, accum.amax), .asum = at + accum.asum, .sumsq = t * t + accum.sumsq};
83 }
84
85 /// Combine two accumulators.
86 result operator()(result accum, result t) const {
87 using std::max;
88 return {max(accum.amax, t.amax), accum.asum + t.asum, accum.sumsq + t.sumsq};
89 }
90
91 /// Identity element for the reduction.
92 static result zero() { return {}; }
93};
94
95/// @}
96
97} // namespace batmat::linalg
T amax
Maximum absolute value (ignoring NaNs).
Definition norms.hpp:60
static result zero()
Identity element for the reduction.
Definition norms.hpp:92
result operator()(result accum, result t) const
Combine two accumulators.
Definition norms.hpp:86
result operator()(result accum, T t) const
Update the accumulator with a new value.
Definition norms.hpp:78
Utilities for computing vector norms.
Definition norms.hpp:26
result operator()(result_simd accum) const
Reduce the SIMD accumulator to a scalar result.
Definition norms.hpp:47
result_simd operator()(result_simd accum, simd t) const
Update the accumulator with a new value.
Definition norms.hpp:39
static result_simd zero_simd()
Definition norms.hpp:53
typename norms< T >::result result
Accumulator.
Definition norms.hpp:28
Lane-wise accumulators.
Definition norms.hpp:30