Automatic Differentiation
 
Loading...
Searching...
No Matches
positive_constrain.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_CONSTRAINT_POSITIVE_CONSTRAIN_HPP
2#define STAN_MATH_PRIM_CONSTRAINT_POSITIVE_CONSTRAIN_HPP
3
7#include <cmath>
8
9namespace stan {
10namespace math {
11
22template <typename T>
23inline auto positive_constrain(T&& x) {
24 return exp(std::forward<T>(x));
25}
26
43template <typename T, typename S>
44inline auto positive_constrain(T&& x, S& lp) {
45 auto&& x_ref = to_ref(std::forward<T>(x));
46 lp += sum(x_ref);
47 return exp(std::forward<decltype(x_ref)>(x_ref));
48}
49
67template <bool Jacobian, typename T, typename Lp,
70inline auto positive_constrain(T&& x, Lp& lp) {
71 if constexpr (Jacobian) {
72 return positive_constrain(std::forward<T>(x), lp);
73 } else {
74 return positive_constrain(std::forward<T>(x));
75 }
76}
77
96template <bool Jacobian, typename T, typename Lp,
97 require_std_vector_t<T>* = nullptr,
99inline auto positive_constrain(T&& x, Lp& lp) {
100 return apply_vector_unary<T>::apply(std::forward<T>(x), [&lp](auto&& v) {
101 return positive_constrain<Jacobian>(std::forward<decltype(v)>(v), lp);
102 });
103}
104
105} // namespace math
106} // namespace stan
107
108#endif
require_t< std::is_convertible< std::decay_t< T >, std::decay_t< S > > > require_convertible_t
Require types T and S satisfies std::is_convertible.
require_not_t< is_std_vector< std::decay_t< T > > > require_not_std_vector_t
Require type does not satisfy is_std_vector.
require_t< is_std_vector< std::decay_t< T > > > require_std_vector_t
Require type satisfies is_std_vector.
auto positive_constrain(T &&x)
Return the positive value for the specified unconstrained input.
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:18
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:15
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...