Automatic Differentiation
 
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
6#include <vector>
7#include <tuple>
8#include <type_traits>
9
10namespace stan {
11namespace math {
12
21template <typename PromotionScalar, typename UnPromotedType,
22 require_constructible_t<PromotionScalar, UnPromotedType>* = nullptr,
23 require_not_same_t<PromotionScalar, UnPromotedType>* = nullptr,
24 require_all_not_tuple_t<PromotionScalar, UnPromotedType>* = nullptr>
25inline constexpr auto promote_scalar(UnPromotedType&& x) {
26 return PromotionScalar(std::forward<UnPromotedType>(x));
27}
28
37template <
38 typename PromotionScalar, typename UnPromotedType,
40inline constexpr auto promote_scalar(UnPromotedType&& x) noexcept {
41 return std::forward<UnPromotedType>(x);
42}
43
52template <typename PromotionScalar, typename UnPromotedType,
54 require_not_same_t<PromotionScalar,
56inline auto promote_scalar(UnPromotedType&& x) {
57 return x.template cast<PromotionScalar>();
58}
59
60// Forward decl for iterating over tuples used in std::vector<tuple>
61template <typename PromotionScalars, typename UnPromotedTypes,
62 require_all_tuple_t<PromotionScalars, UnPromotedTypes>* = nullptr,
64inline constexpr promote_scalar_t<PromotionScalars, UnPromotedTypes>
65promote_scalar(UnPromotedTypes&& x);
66
75template <typename PromotionScalar, typename UnPromotedType,
77 require_not_same_t<PromotionScalar,
79inline auto promote_scalar(UnPromotedType&& x) {
80 const auto x_size = x.size();
81 promote_scalar_t<PromotionScalar, UnPromotedType> ret(x_size);
82 for (size_t i = 0; i < x_size; ++i) {
83 ret[i] = promote_scalar<PromotionScalar>(x[i]);
84 }
85 return ret;
86}
87
97template <typename PromotionScalars, typename UnPromotedTypes,
98 require_all_tuple_t<PromotionScalars, UnPromotedTypes>*,
99 require_not_same_t<PromotionScalars, UnPromotedTypes>*>
100inline constexpr promote_scalar_t<PromotionScalars, UnPromotedTypes>
101promote_scalar(UnPromotedTypes&& x) {
102 return index_apply<std::tuple_size<std::decay_t<UnPromotedTypes>>::value>(
103 [&x](auto... Is) {
104 return std::make_tuple(
105 promote_scalar<std::decay_t<decltype(std::get<Is>(
106 std::declval<PromotionScalars>()))>>(std::get<Is>(x))...);
107 });
108}
109
110} // namespace math
111} // namespace stan
112
113#endif
require_t< is_eigen< std::decay_t< T > > > require_eigen_t
Require type satisfies is_eigen.
Definition is_eigen.hpp:110
require_not_t< std::is_same< std::decay_t< T >, std::decay_t< S > > > require_not_same_t
Require types T and S does not satisfy std::is_same.
require_t< std::is_same< std::decay_t< T >, std::decay_t< S > > > require_same_t
Require types T and S satisfies std::is_same.
require_t< is_std_vector< std::decay_t< T > > > require_std_vector_t
Require type satisfies is_std_vector.
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
constexpr auto promote_scalar(UnPromotedType &&x)
Promote a scalar to another scalar type.
typename scalar_type< T >::type scalar_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...