Automatic Differentiation
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
promote_scalar.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_PROMOTE_SCALAR_HPP
2#define STAN_MATH_PRIM_FUN_PROMOTE_SCALAR_HPP
3
7#include <vector>
8#include <tuple>
9#include <type_traits>
10
11namespace stan {
12namespace math {
13
14template <typename PromotionScalars, typename UnPromotedTypes>
15inline constexpr auto promote_scalar(UnPromotedTypes&& x) {
16 using unpromoted_scalar_t = scalar_type_t<UnPromotedTypes>;
17 constexpr bool both_tuples
18 = is_tuple_v<PromotionScalars> && is_tuple_v<UnPromotedTypes>;
19 if constexpr (std::is_same_v<PromotionScalars, unpromoted_scalar_t>) {
20 return std::forward<UnPromotedTypes>(x);
21 } else if constexpr (both_tuples) {
22 return index_apply<std::tuple_size<std::decay_t<UnPromotedTypes>>::value>(
23 [&x](auto... Is) {
24 return std::make_tuple(
25 promote_scalar<std::decay_t<decltype(std::get<Is>(
26 std::declval<PromotionScalars>()))>>(std::get<Is>(x))...);
27 });
28 } else if constexpr (is_tuple_v<UnPromotedTypes>) {
29 return stan::math::apply(
30 [](auto&&... args) {
31 return std::make_tuple(promote_scalar<PromotionScalars>(
32 std::forward<decltype(args)>(args))...);
33 },
34 std::forward<UnPromotedTypes>(x));
35 } else if constexpr (is_std_vector_v<UnPromotedTypes>) {
36 const auto x_size = x.size();
38 for (size_t i = 0; i < x_size; ++i) {
39 ret[i] = promote_scalar<PromotionScalars>(x[i]);
40 }
41 return ret;
42 } else if constexpr (is_eigen_v<UnPromotedTypes>) {
43 return std::forward<UnPromotedTypes>(x).template cast<PromotionScalars>();
44 } else if constexpr (is_stan_scalar_v<UnPromotedTypes>) {
45 return PromotionScalars(std::forward<UnPromotedTypes>(x));
46 } else {
47 static_assert(sizeof(std::decay_t<UnPromotedTypes>*) == 0,
48 "INTERNAL ERROR:(promote_scalar) "
49 "Unrecognized type for promotion. "
50 "This is an internal error, please report it: "
51 "https://github.com/stan-dev/math/issues");
52 }
53}
54
55} // namespace math
56} // namespace stan
57
58#endif
constexpr auto promote_scalar(UnPromotedTypes &&x)
typename promote_scalar_type< std::decay_t< T >, std::decay_t< S > >::type promote_scalar_t
constexpr decltype(auto) apply(F &&f, Tuple &&t, PreArgs &&... pre_args)
Definition apply.hpp:51
typename scalar_type< T >::type scalar_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...