| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

cov_matrix_constrain.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_COV_MATRIX_CONSTRAIN_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_COV_MATRIX_CONSTRAIN_HPP
3
4 #include <stan/math/prim/mat/fun/Eigen.hpp>
5 #include <stan/math/prim/mat/fun/multiply_lower_tri_self_transpose.hpp>
6 #include <stan/math/prim/mat/meta/index_type.hpp>
7 #include <stan/math/prim/scal/err/check_size_match.hpp>
8 #include <stan/math/prim/scal/fun/constants.hpp>
9 #include <cmath>
10
11 namespace stan {
12
13 namespace math {
14
27 template <typename T>
28 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
29 cov_matrix_constrain(const Eigen::Matrix<T, Eigen::Dynamic, 1>& x,
30 typename math::index_type
31 <Eigen::Matrix<T, Eigen::Dynamic, 1> >::type K) {
32 using Eigen::Dynamic;
33 using Eigen::Matrix;
34 using std::exp;
35 typedef typename index_type<Matrix<T, Dynamic, Dynamic> >::type index_t;
36
37 Matrix<T, Dynamic, Dynamic> L(K, K);
38 check_size_match("cov_matrix_constrain",
39 "x.size()", x.size(),
40 "K + (K choose 2)", (K * (K + 1)) / 2);
41 int i = 0;
42 for (index_t m = 0; m < K; ++m) {
43 for (int n = 0; n < m; ++n)
44 L(m, n) = x(i++);
45 L(m, m) = exp(x(i++));
46 for (index_t n = m + 1; n < K; ++n)
47 L(m, n) = 0.0;
48 }
49 return multiply_lower_tri_self_transpose(L);
50 }
51
64 template <typename T>
65 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
66 cov_matrix_constrain(const Eigen::Matrix<T, Eigen::Dynamic, 1>& x,
67 typename math::index_type<Eigen::Matrix<T,
68 Eigen::Dynamic,
69 Eigen::Dynamic> >::type K,
70 T& lp) {
71 using Eigen::Dynamic;
72 using Eigen::Matrix;
73 using std::exp;
74 using std::log;
75 typedef typename index_type<Matrix<T, Dynamic, Dynamic> >::type index_t;
76 check_size_match("cov_matrix_constrain",
77 "x.size()", x.size(),
78 "K + (K choose 2)", (K * (K + 1)) / 2);
79 Matrix<T, Dynamic, Dynamic> L(K, K);
80 int i = 0;
81 for (index_t m = 0; m < K; ++m) {
82 for (index_t n = 0; n < m; ++n)
83 L(m, n) = x(i++);
84 L(m, m) = exp(x(i++));
85 for (index_t n = m + 1; n < K; ++n)
86 L(m, n) = 0.0;
87 }
88 // Jacobian for complete transform, including exp() above
89 lp += (K * LOG_2); // needless constant; want propto
90 for (index_t k = 0; k < K; ++k)
91 lp += (K - k + 1) * log(L(k, k)); // only +1 because index from 0
92 return multiply_lower_tri_self_transpose(L);
93 }
94
95 }
96 }
97 #endif
stan::math::LOG_2
const double LOG_2
The natural logarithm of 2, .
Definition: constants.hpp:32
multiply_lower_tri_self_transpose.hpp
stan::math::cov_matrix_constrain
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > cov_matrix_constrain(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &x, typename math::index_type< Eigen::Matrix< T, Eigen::Dynamic, 1 > >::type K)
Return the symmetric, positive-definite matrix of dimensions K by K resulting from transforming the s...
Definition: cov_matrix_constrain.hpp:29
check_size_match.hpp
stan
Definition: log_sum_exp.hpp:8
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:14
stan::math::multiply_lower_tri_self_transpose
Eigen::Matrix< fvar< T >, R, R > multiply_lower_tri_self_transpose(const Eigen::Matrix< fvar< T >, R, C > &m)
Definition: multiply_lower_tri_self_transpose.hpp:17
stan::math::index_type
Primary template class for the metaprogram to compute the index type of a container.
Definition: index_type.hpp:18
index_type.hpp
stan::math::exp
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
stan::math::check_size_match
bool check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Return true if the provided sizes match.
Definition: check_size_match.hpp:30
Eigen.hpp
constants.hpp

[ Stan Home Page ] © 2011–2016, Stan Development Team.