From f87e167540737b6b7f189ee0b25f040d76f4a4a7 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 5 Nov 2024 17:38:54 -0500 Subject: [PATCH 01/19] testing encapsulation of std complex --- stan/math/prim/fun/abs.hpp | 8 ++- stan/math/prim/meta.hpp | 1 + stan/math/prim/meta/base_type.hpp | 13 +---- stan/math/prim/meta/is_complex.hpp | 18 +++++++ stan/math/prim/meta/is_real.hpp | 79 ++++++++++++++++++++++++++++++ 5 files changed, 106 insertions(+), 13 deletions(-) create mode 100644 stan/math/prim/meta/is_real.hpp diff --git a/stan/math/prim/fun/abs.hpp b/stan/math/prim/fun/abs.hpp index 317fd6a846d..0bea6e321e0 100644 --- a/stan/math/prim/fun/abs.hpp +++ b/stan/math/prim/fun/abs.hpp @@ -34,7 +34,13 @@ inline T abs(T x) { * @param x argument * @return absolute value of argument (a real number) */ -template * = nullptr> +template * = nullptr> +inline auto abs(T x) { + using std::hypot; + return hypot(x.real(), x.imag()); +} + +template * = nullptr> inline auto abs(T x) { return hypot(x.real(), x.imag()); } diff --git a/stan/math/prim/meta.hpp b/stan/math/prim/meta.hpp index 68d89d34ba5..445816ca69a 100644 --- a/stan/math/prim/meta.hpp +++ b/stan/math/prim/meta.hpp @@ -110,6 +110,7 @@ #include #include #include +#include #include #include #include diff --git a/stan/math/prim/meta/base_type.hpp b/stan/math/prim/meta/base_type.hpp index 80cebb44cb0..eb70e7592b0 100644 --- a/stan/math/prim/meta/base_type.hpp +++ b/stan/math/prim/meta/base_type.hpp @@ -2,7 +2,6 @@ #define STAN_MATH_PRIM_META_BASE_TYPE_HPP #include -#include #include #include #include @@ -53,17 +52,7 @@ struct base_type::value>> { using type = base_type_t::Scalar>; }; -/** - * Template metaprogram defining the base type for values - * stored in a complex number. - * - * @tparam T type of complex number - * @ingroup type_trait - */ -template -struct base_type::value>> { - using type = base_type_t::value_type>; -}; + } // namespace stan #endif diff --git a/stan/math/prim/meta/is_complex.hpp b/stan/math/prim/meta/is_complex.hpp index 3d76b1b16ac..2a7d77d39f1 100644 --- a/stan/math/prim/meta/is_complex.hpp +++ b/stan/math/prim/meta/is_complex.hpp @@ -1,6 +1,7 @@ #ifndef STAN_MATH_PRIM_META_IS_COMPLEX_HPP #define STAN_MATH_PRIM_META_IS_COMPLEX_HPP +#include #include #include #include @@ -55,6 +56,18 @@ struct scalar_type::value>> { using type = std::complex::value_type>; }; +/** + * Template metaprogram defining the base type for values + * stored in a complex number. + * + * @tparam T type of complex number + * @ingroup type_trait + */ +template +struct base_type::value>> { + using type = base_type_t::value_type>; +}; + /*! \ingroup require_stan_scalar_complex */ /*! \defgroup complex_types complex */ /*! \addtogroup complex_types */ @@ -107,6 +120,11 @@ using require_not_vt_complex template using require_not_st_complex = require_not_t>>>; + +/*! \brief Require `T` is complex and it's @ref base_type satisfies a given type trait */ +/*! @tparam T A type with a valid overload of @ref base_type available */ +template