Stan Math Library: stan/math/prim/mat/fun/cov_matrix_free.hpp Source File

| Stan Math Library 2.10.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/mat/fun/Eigen.hpp>
5 #include <stan/math/prim/mat/meta/index_type.hpp>
6 #include <cmath>
7 #include <stdexcept>
8
9 namespace stan {
10
11 namespace math {
12
35 template <typename T>
36 Eigen::Matrix<T, Eigen::Dynamic, 1>
37 cov_matrix_free(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& y) {
38 using std::log;
39 int K = y.rows();
40 if (y.cols() != K)
41 throw std::domain_error("y is not a square matrix");
42 if (K == 0)
43 throw std::domain_error("y has no elements");
44 for (int k = 0; k < K; ++k)
45 if (!(y(k, k) > 0.0))
46 throw std::domain_error("y has non-positive diagonal");
47 Eigen::Matrix<T, Eigen::Dynamic, 1> x((K * (K + 1)) / 2);
48 // FIXME: see Eigen LDLT for rank-revealing version -- use that
49 // even if less efficient?
50 Eigen::LLT<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> >
51 llt(y.rows());
52 llt.compute(y);
53 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> L = llt.matrixL();
54 int i = 0;
55 for (int m = 0; m < K; ++m) {
56 for (int n = 0; n < m; ++n)
57 x(i++) = L(m, n);
58 x(i++) = log(L(m, m));
59 }
60 return x;
61 }
62
63 }
64
65 }
66
67 #endif
stan
Definition: log_sum_exp.hpp:8
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
index_type.hpp
stan::math::domain_error
void domain_error(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
Throw a domain error with a consistently formatted message.
Definition: domain_error.hpp:32
Eigen.hpp
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:37

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