All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
cov_matrix_free.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_PRIM_MAT_FUN_COV_MATRIX_FREE_HPP
#define STAN_MATH_PRIM_MAT_FUN_COV_MATRIX_FREE_HPP
#include <stan/math/prim/mat/fun/Eigen.hpp>
#include <stan/math/prim/mat/meta/index_type.hpp>
#include <cmath>
#include <stdexcept>
namespace stan {
namespace math {
template <typename T>
Eigen::Matrix<T, Eigen::Dynamic, 1>
cov_matrix_free(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& y) {
using std::log;
int K = y.rows();
if (y.cols() != K)
throw std::domain_error("y is not a square matrix");
if (K == 0)
throw std::domain_error("y has no elements");
for (int k = 0; k < K; ++k)
if (!(y(k, k) > 0.0))
throw std::domain_error("y has non-positive diagonal");
Eigen::Matrix<T, Eigen::Dynamic, 1> x((K * (K + 1)) / 2);
// FIXME: see Eigen LDLT for rank-revealing version -- use that
// even if less efficient?
Eigen::LLT<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> >
llt(y.rows());
llt.compute(y);
Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> L = llt.matrixL();
int i = 0;
for (int m = 0; m < K; ++m) {
for (int n = 0; n < m; ++n)
x(i++) = L(m, n);
x(i++) = log(L(m, m));
}
return x;
}
}
}
#endif
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–2015, Stan Development Team.