Stan Math Library  2.20.0
reverse mode automatic differentiation
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 
9 #include <cmath>
10 
11 namespace stan {
12 namespace math {
13 
36 template <typename T>
37 Eigen::Matrix<T, Eigen::Dynamic, 1> cov_matrix_free(
38  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> > llt(y.rows());
50  llt.compute(y);
51  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> L = llt.matrixL();
52  int i = 0;
53  for (int m = 0; m < K; ++m) {
54  for (int n = 0; n < m; ++n)
55  x(i++) = L(m, n);
56  x(i++) = log(L(m, m));
57  }
58  return x;
59 }
60 
61 } // namespace math
62 } // namespace stan
63 #endif
void check_nonzero_size(const char *function, const char *name, const T_y &y)
Check if the specified matrix/vector is of non-zero size.
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:12
void check_square(const char *function, const char *name, const matrix_cl &y)
Check if the matrix_cl is square.
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
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...

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