| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

cov_matrix_free.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_COV_MATRIX_FREE_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_COV_MATRIX_FREE_HPP
3
4 #include <stan/math/prim/arr/err/check_nonzero_size.hpp>
5 #include <stan/math/prim/mat/fun/Eigen.hpp>
6 #include <stan/math/prim/mat/meta/index_type.hpp>
7 #include <stan/math/prim/mat/err/check_square.hpp>
8 #include <stan/math/prim/scal/err/check_positive.hpp>
9 #include <cmath>
10
11 namespace stan {
12 namespace math {
13
36 template <typename T>
37 Eigen::Matrix<T, Eigen::Dynamic, 1>
38 cov_matrix_free(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& y) {
39 check_square("cov_matrix_free", "y", y);
40 check_nonzero_size("cov_matrix_free", "y", y);
41
42 using std::log;
43 int K = y.rows();
44 for (int k = 0; k < K; ++k)
45 check_positive("cov_matrix_free", "y", y(k, k));
46 Eigen::Matrix<T, Eigen::Dynamic, 1> x((K * (K + 1)) / 2);
47 // FIXME: see Eigen LDLT for rank-revealing version -- use that
48 // even if less efficient?
49 Eigen::LLT<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> >
50 llt(y.rows());
51 llt.compute(y);
52 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> L = llt.matrixL();
53 int i = 0;
54 for (int m = 0; m < K; ++m) {
55 for (int n = 0; n < m; ++n)
56 x(i++) = L(m, n);
57 x(i++) = log(L(m, m));
58 }
59 return x;
60 }
61
62 }
63 }
64 #endif
check_positive.hpp
stan
Definition: log_sum_exp.hpp:8
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:14
stan::math::check_nonzero_size
bool check_nonzero_size(const char *function, const char *name, const T_y &y)
Return true if the specified matrix/vector is of non-zero size.
Definition: check_nonzero_size.hpp:29
index_type.hpp
stan::math::check_positive
bool check_positive(const char *function, const char *name, const T_y &y)
Return true if y is positive.
Definition: check_positive.hpp:66
Eigen.hpp
check_nonzero_size.hpp
stan::math::check_square
bool check_square(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Return true if the specified matrix is square.
Definition: check_square.hpp:28
stan::math::cov_matrix_free
Eigen::Matrix< T, Eigen::Dynamic, 1 > cov_matrix_free(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &y)
The covariance matrix derived from the symmetric view of the lower-triangular view of the K by K spec...
Definition: cov_matrix_free.hpp:38
check_square.hpp

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