Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
complex_norm.hpp
Go to the documentation of this file.
1// parasoft-suppress AUTOSAR-A2_8_1-a "False positive: complex_l1_norm_impl implements the complex norm"
2
3// Copyright 2026, Toyota Motor Corporation
4//
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
7#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_COMPLEX_NORM_HPP_
8#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_COMPLEX_NORM_HPP_
9
10// IWYU pragma: private, include "arene/base/math.hpp"
11// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
12
13// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
18#include "arene/base/stdlib_choice/declval.hpp"
19#include "arene/base/stdlib_choice/enable_if.hpp"
20// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
21
22namespace arene {
23namespace base {
24
25namespace complex_norm_detail {
26
27/// @brief Function object class implementing @c arene::base::complex_l1_norm
28class complex_l1_norm_impl {
29 public:
30 /// @brief Compute the L1-norm of a possibly-complex value: |re(x)| + |im(x)|
31 /// @tparam T Type of the value to compute the norm of
32 /// @param value Value to compute the norm of
33 /// @return The sum of the absolute values of the real part of @c value and the imaginary part of @c value
34 /// @warning This is not the same operation as the @c norm member function of <c>std::complex</c>.
35 template <typename T, constraints<std::enable_if_t<math::is_complex_v<T>>> = nullptr>
36 constexpr auto operator()(T const& value) const noexcept(
37 noexcept(math::abs(math::real(std::declval<T const&>())) + math::abs(math::imag(std::declval<T const&>())))
38 ) -> decltype(math::abs(math::real(value)) + math::abs(math::imag(value))) {
39 // parasoft-begin-suppress AUTOSAR-M5_0_4-a "We must allow implicit conversions here to support user-defined types"
40 return math::abs(math::real(value)) + math::abs(math::imag(value));
41 // parasoft-end-suppress AUTOSAR-M5_0_4-a
42 }
43
44 /// @brief Compute the L1-norm of a possibly-complex value: |re(x)| + |im(x)|
45 /// @tparam T Type of the value to compute the norm of
46 /// @param value Value to compute the norm of
47 /// @return The sum of the absolute values of the real part of @c value and the imaginary part of @c value
48 /// @warning This is not the same operation as the @c norm member function of <c>std::complex</c>.
49 template <typename T, constraints<std::enable_if_t<!math::is_complex_v<T>>> = nullptr>
50 constexpr auto operator()(T const& value) const noexcept(noexcept(math::abs(std::declval<T const&>())))
51 -> decltype(math::abs(value)) {
52 return math::abs(value);
53 }
54};
55
56} // namespace complex_norm_detail
57
58// parasoft-begin-suppress AUTOSAR-M7_3_3-a "Unnamed namespace creates a per-TU reference to a single global object."
59// parasoft-begin-suppress CERT_CPP-DCL59-a "Unnamed namespace creates a per-TU reference to a single global object."
60/// @brief Compute the L1 norm of a possibly complex value: |re(x)| + |im(x)|
61/// @warning This is not the same operation as the @c norm member function of <c>std::complex</c>.
62ARENE_CPP14_INLINE_VARIABLE(complex_norm_detail::complex_l1_norm_impl, complex_l1_norm);
63// parasoft-end-suppress AUTOSAR-M7_3_3-a
64// parasoft-end-suppress CERT_CPP-DCL59-a
65
66} // namespace base
67} // namespace arene
68
69#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_COMPLEX_NORM_HPP_
#define ARENE_CPP14_INLINE_VARIABLE(type, name)
Emulates C++17's inline variable declarations for function objects.
Definition cpp14_inline.hpp:69
Definition accumulate.hpp:37
Definition accumulate.hpp:36
auto declval() noexcept -> T &&
A dummy function that is declared but not defined, with a user-specified return type,...