| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

cholesky_factor_constrain.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_CHOLESKY_FACTOR_CONSTRAIN_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_CHOLESKY_FACTOR_CONSTRAIN_HPP
3
4 #include <stan/math/prim/mat/fun/Eigen.hpp>
5 #include <stan/math/prim/mat/fun/sum.hpp>
6 #include <stan/math/prim/scal/err/check_greater_or_equal.hpp>
7 #include <stan/math/prim/scal/err/check_size_match.hpp>
8 #include <cmath>
9 #include <stdexcept>
10 #include <vector>
11
12 namespace stan {
13 namespace math {
14
26 template <typename T>
27 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
28 cholesky_factor_constrain(const Eigen::Matrix<T, Eigen::Dynamic, 1>& x,
29 int M,
30 int N) {
31 using std::exp;
32 check_greater_or_equal("cholesky_factor_constrain",
33 "num rows (must be greater or equal to num cols)",
34 M, N);
35 check_size_match("cholesky_factor_constrain",
36 "x.size()", x.size(),
37 "((N * (N + 1)) / 2 + (M - N) * N)",
38 ((N * (N + 1)) / 2 + (M - N) * N));
39 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> y(M, N);
40 T zero(0);
41 int pos = 0;
42
43 for (int m = 0; m < N; ++m) {
44 for (int n = 0; n < m; ++n)
45 y(m, n) = x(pos++);
46 y(m, m) = exp(x(pos++));
47 for (int n = m + 1; n < N; ++n)
48 y(m, n) = zero;
49 }
50
51 for (int m = N; m < M; ++m)
52 for (int n = 0; n < N; ++n)
53 y(m, n) = x(pos++);
54 return y;
55 }
56
71 template <typename T>
72 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
73 cholesky_factor_constrain(const Eigen::Matrix<T, Eigen::Dynamic, 1>& x,
74 int M,
75 int N,
76 T& lp) {
77 check_size_match("cholesky_factor_constrain",
78 "x.size()", x.size(),
79 "((N * (N + 1)) / 2 + (M - N) * N)",
80 ((N * (N + 1)) / 2 + (M - N) * N));
81 int pos = 0;
82 std::vector<T> log_jacobians(N);
83 for (int n = 0; n < N; ++n) {
84 pos += n;
85 log_jacobians[n] = x(pos++);
86 }
87 lp += sum(log_jacobians);
88 return cholesky_factor_constrain(x, M, N);
89 }
90
91 }
92 }
93 #endif
stan::math::sum
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition: sum.hpp:20
stan::math::check_greater_or_equal
bool check_greater_or_equal(const char *function, const char *name, const T_y &y, const T_low &low)
Return true if y is greater or equal than low.
Definition: check_greater_or_equal.hpp:84
check_size_match.hpp
stan
Definition: log_sum_exp.hpp:8
stan::math::cholesky_factor_constrain
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > cholesky_factor_constrain(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &x, int M, int N)
Return the Cholesky factor of the specified size read from the specified vector.
Definition: cholesky_factor_constrain.hpp:28
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
sum.hpp
check_greater_or_equal.hpp

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