Automatic Differentiation
 
Loading...
Searching...
No Matches
stochastic_column_constrain.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_CONSTRAINT_SIMPLEX_COLUMN_CONSTRAIN_HPP
2#define STAN_MATH_PRIM_CONSTRAINT_SIMPLEX_COLUMN_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 M = y_ref.cols();
27 plain_type_t<Mat> ret(y_ref.rows() + 1, M);
28 for (Eigen::Index i = 0; i < M; ++i) {
29 ret.col(i) = simplex_constrain(y_ref.col(i));
30 }
31 return ret;
32}
33
49template <typename Mat, typename Lp,
51 require_not_st_var<Mat>* = nullptr,
53inline plain_type_t<Mat> stochastic_column_constrain(const Mat& y, Lp& lp) {
54 auto&& y_ref = to_ref(y);
55 const Eigen::Index M = y_ref.cols();
56 plain_type_t<Mat> ret(y_ref.rows() + 1, M);
57 for (Eigen::Index i = 0; i < M; ++i) {
58 ret.col(i) = simplex_constrain(y_ref.col(i), lp);
59 }
60 return ret;
61}
73template <typename T, require_std_vector_t<T>* = nullptr>
74inline auto stochastic_column_constrain(T&& y) {
75 return apply_vector_unary<T>::apply(std::forward<T>(y), [](auto&& v) {
76 return stochastic_column_constrain(std::forward<decltype(v)>(v));
77 });
78}
79
94template <typename T, typename Lp, require_std_vector_t<T>* = nullptr,
95 require_convertible_t<return_type_t<T>, Lp>* = nullptr>
96inline auto stochastic_column_constrain(T&& y, Lp& lp) {
97 return apply_vector_unary<T>::apply(std::forward<T>(y), [&lp](auto&& v) {
98 return stochastic_column_constrain(std::forward<decltype(v)>(v), lp);
99 });
100}
101
120template <bool Jacobian, typename Mat, typename Lp,
123 if constexpr (Jacobian) {
124 return stochastic_column_constrain(std::forward<Mat>(y), lp);
125 } else {
126 return stochastic_column_constrain(std::forward<Mat>(y));
127 }
128}
129
130} // namespace math
131} // namespace stan
132
133#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_column_constrain(const Mat &y)
Return a column 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 ...