Automatic Differentiation
 
Loading...
Searching...
No Matches
corr_matrix_constrain.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_CONSTRAINT_CORR_MATRIX_CONSTRAIN_HPP
2#define STAN_MATH_PRIM_CONSTRAINT_CORR_MATRIX_CONSTRAIN_HPP
3
12#include <utility>
13
14namespace stan {
15namespace math {
16
42template <typename T, require_eigen_col_vector_t<T>* = nullptr>
43inline Eigen::Matrix<value_type_t<T>, Eigen::Dynamic, Eigen::Dynamic>
44corr_matrix_constrain(T&& x, Eigen::Index k) {
45 Eigen::Index k_choose_2 = (k * (k - 1)) / 2;
46 decltype(auto) x_ref = to_ref(std::forward<T>(x));
47 check_size_match("cov_matrix_constrain", "x.size()", x_ref.size(),
48 "k_choose_2", k_choose_2);
50 internal::cholesky_corr_factor<internal::CorrOrder::column>(
51 x_ref, log_cosh(x_ref), k));
52}
53
76template <typename T, typename Lp, require_eigen_col_vector_t<T>* = nullptr,
77 require_convertible_t<return_type_t<T>, Lp>* = nullptr>
78inline Eigen::Matrix<value_type_t<T>, Eigen::Dynamic, Eigen::Dynamic>
79corr_matrix_constrain(T&& x, Eigen::Index k, Lp& lp) {
80 Eigen::Index k_choose_2 = (k * (k - 1)) / 2;
81 decltype(auto) x_ref = to_ref(std::forward<T>(x));
82 check_size_match("cov_matrix_constrain", "x.size()", x_ref.size(),
83 "k_choose_2", k_choose_2);
84 decltype(auto) log_cosh_x = to_ref(log_cosh(x_ref));
86 log_cosh_x);
88 internal::cholesky_corr_factor<internal::CorrOrder::column>(
89 x_ref, log_cosh_x, k));
90}
91
105template <typename T, require_std_vector_t<T>* = nullptr>
106inline auto corr_matrix_constrain(T&& y, int K) {
107 return apply_vector_unary<T>::apply(std::forward<T>(y), [K](auto&& v) {
108 return corr_matrix_constrain(std::forward<decltype(v)>(v), K);
109 });
110}
111
128template <typename T, typename Lp, require_std_vector_t<T>* = nullptr,
129 require_convertible_t<return_type_t<T>, Lp>* = nullptr>
130inline auto corr_matrix_constrain(T&& y, int K, Lp& lp) {
131 return apply_vector_unary<T>::apply(std::forward<T>(y), [&lp, K](auto&& v) {
132 return corr_matrix_constrain(std::forward<decltype(v)>(v), K, lp);
133 });
134}
135
157template <bool Jacobian, typename T, typename Lp,
159inline auto corr_matrix_constrain(T&& x, Eigen::Index k, Lp& lp) {
160 if constexpr (Jacobian) {
161 return corr_matrix_constrain(std::forward<T>(x), k, lp);
162 } else {
163 return corr_matrix_constrain(std::forward<T>(x), k);
164 }
165}
166
167} // namespace math
168} // namespace stan
169
170#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.
auto corr_matrix_log_jacobian_coefficients(Eigen::Index K)
Return a lazy expression of the coefficients c for which the log absolute Jacobian determinant of cor...
Eigen::Matrix< value_type_t< EigMat >, EigMat::RowsAtCompileTime, EigMat::RowsAtCompileTime > multiply_lower_tri_self_transpose(const EigMat &m)
fvar< T > log_cosh(const fvar< T > &x)
Return the natural logarithm of the hyperbolic cosine of the argument.
Definition log_cosh.hpp:21
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:18
Eigen::Matrix< value_type_t< T >, Eigen::Dynamic, Eigen::Dynamic > corr_matrix_constrain(T &&x, Eigen::Index k)
Return the correlation matrix of the specified dimensionality derived from the specified vector of un...
void check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Check if the provided sizes match.
auto dot_product(const T_a &a, const T_b &b)
Returns the dot product of the specified vectors.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...