Stan Math Library  2.14.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>
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
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:14
void check_square(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Check if the specified matrix 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–2016, Stan Development Team.