| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | //====================================================================================================================== | ||
| 2 | /* | ||
| 3 | POLYFLOAT - Extended precision floating points | ||
| 4 | Copyright : POLYFLOAT Contributors & Maintainers | ||
| 5 | SPDX-License-Identifier: BSL-1.0 | ||
| 6 | */ | ||
| 7 | //====================================================================================================================== | ||
| 8 | #pragma once | ||
| 9 | |||
| 10 | #include <polyfloat/details/callable.hpp> | ||
| 11 | #include <polyfloat/types/concepts.hpp> | ||
| 12 | #include <polyfloat/types/traits.hpp> | ||
| 13 | #include <type_traits> | ||
| 14 | #include <polyfloat/module/math/pow_abs.hpp> | ||
| 15 | #include <polyfloat/module/core/maxmag.hpp> | ||
| 16 | |||
| 17 | namespace plf | ||
| 18 | { | ||
| 19 | |||
| 20 | template<typename Options> | ||
| 21 | struct lpnorm_t : eve::strict_tuple_callable<lpnorm_t, Options, raw_option, pedantic_option> | ||
| 22 | { | ||
| 23 | template<typename... Ts> struct result : as_polyfloat_like<Ts...> | ||
| 24 | { | ||
| 25 | }; | ||
| 26 | |||
| 27 | template<concepts::polyfloat_like... Ts> | ||
| 28 | requires(eve::same_lanes_or_scalar<Ts...>) | ||
| 29 | 52 | POLYFLOAT_ABI typename result<Ts...>::type constexpr operator()(Ts... ts) const noexcept | |
| 30 | { | ||
| 31 | 52 | return POLYFLOAT_CALL(ts...); | |
| 32 | } | ||
| 33 | |||
| 34 | template<eve::non_empty_product_type Tup> | ||
| 35 | requires(eve::same_lanes_or_scalar_tuple<Tup> && !concepts::polyfloat_like<Tup>) | ||
| 36 | POLYFLOAT_ABI constexpr kumi::apply_traits_t<result, Tup> operator()(Tup const& t) const noexcept | ||
| 37 | requires(kumi::size_v<Tup> >= 1) | ||
| 38 | { | ||
| 39 | return POLYFLOAT_CALL(t); | ||
| 40 | } | ||
| 41 | |||
| 42 | 52 | POLYFLOAT_CALLABLE_OBJECT(lpnorm_t, lpnorm_); | |
| 43 | }; | ||
| 44 | //====================================================================================================================== | ||
| 45 | //! @addtogroup math | ||
| 46 | //! @{ | ||
| 47 | //! @var lpnorm | ||
| 48 | //! @brief object computing the lpnorm operation \f$ \left(\sum_{i = 0}^n |x_i|^p\right)^{\frac1p} \f$. | ||
| 49 | //! | ||
| 50 | //! | ||
| 51 | //! @groupheader{Header file} | ||
| 52 | //! | ||
| 53 | //! @code | ||
| 54 | //! #include <polyfloat/core.hpp> | ||
| 55 | //! @endcode | ||
| 56 | //! | ||
| 57 | //! @groupheader{Callable Signatures} | ||
| 58 | //! | ||
| 59 | //! @code | ||
| 60 | //! namespace polyfloat | ||
| 61 | //! { | ||
| 62 | //! template<polyfloat::concepts::polyfloat_like Z, polyfloat_like ... Zs> constexpr auto lpnorm(Z z, Zs... zs) noexcept; | ||
| 63 | //! } | ||
| 64 | //! @endcode | ||
| 65 | //! | ||
| 66 | //! **Parameters** | ||
| 67 | //! | ||
| 68 | //! * `z, zs...`: Values to process. | ||
| 69 | //! | ||
| 70 | //! **Return value** | ||
| 71 | //! | ||
| 72 | //! Returns the sum of the arguments. | ||
| 73 | //! | ||
| 74 | //! @groupheader{Example} | ||
| 75 | //! | ||
| 76 | //! @godbolt{doc/math/lpnorm.cpp} | ||
| 77 | //====================================================================================================================== | ||
| 78 | |||
| 79 | inline constexpr auto lpnorm = eve::functor<lpnorm_t>; | ||
| 80 | //====================================================================================================================== | ||
| 81 | //! @} | ||
| 82 | //====================================================================================================================== | ||
| 83 | } | ||
| 84 | |||
| 85 | namespace plf::_ | ||
| 86 | { | ||
| 87 | template<typename T0, eve::callable_options O> constexpr auto lpnorm_(POLYFLOAT_DELAY(), O const&, T0 t0) noexcept | ||
| 88 | { | ||
| 89 | return plf::abs(t0); | ||
| 90 | } | ||
| 91 | |||
| 92 | template<typename P, typename... Ts, eve::callable_options O> | ||
| 93 | 52 | constexpr auto lpnorm_(POLYFLOAT_DELAY(), O const& o, P const& p, Ts... ts) noexcept | |
| 94 | { | ||
| 95 | using r_t = as_polyfloat_like_t<P, Ts...>; | ||
| 96 | // using u_t = eve::underlying_type_t<r_t>; | ||
| 97 | using e_t = eve::element_type_t<r_t>; | ||
| 98 | 74 | auto cvt = [](auto a) { return plf::convert(a, eve::as<e_t>()); }; | |
| 99 | 52 | r_t rp(p); | |
| 100 | 52 | auto e = -eve::maxmag(plf::if_else(plf::is_nan(ts), zero, eve::exponent(hi(ts)))...); | |
| 101 | 126 | auto f = [&](auto a) { return plf::pow_abs(plf::ldexp[eve::pedantic](cvt(a), e), rp); }; | |
| 102 | 52 | r_t that = plf::add[o](f(ts)...); | |
| 103 | 52 | return plf::ldexp[eve::pedantic](plf::pow_abs(that, plf::rec[eve::pedantic](rp)), -e); | |
| 104 | } | ||
| 105 | } | ||
| 106 |