| 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/core/constants/eps.hpp> | ||
| 15 | #include <polyfloat/module/core/abs.hpp> | ||
| 16 | #include <polyfloat/module/core/average.hpp> | ||
| 17 | #include <polyfloat/module/core/exponent.hpp> | ||
| 18 | #include <polyfloat/module/core/if_else.hpp> | ||
| 19 | #include <polyfloat/module/core/is_eqz.hpp> | ||
| 20 | #include <polyfloat/module/core/is_infinite.hpp> | ||
| 21 | #include <polyfloat/module/core/is_lez.hpp> | ||
| 22 | #include <polyfloat/module/core/is_unordered.hpp> | ||
| 23 | #include <polyfloat/module/core/ldexp.hpp> | ||
| 24 | #include <polyfloat/module/core/sign.hpp> | ||
| 25 | #include <polyfloat/module/core/sqrt.hpp> | ||
| 26 | |||
| 27 | namespace plf | ||
| 28 | { | ||
| 29 | |||
| 30 | template<typename Options> struct agm_t : plf::callable<agm_t, eve::agm_t, Options, raw_option, pedantic_option> | ||
| 31 | { | ||
| 32 | template<concepts::polyfloat_like T0, concepts::polyfloat_like T1> | ||
| 33 | requires(eve::same_lanes_or_scalar<T0, T1>) | ||
| 34 | 20 | POLYFLOAT_ABI as_polyfloat_like_t<T0, T1> constexpr operator()(T0 t0, T1 t1) const noexcept | |
| 35 | { | ||
| 36 | 20 | return POLYFLOAT_CALL(t0, t1); | |
| 37 | } | ||
| 38 | |||
| 39 | 20 | POLYFLOAT_CALLABLE_OBJECT(agm_t, agm_); | |
| 40 | }; | ||
| 41 | //====================================================================================================================== | ||
| 42 | //! @addtogroup core | ||
| 43 | //! @{ | ||
| 44 | //! @var agm | ||
| 45 | //! @brief return the arithmetic-geometric mean. | ||
| 46 | //! | ||
| 47 | //! @groupheader{Header file} | ||
| 48 | //! | ||
| 49 | //! @code | ||
| 50 | //! #include <polyfloat/module/core.hpp> | ||
| 51 | //! @endcode | ||
| 52 | //! | ||
| 53 | //! @groupheader{Callable Signatures} | ||
| 54 | //! | ||
| 55 | //! @code | ||
| 56 | //! namespace polyfloat | ||
| 57 | //! { | ||
| 58 | //! template<polyfloat::concepts::polyfloat_like Z1, polyfloat_like Z2> constexpr auto agm(Z1 z1, Z2 z2) noexcept; | ||
| 59 | //! } | ||
| 60 | //! @endcode | ||
| 61 | //! | ||
| 62 | //! **Parameters** | ||
| 63 | //! | ||
| 64 | //! * `z1`, `z2`: Values to process. | ||
| 65 | //! | ||
| 66 | //! **Return value** | ||
| 67 | //! | ||
| 68 | //! The value of the arithmetic-geometric mean is returned. No overflow can occur. | ||
| 69 | //! The two parameters must share the same sign. | ||
| 70 | //! | ||
| 71 | //! @groupheader{External references} | ||
| 72 | //! * [Wolfram MathWorld](https://mathworld.wolfram.com/Arithmetic-GeometricMean.html) | ||
| 73 | //! * [Wikipedia](https://en.wikipedia.org/wiki/Arithmetic-geometric_mean) | ||
| 74 | //! | ||
| 75 | //! @groupheader{Example} | ||
| 76 | //! | ||
| 77 | //! @godbolt{doc/core/agm.cpp} | ||
| 78 | //====================================================================================================================== | ||
| 79 | |||
| 80 | inline constexpr auto agm = eve::functor<agm_t>; | ||
| 81 | //====================================================================================================================== | ||
| 82 | //! @} | ||
| 83 | //====================================================================================================================== | ||
| 84 | } | ||
| 85 | |||
| 86 | namespace plf::_ | ||
| 87 | { | ||
| 88 | |||
| 89 | template<typename T0, typename T1, eve::callable_options O> | ||
| 90 | 20 | constexpr auto agm_(POLYFLOAT_DELAY(), O const&, T0 const& t0, T1 const& t1) noexcept | |
| 91 | { | ||
| 92 | using r_t = as_polyfloat_like_t<T0, T1>; | ||
| 93 | 20 | auto cvt = [](auto a) { return plf::convert(a, eve::as_element<r_t>{}); }; | |
| 94 | 20 | auto a = cvt(t0); | |
| 95 | 20 | auto b = cvt(t1); | |
| 96 | 20 | auto ex = exponent(average(a, b)); | |
| 97 | 20 | auto r = nan(as<r_t>()); | |
| 98 | 20 | auto null = is_eqz(a) || is_eqz(b); | |
| 99 | 20 | r = if_else(null, zero, r); | |
| 100 | 20 | auto infi = is_infinite(a) || is_infinite(b); | |
| 101 | 20 | r = if_else(infi, a + b, r); | |
| 102 | 20 | auto unord = is_unordered(a, b); | |
| 103 | 20 | auto done = is_lez(sign(a) * sign(b)) || unord || infi; | |
| 104 | 20 | a = if_else(done, zero, a); | |
| 105 | 20 | b = if_else(done, zero, b); | |
| 106 | 20 | a = ldexp(a, -ex); | |
| 107 | 20 | b = ldexp(b, -ex); | |
| 108 | 20 | auto c = average(a, -b); | |
| 109 |
2/2✓ Branch 6 taken 68 times.
✓ Branch 7 taken 10 times.
|
156 | while (eve::any(abs(c) > 2 * eps(as(c)))) |
| 110 | { | ||
| 111 | 136 | auto an = average(a, b); | |
| 112 | 136 | auto bn = sqrt(a * b); | |
| 113 | 136 | c = average(a, -b); | |
| 114 | 136 | a = an; | |
| 115 | 136 | b = bn; | |
| 116 | } | ||
| 117 | 20 | return if_else(done, r, ldexp(b, ex)); | |
| 118 | } | ||
| 119 | } | ||
| 120 |