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
10#include <stdexcept>
11
12namespace stan {
13namespace math {
14
40template <typename T, require_eigen_col_vector_t<T>* = nullptr>
41inline Eigen::Matrix<value_type_t<T>, Eigen::Dynamic, Eigen::Dynamic>
42corr_matrix_constrain(T&& x, Eigen::Index k) {
43 Eigen::Index k_choose_2 = (k * (k - 1)) / 2;
44 decltype(auto) x_ref = to_ref(std::forward<T>(x));
45 check_size_match("cov_matrix_constrain", "x.size()", x_ref.size(),
46 "k_choose_2", k_choose_2);
47 decltype(auto) cpc_ref
48 = to_ref(corr_constrain(std::forward<decltype(x_ref)>(x_ref)));
49 return read_corr_matrix(cpc_ref, k);
50}
51
74template <typename T, typename Lp, require_eigen_col_vector_t<T>* = nullptr,
75 require_convertible_t<return_type_t<T>, Lp>* = nullptr>
76inline Eigen::Matrix<value_type_t<T>, Eigen::Dynamic, Eigen::Dynamic>
77corr_matrix_constrain(T&& x, Eigen::Index k, Lp& lp) {
78 Eigen::Index k_choose_2 = (k * (k - 1)) / 2;
79 decltype(auto) x_ref = to_ref(std::forward<T>(x));
80 check_size_match("cov_matrix_constrain", "x.size()", x_ref.size(),
81 "k_choose_2", k_choose_2);
82 decltype(auto) cpc_ref
83 = to_ref(corr_constrain(std::forward<decltype(x_ref)>(x_ref), lp));
84 return read_corr_matrix(cpc_ref, k, lp);
85}
86
100template <typename T, require_std_vector_t<T>* = nullptr>
101inline auto corr_matrix_constrain(T&& y, int K) {
102 return apply_vector_unary<T>::apply(std::forward<T>(y), [K](auto&& v) {
103 return corr_matrix_constrain(std::forward<decltype(v)>(v), K);
104 });
105}
106
123template <typename T, typename Lp, require_std_vector_t<T>* = nullptr,
124 require_convertible_t<return_type_t<T>, Lp>* = nullptr>
125inline auto corr_matrix_constrain(T&& y, int K, Lp& lp) {
126 return apply_vector_unary<T>::apply(std::forward<T>(y), [&lp, K](auto&& v) {
127 return corr_matrix_constrain(std::forward<decltype(v)>(v), K, lp);
128 });
129}
130
152template <bool Jacobian, typename T, typename Lp,
154inline auto corr_matrix_constrain(T&& x, Eigen::Index k, Lp& lp) {
155 if constexpr (Jacobian) {
156 return corr_matrix_constrain(std::forward<T>(x), k, lp);
157 } else {
158 return corr_matrix_constrain(std::forward<T>(x), k);
159 }
160}
161
162} // namespace math
163} // namespace stan
164
165#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.
plain_type_t< T > corr_constrain(T &&x)
Return the result of transforming the specified scalar or container of values to have a valid correla...
Eigen::Matrix< value_type_t< T_CPCs >, Eigen::Dynamic, Eigen::Dynamic > read_corr_matrix(const T_CPCs &CPCs, size_t K)
Return the correlation matrix of the specified dimensionality corresponding to the specified canonica...
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.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...