Automatic Differentiation
 
Loading...
Searching...
No Matches
stochastic_row_constrain.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_CONSTRAINT_STOCHASTIC_ROW_CONSTRAIN_HPP
2#define STAN_MATH_PRIM_CONSTRAINT_STOCHASTIC_ROW_CONSTRAIN_HPP
3
7#include <cmath>
8
9namespace stan {
10namespace math {
11
22template <typename Mat, require_eigen_matrix_dynamic_t<Mat>* = nullptr,
23 require_not_st_var<Mat>* = nullptr>
25 auto&& y_ref = to_ref(y);
26 const Eigen::Index N = y_ref.rows();
27 plain_type_t<Mat> ret(N, y_ref.cols() + 1);
28 for (Eigen::Index i = 0; i < N; ++i) {
29 ret.row(i) = simplex_constrain(y_ref.row(i));
30 }
31 return ret;
32}
33
46template <typename Mat, typename Lp,
48 require_not_st_var<Mat>* = nullptr,
50inline plain_type_t<Mat> stochastic_row_constrain(const Mat& y, Lp& lp) {
51 auto&& y_ref = to_ref(y);
52 const Eigen::Index N = y_ref.rows();
53 plain_type_t<Mat> ret(N, y_ref.cols() + 1);
54 for (Eigen::Index i = 0; i < N; ++i) {
55 ret.row(i) = simplex_constrain(y_ref.row(i), lp);
56 }
57 return ret;
58}
59
70template <typename T, require_std_vector_t<T>* = nullptr>
71inline auto stochastic_row_constrain(T&& y) {
72 return apply_vector_unary<T>::apply(std::forward<T>(y), [](auto&& v) {
73 return stochastic_row_constrain(std::forward<decltype(v)>(v));
74 });
75}
76
90template <typename T, typename Lp, require_std_vector_t<T>* = nullptr,
91 require_convertible_t<return_type_t<T>, Lp>* = nullptr>
92inline auto stochastic_row_constrain(T&& y, Lp& lp) {
93 return apply_vector_unary<T>::apply(std::forward<T>(y), [&lp](auto&& v) {
94 return stochastic_row_constrain(std::forward<decltype(v)>(v), lp);
95 });
96}
97
116template <bool Jacobian, typename Mat, typename Lp,
119 if constexpr (Jacobian) {
120 return stochastic_row_constrain(std::forward<Mat>(y), lp);
121 } else {
122 return stochastic_row_constrain(std::forward<Mat>(y));
123 }
124}
125
126} // namespace math
127} // namespace stan
128
129#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_t< is_eigen_matrix_dynamic< std::decay_t< T > > > require_eigen_matrix_dynamic_t
Require type satisfies is_eigen_matrix_dynamic.
require_not_t< is_var< scalar_type_t< std::decay_t< T > > > > require_not_st_var
Require scalar type does not satisfy is_var.
Definition is_var.hpp:182
plain_type_t< Mat > stochastic_row_constrain(const Mat &y)
Return a row stochastic matrix.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:18
plain_type_t< Vec > simplex_constrain(const Vec &y)
Return the simplex corresponding to the specified free vector.
typename plain_type< std::decay_t< T > >::type plain_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...